## 那些年我写过的代码 | 开源中国10周年

LeoXu 发布于 2018/08/08 01:55

08年我还在读大二，同学对我的印象比较接近书呆子那一类，而那时的我自认为存在感比较弱，所以说故事嘛，除了患点单相思之外，并没有什么好博诸位一哂的。

``````/* 标准文档模板 */

#include "Stdio.h"
#include "Conio.h"
#include "String.h"

#define Header2 "|Number  |Name    |Sex|Age |Math    |Chinese |English |\n"
#define FORMAT  "|%-8s|%-8s|%-3c|%-4d|%-8d|%-8d|%-8d|\n"
#define DATA p->num,p->name,p->sex,p->age,p->score[0],p->score[1],p->score[2]

/*int  saveflag=0;*/

typedef struct student
{
char num[20];
char name[20];
char sex;  /*1代表男，2代表女*/
int age;
int score[3];
int sum;
float avg;
struct student *next;
}Stu;

/*保存修改*/
{
Stu *p,*q;
FILE *fp;
fp=fopen(sl,"wb");
if(fp==NULL)
{
printf("\nfile save error!");
return;
}
while(p!=NULL)
{
q=p;
p=p->next;
}
/*链结新旧链表*/
while(p)
{
fwrite(p,sizeof(Stu),1,fp);
p=p->next;
}
fclose(fp);
}

/*新建学生成绩数据表*/
void new_sl()
{
FILE *fp;
Stu s;
float x;
int i,sum=0,avg;
char bname[20];
char c;
printf("input the new score list name:");
/*输入字符串为则空循环*/
do
{
scanf("%s",bname);
}
while(!(sizeof(bname)-1));
if((fp=fopen(bname,"wb"))==NULL)
{
printf("score list init error.\n");
exit(0);
}
do
{
printf("input the student number(input '-1' to exit):");
do
{
scanf("%s",&s.num);
}
while(!(sizeof(s.num)-1));
getchar();
if(strcmp(s.num,"-1")!=0)
{
printf("input student's name:");
do
{
scanf("%s",&s.name);
}
while(!(sizeof(s.name)-1));
getchar();
printf("input the student's sex:(m\\f)\n");
do
{
scanf("%c",&c);
}
while((c!='f')&&(c!='m'));
s.sex=c;
printf("input the student's age(10~20):");
do
{
scanf("%d",&s.age);
}
while(!(s.age>=10&&s.age<=20));
printf("input the score of Yuwen,Yingyu and Shuxue(0~100):");
for(i=0;i<3;i++)
{
do
{
scanf("%d",&s.score[i]);
sum+=s.score[i];
}
while(!(s.score[i]>=0&&s.score[i]<=100));
}
s.sum=sum;
s.avg=(float)(sum/3);
s.next=NULL;
fwrite(&s,sizeof(Stu),1,fp);
}
else break;
}
while(1);
fclose(fp);
}

/* 判定是否创建新的学生成绩数据表的函数*/
void sl_create()
{
char ch;
textcolor(2);
printf("Do you want to create new score list?\n");
printf("Press 'y' to create or press any key to ship:");
ch=getchar();
getchar();
if(ch=='y'||ch=='Y') new_sl();
}

/*打开并读取一个学生成绩文件函数*/
{
FILE *sfp;
Stu *s_last;
Stu *p;
/*打开文件*/
sfp=fopen(slname,"rb");
if(sfp==NULL)
{
return NULL;
}
/*从文件中读取学生成绩表，放入链表中存放*/
while(!feof(sfp))
{
p=(Stu *)malloc(sizeof(Stu));
{
p->next=NULL;
s_last->next=p;
s_last=p;
}
}
fclose(sfp);
/*返回读出的成绩表文件头指针*/
exit(0);
}

/*浏览学生成绩*/
{
Stu *p;
clrscr();
{
printf("The list is empty.\n");
return;
}
if(!p)
{
printf("\nNo score record!\n");
getchar();
}
printf("\n\n");
while(p)
{
printf(FORMAT,DATA);
p=p->next;
}
getchar();
return;
}

