学生管理系统(c++)
#include "targetver.h"#include <stdio.h>#include <string.h>#include <Windows.h>#include <tchar.h>#include <math.h>#include <fstream>#include <stdlib.h>#includ
任务说明
输入30个学生的学号、姓名和5门课程的成绩,计算总分并按照总分排出名次,最后按照学号顺序打印成绩单, 并把成绩单输出为excel文件。
【源cpp】
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include <tchar.h>
#include <math.h>
#include <fstream>
#include <stdlib.h>
#include <streambuf>
#include <iostream>
using namespace std;
/*----------------------------------*
学生信息-结构体设计
*-----------------------------------*/
typedef struct Student
{
long sid; //学号
char name[3]; //姓名
int score[5];//成绩
int total; //总分
int rank; //名次
}SS;
//1.读取学生基本数据
SS* readDataFromFile(char *s)
{
cout << "-------------第一步: 从文件读取学生的成绩信息-----------" << endl;
SS *stu;// 开辟新空间,存取文件中的每个学生信息
int index = 0;
ifstream inf(s);
if (!inf)
{
cout << "Cannot open the file!\n";
return 0;
}
stu = (SS*)malloc(sizeof(SS)*30);
for (int i = 0; i < 30; i++)
{
inf >> (stu[i].sid) >>(stu[i].name) >>stu[i].score[0]>> stu[i].score[1]>> stu[i].score[2]>> stu[i].score[3]>> stu[i].score[4];
}
for (int i = 0; i < 30; i++)
{
cout<< stu[i].sid<<" " << stu[i].name<<" " << stu[i].score[0]<<" " << stu[i].score[1]<<" " << stu[i].score[2]<<" " << stu[i].score[3]<<" " << stu[i].score[4]<<endl;
}
inf.close();
return stu;
}
//2.计算总分
void calcuScore(SS stu[])
{
cout<<"------第二步: 计算每个学生的总分--------"<<endl;
for (int i = 0; i < 30; i++)
{
stu[i].total =stu[i].score[0] + stu[i].score[1] + stu[i].score[2] + stu[i].score[3] + stu[i].score[4];
}
for (int i = 0; i < 30; i++)
{
cout << stu[i].total << " ";
}
}
//3.按照总分排出名次
void Sort(SS stu[])
{
cout << "-------------第三步:按照总分排出名次--------------" << endl;
for (int i = 0; i < 30; i++)
{
int num = 0;
for (int j = 0; j < 30; j++)
{
if (stu[i].total < stu[j].total)num++;
}
stu[i].rank = num + 1;
}
for (int i = 0; i < 30; i++)
{
cout << stu[i].rank << " ";
}
}
//4.按照学号顺序打印成绩单
void Write_In_Excel(SS stu[])
{
//定义文件输出流
ofstream oFile;
//打开要输出的文件
oFile.open("scoresheet.csv", ios::out | ios::trunc); // 这样就很容易的输出一个需要的excel 文件
oFile << "学号" << "," << "姓名" << "," << "成绩1" << "," << "成绩2" << "," << "成绩3"<<"," << "成绩4"<< "," << "成绩5"<<","<<"总分"<<","<<"名次"<< endl;
SS *temp;
temp = (SS*)malloc(sizeof(SS));
for (int i = 0; i < 30; i++)
{
for (int j = i + 1; j < 30; j++)
{
if (stu[i].sid > stu[j].sid)
{
temp[0] = stu[i];
stu[i] = stu[j];
stu[j] = temp[0];
}
}
}
for (int i = 0; i < 30; i++)
{
oFile <<stu[i].sid << "," <<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();
}
int main()
{
SS *pstu = NULL; //学生数组-结构体数组指针实现
//2.读取学生信息
pstu=readDataFromFile("score.txt");
calcuScore(pstu);
Sort(pstu);
Write_In_Excel(pstu);
return 0;
}
【运行结果】
记得三连支持啊😀😀
附:学生成绩文件
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
更多推荐
所有评论(0)