#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <iostream.h>
#define N 50 //系統中學生的最大數量
int n=3; //學生數量的初始值
struct student
{
int sid;
char name[10];
char num[14];
float chinese;
float english;
float clanguage;
float totalscore;
float average;
};
void print(bool menu)//打印基本信息
{
if(!menu)
{
cout<<"\t\t"<<"----------------------------------------------"<<endl;
cout<<"\t\t"<<"----------------------------------------------"<<endl;
cout<<"\t\t"<<"*** 學生成績管理系統 ***"<<endl;
cout<<"\t\t"<<"*** 1.文件錄入學生信息 ***"<<endl;
cout<<"\t\t"<<"*** 2.增加學生信息 ***"<<endl;
cout<<"\t\t"<<"*** 3.刪除學生信息 ***"<<endl;
cout<<"\t\t"<<"*** 4.查找學生信息 ***"<<endl;
cout<<"\t\t"<<"*** 5.學生成績排序 ***"<<endl;
cout<<"\t\t"<<"*** 6.顯示學生信息 ***"<<endl;
cout<<"\t\t"<<"*** 7.退出系統 ***"<<endl;
cout<<"\t\t"<<"----------------------------------------------"<<endl;
cout<<"\t\t"<<"----------------------------------------------"<<endl;
}
}
void GetInformation(struct student *arr,int n)//學生成績信息錄入
{
FILE *fp;
int i;
char buffer[100];
if(fp=fopen("stu.dat","r"))
{
for(i=0;i<n;i++)
{
fgets(buffer,100,fp);
sscanf(buffer,"%s %s %f %f %f",&arr[i].name,arr[i].num,&arr[i].chinese,&arr[i].english,&arr[i].clanguage);
arr[i].totalscore=arr[i].chinese + arr[i].english + arr[i].clanguage;
arr[i].average=arr[i].totalscore/3;
if(feof(fp))
break;
}
cout<<"所有學生的成績信息已錄入!"<<endl<<endl;
fclose(fp);
}
}
void Display(struct student* arr,int n)//學生成績信息顯示
{
cout<<"序號"<<"\t"<<"姓名"<<"\t"<<" 學號"<<"\t\t"<<"語文"<<"\t"<<"英語"<<"\t"<<"C語言"<<"\t"<<"平均分"<<"\t"<<"總分"<<endl;
for(int i=0;i<n;i++)
{
cout<<" "<<i+1<<"\t"<<arr[i].name<<"\t"<<arr[i].num<<"\t "<<arr[i].chinese<<"\t "<<arr[i].english<<"\t "<<arr[i].clanguage
<<"\t "<<arr[i].average<<"\t"<<arr[i].totalscore<<endl;
}
cout<<endl;
}
void Save(struct student* arr,int n)//學生成績信息保存,保存在源目錄下的student.txt
{
FILE *fp;
fp=fopen("stu1.dat","w+");
if(fp==NULL)
{
cout<<"文件不存在!"<<endl;
exit(0);
}
fwrite(arr,sizeof(struct student)*n,n,fp);
fclose(fp);
cout<<endl<<"*所有學生的成績信息已保存!!!"<<endl<<endl;
}
void Delete(struct student* arr,int n)//學生成績信息刪除
{
char number[10];
bool flag=false;
cout<<"要刪除的學生信息:"<<endl;
cout<<"請輸入該學生的學號:"<<endl;
cin>>number;
for(int i=0;i<n;i++)
{
if (!strcmp(number,arr[i].num))
{
strcpy(arr[i].name," ");
strcpy(arr[i].num," ");
arr[i].chinese=0;
arr[i].english=0;
arr[i].clanguage=0;
arr[i].totalscore=0;
arr[i].average=0;
Save(arr,n);
flag=true;
}
}
if(flag==true)
{
cout<<endl<<"該學生不存在!"<<endl;
}
}
void Search(struct student* arr,int n)//學生成績信息查詢
{
int temp,i;
bool flag=false;
cout<<"---------------1.按學號查找---------------"<<endl;
cout<<"---------------2.按姓名查找---------------"<<endl;
cout<<"---------------3.返回上層菜單-------------"<<endl;
cout<<"請按數字鍵(1—3)選擇相應的功能:"<<endl;
cin>>temp;
switch (temp)
{
case 1://按照學號查詢
{
char number[14];
cout<<"請輸入學號(10位整數的學號):"<<endl;
cin>>number;
for(i=0;i<n;i++)
{
if(!strcmp(number,arr[i].num))
{
cout<<"姓名"<<"\t"<<" 學號"<<"\t\t"<<"語文"<<"\t"<<"英語"<<"\t"<<"C語言"<<"\t"<<"平均分"<<"\t"<<"總分"<<endl;
cout<<arr[i].name<<"\t"<<arr[i].num<<"\t "<<arr[i].chinese<<"\t "<<arr[i].english<<"\t "<<arr[i].clanguage
<<"\t "<<arr[i].average<<"\t"<<arr[i].totalscore<<endl<<endl;
flag=true;
}
}
if(!flag)
{
cout<<endl<<"該學生不存在."<<endl<<endl;
}
}
break;
case 2://按照姓名查詢
{
char nam[10];
cout<<"請輸入姓名(漢字):"<<endl;
cin>>nam;
for(i=0;i<n;i++)
{
if(!strcmp(nam,arr[i].name))
{
cout<<"姓名"<<"\t"<<"學號"<<"\t\t"<<"語文"<<"\t"<<"英語"<<"\t"<<"C語言"<<"\t"<<"平均分"<<"\t"<<"總分"<<endl;
cout<<arr[i].name<<"\t"<<arr[i].num<<"\t"<<arr[i].chinese<<"\t"<<arr[i].english<<"\t"<<arr[i].clanguage
<<"\t"<<arr[i].average<<"\t"<<arr[i].totalscore<<endl<<endl;
flag=true;
}
}
if(!flag)
{
cout<<endl<<"該學生不存在."<<endl<<endl;
}
}
break;
default:
break;
}
}
void Sort(struct student* arr,int n)//學生成績排序
{
int temp,i;
bool flag=false;
student stemp;
cout<<"---------------1.按學號排序---------------"<<endl;
cout<<"---------------2.按總分排序---------------"<<endl;
cout<<"---------------3.返回上層菜單-------------"<<endl;
cout<<"請按數字鍵(1—3)選擇相應的功能:"<<endl;
cin>>temp;
switch (temp)
{
case 1://按照學號排序
{
int j,index;
char min[14];
for(i=0;i<n;i++)
{
index=i;
strcpy(min,arr[i].num);
for(j=i+1;j<n;j++)
{
if(strcmp(min,arr[j].num)>0)
{
index=j;
}
}
if(index!=i)
{
stemp=arr[index];
arr[index]=arr[i];
arr[i]=stemp;
}
}
Display(arr,n);
}
break;
case 2://按照總分排序
{
int bound,exchange=n-1;
while(exchange!=0)
{
bound=exchange;
exchange=0;
for(i=0;i<bound;i++)
{
if(arr[i].totalscore<arr[i+1].totalscore)
{
stemp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=stemp;
exchange=i;
}
}
}
Display(arr,n);
}
break;
default:
break;
}
}
void Add(struct student* arr)
{
bool flag=false;
n+=1;
cout<<"請根據下面提示輸入新的學生信息:"<<endl;
cout<<"姓名:";
cin>>arr[n-1].name;
cout<<"學號:";
cin>>arr[n-1].num;
cout<<"語文:";
cin>>arr[n-1].chinese;
cout<<"英語:";
cin>>arr[n-1].english;
cout<<"C語言:";
cin>>arr[n-1].clanguage;
arr[n-1].totalscore=arr[n-1].chinese + arr[n-1].english + arr[n-1].clanguage;
arr[n-1].average=arr[n-1].totalscore/3;
cout<<"新的學生信息已錄入!"<<endl<<endl;
flag=true;
}
int main(int argc,char* argv[])
{
struct student arr[N];
bool flag=true;
if(flag)
{
int select;
print(false);
cout<<"請按數字鍵(1或7)選擇相應的功能:"<<endl;
cin>>select;
while(0<select && select<7)
{
switch (select)
{
case 1:
GetInformation(arr,N);
print(true);
break;
case 2:
;
case 3:
;
case 4:
;
case 5:
;
case 6:
cout<<"學生基本信息未錄入,功能無法實現。"<<endl<<endl;
print(false);
cout<<"請按數字鍵(1或7)選擇相應的功能:"<<endl;
cin>>select;
break;
default:
break;
}
if(select==1)
break;
}
if(select>=7)
flag=false;
else
flag=true;
if(flag)
{
cout<<"請按數字鍵(1—7)選擇相應的功能:"<<endl;
cin>>select;
while(select>0 && select<7)
{
switch (select)
{
case 1:
cout<<"學生信息已錄入!"<<endl;
print(true);
break;
case 2:
Add(arr);
print(true);
break;
case 3:
Delete(arr,n);
print(true);
break;
case 4:
Search(arr,n);
print(true);
break;
case 5:
Sort(arr,n);
print(true);
break;
case 6:
Display(arr,n);
print(true);
break;
default:
break;
}
cout<<"請按數字鍵(1—7)選擇相應的功能:"<<endl;
cin>>select;
}
}
Save(arr,n);
cout<<"感謝使用本系統!"<<endl;
}
return 0;
}
另外c裡面是沒有bool類型的可以用int的0和1來代替