{
int select5;
int i=0,max,min,m1=0,m2=0,m3=0,mi1=0,mi2=0,mi3=0,s1,s2,s3,a1,a2,a3;
Stu *p;
scanf("%d",&select5);
switch(select5)
{
case 0:break;
case 1:
printf("~~~~~~~~~~~~~~~~~~~REPORT BY STUDENT!~~~~~~~~~~~~~~~~~~\n");
printf("|NUM     |NAME    |SUM     |AVG     |MAX     |MIN     |\n");
while(p)
{
max=p->score[0];
min=p->score[0];
if(p->score[0]<p->score[1]) max=p->score[1];
if(p->score[1]<p->score[2]) max=p->score[2];
if(p->score[0]>p->score[1]) min=p->score[1];
if(p->score[1]>p->score[2]) min=p->score[2];
printf("|%-8s|%-8s|%-8d|%-8.0f|%-8d|%-8d|\n",p->num,p->name,p->sum,p->avg,max,min);
p=p->next;
}
printf("Press any key to continue:");
getchar();
break;
case 2:
printf("~~~~~~~~~~~~~REPORT BY STUDENT!~~~~~~~~~~~~~~~\n");
printf("|Courses |Sum     |Avg     |Max     |Min     |\n");
while(p)
{
s1=s1+p->score[0];
s2=s2+p->score[1];
s3=s3+p->score[2];
if(m1<p->score[0])m1=p->score[0];
if(mi1>p->score[0])mi1=p->score[0];
if(m2<p->score[1])m2=p->score[1];
if(mi2>p->score[1])mi2=p->score[1];
if(m3<p->score[2])m3=p->score[2];
if(mi3>p->score[2])mi3=p->score[2];
i=i+1;
p=p->next;
}
a1=(float)(s1/i);
a2=(float)(s2/i);
a3=(float)(s3/i);
printf("|Math    |%-8d|%-8.0f|%-8d|%-8d|\n",s1,a1,m1,mi1);
printf("|Chinese |%-8d|%-8.0f|%-8d|%-8d|\n",s2,a2,m2,mi2);
printf("|English |%-8d|%-8.0f|%-8d|%-8d|\n",s3,a3,m3,mi3);
printf("Press any key to continue:");
getchar();
break;
default:
break;
}
getch();
}

{
Stu *p;
clrscr();
{
printf("The list is empty.\n");
return;
}
if(!p)
{
printf("\nNo score record!\n");
getchar();
}
printf("\n\n");
while(p)
{
printf(FORMAT,DATA);
p=p->next;
break;
}
getchar();
return;
}

/*查看学校是否已经存在函数*/
{
Stu *p;
while(p)
{
if(strcmp(p->num,num)==0) return 1;
p=p->next;
}
return 0;
}

/*添加学生信息函数*/
{
int sum=0,avg,i,j=1;
char c;
clrscr();
while(r->next!=NULL)
{
r=r->next;
}
while(j)
{
printf("input student's number(input '-1'to back main menu):");
p=(Stu *)malloc(sizeof(Stu));
do
{
scanf("%s",&p->num);
}
while(!(sizeof(p->num)-1));
/*查看学号是否已经存在循环*/
{
printf("the students has existed,input again:\n");
do
{
scanf("%s",&p->num);
}
while(!(sizeof(p->num)-1));
}
if(strcmp(p->num,"-1")==0)
{
printf("Press any key to exit:\n");
getchar();
j=0;
}
else
{
printf("input student's name:");
do
{
scanf("%s",&p->name);
}
while(!(sizeof(p->name)-1));
getchar();
printf("input the student's sex:(m\\f)\n");
do
{
scanf("%c",&c);
}
while((c!='f')&&(c!='m'));
p->sex=c;
printf("input the student's age(10~20):");
do
{
scanf("%d",&p->age);
}
while(!(p->age>=10&&p->age<=20));
printf("input the score of Yuwen,Yingyu and Shuxue(0~100):");
for(i=0;i<3;i++)
{
do
{
scanf("%d",&p->score[i]);
sum+=p->score[i];
}
while(!(p->score[i]>=0&&p->score[i]<=100));
}
p->sum=sum;
p->avg=(float)(sum/3);
p->next=NULL;
r->next=p;
r=p;
}
}
printf("Save or not(y\\n)?\n");
do
{
scanf("%c",&c);
}
while((c!='y')&&(c!='n'));
getchar();
if(c=='y')
{
printf("Save success,Press any key to continue:");
getchar();
}
else
{
printf("Adding process over,Press any key to continue:");
getchar();
}
}

