关于 结构体 函数的应用
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);
}
小感想 :现定义结构体确定需要的数据类型。然后在主函数中定义数组,确定整个程序的模块(输入,计算 ,排序,输出等)最后完成函数构造;
示例::
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);
}
相关文章推荐
- 关于ngx的一些常用函数和结构体
- 关于报表自定义函数的应用
- mysql关于时间函数的应用
- 结构体对函数指针的高级封装应用
- 关于msgbox几个函数的应用
- PHP中关于系统相关函数的应用
- win32 中关于ScrollWindow函数的应用
- C++初学者(关于函数指针的应用)
- C语言学习10:结构体,结构体应用,联合用法,枚举,fopen函数使用,fseek,ftell的作用和文件结束符EOF,数组和文件交换数据,个人信息管理。
- 多个结构体的函数应用
- EXCEL 中自定义函数的应用(关于 XOR 在 EXCEL 中的用法 及 文本型数字求和)
- 关于函数指针在单片机上的应用
- 把结构体数组应用在vc的函数中当形式参数遇到的问题
- 关于函数模板和类模板的应用
- 关于返回结构体的函数
- 关于row_number、desek_rank、ntile等函数应用
- C语言中结构体在函数中的应用
- 关于vc访问底层应用函数,win7不能调试的问题
- 关于C语言通过函数传递多个数据————指针的应用
- linux下关于结构体stat的一些应用