定义一个公司员工类,包含员工的员工号,姓名,性别,出生年月,年龄,工资金额等字段和属性,其中规定姓名不能为空,工资金额不低于当地最低工资600元,员工号从1000到5999,并给出将员工所有信息打印出来的ToString()方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharp_9_01
{
class Employee
{
private static int Id;
public static int Id1
{
get
{
if (Employee.Id >= 1000 && Employee.Id <= 5999)
{
return Employee.Id;
}
else
{
Console.WriteLine("员工工号应为1000到5999之间,已经初始化为1000");
return 1000;
}
}
set { Employee.Id = value; }
}
private static string Name;
public static string Name1
{
get
{
if (Employee.Name != null)
{
return Employee.Name;
}
else
Console.WriteLine("姓名不能为空,已将姓名初始化为小明");
return "小明";
}
set
{
if (Employee.Name != null)
{
Employee.Name = value;
}
else
{
Console.WriteLine("姓名不能为空,已将姓名初始化为小明");
Employee.Name = "小明";
}
}
}
private static char Sex = '男';
private static int Year = 1997;
private static int Month = 09;
private static int Day = 09;
private static int Age = 20;
private static float Salary;
public static float Salary1
{
get
{
if (Employee.Salary >= 600)
{
return Employee.Salary;
}
else
{
Console.WriteLine("最低工资不低于600,已经初始化为600");
return 600;
}
}
set
{
if (Employee.Salary >= 600)
{
Employee.Salary = value;
}
else
{
Console.WriteLine("最低工资不低于600,已经初始化为600");
Employee.Salary = 600;
}
}
}
public override string ToString()
{
return (string.Format("员工工号为:{0}\n员工姓名为:{1}\n员工性别为:{2}\n员工出生年月日为:{3}年{4}月{5}日\n员工年龄为:{6}\n员工工资为:{7}\n", Id1, Name1, Sex, Year, Month, Day, Age, Salary1));
}
static void Main(string[] args)
{
Employee e = new Employee();
Console.WriteLine(e);
}
}
}
定义一个电视机类,具有以下特性:
1、生产厂家;
2、是否支持数字信号;
3、价格;
4、当前频道(最多有100个频道);
一台电视机必须有以下操作功能:
1、打开电视;
2、关闭电视;
3、选台;
4、显示当前频道;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharp_9_02
{
class Tv
{
static int num = 0;
static string factory="熊猫牌";
static bool IssupNub=true;
static float Price=1999;
static int[] Channel = new int[100];
static void OpenTv()
{
Console.WriteLine("输入要打开的频道数字:");
num++;
Channel[num] = int.Parse(Console.ReadLine());
Console.WriteLine("正在打开电视即将打开:频道{0}……", Channel[num]);
Console.WriteLine("打开成功!");
}
static void ChangeChannel()
{
Console.WriteLine("输入要切换的频道数字:");
num++;
Channel[num] = int.Parse(Console.ReadLine());
Console.WriteLine("正在切换到频道{0}……", Channel[num]);
}
static void ShowTv()
{
Console.WriteLine("当前所有已打开频道列表为:");
for (int i = 1; i <= num; i++)
{
Console.WriteLine("channel:{0}",Channel[i]);
}
Console.WriteLine("当前正在收看频道为:{0}", Channel[num]);
}
static void CloseTv()
{
num--;
Console.WriteLine("正在关闭电视……");
}
static void Main(string[] args)
{
OpenTv();
ChangeChannel();
ShowTv();
CloseTv();
}
}
}
定义复数类complex,使用运算符重载实现复数相加、想减、相乘和相除的运算。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharp_9_03
{
public struct Complex
{
public double real;
public double image;
public Complex(double real, double image)
{
this.real = real;
this.image = image;
}
//运算符重载+
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.image + c2.image);
}
//运算符重载-
public static Complex operator -(Complex c1, Complex c2)
{
return new Complex(c1.real - c2.real, c1.image - c2.image);
}
//运算符重载* (a+bi)(c+di)=(ac-bd)-(bc+ad)i
public static Complex operator *(Complex c1, Complex c2)
{
double a=c1.real * c2.real - c1.image * c2.image;
double b= c1.image * c2.real + c1.real * c2.image;
return new Complex(a,b);
}
//运算符重载/
public static Complex operator /(Complex c1, Complex c2)
{
double a = (c1.real * c2.real + c1.image * c2.image) / (c2.image * c2.image + c2.real * c2.real);
double b = (c1.image * c2.real - c1.real * c2.image) / (c2.real * c2.real + c2.image * c2.image);
return new Complex(a, b);
}
public override string ToString()
{
if (image < 0) return (string.Format("{0}{1}i", real, image));
return (string.Format("{0}+{1}i", real, image));
}
}
class Program
{
static void Main(string[] args)
{
Complex num1 = new Complex(2, 3);
Complex num2 = new Complex(3, 4);
Console.WriteLine("复数1:{0:f2}", num1);
Console.WriteLine("复数2:{0:f2}", num1);
Console.WriteLine("复数相加:{0:f2}", num1 + num2);
Console.WriteLine("复数相减:{0:f2}", num1 - num2);
Console.WriteLine("复数相乘:{0:f2}", num1 * num2);
Console.WriteLine("复数相除:{0:f2}", num1 / num2);
}
}
}