/*删除成绩记录相关函数*/
/*保存删除结果*/
{
Stu *p;
FILE *fp;
fp=fopen(sl,"wb");
if(fp==NULL)
{
printf("\nfile save error!");
return;
}
while(p)
{
fwrite(p,sizeof(Stu),1,fp);
p=p->next;
}
fclose(fp);
}

/*按姓名删除*/
{
Stu *p,*r;
char sname[20];
clrscr();
{
printf("\nNO student record!\n");
}
printf("Input student's name:\n");
do
{
scanf("%s",sname);
}
while(!(sizeof(sname)-1));
while(p&&strcmp(p->name,sname)!=0)
{
r=p;
p=p->next;
}
if(strcmp(p->name,sname)==0)
{
r->next=p->next;
free(p);
printf("Delete success.\n");
}
else printf("No fine.\n");
getchar();
}

/*按学号删除*/
{
Stu *p,*r;
char snum[20];
clrscr();
{
printf("\nNO student record!\n");
}
printf("Input student's name:\n");
do
{
scanf("%s",snum);
}
while(!(sizeof(snum)-1));
while(p&&strcmp(p->num,snum)!=0)
{
r=p;
p=p->next;
}
if(strcmp(p->num,snum)==0)
{
r->next=p->next;
free(p);
printf("Delete success.\n");
}
else printf("No fine.\n");
getchar();
}

/*删除进程主函数*/
{
int select2;
Stu *p;
char c;
scanf("%d",&select2);
switch(select2)
{
case 1:
getchar();
break;
case 2:
break;
case 0:
break;
default:
break;
}
printf("Save or not(y\\n)?\n");
do
{
scanf("%c",&c);
}
while((c!='y')&&(c!='n'));
getchar();
if(c=='y')
{
printf("Save success,Press any key to continue:");
getchar();
}
else
{
printf("Delete process over,Press any key to continue:");
getchar();
return p;
}
}

/*查找学生成绩记录函数*/
{
char c;
int select3,sum,i;
char s[20];
printf("\nIput your select(Press keys except '1'and '2'to exit) :");
scanf("%d",&select3);
switch(select3)
{
case 1:
printf("Input the student's name:");
do
{
scanf("%s",s);
}
while(!(sizeof(s)-1));
while(p)
{
if(strcmp(p->name,s)==0)
{
break;
}
else p=p->next;
}
break;
case 2:
printf("Input the student's number:");
do
{
scanf("%s",s);
}
while(!(sizeof(s)-1));
while(p)
{
if(strcmp(p->num,s)==0)
{
break;
}
else p=p->next;
}
break;
case 0:
break;
default:
break;
}
printf("Search process over,press any key to continue:");
getchar();
}

/*修改学生成绩记录函数*/
{
int select4,sum,i;
char c;
char s[20];
Stu *p,*q,*r;
scanf("%d",&select4);
switch(select4)
{
case 1:
q=(Stu *)malloc(sizeof(Stu));
clrscr();
printf("Input the student's name(input '-1' to exit):");
do
{
scanf("%s",q->name);
}
while(!(sizeof(q->name)-1));
while(p)
{
if(strcmp(p->name,q->name)==0)
{
printf("\nInput student's num:");
do
{
scanf("%s",&q->num);
}
while(!(sizeof(q->num)-1));
/*查看学号是否已经存在循环*/
{
printf("the students has existed,input again:\n");
do
{
scanf("%s",&q->num);
}
while(!(sizeof(q->num)-1));
}
printf("input the student's sex:(m\\f)\n");
do
{
scanf("%c",&c);
}
while((c!='f')&&(c!='m'));
q->sex=c;
printf("input the student's age(10~20):");
do
{
scanf("%d",&q->age);
}
while(!(q->age>=10&&q->age<=20));
printf("input the score of Yuwen,Yingyu and Shuxue(0~100):");
for(i=0;i<3;i++)
{
do
{
scanf("%d",&q->score[i]);
sum+=q->score[i];
}
while(!(q->score[i]>=0&&q->score[i]<=100));
}
q->sum=sum;
q->avg=(float)(sum/3);
q->next=NULL;
printf("Save or not(y\\n)?\n");
do
{
scanf("%c",&c);
}
while((c!='y')&&(c!='n'));
if(c=='y')
{
while(p)
{
if(strcmp(p->name,q->name)==0)
{
r->next=q;
q->next=p;
free(p);
break;
}
else
{
r=p;
p=p->next;
}
}
printf("Save success,Press any key to continue:");
getchar();
}
else
{
printf("Alting process over,Press any key to continue:");
getchar();
}
printf("hahah");
getchar();
getchar();
}
else
{
r=p;
p=p->next;
}
}
break;
case 2:
q=(Stu *)malloc(sizeof(Stu));
clrscr();
printf("Input the student's num(input '-1' to exit):");
do
{
scanf("%s",q->num);
}
while(!(sizeof(q->num)-1));
while(p)
{
if(strcmp(p->num,q->num)==0)
{
printf("\nInput student's name:");
do
{
scanf("%s",&q->name);
}
while(!(sizeof(q->name)-1));
/*查看学号是否已经存在循环*/
do
{
scanf("%c",&c);
}
while((c!='f')&&(c!='m'));
q->sex=c;
printf("input the student's age(10~20):");
do
{
scanf("%d",&q->age);
}
while(!(q->age>=10&&q->age<=20));
printf("input the score of Yuwen,Yingyu and Shuxue(0~100):");
for(i=0;i<3;i++)
{
do
{
scanf("%d",&q->score[i]);
sum+=q->score[i];
}
while(!(q->score[i]>=0&&q->score[i]<=100));
}
q->sum=sum;
q->avg=(float)(sum/3);
q->next=NULL;
printf("Save or not(y\\n)?\n");
do
{
scanf("%c",&c);
}
while((c!='y')&&(c!='n'));
if(c=='y')
{
while(p)
{
if(strcmp(p->name,q->name)==0)
{
r->next=q;
q->next=p;
free(p);
break;
}
else
{
r=p;
p=p->next;
}
}
printf("Save success,Press any key to continue:");
getchar();
}
else
{
printf("Alting process over,Press any key to continue:");
getchar();
}
printf("hahah");
getchar();
getchar();
}
else
{
r=p;
p=p->next;
}
}
break;
case 0:
break;
default:break;
}
}
/*排序汇总函数*/
{
int select6;
Stu *p,*q,*r;
{
printf("No Record.Press Any Key To Continue:");
getch();
return;
}
scanf("%d",&select6);
switch(select6)
{
case 0:
break;
case 1:
if(p->next=NULL)
{
clrscr();
printf("Press any key to continue:");
getch();
return;
}
q=p->next;
while(q)
{
if(p->score[0]<q->score[0])
{
r->next=p->next;
p->next=q->next;
q->next=p;
q=p->next;
}
else
{
r=p;
p=p->next;
q=p->next;
}
}
clrscr();
printf("Press any key to continue:");
break;
case 2:
if(p->next=NULL)
{
clrscr();
printf("Press any key to continue:");
getch();
return;
}
q=p->next;
while(q)
{
if(p->score[1]<q->score[1])
{
r->next=p->next;
p->next=q->next;
q->next=p;
q=p->next;
}
else
{
r=p;
p=p->next;
q=p->next;
}
}
clrscr();
printf("Press any key to continue:");
break;
case 3:
if(p->next=NULL)
{
clrscr();
printf("Press any key to continue:");
getch();
return;
}
q=p->next;
while(q)
{
if(p->score[2]<q->score[2])
{
r->next=p->next;
p->next=q->next;
q->next=p;
q=p->next;
}
else
{
r=p;
p=p->next;
q=p->next;
}
}
clrscr();
printf("Press any key to continue:");
break;
default:
break;
}
getch();
}

/*主界面函数*/
{
system("cls");
textcolor(2);
gotoxy(10,5);
cprintf("      The Student Score Management System--%-s\n",s);
gotoxy(10,8);
gotoxy(10,9);
cprintf("** 1 Scan                 2 Add           3 Delete  **\n");
gotoxy(10,10);
cprintf("** 4 Search               5 Alt           6 Report  **\n");
gotoxy(10,11);
cprintf("** 7 Sort                                 0 Exit    **\n");
gotoxy(10,12);
cprintf("******************************************************\n");
}

/*删除界面函数*/
{
system("cls");
textcolor(2);
gotoxy(10,5);
cprintf("      The Student Score Management System--%-s\n",s);
gotoxy(10,8);
gotoxy(10,9);
cprintf("** 1 Delete By Student Name                         **\n");
gotoxy(10,10);
cprintf("** 2 Delete By Student Number                       **\n");
gotoxy(10,11);
cprintf("**                                        0 Exit    **\n");
gotoxy(10,12);
cprintf("******************************************************\n");
}

/*查找学生界面函数*/
{
system("cls");
textcolor(2);
gotoxy(10,5);
cprintf("      The Student Score Management System--%-s\n",s);
gotoxy(10,8);
gotoxy(10,9);
cprintf("** 1 Search By Student Name                         **\n");
gotoxy(10,10);
cprintf("** 2 Search By Student Number                       **\n");
gotoxy(10,11);
cprintf("**                                        0 Exit    **\n");
gotoxy(10,12);
cprintf("******************************************************\n");
}

/*修改学生界面函数*/
{
system("cls");
textcolor(2);
gotoxy(10,5);
cprintf("      The Student Score Management System--%-s\n",s);
gotoxy(10,8);
gotoxy(10,9);
cprintf("** 1 Alt By Student Name                            **\n");
gotoxy(10,10);
cprintf("** 2 Alt By Student Number                          **\n");
gotoxy(10,11);
cprintf("**                                        0 Exit    **\n");
gotoxy(10,12);
cprintf("******************************************************\n");
}

/*成绩汇总*/
{
system("cls");
textcolor(2);
gotoxy(10,5);
cprintf("      The Student Score Management System--%-s\n",s);
gotoxy(10,8);
gotoxy(10,9);
cprintf("** 1 Report By Student                              **\n");
gotoxy(10,10);
cprintf("** 2 Report By Course                               **\n");
gotoxy(10,11);
cprintf("**                                        0 Exit    **\n");
gotoxy(10,12);
cprintf("******************************************************\n");
}

/*排序汇总界面函数*/
{
system("cls");
textcolor(2);
gotoxy(10,5);
cprintf("      The Student Score Management System--%-s\n",s);
gotoxy(10,8);
gotoxy(10,9);
cprintf("** 1 Sort By Chinese                                **\n");
gotoxy(10,10);
cprintf("** 2 Sort By Mathematics                            **\n");
gotoxy(10,11);
cprintf("** 3 Sort By English                      0 Exit    **\n");
gotoxy(10,12);
cprintf("******************************************************\n");
}

void main()
{
FILE *fp;
char sl[20];
int select1;
/* 是否创建新的学生成绩数据表*/
sl_create();
/*打开读取学生成绩数据文件*/
clrscr();
printf("Choose a student score list to open and read.\n");
printf("Input the student list file name(input '-1' to exit):");
do
{
do
{
scanf("%s",sl);
if(strcmp(sl,"-1")==0) exit(0);
}
while(!(sizeof(sl)-1));
}
clrscr();
/*主界面*/
do
{
scanf("%d",&select1);
switch(select1)
{
case 0:
exit(0);
break;
case 1:
getch();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
default:
break;
}
}
while(1);
printf("GO ON!!!\n");
getch();
}
``````

2008-2018，你好，开源时光机 | 开源中国10周年故事征集活动正在进行中

0