学生管理系统(c++)

任务说明

输入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

相当不错的一个成绩管理系统 #include #include #include #include using namespace std; enum {SUBJECT=5};//一共五门 typedef struct { char subject[10];//科目名称 int score;//科目成绩 }markinfo; typedef struct studentnode { markinfo mark[SUBJECT]; int totalmark; char name[10];//学生姓名 studentnode * next; }studentnode; class student { studentnode * head; public: student(); int addstudent(); ~student(); int countmark(); int sortbymark(); int save(); int show(); int display(); int readfiletolist(); int searchbyname(); }; student::student() //用构造函数来初始化。 { head=new studentnode; head->next=NULL; } //1.输入学生姓名、成绩等数据,并保存在链表中。 int student::addstudent() { studentnode * p; int i; char check; system("cls"); cout<<"**********************"<<endl; cout<<"请输入学生信息:"<<endl; do { p=new studentnode; cin.ignore(); cout<name); i=0; p->totalmark=0; do { cout<mark[i].subject); cout<>p->mark[i].score; } while(p->mark[i].score>100||p->mark[i].scoretotalmark=p->totalmark+p->mark[i].score; getchar(); } while(++i!=SUBJECT); if(head->next==NULL) { head->next=p;p->next=NULL; } else { p->next=head->next; head->next=p; } cout<next; if(p==NULL) { cout<<"没有学生,请重新输入"<<endl;system("pause");return 0; } else { cout<<"***************"<<endl; cout<<"学生成绩汇总:"<<endl; while(p) { cout<<"姓名:"<name<<" 总成绩:"<totalmark<next; } } system("pause"); return 0; } //4.输出所有学生成绩到一个文件中。 int student::save() { char address[35]; int i; studentnode * p=head->next; cout<<"请输入保存的地址"<<endl; cin.ignore(); gets(address); ofstream fout; fout.open(address,ios::app|ios::out); while(p) { fout<<"*"; fout<name<<"*"; i=0; while(i!=SUBJECT) { fout<mark[i].subject<<"*"; fout<mark[i].score; i++; } //fout<next; } fout.flush(); fout.close(); cout<next; while(p) { s=p->next; delete p; p=s; } delete head; } //3.按照总成绩大小对记录进行排序 int student::sortbymark() { studentnode *move1=head->next; studentnode *move2,*max,*pre1,*pre2,*maxpre,*s=move1; if(head->next==NULL) { cout<<"没有记录,请添加"<next!=NULL;pre1=move1,maxpre=pre1,move1=move1->next,max=move1) { for(pre2=move1,move2=move1->next;move2!=NULL;pre2=move2,move2=move2->next) if(move2->totalmark>max->totalmark) { maxpre=pre2; max=move2; } if(move1->next==max) //交换max和move1。 { pre1->next=max; move1->next=max->next; max->next=move1; move1=max; } else { s=move1->next; move1->next=max->next; max->next=s; maxpre->next=move1; pre1->next=max; move1=max; } } cout<<"已经按照从大到小排序"<next; int i; if(head->next==NULL){cout<<"没有学生记录,请添加"<<endl;system("pause"); return 0;} else { while(p) { cout<<"姓名:"<name; i=1; while(i!=SUBJECT+1) { cout<<"科目:"<mark[i-1].subject; cout<<" 成绩:"<mark[i-1].score; i++; } cout<next; } } system("pause"); return 0; } //6:从文件按读取记录 int student::display() { ifstream fin; char buf[100]; char str[25]; cout<<"请输入路径及文件名:"<<endl; cin.ignore(); gets(str); fin.open(str); if(!fin) { cout<<"没有此文件"<<endl; system("pause"); return 0; } while(fin) { fin.getline(buf,sizeof(buf)); cout<<buf<<endl; } system("pause"); return 0; } //8从文件中读取数据,并将数据保存在链表中 int student::readfiletolist() { ifstream fin; int i; char str[25]; cout<<"请输入路径及文件名:"<<endl; cin.ignore(); gets(str); fin.open(str); if(!fin) { cout<<"没有此文件"<totalmark=0; fin.getline(p->name,100,'*'); i=0; while(i!=SUBJECT) { fin.getline(p->mark[i].subject,100,'*'); fin>>p->mark[i].score; p->totalmark+=p->mark[i].score; i++; } if(head->next==NULL) { head->next=p; p->next=NULL; } else { p=head->next; head->next=p; } } cout<<"信息已经保存在链表中"<next==NULL) { cout<<"没有学生,请添加或者从文件中读取"<next; char findname[10]; int i; cout<name,findname)) { cout<<"经查找,找到该生信息如下:"<<endl<<endl; cout<<"姓名:"<name; i=1; while(i!=SUBJECT+1) { cout<<"科目:"<mark[i-1].subject; cout<<" 成绩:"<mark[i-1].score; i++; } cout<next; } cout<<"没有此学生,请添加或者从文件中读取"<<endl; system("pause"); return 0; } int showmenu() { int choice; char * menu[9]={ "1:输入学生成绩保存到链表\n", "2:计算每位学生总成绩\n", "3:按照总成绩大小对记录进行排序\n", "4:输出所有学生成绩到一个文件中\n", "5:显示新输入的学生信息\n", "6:从文件中读取信息\n", "7:将文件信息保存在链表中\n", "8:根据姓名查找学生记录\n", "9:结束程序\n" }; cout<<" "<<"*****************************************************"<<endl; cout<<" *"<<" "<<"学生成绩管理系统"<<" *"<<endl; cout<<" "<<"*****************************************************"<<endl; for(choice=0;choice<9;choice++) cout<<" "<<menu[choice]; cout<<" "<<"*****************************************************"<<endl; cout<<"please choose to continue"<>choice; } while(choice>9||choice<1); return choice; } int main() { int menuitem,flag=1; student stu; while(flag) { system("cls"); menuitem=showmenu(); switch(menuitem) { case 1:{stu.addstudent();break;} case 2:{stu.countmark();break;} case 3:{stu.sortbymark();break;} case 4:{stu.save();break;} case 5:{stu.show();break;} case 6:{stu.display();break;} case 7:{stu.readfiletolist();break;} case 8:{stu.searchbyname();break;} case 9:{flag=0;break;} } } return 0; }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值