标签:
结构体:相当于是我们自己定义的一种复杂的类型。
int... double float bool char string DateTime 数组类型
生活中大部份的对象都是复合型的对象。
如何定义结构体类型?
一般来说结构体的定义要放在class的外面或class的里面,尽量不放在Main的里面。
struct 自定义类型名
{
public 变量类型 变量名; ......; ......; ......;
}
例如:
struct YuanGong //自定义的数据类型。用来描述员工的信息。
{
public string NO;
public string Name;
public int Age;
public string Nation;
public bool Sex;
}
如何用自定义的类型来定义变量? 自定义类型名 变量 = new 自定义类型名();
如何使用自定义类型的变量?
变量.子变量 = "xxxx"; Console.WriteLine(变量名.子变量);
例如:
//定义自定义类型的变量
YuanGong zhangsan = new YuanGong();
//给变量赋值
zhangsan.NO = "Y001";
zhangsan.Name = "张三";
zhangsan.Age = 22;
zhangsan.Sex = true;
zhangsan.Nation = "汉族";
//对变量取值
Console.WriteLine(zhangsan.NO+"\t"+zhangsan.Name+"\t"+zhangsan.Age);
Console.WriteLine(zhangsan.Nation+"\t"+(zhangsan.Sex?"男":"女"));
例1:
struct YuanGong //自定义的数据类型。用来描述员工的信息。
{
public string NO;
public string Name;
public int Age;
public string Nation;
public bool Sex;
public LianXiFangShi LianXi;
}
struct LianXiFangShi
{
public string QQ;
public string WeiXin;
public string Email;
public string Telephone;
public string Address;
public string ZipCode;
}
class Program
{
static void Mai0n(string[] args)
{
YuanGong zhangsan = new YuanGong();
zhangsan.NO = "Y001";
zhangsan.Name = "张三";
zhangsan.Age = 22;
zhangsan.Sex = true;
zhangsan.Nation = "汉族";
zhangsan.LianXi.QQ = "434354546";
//zhangsan.LianXi.WeiXin = "张三三";
//zhangsan.LianXi.Email = "zhangsan@tom.com";
zhangsan.LianXi.Address = "张店区张家胡同";
zhangsan.LianXi.ZipCode = "25000";
zhangsan.LianXi.Telephone = "";
YuanGong lisi = new YuanGong();
lisi.NO = "Y002";
lisi.Name = "李四";
lisi.Age = 25;
lisi.Sex =false;
lisi.Nation = "回族";
Console.WriteLine("**********张三的个人信息***********");
Console.WriteLine(zhangsan.NO+"\t"+zhangsan.Name+"\t"+zhangsan.Age);
Console.WriteLine(zhangsan.Nation+"\t"+(zhangsan.Sex?"男":"女"));
Console.WriteLine("联系方式:");
Console.WriteLine(
"QQ:"+(zhangsan.LianXi.QQ==null?"无":zhangsan.LianXi.QQ)+"\t"
+"微信:"+(zhangsan.LianXi.WeiXin == null?"无":zhangsan.LianXi.WeiXin)+"\t"
+"手机:"+(zhangsan.LianXi.Telephone==null?"无":zhangsan.LianXi.Telephone)+"\t"
+"邮箱:"+(zhangsan.LianXi.Email==null?"无":zhangsan.LianXi.Email)+"\t"
+"地址:"+zhangsan.LianXi.Address+"\t"+zhangsan.LianXi.ZipCode);
}
}
例2:
struct XueSheng
{
public int XueHao;
public string XingMing;
public double YuWen;
public double ShuXue;
public double WaiYu;
public double ZongFen;
public int MinCi;
}
class Class1
{
static void Mai1n(string[] args)
{
//int[] a = new int[5];
XueSheng[] s = new XueSheng[5];
//输入
ShuRu(s);
//排序
PaiXu(s);
//填名次
XieMingCi(s);
//输出显示
ShuChu(s);
}
private static void ShuChu(XueSheng[] s)
{
for (int i = 0; i < s.Length; i++)
{
Console.WriteLine(s[i].XueHao + "\t" + s[i].XingMing + "\t" + s[i].YuWen + "\t" + s[i].ShuXue + "\t"
+ s[i].WaiYu + "\t" + s[i].ZongFen + "\t" + s[i].MinCi);
}
}
private static void XieMingCi(XueSheng[] s)
{
for (int i = 0; i < s.Length; i++)
{
s[i].MinCi = i + 1;
}
}
private static void PaiXu(XueSheng[] s)
{
for (int i = 1; i <= s.Length - 1; i++)
{
for (int j = 1; j <= s.Length - i; j++)
{
if (s[j].ZongFen > s[j - 1].ZongFen)
{
XueSheng temp = s[j];
s[j] = s[j - 1];
s[j - 1] = temp;
}
}
}
}
private static void ShuRu(XueSheng[] s)
{
for (int i = 0; i < s.Length; i++)
{
Console.WriteLine("正在输入第" + (i + 1) + "个学生的信息");
s[i].XueHao = i + 1;
Console.Write("姓名:");
s[i].XingMing = Console.ReadLine();
Console.Write("语文:");
s[i].YuWen = Convert.ToDouble(Console.ReadLine());
Console.Write("数学:");
s[i].ShuXue = Convert.ToDouble(Console.ReadLine());
Console.Write("外语:");
s[i].WaiYu = Convert.ToDouble(Console.ReadLine());
//总分:
s[i].ZongFen = s[i].YuWen + s[i].ShuXue + s[i].WaiYu;
}
}
}
标签:
原文地址:http://www.cnblogs.com/981971554nb/p/4309776.html