学生成绩管理系统: 统计成绩排序并打印(c++)(内含读取文件.txt及将文件输出excel的方法)
实验要求:输入30个学生的学号、姓名和5门课程的成绩,计算总分并按照总分排出名次,最后按照学号顺序打印成绩单, 并把成绩单输出为excel文件。
txt数据:
2015020981 甲 90 89 99 88 79
2015020986 戌 97 87 97 60 79
2015020970 鹏 97 88 77 80 79
2015020983 丙 92 89 70 88 79
2015020984 丁 93 84 96 36 77
2015020982 乙 61 88 99 84 70
2015020985 戊 94 81 94 82 75
2015020989 三 93 89 99 88 50
2015020994 八 91 88 49 84 70
2015020987 一 99 89 99 88 60
2015020988 二 91 58 69 84 70
2015020969 将 94 51 94 82 75
2015020960 孙 91 88 99 84 99
2015020990 四 93 84 96 86 77
2015020995 九 92 50 99 88 79
2015020992 六 97 87 97 80 79
2015020993 七 90 69 99 88 79
2015020997 张 74 81 54 82 75
2015020996 十 63 84 96 86 77
2015020965 郑 90 88 99 88 79
2015020998 王 97 87 100 80 79
2015020999 李 40 89 99 88 79
2015020963 刘 94 89 94 82 75
2015020961 赵 92 89 99 88 79
2015020962 岳 93 84 96 86 100
2015020966 林 51 88 99 84 70
2015020964 宋 97 87 47 80 79
2015020968 宗 93 84 96 86 57
2015020967 任 92 89 99 70 79
2015020991 五 94 81 44 82 75
源代码:
#include<iostream>
#include<cstdio>
#include<fstream>
#include<string>
#include<algorithm>
using namespace std;
struct Student
{
int id;
string name;
int score[5];
int total;
int rank;
public:
friend ostream & operator<<(ostream & out, const Student &stu);
friend istream & operator>>(istream & in, Student &stu);
};
ostream & operator <<(ostream & out, const Student &stu)
{
out << stu.id<<"\t"<<stu.name << "\t" << stu.score[0]<<"\t"<< stu.score[1]
<<"\t" << stu.score[2]<<"\t"<< stu.score[3]<<"\t" << stu.score[4] <<"\t"
<<stu.total<<"\t"<<stu.rank<<endl ;
return out;
}
istream & operator >>(istream & in, Student &stu)
{
in>>stu.id>>stu.name >> stu.score[0]>> stu.score[1]>> stu.score[2]
>> stu.score[3] >>stu.score[4];
return in;
}
int main()
{
string filename1;
int num = 0;
Student stu[100];
int ReadTextFile(string fname, Student stu[]);//从文件中读取TXT数据
void PrintScreen(Student stu[], int n);//打印数据在屏幕中
void Total(Student stu[],int n);//计算总分
void Rank(Student stu[], int n);//计算排名
void Sort(Student stu[], int n);//按学号排序
void WExcel(Student stu[], int n);//在Excel中输出
filename1 = "c:\\score.txt";
num=ReadTextFile(filename1, stu);
Total(stu,num);
Rank(stu, num);
Sort(stu, num);
WExcel(stu, num);
PrintScreen(stu, num);
}
//从文件中读取TXT数据
int ReadTextFile(string fname, Student stu[])
{
int i = 0;
ifstream fin(fname);
if (!fin)
{
cout << "source text file error\n";
return 0;
}
fin >> stu[i];
while (!fin.eof())
{
i++;
fin >> stu[i];
}
fin.close();
return i;
}
//打印数据在屏幕中
void PrintScreen(Student stu[], int n)
{
for (int i = 0; i <n; i++)
cout << stu[i];
}
void Total(Student stu[],int n)
{
for (int i=0; i < n; i++)
{
stu[i].total= stu[i].score[0] + stu[i].score[1] + stu[i].score[2]
+ stu[i].score[3] + stu[i].score[4];
}
}
//计算总分
void Rank(Student stu[], int n)
{
int temp,i,j;
for ( i = 0; i < n; i++)
{
temp = 0;
{for (j = 0; j < n; j++)
if (stu[i].total<=stu[j].total)
temp++;
stu[i].rank = temp;
}
}
}
//按学号排序
void Sort(Student stu[],int n)
{
int i, j;
Student temp;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if (stu[i].id>stu[j].id)
{
temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
//在Excel中输出
void WExcel(Student stu[], int n)
{
ofstream oFile;
oFile.open("1.csv", ios::out | ios::trunc);
oFile << "学号"<<"," << "姓名" << "," << "成绩1" << "," << "成绩2"
<< ","<<"成绩3"<<"," << "成绩4"<<" ,"<< "成绩5"<<","
<<"总分"<<","<<"排名"<<endl;
for (int i = 0; i < n; i++)
{
oFile << stu[i].id << "," << stu[i].name << "," << stu[i].score[0]
<< "," << stu[i].score[1]<< "," << stu[i].score[2] << ","
<< stu[i].score[3] << "," << stu[i].score[4]<<","<<stu[i].total
<<","<<stu[i].rank<<endl;
}
oFile.close();
}
最新文章
- 08.LoT.UI 前后台通用框架分解系列之——多样的Tag选择器
- SVN Client
- oracle 11g dbf数据文件从C盘迁移到D盘
- 上海敏行医学招聘物理仿真,3D图形人才
- 用jquery怎么实现点击显示,再一次点击隐藏
- xmind的第九天笔记
- Android学习四:数据库操作
- [DFNews] EIFT更新至1.2,支持iPhone4s及iPhone5物理获取
- spring 第一篇(1-1):让java开发变得更简单(下)
- linux 入门
- SqlSever中Index Seek的匹配规则(一)
- CSS设计之页面滚动条出现时防止页面跳动的方法
- XAML特殊字符
- rowid
- Spring AOP体系学习总结:
- linux 解压操作命令
- @Resource @Autowired 区别
- 【初码干货】关于.NET玩爬虫这些事
- Duilib第一步(III)-知识进阶
- 日常开发自己遇到异常(BUG未完待续!!!)
热门文章
- 本地web项目部署到服务器里连接不上数据库的解决办法
- LeetCode初级算法之数组:36 有效数独
- Day4 【Scrum 冲刺博客】
- filereader 和 window.URL.createObjectURL
- 重磅!Panda Global获悉立陶宛下周将发行区块链数字货币!
- elasticsearch的基本了解
- 使用MySQL乐观锁解决超卖问题
- [日常摸鱼]Vijos1083小白逛公园-线段树
- [日常摸鱼][POI2000]病毒-Tire图(AC自动机)+dfs
- 深度学习炼丹术 —— Taoye不讲码德,又水文了,居然写感知器这么简单的内容