四、程序分析
1.下面程序,输入不同参数,观察并分析运行结果。
using System;
namespace Test
{
class Program
{
static void Main(string[] args)
{
while (true)
{
String s = Console.ReadLine();
if (s == "exit") break;
igitry
{
int i = 10 / Int32.Par(s);
Console.WriteLine("结果是" + i);
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine(e.Message);
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
}
catch (DivideByZeroException e)
{
Console.WriteLine(e.Message);
学业水平考试报名系统 }
catch (OverflowException e)
{
Console.WriteLine(e.Message);
}
}
Console.Read();
}
}
}
2.
代码改为:
using System;
namespace Test
{
interface I1
{
void MyFunction1();
}
interface I2
{
void MyFunction2();
}
class Test : I1, I2
{
public void MyFunction1()
{
Console.WriteLine("Now I can say this here is I1 implemented!");
}
public void MyFunction2()
{
Console.WriteLine("Now I can say this here is I2 implemented!");
}
}
class PavelTeskov
{
static void Main(string[] args)
{
Test t = new Test();
t.MyFunction1();
locksmith t.MyFunction2();
Console.Read();
}
}
}
3. 用户自定义从 Digit 到 byte 的隐式转换
using System;
namespace Test
{
public class Digit
{
byte value;英文爱情签名
public Digit(byte value)
{
if (value < 0 || value > 9)
throw new ArgumentException();
this.value = value;
}
public static implicit operator byte(Digit d)
{
return d.value;
}
}
class dig
{
static void Main(string[] args)
{
Digit dig = new Digit(7);
byte num = dig;
Console.WriteLine("num={0}", num);
Console.ReadLine();
}
}
}
五、编程题
1.定义描述复数的类,并实现复数的输入和输出。设计三个方法分别完成复数的加、减和乘法运算。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Chapter3
{
public class Complex
{
private规格英文 double real;
privatehappy valentine day double image;
public Complex(double real, double image)
{
this.real = real;
this.image = image;
}feellike
public double Real
{
get
{
return real;
}
t
{
real = value;
}
}
public double Image
{
get
{
return image;
}
t
{
image = value;
}
}
public static Complex add(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.image + c2.image);
}
public static Complex sub(Complex c1, Complex c2)
{
return new Complex(c1.real - c2.real, c1.image - c2.image);
}
public static Complex mul(Complex c1, Complex c2)
{
return new Complex(c1.real * c2.real - c1.image * c2.image, c1.image * c2.real + c1.real * c2.image);
}
狭隘
public static void print(纪念品英文Complex莫言 檀香刑 c)
{
Console.WriteLine("结果为: "+c.real + " + " + c.image + " i");
}
}
class test1
{
static void Main(string[] args)
{
while (true)
{
Console.Write("输入<1>计算加法,<2>计算减法,<3>计算乘法,<0>退出:>>");
int i = int.Par(Console.ReadLine());
if(i == 0)
{
Console.WriteLine("退出程序!");
break;
}
Console.Write("请输入第一个复数的实部:");
double a = double.Par(Console.ReadLine());
Console.Write("请输入第一个复数的虚部:");