?? finalwork.c
字號:
#include<stdio.h>
#include<string.h>
void file();
void init(); //聲明函數
void init1();
void init2();
void init3();
void init4();
void doChoice1();
void doChoice2();
void doChoice3();
void doChoice4();
void doChoice5();
///////////////////////////////////////////////////////////////////////////////////////////////
#define FORMAT "%-7s%-9s%-10.3f%-14.3f%-12.3f%-14.3f%.3f" //定義FORMAT輸出格式
float total_ave,math_ave,data_ave,eng_ave,c_ave;
//定義全局變量 total_ave總平均分,math_ave數學,data_ave數據庫,eng_ave英語,c_ave C語言平均分
int n=0; //n學生個數
FILE *fp;
struct student //定義student結構體
{
char num[7]; //學號
char name[20]; //姓名
float score[4]; //成績
float average; //平均分
}stu[80]; //學生數組長度
struct student *p; //定義全局結構體指針變量
void load(char filename[]) //定義讀取文件中數據的函數
{
n=0; //學生個數n
if((fp=fopen(filename,"rb"))==NULL) //判斷文件是否存在
{
printf("Cannot open file.\n");
return; //文件不存在,返回主函數
}
do //讀取數據
{
if(fread(&stu[n],sizeof(struct student),1,fp)!=1) //判斷讀取是否成功
{
if(feof(fp)) return; //若讀取結束,返回主函數
printf("file read error!!!\n\n"); //讀取出錯
}
n++; //n學生人數加1
}while(1);
fclose(fp); //關閉文件
}
void save(char filename[]) //保存數據
{
int i;
if((fp=fopen(filename,"wb"))==NULL) //判斷文件是否存在
{
printf("Cannot open file.\n");
return;
}
else //保存數據
{
for(i=0;i<n;i++)
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1) //判斷數據保存是否成功
printf("file write error!!!");
fclose(fp); //關閉文件
}
}
void Edit() //編輯數據
{
char choice;
do //輸出菜單
{
init();
choice=getch(); //賦值
if(choice=='1')
doChoice1(); //執行命令
else if(choice=='2')
doChoice2();
else if(choice=='3')
doChoice3();
else if(choice=='4')
doChoice4();
else if(choice=='5')
doChoice5();
else if(choice=='6')
return; //返回
else
printf("Input error!!!!\n\n");
} while(1);
}
void newfile() //文件菜單
{
char ch,filename[15]; //filename文件名
do
{
file(); //輸出菜單
ch=getch();
if(ch=='1') //新建文件
{
printf("請輸入filename:");
gets(filename); //取文件名
fp=fopen(filename,"wb+");
printf("已成功建立數據庫文件。\n\n");
fclose(fp);
}
else if(ch=='2') //載入數據
{
printf("\n請輸入filename:");
gets(filename);
fp=fopen(filename,"rb"); //判斷是否存在該文件
if(fp==NULL) printf("輸入有誤,不存在該文件!!!\n\n"); //不存在該文件
else //存在該文件
{
load(filename); //載入數據
printf("已成功載入數據。\n\n");
}
}
else if(ch=='3') //編輯數據
{
printf("\n");
Edit();
}
else if(ch=='4')
{
printf("\n請輸入filename:");
gets(filename);
fp=fopen(filename,"rb"); //判斷是否存在該文件
if(fp==NULL) printf("輸入有誤,不存在該文件,請新建立文件?。?!\n\n"); //不存在該文件
else //存在該文件
{
save(filename); //保存數據
printf("已成功保存數據。\n\n");
}
}
else if(ch=='5') return; //返回主函數
else printf("Input error!!!\n\n");
}while(1);
}
main() //主函數
{ //filename文件名
system("cls"); //清屏 //調用函數
printf("\t\t\t歡迎使用學生考試成績數據處理系統\n");
printf("\t\t\t作者 麻閩政 電31 2003010885\n");
printf("\t*****************************************************************\n");
newfile();
printf("謝謝本處理系統^_^。\n");
}
void file() //文件菜單函數
{
printf("請選擇操作菜單:\n");
printf("\t②.載入數據\n");
printf("\t③.編輯數據\n");
printf("\t④.保存\n");
printf("\t⑤.退出\n");
}
void init() //菜單函數
{
printf("請選擇操作菜單:\n");
printf("\t①.增加或刪除學生記錄\n");
printf("\t②.查詢成績\n");
printf("\t③.統計平均成績\n");
printf("\t④.不及格統計\n");
printf("\t⑤.特定查詢\n");
printf("\t⑥.返回\n");
}
////////////////////////////////////////////////////////////////////////////////////////////
void init1() //菜單函數
{
printf("\n請選擇操作菜單:\n");
printf("\t①.增加學生的記錄\n");
printf("\t②.刪除學生的記錄\n");
printf("\t③.返回!!\n");
}
void Add() //添加記錄
{
char c;
do
{
printf("\n請輸入學生的學號:");
scanf("%s",stu[n].num);
printf("請輸入學生的姓名:");
scanf("%s",stu[n].name);
printf("請輸入<數學>成績:");
scanf("%f",&stu[n].score[0]);
printf("請輸入<數據庫概論>成績:");
scanf("%f",&stu[n].score[1]);
printf("請輸入<英語>成績:");
scanf("%f",&stu[n].score[2]);
printf("請輸入<C語言程序設計>成績:");
scanf("%f",&stu[n].score[3]);
getchar();
n++; //人數增加1
printf("繼續錄入附否?(按y繼續,其他退出)");
c=getch();
printf("\n");
}while(c=='y'||c=='Y');
printf("\t數據已成功輸入^_^!\n");
}
void Del() //刪除記錄
{
int i,flag=1; //定一變量,flag作記號
char number[8];
printf("請輸入學生的學號:"); //輸入學號
gets(number);
for(i=0;i<n;i++)
if(strcmp(stu[i].num ,number)==0) //尋找要刪除的數據
{
stu[i]=stu[n-1]; //換數據
n--; //人數減1
flag=0; //判斷是否存在該學生數據
}
if(flag) printf("輸入有誤,不存在該學生數據!?。n"); //flag=1,不存在該數據
printf("\t已成功刪除該學生數據^_^!\n");
}
void doChoice1() //選項1
{
char choice1;
do
{
init1(); //輸出菜單
choice1=getch();
if(choice1=='1') Add(); //添加記錄
else if(choice1=='2') Del(); //刪除記錄
else if(choice1=='3') return; //返回上一級菜單
else printf("Input error!!!!\n");
}while(1);
printf("\n");
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void init2() //菜單
{
printf("\n請選擇操作菜單:\n");
printf("\t①.輸入學號查成績(包括平均成績)\n");
printf("\t②.按平均成績順序輸出學生成績\n");
printf("\t③.按學號順序輸出所有學生成績\n");
printf("\t④.按某門課成績好壞順序輸出學生成績\n");
printf("\t⑤.返回??!\n");
}
void average() //求平均成績
{
float sum=0,sum0=0,sum1=0,sum2=0,sum3=0;
for(p=stu;p<stu+n;p++)
{
p->average=(p->score[0]+p->score[1]+p->score[2]+p->score[3])/4;
sum=sum+p->average;
sum0=sum0+p->score[0];
sum1=sum1+p->score[1];
sum2=sum2+p->score[2];
sum3=sum3+p->score[3];
}
total_ave=sum/n; //總成績求平均
math_ave=sum0/n; //數學平均成績
data_ave=sum1/n; //數據庫概論平均成績
eng_ave=sum2/n; //英語平均成績
c_ave=sum3/n; //c語言平均成績
}
void doChoice2_1() //輸入學號查成績
{
char number[8]; //學號字符數組
int flag=1;
printf("\n請輸入學號:"); //輸入學號
gets(number);
for(p=stu;p<stu+n;p++)
if(strcmp(number,p->num)==0) //尋找該學生數據
{ //輸出成績
printf("\n姓名%s 數學%.3f 數據庫概論%.3f ",p->name,p->score[0],p->score[1]);
printf("\n英語%.3f C語言程序設計%.3f 平均成績%.3f",p->score[2],p->score[3],p->average);
flag=0; //判斷是否存在該學生數據
}
printf("\n");
if(flag) printf("輸入有誤,沒有該學生數據?。?!\n"); //不存在該數據
}
void Oder1() //按成績排序
{
int i,j;
struct student t; //定義結構體t
for(i=0;i<=n-2;i++) //冒泡法排序
for(j=0;j<=n-2-i;j++)
if(stu[j].average<stu[j+1].average)
{
t=stu[j]; //互換數據
stu[j]=stu[j+1];
stu[j+1]=t;
}
}
void Oder2() //按學號排序
{
int i,j;
struct student t;
for(i=0;i<=n-2;i++) //冒泡法排序
for(j=0;j<=n-2-i;j++)
if(strcmp(stu[j].num,stu[j+1].num)>0)
{
t=stu[j];
stu[j]=stu[j+1];
stu[j+1]=t;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -