📄 1.cpp
字号:
// 1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
//LLIST
typedef struct Student_Score{
char Number[12];
char Name[16];
int Course_Score[3];
}*student;
#define Elem student
typedef struct LLIST{
Elem data;
struct LLIST *next;
}*LList;
void IninList(LList &L){//构造空链表L
L = NULL;
}
void DestroyList(LList &L){//销毁链表L
LList tmp = NULL;
while(L != NULL){
tmp = L;
L = L->next;
free(tmp);
}
}/**/
/*
void ClearList(LList &L){//重置链表L
DestroyList(L);
}
bool ListEmpty(LList L){//链表L判空
if(L == NULL) return true;
else return false;
}
*/
int ListLength(LList L){//返回链表L长度
int i = 0;
LList tmp = L;
while(tmp != NULL){
tmp = tmp->next;
i++;
}
return i;
}
bool GetElem(LList L, int i,Elem &e){//返回链表L中第i个元素,并赋值到e
int count = 0;
LList tmp = L;
while(tmp != NULL){
if(count == i){
e = tmp->data;
return true;
}
tmp = tmp->next;
count++;
}
return false;
}
void ListInsert(LList &L,Elem e){//将元素e插入链表L中的第i个元素之后
LList node = (LList)malloc(sizeof( LLIST));
node->data = e;
node->next = NULL;
if(L == NULL){
L = node;
}
else{
LList tmp = L;
while(tmp->next != NULL) tmp = tmp->next;
node->next = tmp->next;
tmp->next = node;
}
}
bool ListDelete(LList &L,int i,Elem &e){//删除链表L中第i个元素,并将其值返回到e
LList tmp = L;
if(L != NULL){
if(i == 0){
L = L->next;
e = tmp->data;
free(tmp);
return true;
}
else{
int count = 1;
while(tmp->next->next != NULL && i != count) {tmp = tmp->next;count++;}
//此时tmp的值为第i-1个元素的地址值或为倒数第二个元素的地址值
if(i >= count){
LList t = tmp->next;
e = t->data;
tmp->next = t->next;
free(t);
return true;
}
}
}
return false;
}
/**/
bool check(char *a,const char *b){//
for(;*a&&*b;a++,b++){
if(*a!=*b)return false;
}
if(*a||*b)return false;
return true;
}
bool Query(LList &L,char *key,int &count){
if(!L)return false;
LList l=L;
count=0;
if(check(l->data->Number,key) || check(l->data->Name,key))return true;
else l=l->next;
count++;
while(l && !( check(l->data->Number,key) || check(l->data->Name,key) ) ){
l=l->next;
count++;
}
if(!l)return false;
else return true;
}
bool Change(char *c,int &n){
int sum=0;
char *s;
s=c;
if( !s || *s<47 || *s>57)return false;
for(sum=*s-48;*(++s)!=0;)
{
if( *s>47 && *s<58)
{
sum*=10;
sum+=*s-48;
}
else return false;
}
n = sum;
return true;
}
void read(LList &L)
{
FILE *fp;
int i;
char infile[16];
LList &l=L;
scanf("%s",&infile);
if( !(fp=fopen(infile,"r")) ){printf("无法打开该文件\n");return;}
student tmp=(student)malloc(sizeof(Student_Score));
for(i=0;!feof(fp);i++)
{
fscanf(fp,"%s %s %d %d %d\n",tmp->Number,tmp->Name,&tmp->Course_Score[0],&tmp->Course_Score[1],&tmp->Course_Score[2]);
ListInsert(l,tmp);
sprintf(tmp->Number,"%d",ListLength(l));
tmp=(student)malloc(sizeof(Student_Score));
}
fclose(fp);
printf("已从%s文件中读入%d行学生成绩信息内容写入\n",infile,i);
}
void write(LList L){
FILE *fp;
int i,t=0;
char outfile[16];
LList l=L;
scanf("%s",&outfile);
if( !(fp=fopen(outfile,"w+")) ){printf("无法打开该文件\n");return;}
student tmp=(student)malloc(sizeof(Student_Score));
for(i=ListLength(l);t<i;t++){
GetElem(l,t,tmp);
fprintf(fp,"%s %s %4d %4d %4d\n",&tmp->Number,&tmp->Name,tmp->Course_Score[0],tmp->Course_Score[1],tmp->Course_Score[2]);
}
fclose(fp);
printf("已将%d行学生成绩信息内容写入%s文件中\n",i,outfile);
}
void PrintStuInfo(student tmp){
if(tmp)
printf("%s\t%s\t%d\t%d\t%d\n",tmp->Number,tmp->Name,tmp->Course_Score[0],tmp->Course_Score[1],tmp->Course_Score[2]);
else printf("错误!\n");
}
void StudentInfoTraverse(LList &L){//遍历学生成绩链表的内容
LList l=L;
if(!l){printf("当前学生成绩系统为空!\n");return;}
printf("以下为学生成绩信息\nNumber Name Math English Computer\n");
while(l)
{
printf("%s\t%s\t%d\t%d\t%d\n",l->data->Number,l->data->Name,l->data->Course_Score[0],l->data->Course_Score[1],l->data->Course_Score[2]);
l=l->next;
}
}
void AddStudent(LList &L){//插入学生成绩,输入信息包括姓名、三科成绩
char tmp[16];
int count=0,i=0,t;
student st=(student)malloc(sizeof(Student_Score));
for( i=0;;i++){
scanf("%s",tmp);
if(check(tmp,"end"))break;
if(!i && Query(L,tmp,t)){printf("该学生信息已经存在!\n");i=-1;flushall();continue;}
if(!i){
strcpy(st->Name,tmp);
sprintf(st->Number,"%d",++count);
int k=0;
while(k<3)
{
st->Course_Score[k]=0;//所有成绩清0
k++;
}
}
if(i) {
if( !Change(tmp,st->Course_Score[i-1]) || st->Course_Score[i-1]>100 ){printf("输入成绩不正确!\n");i=-1;continue;}
}
if(i==3){ListInsert(L,st);break;}
}
if(i==1)ListInsert(L,st);
printf("已插入第个%d学生成绩信息\n",ListLength(L));
}
void DeleteStudent(LList &L){
LList &l=L;
char Input[16];
int i;
if(!l){printf("当前信息为空\n");return;}
scanf("%s",Input);
if(Query(l,Input,i)){
printf("你要删除的信息为第%d行学生的信息\n",i+1);
student tmp=(student)malloc(sizeof(Student_Score));
GetElem(l,i,tmp);
PrintStuInfo(tmp);
ListDelete(l,i,tmp);
}
else printf("当前并没有包含%s的信息\n",Input);
}
void ChangeStudent(LList &L){
LList l=L;
char Input[16],change[16];
int i;
if(!l){printf("当前信息为空\n");return;}
scanf("%s",Input);
if(Query(l,Input,i)){
student tmp=(student)malloc(sizeof(Student_Score));
GetElem(l,i,tmp);
printf("要修改的数据内容为 ");
PrintStuInfo(tmp);
scanf("%s",change);
char *Score[]={"Name","Math","English","Computer",""};
int t=0;
for(;Score[t]!="";t++){
if(check(change,Score[t]))break;
}
if(t==4){printf("该行并没有包含%s项数据,可修改的数据项为Number Name Math English Computer",change);}
scanf("%s",change);
if(t)Change(change,tmp->Course_Score[t-1]);
else strcpy(tmp->Name,change);
}
else printf("当前并没有包含%s的信息\n",Input);
}
bool AverageScore(LList &L,float average[]){
if(!L){printf("当前并无学生信息\n");return false;}
LList l=L;
int i;
for(i=0;i<3;i++)average[i]=0;
for(;l;l=l->next){
for(i=0;i<3;i++){
average[i]+=l->data->Course_Score[i];
}
}
l=L;
for(i=0;i<3;i++)average[i]/=ListLength(l);
return true;
}
int main(int argc, char* argv[])
{
char *order[]={"query","add","change","delete","average","read","write","destroy","print","exit",""},Input[16],keyword[16];
int i;
printf("请输入以下指令集中的指令操作学生成绩系统.\n");
printf("指令集包括query(查询)、add(添加学生信息)、change(修改学生成绩)、\ndelete(删除学生信息)、average(求各科全班平均分)、");
printf("read(读取文件信息)、\nwrite(将信息写入文件)、destroy(销毁数据)、exit(退出)、print(打印当前系统信息)\n");
printf("指令格式:query keyword,delete keyword,read file,write file,average,exit,print\n\t add Name Math English Computer,change keyword value\n");
printf(" 其中keyword为查询、修改、删除的关键字,可以为学生编号或者学生姓名,\n");
printf(" 而value值为Name,Math,English,Computer中的任意一个\n (注:学生编号由系统生成)\n");
//printf("\n ");
LList L;
IninList(L);
while(1){
if(!L)printf(" 当前并无学生信息\n 建议插入学生数据,或者可输入exit退出\n");
scanf("%s",Input);
for(i=0;order[i]!="";i++){if(check(Input,order[i]))break;}
switch(i){
case 0: scanf("%s",keyword);
if(!Query(L,keyword,i))
printf("查询失败。\n无法找到关键字为%s字段的信息,keyword只包含学生编号或者学生姓名\n",keyword);
else {
printf("查询结果\n");
student tmp=(student)malloc(sizeof(Student_Score));
GetElem(L,i,tmp);
PrintStuInfo(tmp);
}
break;
case 1:AddStudent(L);break;
case 2:ChangeStudent(L);break;
case 3:DeleteStudent(L);break;
case 4:float average[3];
if(AverageScore(L,average)){
printf("averageScore Math English Computer\n");
printf(" %f %f %f \n",average[0],average[1],average[2]);
}
break;
case 5:read(L);break;
case 6:write(L);break;
case 7:DestroyList(L);IninList(L);break;
case 8:StudentInfoTraverse(L);break;
case 9:return 0;
default:printf("输入指令错误!\n");break;
}
flushall();//清空输入的缓存
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -