第17周报告1
实验目的:理解结构体的概念,并学会结构初步的应用
实验内容:利用结构体解决一些成员的输入与输出
/* 程序头部注释开始(为避免提交博文中遇到的问题,将用于表明注释的斜杠删除了)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 利用结构体输出成绩单
* 作 者: 2011级 114-3 张宗佳
* 完成日期:2011年12月 19 日
* 版 本 号: vc.1
* 对任务及求解方法的描述部分
* 输入描述:将文件中的数据导入程序中
* 问题描述:将文件中的成绩按总分的高低输出
* 程序输出:学生成绩的名次以及前三十名获奖学金同学的名单
* 问题分析: ....
* 算法设计:……
*/
#include<iostream>
#include<fstream>
using namespace std;
void input_file();//将文件中的数据导入,并计算总分
void bubble_sort(int num);//按照总分排序
void show_grade(int num);//输出成绩单
void rewarding();//输出获得奖学金同学的名单
struct student
{
char num[12];
char name[20];
float grade[4];
};
student stu[180];
int num=180;
int main(void)
{
input_file();
bubble_sort(num);
cout<<"排序后的成绩单是:"<<endl;
show_grade(num);
cout<<"获得奖学金的名单是:"<<endl;
rewarding();
return 0;
}
void input_file()
{
int i;
ifstream infile("score.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(i = 0; i < num; i++)
{
infile >> stu[i].num >> stu[i].name >> stu[i].grade[0] >> stu[i].grade[1] >> stu[i].grade[2];
stu[i].grade[3] = stu[i].grade[0] + stu[i].grade[1] + stu[i].grade[2];
}
infile.close();
return ;
}
void bubble_sort(int num)
{
int i, j;
student score;
for(j = 0; j < num - 1; j++)
{
for(i = 0; i < num - j - 1; i++)
{
if(stu[i].grade[3] <= stu[i+1].grade[3])
{
score = stu[i];
stu[i] = stu[i+1];
stu[i+1] = score;
}
}
}
return ;
}
void show_grade(int num)
{
int i;
cout << "学号" << "\t" << " " << "姓名" << "\t" << "\t" << "c++" << "\t" << "高数" << "\t" << "英语" << "\t" << "总分" << endl;
for(i = 0; i < num; i++)
{
cout << stu[i].num << "\t" << stu[i].grade[0] << "\t" << stu[i].grade[1] << "\t" << stu[i].grade[2] << "\t"<<stu[i].grade[3] << endl;
}
cout << endl;
return ;
}
void rewarding()
{
int n = 1, i;
for(i = 0; i < num, n <= 30; i++)
{
if(stu[i].grade[0] >= 60&&stu[i].grade[1] >= 60&&stu[i].grade[2] >= 60)
{
cout << n << "." << stu[i].name << " ";
if(n % 5 == 0)
{
cout << endl;
}
n++;
}
}
return ;
}
经验积累:
1.如果要将一串字符输给一个变量要定义字符数组或string类型,如果单纯定义一个字符变量,无法将这字符串赋给此变量的就像此程序中定义的结构体内的成员 char name[20],也可以定义成strng name
2.struct student
{
};
student stu[180];在定义结构体类型之后,}外要加分号,再定义结构体变量
只能对结构体变量中的各个成员分别进行输入和输出,而不能一次性的全部输出,这一点很重要
3.在导入文件时可以直接进行总分的计算stu[i].grade[3]=stu[i].grade[0]+stu[i].grade[1]+stu[i].grade[2];
上机感言:做了好长时间才做出来,在编写的过程中,漏洞太多了,不是这里用错了,就是忘了怎么用...其实还是那句话,课本真的很重要,我也在努力的去看,这学期一定要把学过的只是掌握好