標籤:
結構體:相當於是我們自己定義的一種複雜的類型。
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 = "[email protected]";
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;
}
}
}
C#9 結構體 員工資訊 學產生績