📄 qq d.cpp
字号:
#include "stdafx.h"
#include "iostream"
#include <stdio.h>
#include <iomanip>
#include <stdlib.h>
#include <malloc.h>
#include "fstream"
#include "windows.h"
using namespace std;
//定义学生数据结构体
struct Student
{
int Number;//学号
char Name[10];//姓名
float phy;//物理
float math;//数学
float eng;//英语
float chin;//语文
float zh;//综合
float total;//总分
Student *next;
};
fstream outstuf;
//函数声明部分
void Pause( );
void OutCaption( );
void OutSpace(int);
void TableLine(int);
void TableHead( );
void ShowLink( Student *);
int FindLink( Student *);
int MainMenu( );
void SaveLink( Student *);
Student *DeleteLink( Student *);
Student *GreatLink();
void LoadLink();
//清空输入流并无回显暂停等待回车
void Pause( )
{
int c;
while ( (c = getchar()) != '\n' && c != EOF )
clearerr(stdin);
getchar();
}
//该函数用于输出n个空格
void OutSpace(int n)
{
int i;
for(i=1;i<=n;i++)cout<<" ";
}
//输出首行或尾行制表符
void TableLine(int Judge)
{
int i;
if(Judge==0)cout<<" ┏";
else cout<<" ┗";
for(i=1;i<=32;i++)
{
if((i%4==0)&&(i!=32))
{
if(Judge==0)cout<<"┳";
if(Judge==1)cout<<"┻";
}
else cout<<"━";
};
if(Judge==0)cout<<"┓\n";
else cout<<"┛\n";
}
//该函数用于输出格式头
void TableHead( )
{
TableLine(0);
cout<<" ┃学号 ┃姓名 ┃物理 ┃数学 ┃英语 ┃语文 ┃综合 ┃总分 ┃";
}
//该函数用于输出程序标题
void OutCaption( )
{
int i;
//以下用制表符制表
cout<<"┏";
for(i=1;i<=38;i++)cout<<"━";
cout<<"┓┃";
OutSpace(26);
cout<<"学 生 成 绩 查 询 系 统";
OutSpace(20);
cout<<"┃";
cout<<"┗";
for(i=1;i<=38;i++)cout<<"━";
cout<<"┛";
}
//该函数用于绘制主界面
int MainMenu( )
{
system("cls");
system( "color 1F" );
OutCaption();
cout<<"\n\n";
OutSpace(32);
cout<<"1.输入学生数据\n";
OutSpace(32);
cout<<"2.查询学生成绩\n";
OutSpace(32);
cout<<"3.删除学生数据\n";
OutSpace(32);
cout<<"4.存储学生数据\n";
OutSpace(32);
cout<<"5.读取学生数据\n";
OutSpace(32);
cout<<"0.退出\n";
cout<<"\n\n\n";
OutSpace(31);
cout<<"请输入您的选项:";
int temp;
fflush(stdin);
cin>>temp;
return (temp);
}
//该函数用于建立单向链表并赋值
Student *GreatLink( )
{
system("cls");
OutCaption();
cout<<"\n\n";
OutSpace(26);
cout<<"请输入学生信息,以 0 结束\n\n\n";
OutSpace(32);
Student *head,*temp1,*temp2,*temp3;
head=NULL;
temp1=(struct Student *)malloc(sizeof( Student));
cout<<"请输入学生学号:";
fflush(stdin);
cin>>temp1->Number;
if(temp1->Number!=0)
{
OutSpace(32);
cout<<"请输入学生姓名:";
cin>>temp1->Name;
OutSpace(32);
cout<<"请输入物理成绩:";
cin>>temp1->phy;
OutSpace(32);
cout<<"请输入数学成绩:";
cin>>temp1->math;
OutSpace(32);
cout<<"请输入英语成绩:";
cin>>temp1->eng;
OutSpace(32);
cout<<"请输入语文成绩:";
cin>>temp1->chin;
OutSpace(32);
cout<<"请输入综合成绩:";
cin>>temp1->zh;
temp1->total = temp1->chin + temp1->math + temp1->eng + temp1->zh + temp1->phy ;
if( temp1->phy>150||temp1->phy<0||
temp1->math>150||temp1->math<0||
temp1->eng>150||temp1->eng<0||
temp1->chin>150||temp1->chin<0||
temp1->zh>150||temp1->zh<0 )
{
cout<<"分数输入错误,按ENTER重新操作\n";
Pause();
return 0;
}
while(temp1->Number!=0)
{
if(head==NULL)head=temp1;
temp2=(struct Student *)malloc(sizeof(struct Student));
temp1->next=temp2;//前一节点的后继指针指向新开辟的节点
temp3=temp1;//保留住前一节点的指针
temp1=temp2;//方面的循环
system("cls");
OutCaption();
cout<<"\n\n";
OutSpace(26);
cout<<"请输入学生信息,以 0 结束\n\n\n";
OutSpace(32);
cout<<"请输入学生学号:";
cin>>temp1->Number;
if(temp1->Number!=0)
{
OutSpace(32);
cout<<"请输入学生姓名:";
cin>>temp1->Name;
OutSpace(32);
cout<<"请输入物理成绩:";
cin>>temp1->phy;
OutSpace(32);
cout<<"请输入数学成绩:";
cin>>temp1->math;
OutSpace(32);
cout<<"请输入英语成绩:";
cin>>temp1->eng;
OutSpace(32);
cout<<"请输入语文成绩:";
cin>>temp1->chin;
OutSpace(32);
cout<<"请输入综合成绩:";
cin>>temp1->zh;
temp1->total = temp1->chin + temp1->math + temp1->eng + temp1->zh + temp1->phy ;
}
else
{
temp3->next=NULL;//前节点的后继指针为空,链表结束于前节点
}
if( temp1->Number == 0 ) break;
if( temp1->phy>150||temp1->phy<0||
temp1->math>150||temp1->math<0||
temp1->eng>150||temp1->eng<0||
temp1->chin>150||temp1->chin<0||
temp1->zh>150||temp1->zh<0 )
{
cout<<"分数输入错误,请重新输入\n";
delete temp1;
Student *temp1 = NULL;
temp1 = head;
Pause();
temp3->next=NULL;
continue;
}
}
}
free(temp1);//释放新开辟的未用节点,节省内存开销
return head;
}
//该函数用于查找指定学号的学生信息
int FindLink( Student *temp)
{
int iSearchNum;
system("cls");
OutCaption();
cout<<"\n\n";
if(temp!=NULL)
{
OutSpace(23);
cout<<"请在下面输入学生学号,输入返回\n\n\n";
OutSpace(32);
cout<<"请输入查询学号:";
fflush(stdin);
cin>>iSearchNum;
while((temp->Number!=iSearchNum)&&(temp->next!=NULL))
{
temp=(temp->next);
}
if(temp->Number==iSearchNum)
{
system("cls");
OutCaption();
cout<<"\n\n";
TableHead();
cout<<"\n ";
cout<<"┃"<<setw( 4 )<<temp->Number;
cout<<setw( 4 )<<"┃"<<setw( 4 )<<temp->Name;
cout<<setw( 4 )<<"┃"<<setw( 4 )<<temp->phy;
cout<<setw( 4 )<<"┃"<<setw( 3 )<<temp->math;
cout<<setw( 5 )<<"┃"<<setw( 4 )<<temp->eng;
cout<<setw( 4 )<<"┃"<<setw( 4 )<<temp->chin;
cout<<setw( 4 )<<"┃"<<setw( 4 )<<temp->zh;
cout<<setw( 4 )<<"┃"<<setw( 4 )<<temp->total
<<setw( 6 )<<"┃";
cout<<"\n";
TableLine(1);
cout<<"\n\n\n";
OutSpace(34);
cout<<"按回车键返回";
Pause();
return 0;
}
else
{
system("cls");
OutCaption();
cout<<"\n\n";
OutSpace(23);
cout<<"未能找到该笔数据,按回车键返回\n";
OutSpace(23);
Pause();
return 1;
}
}
else
{
OutSpace(23);
cout<<"尚未创建或读取列表,按回车键返回";
Pause();
return 1;
}
}
//用于向屏幕输出显示链表
void ShowLink( Student *temp)
{
system("cls");
OutCaption();
cout<<"\n\n";
TableHead();
cout<<"\n";
while(temp!=NULL)
{
cout<<"┃"<<setw( 4 )<<temp->Number;
cout<<setw( 4 )<<"┃"<<setw( 4 )<<temp->Name;
cout<<setw( 4 )<<"┃"<<setw( 4 )<<temp->phy;
cout<<setw( 4 )<<"┃"<<setw( 3 )<<temp->math;
cout<<setw( 5 )<<"┃"<<setw( 4 )<<temp->eng;
cout<<setw( 4 )<<"┃"<<setw( 4 )<<temp->chin;
cout<<setw( 4 )<<"┃"<<setw( 4 )<<temp->zh;
cout<<setw( 6 )<<"┃"<<setw( 4 )<<temp->total
<<setw( 6 )<<"┃";
cout<<"\n";
temp=temp->next;
}
TableLine(1);
cout<<"\n\n\n";
OutSpace(34);
cout<<"按回车键返回";
Pause();
}
//该函数用于删除学生信息
Student *DeleteLink( Student *temp)
{
Student *psctSaveHead=temp,*psctNewTemp;
int iSearchNum;
char cJudge;
system("cls");
OutCaption();
cout<<"\n\n";
if(temp!=NULL)
{
OutSpace(23);
cout<<"请在下面输入学生学号,输入返回\n\n\n";
OutSpace(32);
cout<<"请输入删除的学号:";
fflush(stdin);
cin>>iSearchNum;
if(iSearchNum==0)return (temp);
while((temp->Number!=iSearchNum)&&(temp->next!=NULL))
{
psctNewTemp=temp;
temp=(temp->next);
}
if(temp->Number==iSearchNum)
{
system("cls");
OutCaption();
cout<<"\n\n";
OutSpace(4);
cout<<"您要删除的数据是:\n";
TableHead();
cout<<"\n ";
cout<<"┃"<<setw( 4 )<<temp->Number;
cout<<setw( 4 )<<"┃"<<setw( 4 )<<temp->Name;
cout<<setw( 4 )<<"┃"<<setw( 4 )<<temp->phy;
cout<<setw( 4 )<<"┃"<<setw( 3 )<<temp->math;
cout<<setw( 5 )<<"┃"<<setw( 4 )<<temp->eng;
cout<<setw( 4 )<<"┃"<<setw( 4 )<<temp->chin;
cout<<setw( 4 )<<"┃"<<setw( 4 )<<temp->zh;
cout<<setw( 6 )<<"┃"<<setw( 4 )<<temp->total
<<setw( 6 )<<"┃";
TableLine(1);
cout<<"\n\n\n";
OutSpace(34);
cout<<"确定删除(y/n)?";
fflush(stdin);
cin>>&cJudge;
if(cJudge=='y'||cJudge=='Y')
{
if(temp==psctSaveHead)
{
temp=psctSaveHead;
psctSaveHead=(psctSaveHead->next);
free(temp);
}
else
if(temp->next==NULL)
{
psctNewTemp->next=NULL;
free(temp);
}
else
{
psctNewTemp->next=temp->next;
free(temp);
}
return (psctSaveHead);
}
else
return (psctSaveHead);
}
else
{
system("cls");
OutCaption();
cout<<"\n\n";
OutSpace(23);
cout<<"未能找到该笔数据,按回车键返回\n";
OutSpace(23);
Pause();
return (psctSaveHead);
}
}
else
{
OutSpace(23);
cout<<"尚未创建或读取列表,按回车键返回";
Pause();
return (psctSaveHead);
}
}
//该函数用于存储学生信息到文件
void SaveLink( Student *temp)
{
char cDir[100];
system("cls");
OutCaption();
cout<<"\n\n";
OutSpace(26);
cout<<"请输入保存文件的路径:\n";
OutSpace(26);
cin>>cDir;
outstuf.open( cDir,ios_base::out );
if( !outstuf )
{
system("cls");
OutCaption( );
cout<<"\n\n";
OutSpace(26);
cout<<"无法存储该文件,输入的路径有误!\n";
}
else
{
cout << "正在写入,请稍候.......\n\n\n\n";
Sleep( 1200 );
while(temp!=NULL)
{
outstuf<<"学生学号: "<<temp->Number
<<"\n学生姓名: "<<temp->Name
<<"\n物理成绩: "<<temp->phy
<<"\n数学成绩: "<<temp->math
<<"\n英语成绩: "<<temp->eng
<<"\n语文成绩: "<<temp->chin
<<"\n综合成绩: "<<temp->zh
<<"\n总分: "<<temp->total<<"\n\n";
temp=temp->next;
}
cout << "文件写入成功,1秒后自动返回!\n";
Sleep( 1000 );
outstuf.close();
}
}
//该函数用于从文件读取学生信息
void LoadLink( )
{
ifstream instuf;
char cDir[100];
system("cls");
OutCaption();
cout<<"\n\n";
OutSpace(26);
cout<<"请输入需要读取文件的路径:\n";
OutSpace(26);
cin>>cDir;
instuf.open( cDir,ios::in );
if( !instuf )
{
system("cls");
OutCaption();
cout<<"\n\n";
OutSpace(26);
cout<<"无法打开该文件,输入的文件名有误或不存在!\n";
OutSpace(26);
cout<<"请按回车键返回!";
Pause();
}
else
{
cout << "文件打开成功......."<<endl;
Sleep( 1200 );
char ch[100];
int i = 1;
while(!instuf.eof())
{
instuf >> ch;
cout << ch <<" "<<endl ;
i++;
}
}
instuf.close();
system("pause");
}
int _tmain(int argc, _TCHAR* argv[])
{
int iJudge=1,t=1;
Student *temp;
temp=NULL;
while(1)
{
switch(MainMenu())
{
case 1:
temp=GreatLink( );break;
case 2:
FindLink( temp );break;
case 3:
temp=DeleteLink( temp );break;
case 4:
SaveLink( temp );break;
case 5:
LoadLink( );break;
case 0:
exit( 0 );
default:
{
cout<<"输入错误,请重新输入,2秒后自动返回\n"<<endl;Sleep( 2000 );break;
}
}
}
system("pause");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -