這篇是Exception 的Try 跟Catch的始用範例
後面是運算子的示範using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a, b;
Console.Write("Please input 2 numbers\n");
try
{
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(a / b);
}
catch(DivideByZeroException)
{
Console.WriteLine("\nDivided by Zero !!");
}
catch (FormatException)
{
Console.Write("\ninput error!!");
}
}
}
public class complex_number
{
public double real_number;
public double i_number;
public complex_number(double a, double b)
{
real_number = a;
i_number = b;
}
public double Real
{
set
{
real_number = value;
}
get
{
return real_number;
}
}
public double I_num
{
set
{
i_number = value;
}
get
{
return i_number;
}
}
public static complex_number operator +(complex_number a,complex_number b)
{
return new complex_number(a.real_number+b.real_number,a.i_number+b.i_number);
}
public static complex_number operator -(complex_number a, complex_number b)
{
return new complex_number(a.real_number - b.real_number, a.i_number - b.i_number);
}
public static complex_number operator *(complex_number a, complex_number b)
{
return new complex_number(a.real_number * b.real_number, a.i_number * b.i_number);
}
public static complex_number operator /(complex_number a, complex_number b)
{
return new complex_number(a.real_number / b.real_number, a.i_number / b.i_number);
}
}
}
沒有留言:
張貼留言