?? chengji1.cpp
字號:
#include <stdio.h>
#include <io.h>
#include <string.h>
#include <stdlib.h>
void Query(char *Name,int Course);
void Total(char *Name);
void Average(int Course);
void Add(char *CJ);
void Change(char *Name,int Course,char *XCJ);
void Delete(char *Name);
void TrimRight(char *Buf);
void main()
{ char Name[7];
int Course;
int Item;
while(1)
{ printf("*****************************\n");
printf("電信052 學號05054229 楊寶春制\n");
printf("簡易成績查詢\n");
printf("*****************************\n");
printf("1、查詢結果\n2、修改&增加數據\n3、計算個人總分\n4、計算單科平均\n5、退出系統\n");
printf("*****************************\n請選擇:");
scanf("%d",&Item);
if(Item==1)
{ printf("請輸入姓名:");
scanf("%s",Name);
printf("1、數學,2、物理,3、化學成績\n請選擇:");
scanf("%d",&Course);
Query(Name,Course);
}
else if(Item==2)
{printf("1、新增,2、修改,3、刪除\n請選擇:");
scanf("%d",&Item);
if(Item==1)
{ char CJ[20];
printf("請輸入:姓名數學成績物理成績(例如:吳七123456789)\n");
scanf("%s",CJ);
Add(CJ);
}
else if(Item==2)
{ printf("請輸入姓名:");
scanf("%s",Name);
printf("1、數學,2、物理,3、化學\n請選擇:");
scanf("%d",&Course);
char XCJ[3];
printf("請輸入新成績:");
scanf("%s",XCJ);
Change(Name,Course,XCJ);
}
else
{ printf("請輸入姓名:");
scanf("%s",Name);
Delete(Name);
}
}
else if(Item==3)
{ printf("請輸入姓名:");
scanf("%s",Name);
char buf[100];
FILE *fp=fopen("data.txt","r");
while(fgets(buf,7,fp)!=NULL)
{ TrimRight(buf);
if(strcmp(buf,Name)==0)
{ int Count=0;
for(int i=0;i<3;i++)
{ fgets(buf,4,fp);
Count+=atoi(buf);
}
printf("總成績為:%d\n",Count);
break;
}
else fseek(fp,11,1);
}
fclose(fp);
}
else if(Item==4)
{ printf("1、數學,2、物理,3、化學\n請選擇:");
scanf("%d",&Course);
Average(Course);
}
else break;
}
}
void Query(char *Name,int Course)
{ char buf[1000];
FILE *fp=fopen("data.txt","r");
while(fgets(buf,7,fp)!=NULL) //名字是6個字節,但另外還應該加1
{ TrimRight(buf); //去掉名字里的空格
if(strcmp(buf,Name)==0)
{ printf(strcat(Name,"的"));
if(Course==1)
{ fgets(buf,4,fp);
printf("數學成績為:%s\n",buf);
}
else if(Course==2)
{ fseek(fp,3,1);
fgets(buf,4,fp);
printf("物理成績為:%s\n",buf);
}
else
{ fseek(fp,6,1);
fgets(buf,4,fp);
printf("化學成績為:%s\n",buf);
}
return;
}
else fseek(fp,11,1); //當第一行沒有找到,則要轉到第二行,
} //因為成績是有9個字節,但回車還要2個
printf("查無此人!\n"); //字節,所以是再移9+2=11即可
fclose(fp);
}
void Average(int Course)
{ char buf[100];
FILE *fp=fopen("data.txt","r");
int Count=0;
int RS=0;
while(fgets(buf,7,fp)!=NULL)
{ RS++;
if(Course==2) fseek(fp,3,1);
fgets(buf,4,fp);
Count+=atoi(buf);
fgets(buf,17,fp);
}
if(Course==1) printf("數學的");
else if(Course==2) printf("物理的");
else printf("化學的");
printf("平均成績為:%d\n",Count/RS);
fclose(fp);
}
void Add(char *CJ)
{ FILE *fp=fopen("data.txt","a+");
fputs(strcat(CJ,"\n"),fp);
fclose(fp);
printf("完成增加!\n");
}
void Change(char *Name,int Course,char *XCJ)
{ char buf[100];
FILE *fp=fopen("data.txt","r+");
TrimRight(buf);
while(fgets(buf,7,fp)!=NULL)
{ if(strcmp(buf,Name)==0)
{printf(strcat(Name,"的"));
if(Course==1)
{
fputs(XCJ,fp);
printf("數學成績修改為:%s\n",XCJ);
}
else if(Course==2)
{
fseek(fp,3,1);
fputs(XCJ,fp);
printf("物理成績修改為:%s\n",XCJ);
}
else
{
fseek(fp,6,1);
fputs(XCJ,fp);
printf("化學成績修改為:%s\n",XCJ);
}
fclose(fp);
return;
}
else fseek(fp,11,1);
}
printf("查無此人!\n");
fclose(fp);
}
void Delete(char *Name)
{ char buf[100];
FILE *fp=fopen("data.txt","r+");
fseek(fp,0,2);
int Length=ftell(fp);
fseek(fp,0,0);
int WriteLoc,ReadLoc;
while(fgets(buf,7,fp)!=NULL)
{ TrimRight(buf);
if(strcmp(buf,Name)==0)
{ fseek(fp,-6,1);
WriteLoc=ftell(fp);
ReadLoc=WriteLoc+17;
while(ReadLoc<Length)
{ fseek(fp,ReadLoc,0);
fgets(buf,20,fp);
fseek(fp,WriteLoc,0);
fputs(buf,fp);
ReadLoc+=17;
WriteLoc+=17;
}
break;
}
else fseek(fp,11,1);
}
fclose(fp);
int handle=open("data.txt",2);
int a=chsize(handle,Length-17);
close(handle);
}
void TrimRight(char *Buf) //將名字里的空格用“\0”代替
{ int L=strlen(Buf);
for(int i=L-1;i>=0;i--) if(Buf[i]==' ') Buf[i]='\0';
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -