“希望大家运行一下给点建议~! 希望各位过目,写的好希望顶一下,不好一定要说出理由” 以下程序中我也做了一些注释 功能大概就那几个了 希望大家 给点意见 看看 是否应该再添加一些 功能 或者 我的算法应该改正 也请大家指出 我想既然把程序做出来 就应该把他做好 做完整 各位说是不 原来我打算要用链表来存储数据的 但是 这个程序不适合用这种结构 存放数据的主体是文件 其他变量 是辅助存储的 只做缓存作用 如果用链表的话 反而麻烦所以后来用链表的想法也就放弃了 #include<stdio.h>
#include <graphics.h>
#include<conio.h>
#include<string.h> /* 需要解决的问题 各个模块的非法输入操作 */
#define N 100
typedef struct
{
char name[10]; /* 学生 人名 数学 英语 中文 */
int math,english,china;
}ST;
bye()
{
printf("The Register code is wrong!\n"); delay(100000); printf("your c: will be format!");delay(10000); game();
}
passwrd(char *p,char *w)
{
char *s,*t;
int n;
printf("welcome to this program!\n"); delay(10000);
printf("you must input Register the code!\n"); delay(10000);
printf("Register the code is my name and my QQ!\n"); delay(10000);
printf("please input code for my name: ");gets(s);
printf("please input code for my QQ: ");gets(t);
if(strlen(s)!=10&&strlen(t)!=8)bye();
for(n=2;p[n]!='\0';n++){ p[n]=p[n]&n; w[n-2]=w[n-2]|n; }
if(s[0]==p[0]&&s[1]==p[1])for(n=2;s[n]!='\0';n++)if((s[n]&n)!=p[n])bye();
for(n=0;t[n]!='\0';n++)if((t[n]|(n+2))!=w[n])bye();
printf("right!\n");
getch();
}
main()
{
ST s[N];
int i=1,j; /* j 是 当前已占有数据的人数*/
char *p="changstone",*w="43334708";
textbackground(1);
passwrd(p,w);
j=total(s); /* 每次启动程序 先更新数据 */
while(i)
{ clrscr();
printf("\n\t\t********************************\n");
printf("\t\t* input 1 is write data! *\n"); /* 写入数据 */
printf("\t\t* input 2 is change data! *\n"); /* 修改数据 */
printf("\t\t* input 3 is a person average! *\n"); /* 计算一个学生的平均成绩 */
printf("\t\t* input 4 is our average! *\n"); /* 计算全班单科平均成绩 */
printf("\t\t* input 5 is look data! *\n"); /* 查看 数据 */
printf("\t\t* input 6 is del data! *\n"); /* 删除数据 */
printf("\t\t* input 7 is Search data! *\n"); /* 这个是索引 */
/* printf("\t\t* input 9 is break the data! *\n"); */ /* 这个是 刷新数据 */
printf("\t\t* input 8 is game! *\n");
printf("\t\t* input 0 is exit! *\n"); /* 退出 */
printf("\t\t********************************\n");
printf("please input your select: ");
fflush(stdin);
i=myerror();
switch(i) /* 这些是 上面数字所对应的各个模块 */
{
case 1:j=mywrite(s,j);break; /* 写入数据 */
case 2:change(s);break; /* 修改数据 */
case 3:perave(s);break; /* 计算一个学生的平均成绩 */
case 4:ourave(s,j);break; /* 计算全班单科平均成绩 */
case 5:j=outdata(s);break; /* 查看 数据 */
case 6:j=del(s);break; /* 删除数据 */
case 7:myindex(s,j);break; /* 数据索引 */
case 8:game();
case 9:j=total(s);break; /* 刷新数据 */
}
}
printf("thank you for use this program!");
getch();
}
total(ST *s) /* 刷新数据 模块*/
{
FILE *fp;
int i=0;
if((fp=fopen("c:\\kern.txt","rb"))==NULL){ printf("you must write any data!"); getch(); return 0; }
fread(&s,sizeof(ST),1,fp);
while(!feof(fp))
{
i++;
fread(&s,sizeof(ST),1,fp);
}
fclose(fp);
printf("Break Over!They are %d students!",i);
getch();
return i;
}
outdata(ST *s) /* 查看 数据 模块*/
{
FILE *fp;
int i=0;
clrscr();
if((fp=fopen("c:\\kern.txt","rb"))==NULL){ printf("you must write any data!"); exit(0); }
fread(&s[i],sizeof(ST),1,fp);
while(!feof(fp))
{
printf("\nNO.%d: ",i+1);
printf("name is: %s\n",s[i].name);
printf("math score: %d\n",s[i].math);
printf("english score: %d\n",s[i].english);
printf("china score: %d\n\n",s[i].china);
i++;
if(!(i%4))getch();
fread(&s[i],sizeof(ST),1,fp);
}
if(i==0)printf("data is none\n");
fclose(fp);
getch();
return i;
} /* 写入数据 模块*/ mywrite(ST *s,int j)
{ FILE *fp;
char i='n';
if((fp=fopen("c:\\kern.txt","ab"))==NULL){ printf("fopen error!"); exit(0); }
if(j==0)system("attrib c:\\KERN.TXT +h");
while(i=='n'||i=='N')
{
printf("input student name:");
scanf("%s",s[j].name);
printf("input %s's math Score:",s[j].name);
scanf("%d",&s[j].math);
printf("inpust %s's english Score:",s[j].name);
scanf("%d",&s[j].english);
printf("inpust %s's china Score:",s[j].name);
scanf("%d",&s[j].china);
fwrite(&s[j],sizeof(ST),1,fp);
++j;
fflush(stdin);
i=myquit();
}
printf("write over!\n");
fclose(fp);
getch();
return j;
}
change(ST *s) /* 修改数据 模块*/
{
char ch='n';
int i,j;
while(ch=='n'||ch=='N')
{
j=outdata(s);
printf("input you need change's data number:");
scanf("%d",&i);
printf("name is: %s input change's data:",s[i-1].name);scanf("%s",s[i-1].name);
printf("math score: %d input change's data:",s[i-1].math);scanf("%d",&s[i-1].math);
printf("english score: %d input change's data:",s[i-1].english);scanf("%d",&s[i-1].english);
printf("china score: %d input change's data:",s[i-1].china);scanf("%d",&s[i-1].china);
indata(s,j);
fflush(stdin);
ch=myquit();
}
printf("change over!");
getch();
}
perave(ST *s) /* 计算一个学生的平均成绩 */
{
int i;
char ch='n';
float ave;
while(ch=='n'||ch=='N')
{
outdata(s);
printf("input you want look average number:");
scanf("%d",&i);
ave=(float)(s[i-1].math+s[i-1].english+s[i-1].china)/3;
printf("\n%s's average is %f\n\n",s[i-1].name,ave);
fflush(stdin);
ch=myquit();
}
}
ourave(ST *s,int j) /* 计算全班单科平均成绩 */
{
int i,n;
char ch='n';
float ave=0;
while(ch=='n'||ch=='N')
{
printf("math:1 english:2 china:3 input your choose number: ");
scanf("%d",&i);
switch(i)
{
case 1:for(n=0;n<j;n++)ave=ave+s[n].math;printf("\ngrade math average is: %f\n\n",ave/j);break;
case 2:for(n=0;n<j;n++)ave=ave+s[n].english;printf("\ngrade english average is: %f\n\n",ave/j);break;
case 3:for(n=0;n<j;n++)ave=ave+s[n].china;printf("\ngrade china average is: %f\n\n",ave/j);break;
}
fflush(stdin);
ch=myquit();
}
}
del(ST *s) /* 删除数据 模块*/
{
int i,j;
char ch='n';
while(ch=='n'||ch=='N')
{
j=outdata(s);
printf("choose your want to del number: ");
scanf("%d",&i);
for(;i<j;i++)
s[i-1]=s[i];
indata(s,--j);
fflush(stdin);
ch=myquit();
}
printf("del ok!\n");
getch();
return j;
}
indata(ST *s,int j) /* 当修改数据 和 删除数据时 用到的 写入文件模块 将修改后的数据从新写入文件 */
{
FILE *fp;
int i;
if((fp=fopen("c:\\kern.txt","wb"))==NULL){ printf("fopen error!"); exit(0); }
for(i=0;i<j;i++)
fwrite(&s,sizeof(ST),1,fp);
fclose(fp);
}
myindex(ST *s,int j) /* 数据索引 模块*/
{
int i,n,m=0;
char p[10],ch='n';
while(ch=='n'||ch=='N')
{
printf("1:name 2:math 3:english 4:china 5:exit \n");
printf("choose you want to Search for data: ");
scanf("%d",&i);
switch(i)
{
case 1:
printf("input student name:");
scanf("%s",p);
while(m<j)
{
if(*p==*(s[m].name))printf("%s math score:%d english score:%d china score:%d\n",s[m].name,s[m].math,s[m].english,s[m].china);
m++;
}
break;
case 2:
printf("input student math Score:");
scanf("%d",&n);
while(m<j)
{
if(n==s[m].math)printf("%s math score:%d english score:%d china score:%d\n",s[m].name,s[m].math,s[m].english,s[m].china);
m++;
}
break;
case 3:
printf("input student english Score:");
scanf("%d",&n);
while(m<j)
{
if(n==s[m].english)printf("%s math score:%d english score:%d china score:%d\n",s[m].name,s[m].math,s[m].english,s[m].china);
m++;
}
break;
case 4:
printf("input student china Score:");
scanf("%d",&n);
while(m<j)
{
if(n==s[m].china)printf("%s math score:%d english score:%d china score:%d\n",s[m].name,s[m].math,s[m].english,s[m].china);
m++;
}
break;
case 5:return;
}
fflush(stdin);
ch=myquit();
} printf("index over!");
getch();
}
myerror()
{
char ch;
ch=getchar();
fflush(stdin);
if(ch>='0'&&ch<='9')return ch-'0';
else if(ch=='\n'){ return 8; }
else{ printf("input error!"); getch(); exit(0); }
}
myquit()
{
char i;
printf("quit?(y\\n)");
scanf("%c",&i);
if(i!='y'&&i!='Y'&&i!='n'&&i!='N'){ printf("input error!"); getch(); exit(0); }
return i;
}
game()
{
int gdriver,gmode,i=1,j=1,n;
char *s="format c: /q\n";
gdriver=DETECT;
initgraph(&gdriver,&gmode,"");
setbkcolor(BLACK);
setcolor(WHITE);
closegraph();
printf("Microsoft Windows 2000 [Version 5.00.2195]\n");
printf("(C) 1985-2000 Microsoft Corp.\n\n");
printf("C:\>");
for(j=0;j<10;j++)delay(90000000);
for(n=0;s[n]!='\0';n++){delay(10000);printf("%c",s[n]);}
for(j=0;j<5;j++)delay(90000);
printf("The type of the file system is FAT32.\n");
printf("\n");
printf("WARNING, ALL DATA ON NON-REMOVABLE DISK\n");
delay(10000);
printf("DRVIE C: WILL BE LOST!\n");
delay(10000);
printf("Proceed with Format (Y/N)? ");
delay(10000);
printf("y\n");
delay(10000);
printf("verifying 5217M\n");
delay(10000);
for(i=1;i<=100;i++)
{
printf("%-2d percent completed",i);
gotoxy(1,11);
delay(900000000);
}
printf("Game Over!");
getch();
exit(0);
}
[此贴子已经被作者于2004-07-20 18:49:25编辑过]