您的位置:首页 > 其它

关于 结构体 函数的应用

2015-05-15 16:11 357 查看
前几天学习了函数然后紧接着学了结构体。虽然没有什么新的算法,但是是对以前内容的重新运用,更重要的是,这两个知识点,可以让程序更加简洁而且可以有效地减轻工作量。于是乎,做了一个信息统计的程序(主要实现的功能是,可以增加任何项目包括考核项目等等,并且可以进行排序操作)。

小感想 :现定义结构体确定需要的数据类型。然后在主函数中定义数组,确定整个程序的模块(输入,计算 ,排序,输出等)最后完成函数构造;

示例::

struct chengji//结构体

{

public string xingmin;

public int yuwen;

public int shuxue;

public int zongfen;

// public int mingci;

}

class Program

{

static chengji[] ShuRu(chengji[] cj) //输入函数

{

for (int i = 0; i < cj.Length; i++)

{

Console.WriteLine("正在输入学生" + (i + 1) + "的信息");

Console.WriteLine("请输入姓名:");

cj[i].xingmin = Console.ReadLine();

Console.WriteLine("请输入语文成绩:");

cj[i].yuwen = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("请输入数学成绩:");

cj[i].shuxue = Convert.ToInt32(Console.ReadLine());

cj[i].zongfen = cj[i].yuwen + cj[i].shuxue;

Console.Clear();

}

return cj;

}

static chengji[] PaiXu(chengji[] cj) //排序函数

{

for (int i = 1; i <= cj.Length - 1; i++)

{

{

for (int j = 1; j <= cj.Length - i; j++)

{

if (cj[j].zongfen > cj[j - 1].zongfen)

{

chengji temp = cj[j]; //注意此处排序需要将整个数组进行排序

cj[j] = cj[j - 1]; //此处的chengji 为一个数组类型;

cj[j - 1] = temp;

}

}

}

}

return cj;

}

static chengji[] shuchu(chengji[] cj) //输出函数

{

Console.WriteLine("姓名\t语文\t数学\t总分\t名次 ");

for (int i = 0; i < cj.Length; i++)

{

Console.WriteLine(cj[i].xingmin + "\t" + cj[i].yuwen + "\t" + cj[i].shuxue + "\t" + cj[i].zongfen + "\t" + (i + 1));

}

return cj;

}

static void Main(string[] args)

{//输入 学生成绩并按照总分进行排序

//输入

chengji[] cj = new chengji[5];

cj = ShuRu(cj);

//排序

cj = PaiXu(cj);

//输出;

cj = shuchu(cj);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: