結構體

結構體:
由多種簡單類型,組合成一種複雜的類型。使用這種複雜的類型來解決生活中的實際例子。數組

1、結構體定義:
struct 結構體的名子
{
public 類型名 變量名;
.....
}
struct Student
{
public string NO;
public string Name;
public double YuWen;
public double ShuXue;
public double WaiYu;
public double ZongFen;
}spa

2、結構體的使用:
1.使用結構體來定義變量
Student s1 = new Student();
2.給結構體變量賦值。
s1.Name = "";
s1.NO = "";
3.給結構體變量取值。
s2.YuWen排序

3、複雜結構體。
使用結構體類型的成員變量,來組成更大的結構體。
1.先造個小的結構體
2.使用小的結構體來組合成更大的結構體。
struct Student
{
public string NO;
public string Name;
public ChengJi Score = new ChengJi();
public LianXiFangShi Contact = new LianXiFangShi():
}
struct LianXiFangShi
{
public string DianHua;
public string QQ;
public string YouXiang;
public string ZhuZhi;
}
struct ChengJi
{
public double YuWen;
public double ShuXue;
public double WaiYu;
public double ZongFen;
}
複雜結構體的定義:
Student s1 = new Student();
複雜結構體成員變量的使用。
s1.NO = "s001";
s1.Name = "張三";
s1.Contact.DianHua="18500002222";
s1.Contact.QQ="88888888";
s1.Contact.YouXiang="88888888@qq.com";
s1.Contact.ZhuZhi = "地球";
s1.Score.YuWen=89;
s1.Score.ShuXue=99;
s1.Score.WaiYu=79;
s1.Score.ZongFen = s1.Score.YuWen+s1.Score.ShuXue+s1.Score.WaiYu;數學

4、結構體的數組。string

例題:it

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;io

namespace ConsoleApplication2
{
struct Score
{
public int NO;
public string Name;
public double Chinese;
public double Maths;
public double Englist;
public double Sum;
}
class Program
{
static Score[] ShuRu(Score[] ss)
{
for (int i = 0; i < ss.Length; i++)
{
Console.WriteLine("*****正在輸入第" + (i + 1) + "個同窗的成績****");
ss[i].NO = i + 1;
Console.Write("姓名:");
ss[i].Name = Console.ReadLine();
Console.Write("語文:");
ss[i].Chinese = Convert.ToDouble(Console.ReadLine());
Console.Write("數學:");
ss[i].Maths = Convert.ToDouble(Console.ReadLine());
Console.Write("英語:");
ss[i].Englist = Convert.ToDouble(Console.ReadLine());
ss[i].Sum = ss[i].Chinese + ss[i].Maths + ss[i].Englist;
}
return ss;
}
static void ShuChu(Score[] ss)
{
Console.WriteLine("學號\t姓名\t語文\t數學\t外語\t總分\t名次");
for (int i = 0; i < ss.Length; i++)
{
Console.Write(ss[i].NO + "\t");
Console.Write(ss[i].Name + "\t");
Console.Write(ss[i].Chinese + "\t");
Console.Write(ss[i].Maths + "\t");
Console.Write(ss[i].Englist + "\t");
Console.Write(ss[i].Sum + "\t");
Console.Write((i + 1) + "\t");
}
}
static Score[] PaiXun(Score[] ss)
{
for (int i = 1; i <= ss.Length - 1;i++ )
{
for (int j = 1; j <= ss.Length - i; j++)
{
if (ss[j].Sum > ss[j - 1].Sum)
{
Score temp = ss[j];
ss[j] = ss[j - 1];
ss[j - 1] = temp;class

}
}
}變量

return ss;
}
static void Main(string[] args)
{
//作一個結構體的數組.
Score[] ss = new Score[5];
//輸入
ss = ShuRu(ss);
//排序
ss = PaiXun(ss);
//輸出
ShuChu(ss);
}
}
}qq

相關文章
相關標籤/搜索