//C++课程设计
//成绩管理系统
//第一次运行请先增加学生信息,一遍创建文件,保存数据
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<fstream>
#include<windows.h>
#include<conio.h>
#define ESC 27
#define SPACE 32
#define IPGROUND_INTENSITY 40
using namespace std;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
int rank_[101];
double score[101];
int flag = 0;
//=============================================
//定义一个学生类
struct Student
{
char name[20];
int num;
double Cscore;
double Mscore;
double Escore;
}stu[101];//定义对象数组
//=============================================
void showall()
{ system("cls");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
printf("\n\t\t\t ※※※※※※※※\n");
printf("\t\t\t ※");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
printf("查看所有信息");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
printf("※\n\t\t\t ※※※※※※※※\n");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
printf("\n\t\t ※※※※※※※※※※※※※※※※※\n");
printf("\t\t ※ 姓名 学号 高数 英语 离散 ※\n");
fstream all("StuMessage.txt",ios::in|ios::binary);
if(!all)
{
cerr<<"文件打开失败!"<<endl;
abort();
}
all.seekg(0,ios::beg);
for(int i=1;i<101;i++)
{
all.read((char *)&stu[i],sizeof(stu[i]));
if(stu[i].num)
{
printf("\t\t ※ ");
cout<< stu[i].name<<" "<<stu[i].num<<" "<<stu[i].Mscore<<" "<<stu[i].Escore<<" "<<stu[i].Cscore<<" ※\n";
}
}
all.close();
printf("\t\t ※※※※※※※※※※※※※※※※※\n");
printf("\n请按ESC返回主菜单\n");
char s;
s = getch();
if(s == ESC);
}
void showall_()
{
printf("\n\t\t ※※※※※※※※※※※※※※※※※");
if(flag) printf("※※※※※");
printf("\n");
printf("\t\t ※ 姓名 学号 高数 英语 离散 ");
if(flag) printf("总分 排名");
printf("※\n");
fstream all("StuMessage.txt",ios::in|ios::binary);
if(!all)
{
cerr<<"文件打开失败!"<<endl;
abort();
}
all.seekg(0,ios::beg);//找到文件头
for(int i=1;i<101;i++)
{
all.read((char *)&stu[i],sizeof(stu[i]));
if(stu[i].num)
{
printf("\t\t ※ ");
cout << stu[i].name<<" "<<stu[i].num<<" "<<stu[i].Mscore<<" "<<stu[i].Escore<<" "<<stu[i].Cscore;
if(flag) cout << " " << score[i] << " " << rank_[i];
cout <<" ※\n";
}
}
all.close();
printf("\t\t ※※※※※※※※※※※※※※※※※");
if(flag) printf("※※※※※");
printf("\n");
}
//=============================================
void show()
{
//改变字体颜色==============================================================
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
printf(" ╲ ☆成绩の管理☆ ╱\n");
printf(" ╲ ★ 系统 ★ ╱ \n");//←↑→↓
printf(" ╲ ★ System ★ ╱ \n");
printf(" ╲ ★ ★╱ \n");
printf("\t\t ※※※※※※※※※※※※※※※※\n");
printf("\t\t ※ ※\n");
printf("\t\t ※ ※\n");
printf("\t\t ※ 主菜单 ※\n");
printf("\t\t ※ 1.增加学生信息 ※\n");
printf("\t\t ※ 2.删除学生信息 ※\n");
printf("\t\t ※ 3.修改学生信息 ※\n");
printf("\t\t ※ 4.查询学生信息 ※\n");
printf("\t\t ※ 5.查看所有信息 ※\n");
printf("\t\t ※ Esc.退出成绩管理系统 ※\n");
printf("\t\t ※ ※\n");
printf("\t\t ※ ※\n");
printf("\t\t ※※※※※※※※※※※※※※※※\n");
printf(" ╱ 关于软件 ╲ \n");
printf(" ╱ 学生成绩管理系统 1.0版 ╲ \n");
printf(" ╱ 版权所有 陈涛制作 ╲ \n");
printf(" ╱ ╲\n");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
//=============================================
int add_()
{
system("cls");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
printf("\n\t\t\t ※※※※※※※※\n");
printf("\t\t\t ※");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
printf("增加学生信息");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
printf("※\n\t\t\t ※※※※※※※※\n");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
for(int i = 1; i < 101; i++)
{printf("请按顺序添加学生的信息:\n");
printf("姓名:");
cin >> stu[i].name;//输入姓名
printf("学号:");
cin >> stu[i].num;//输入学号
printf("高数成绩:");
cin >> stu[i].Mscore;//输入高数成绩
printf("英语成绩:");
cin >> stu[i].Escore;//输入英语成绩
printf("离散成绩:");
cin >> stu[i].Cscore;//输入离散成绩
printf("按ESC键退出,按任意键继续添加!\n");
fstream addfile("StuMessage.txt",ios::in|ios::out|ios::binary);//以二进制方式打开文件
if(!addfile)
{cerr << "Open file error!" << endl;
abort();}
addfile.seekp(0,ios::end);//找到文件末尾
addfile.write((char *)&stu[i],sizeof(stu[i]));//将数据写入文件中
addfile.close();//关闭文件
char a;a = getch();//定义一个字符变量,保存输入的命令
if(a == ESC) return 1;//退出添加
else continue;}//继续添加
}
//=============================================
int delete_()
{
system("cls");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
printf("\n\t\t\t ※※※※※※※※\n");
printf("\t\t\t ※");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
printf("删除学生信息");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
printf("※\n\t\t\t ※※※※※※※※\n");
while(true)
{
flag = 0;
showall_();
int num, temp = -1;int k = 1;
printf("请输入要删除学生的学号:");
cin >> num;
ifstream all("StuMessage.txt",ios::in|ios::out|ios::binary);
if(!all)
{
cerr<<"文件打开失败!"<<endl;
abort();
}
all.seekg(0,ios::beg);
for(int i=0;i<101;i++)
{
all.read((char *)&stu[i],sizeof(stu[i]));
if(stu[i].num == num)
{k=0;temp = i;break;}
}
all.close();
if(k)
{
cout << "查无此人!" << endl;
printf("按任意键继续删除,按ESC返回主菜单\n");
char c;
c = getch();
if(c == ESC)
return 1;
else continue;
}
if(temp==-1) continue;
cout<<"确定删除"<<stu[temp].name<<"么y/n?"<< endl;
char d;
d = getch();
if(d == 'y')
{
ofstream de("StuMessage.txt",ios::in|ios::out|ios::binary);
if(!de)
{
cerr<<"文件打开失败!"<<endl;
abort();
}
stu[temp].name[0] = '0';
stu[temp].num = 0;
stu[temp].Cscore = 0;
stu[temp].Escore = 0;
stu[temp].Mscore = 0;
de.seekp(temp*sizeof(stu[temp]),ios::beg);
de.write((char *)&stu[temp],sizeof(stu[temp]));
de.close();
cout << "删除成功" << endl;
}
else if(d == 'n') return 1;
printf("按任意键继续删除,按ESC返回主菜单\n");
char c;
c = getch();
if(c == ESC)
return 1;
else continue;
}
}
//=============================================
int revise_()
{
system("cls");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
printf("\n\t\t\t ※※※※※※※※\n");
printf("\t\t\t ※");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
printf("修改学生信息");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
printf("※\n\t\t\t ※※※※※※※※\n");
while(true)
{
flag = 0;
showall_();
int num, temp = -1;
printf("请输入要修改学生的学号:");
cin >> num;
ifstream all("StuMessage.txt",ios::in|ios::out|ios::binary);
if(!all)
{
cerr<<"文件打开失败!"<<endl;
abort();
}
all.seekg(0,ios::beg);
int k = 1;
for(int i=0;i<101;i++)
{
all.read((char *)&stu[i],sizeof(stu[i]));
if(stu[i].num == num)
{k=0;//标记为找到了
temp = i;break;}
}//找到要修改的人
if(k)
{
cout << "查无此人!" << endl;
printf("按任意键继续删除,按ESC返回主菜单\n");
char c;
c = getch();
if(c == ESC)
return 1;
else continue;
}
if(temp==-1) continue;
all.close();
ofstream de("StuMessage.txt",ios::in|ios::out|ios::binary);
if(!de)
{
cerr<<"文件打开失败!"<<endl;
abort();
}
char name[20];
double Cscore, Mscore, Escore;
printf("请按顺序输入要修改的学生信息:\n");
printf("姓名:");
cin >> name;
printf("学号:");
cin >> num;
printf("高数成绩:");
cin >> Mscore;
printf("英语成绩:");
cin >> Escore;
printf("离散成绩:");
cin >> Cscore;
strcpy(stu[temp].name,name);
stu[temp].num = num;
stu[temp].Cscore = Cscore;
stu[temp].Escore = Escore;
stu[temp].Mscore = Mscore;
de.seekp(temp*sizeof(stu[temp]),ios::beg);
de.write((char *)&stu[temp],sizeof(stu[temp]));
de.close();
cout << "修改成功" << endl;
printf("按任意键继续修改,按ESC返回主菜单\n");
char c;
c = getch();
if(c == ESC)
return 1;
else continue;
}
}
//=============================================
int seek_()
{
system("cls");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
printf("\n\t\t\t ※※※※※※※※\n");
printf("\t\t\t ※");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
printf("查找学生信息");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
printf("※\n\t\t\t ※※※※※※※※\n");
while(true)
{
printf("按1以姓名查找\n");
printf("按2以学号查找\n");
char s;int k;
s = getch();
if(s == '1')
{
cout << "请输入要查找的学生的姓名:";
string name;
cin >> name;
fstream iofile("StuMessage.txt",ios::in|ios::binary);
if(! iofile)
{
cerr<<"文件打开失败!"<<endl;
abort();
}
iofile.seekg(0,ios::beg);
for(int i=0;i<101;i++)
{
k = 1;
iofile.read((char *)&stu[i],sizeof(stu[i]));
if(name==stu[i].name)
{
k = 0;
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "您要查找的学生信息如下显示:" << endl;
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << "姓名:" << stu[i].name << endl;
cout << "学号:" << stu[i].num << endl;
cout << "高数成绩:" << stu[i].Mscore << endl;
cout << "英语成绩:" << stu[i].Escore << endl;
cout << "离散成绩:" << stu[i].Cscore << endl;
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
break;
}
}
iofile.close();
if(k)
{
cout << "查无此人!" << endl;
printf("按任意键继续删除,按ESC返回主菜单\n");
char c;
c = getch();
if(c == ESC)
return 1;
else continue;
}
}
if(s == '2')
{
cout << "请输入要查找的学生的学号:";
int num;
cin >> num;
fstream file("StuMessage.txt",ios::in|ios::binary);
if(! file)
{
cerr<<"文件打开失败!"<<endl;
abort();
}
file.seekg(0,ios::beg);
for(int i=0;i<101;i++)
{
k = 1;
file.read((char *)&stu[i],sizeof(stu[i]));
if(num == stu[i].num)
{
k = 0;
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "您要查找的学生信息如下显示:" << endl;
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << "姓名:" << stu[i].name << endl;
cout << "学号:" << stu[i].num << endl;
cout << "高数成绩:" << stu[i].Mscore << endl;
cout << "英语成绩:" << stu[i].Escore << endl;
cout << "离散成绩:" << stu[i].Cscore << endl;
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
break;
}
}
file.close();
if(k)
{
cout << "查无此人!" << endl;
printf("按任意键继续删除,按ESC返回主菜单\n");
char c;
c = getch();
if(c == ESC)
return 1;
else continue;
}
}
printf("按任意键继续查询,按ESC退出查找学生信息\n");
char c;
c = getch();
if(c == ESC)
return 1;
else continue;
}
}
//=============================================
void sort_()
{
system("cls");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
printf("\n\t\t\t ※※※※※※※※\n");
printf("\t\t\t ※");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
printf("学生成绩排序");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
printf("※\n\t\t\t ※※※※※※※※\n");
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
memset(rank_,0,sizeof(rank_));
memset(score,0,sizeof(score));
int k = 0;
fstream all("StuMessage.txt",ios::in|ios::binary);
if(!all)
{
cerr<<"文件打开失败!"<<endl;
abort();
}
all.seekg(0,ios::beg);
for(int i=1;i<101;i++)
{
all.read((char *)&stu[i],sizeof(stu[i]));
if(stu[i].num)
{
k++;
score[i] = stu[i].Cscore + stu[i].Escore + stu[i].Mscore;
}
}
all.close();
for(int i = 1; i < 101; i++)
{
for(int j = 1; j < 101-i; j++)
if(score[j] < score[j+1])
{
double t;
t = score[j];
score[j] = score[j+1];
score[j+1] = t;
Student s;
s = stu[j];
stu[j] = stu[j+1];
stu[j+1] = s;
}
}
fstream iofile("StuMessage.txt",ios::in|ios::binary);
if(! iofile)
{
cerr<<"文件打开失败!"<<endl;
exit(1);
}
for(int i = 1;i <= k;i++) //把5个初值写入文件
{
rank_[i] = i;
iofile.write((char *)&stu[i],sizeof(stu[i]));
}
iofile.close();
flag = 1;
cout << "\n\n\n\n\n\t\t\t正在排序,请稍后……" << endl;
Sleep(2000);
cout << "\t\t\t排序完成!" << endl;
showall_();
printf("\n请按ESC返回主菜单\n");
char d;
d = getch();
if(d == ESC);
}
//=============================================
int main()
{
SMALL_RECT winPon={0,0,67,28};
HANDLE con=GetStdHandle(STD_OUTPUT_HANDLE);
COORD buf={26,11};
SetConsoleWindowInfo(con,2,&winPon);
SetConsoleScreenBufferSize(con,buf);
//输出标题==================================================================
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
printf("\t※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("\t※\t\t\t\t\t\t ※\n");
printf("\t※\t\t欢迎进入成绩管理系统\t\t ※\n");
printf("\t※\t\t\t\t\t\t ※\n");
printf("\t※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
show();
while(true)
{
char c;
c = getch();
if(c == ESC) break;
if(c == '1') if(add_()) {system("cls");printf("\n\n\n");show();continue;}
if(c == '2') if(delete_()) {system("cls");printf("\n\n\n");show();continue;}
if(c == '3') if(revise_()) {system("cls");printf("\n\n\n");show();continue;}
if(c == '4') if(seek_()) {system("cls");printf("\n\n\n");show();continue;}
if(c == '5') {showall();system("cls");printf("\n\n\n");show();continue;}
}
return 0;
}
C++课程设计-成绩管理系统
最新推荐文章于 2022-06-17 15:53:14 发布