C语言程序100行

作者&投稿:牧穆 (若有异议请与网页底部的电邮联系)
C语言100行的程序,要自己写的 谢谢. 所有分全给了.~

这是我写的贪吃蛇,楼主看行不行?不行换一个
#include
#include
#include
#include

int length=1;//蛇的当前长度,初始值为1
int line[100][2];//蛇的走的路线
int head[2]={40,12};//蛇头
int food[2];//食物的位置
char direction;//蛇运动方向
int x_min=1;x_max=77; y_min=2; y_max=23;//设置蛇的运动区域
int tail_before[2]={40,12};//上一个状态的蛇尾
char direction_before='s';//上一个状态蛇的运动方向
int live_death=1;//死活状态,0死,1活
int eat_flag=0;//吃食物与否的状态。0没吃 1吃了
int max=0;
int delay;//移动延迟时间

void gotoxy(int x, int y)//x为列坐标,y为行坐标
{
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}

void hidden()//隐藏光标
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=0;//赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut,&cci);
}

void update_score()
{
gotoxy(2,1);
printf("我的分数:%d",length);
gotoxy(42,1);
printf("最高记录:%d",max);
}

void create_window()
{
gotoxy(0,0);
printf("╔══════════════════╦═══════════════════╗");
printf("║ ║ ║");
printf("╠══════════════════╩═══════════════════╣");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("╚══════════════════════════════════════╝");




}

void update_line()
{
int i;
if(eat_flag==0)//吃了食物就不用记住上一个状态的蛇尾,否则会被消掉
{
tail_before[0]=line[0][0];//记住上一个状态的蛇尾
tail_before[1]=line[0][1];


for(i=0;i<length-1;i++)//更新蛇头以后部分
{
line[i][0]=line[i+1][0];
line[i][1]=line[i+1][1];
}

line[length-1][0]=head[0];//更新蛇头
line[length-1][1]=head[1];

}

}

void initial()
{
FILE *fp;
gotoxy(head[0],head[1]);
printf("蛇");

line[0][0]=head[0];//把蛇头装入路线
line[0][1]=head[1];

if((fp=fopen("highest","r"))==NULL)
{
fp=fopen("highest","w");
fprintf(fp,"%d",0);
max=0;
fclose(fp);
}//第一次使用时,初始化奖最高分为0

else
{
fp=fopen("highest","r");
fscanf(fp,"%d",&max);
}
update_score();

}

void createfood()
{
int flag,i;
srand((unsigned)time(NULL));
for(;;)
{
for(;;)
{
food[0]=rand()%(x_max+1);
if(food[0]%2==0 && food[0]>x_min)
break;
}//产生一个偶数横坐标

for(;;)
{
food[1]=rand()%(y_max);
if(food[1]>y_min)
break;
}


for(i=0,flag=0;i<length;i++)//判断产生的食物是否在蛇身上,在flag=1,否则为0
if(food[0]==line[i][0] && food[1]==line[i][1])
{ flag=1; break; }

if(flag==0)// 食物不在蛇身上 结束循环
break;

}
gotoxy(food[0],food[1]);
printf("蛇");


}

void show_snake()
{

gotoxy(head[0],head[1]);
printf("蛇");

if(eat_flag==0)//没吃食物时消去蛇尾
{
gotoxy(tail_before[0],tail_before[1]);
printf(" ");//消除蛇尾
}
else
eat_flag=0;//吃了食物就回到没吃状态



}


char different_direction(char dir)
{
switch(dir)
{
case 'a': return 'd';
case 'd': return 'a';
case 'w': return 's';
case 's': return 'w';

}
}

void get_direction()
{
direction_before=direction;//记住蛇上一个状态的运动方向

while(kbhit()!=0) //调试
direction=getch();


if( direction_before == different_direction(direction) || (direction!='a' && direction!='s' && direction!='d' && direction!='w') ) //新方向和原方向相反,或获得的方向不是wasd时,保持原方向
direction=direction_before;


switch(direction)
{
case 'a': head[0]-=2; break;
case 'd': head[0]+=2; break;
case 'w': head[1]--; break;
case 's': head[1]++; break;
}
}

void live_state()//判断蛇的生存状态
{
FILE *fp;
int i,flag;
for(i=0,flag=0;i<length-1;i++)//判断是否自己咬到自己
if( head[0]==line[i][0] && head[1]==line[i][1])
{
flag=1;
break;
}
if(head[0]=x_max || head[1]=y_max || flag==1)
{
system("cls");
create_window();
update_score();
gotoxy(35,12);
printf("游戏结束!
");
Sleep(500);
live_death=0;
fp=fopen("highest","w");
fprintf(fp,"%d",max);//保存最高分
}

}

void eat()
{
if(head[0]==food[0]&&head[1]==food[1])
{
length++;
line[length-1][0]=head[0];//更新蛇头
line[length-1][1]=head[1];
eat_flag=1;
createfood();
if(length>max)
max=length;
update_score();
if(delay>100)
delay-=30;//加速
}
}


main()
{
int x=0,y=0;
int i;
hidden();//隐藏光标
create_window();
initial();
createfood();
for(direction='s',delay=600;;)
{
get_direction();
eat();
update_line();
live_state();//判断生死状态
if(live_death==1)
{
show_snake();


}
else
break;
Sleep(delay);


}

}
这是在VC6.0环境下运行的,如果你用的TC,我可以帮你改成TC环境下运行的

//学生成绩管理系统C代码/*头文件*/#include #include#include /*其它说明*/#include /*字符串函数*/#include /*内存操作函数*/#include /*字符操作函数*/#include /*动态地址分配函数*/#define LEN sizeof(STUDENT)typedef struct stu /*定义结构体数组用于缓存数据*/{char num[6];char name[5];int score[3];int sum;float average;int order;struct stu *next;}STUDENT;/*函数原型*/STUDENT *init(); /*初始化函数*/int menu_select(); /*菜单函数*/STUDENT *create(); /*创建链表*/void print(STUDENT *head); /* 显示全部记录*/void search(STUDENT *head); /*查找记录*/STUDENT *delete(STUDENT *head); /*删除记录*/STUDENT *sort(STUDENT *head); /*排序*/STUDENT *insert(STUDENT *head,STUDENT *newnode); /*插入记录*/void save(STUDENT *head); /*保存文件*/STUDENT *load(); /*读文件*//*主函数界面*/main(){STUDENT *head,newnode;head=init(); /*链表初始化,使head的值为NULL*/for(;;) /*循环无限次*/{switch(menu_select()) {case 1:head=create();break;case 2:print(head);break;case 3:search(head);break;case 4:head=delete(head);break;case 5:head=sort(head);break;case 6:head=insert(head,&newnode);break; /*&newnode表示返回地址*/case 7:save(head);break;case 8:head=load(); break;case 9:exit(0); /*如菜单返回值为9则程序结束*/}}}/*初始化函数*/STUDENT *init(){return NULL; /*返回空指针*/}/*菜单选择函数*/menu_select(){int n;struct date d; /*定义时间结构体*/getdate(&d); /*读取系统日期并把它放到结构体d中*/printf("press any key to enter the menu......"); /*按任一键进入主菜单*/getch(); /*从键盘读取一个字符,但不显示于屏幕*/clrscr(); /*清屏*/printf("********************************************************************************
");printf(" Welcome to
");printf("
The student score manage system
");printf("*************************************MENU***************************************
");printf("1. Enter the record
"); /*输入学生成绩记录*/printf("2. Print the record
"); /*显示*/printf("3. Search record on name
"); /*寻找*/printf("4. Delete a record
"); /*删除*/printf("5. Sort to make new a file
"); /*排序*/printf("6. Insert record to list
"); /*插入*/printf("7. Save the file
"); /*保存*/printf("8. Load the file
"); /*读取*/printf("9. Quit
"); /*退出*/printf("
Made by Hu Haihong.
");printf("********************************************************************************
");printf("%d\\%d\\%d
",d.da_year,d.da_mon,d.da_day); /*显示当前系统日期*/do{ printf("
Enter your choice(1~9):"); scanf("%d",&n); }while(n9); /*如果选择项不在1~9之间则重输*/ return(n); /*返回选择项,主函数根据该数调用相应的函数*/}/*输入函数*/STUDENT *create(){int i,s;STUDENT *head=NULL,*p; /* 定义函数.此函数带回一个指向链表头的指针*/clrscr();for(;;) {p=(STUDENT *)malloc(LEN); /*开辟一个新的单元*/ if(!p) /*如果指针p为空*/ {printf("
Out of memory."); /*输出内存溢出*/ return (head); /*返回头指针,下同*/ } printf("Enter the num(0:list end):"); scanf("%s",p->num); if(p->num[0]=='0') break; /*如果学号首字符为0则结束输入*/ printf("Enter the name:"); scanf("%s",p->name); printf("Please enter the %d scores
",3); /*提示开始输入成绩*/ s=0; /*计算每个学生的总分,初值为0*/ for(i=0;iscore[i]); if(p->score[i]score[i]>100) /*确保成绩在0~100之间*/ printf("Data error,please enter again.
"); }while(p->score[i]score[i]>100); s=s+p->score[i]; /*累加各门成绩*/ } p->sum=s; /*将总分保存*/ p->average=(float)s/3; /*先用强制类型转换将s转换成float型,再求平均值*/ p->order=0; /*未排序前此值为0*/ p->next=head; /*将头结点做为新输入结点的后继结点*/ head=p; /*新输入结点为新的头结点*/ } return(head); }/* 显示全部记录函数*/void print(STUDENT *head){int i=0; /* 统计记录条数*/STUDENT *p; /*移动指针*/clrscr();p=head; /*初值为头指针*/printf("
************************************STUDENT************************************
");printf("-------------------------------------------------------------------------------
");printf("| Rec | Num | Name | Sc1 | Sc2 | Sc3 | Sum | Ave | Order |
");printf("-------------------------------------------------------------------------------
");while(p!=NULL) { i++; printf("| %3d | %4s | %-4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|
", i, p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order); p=p->next; }printf("-------------------------------------------------------------------------------
");printf("**************************************END**************************************
");}/*查找记录函数*/void search(STUDENT *head){STUDENT *p; /* 移动指针*/char s[5]; /*存放姓名用的字符数组*/clrscr();printf("Please enter name for searching.
");scanf("%s",s);p=head; /*将头指针赋给p*/while(strcmp(p->name,s) && p != NULL) /*当记录的姓名不是要找的,或指针不为空时*/ p=p->next; /*移动指针,指向下一结点*/ if(p!=NULL) /*如果指针不为空*/ {printf("
*************************************FOUND************************************
"); printf("-------------------------------------------------------------------------------
"); printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |
"); printf("-------------------------------------------------------------------------------
"); printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|
", p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order); printf("-------------------------------------------------------------------------------
"); printf("***************************************END**************************************
"); } else printf("
There is no num %s student on the list.
",s); /*显示没有该学生*/}/*删除记录函数*/STUDENT *delete(STUDENT *head){int n;STUDENT *p1,*p2; /*p1为查找到要删除的结点指针,p2为其前驱指针*/char c,s[6]; /*s[6]用来存放学号,c用来输入字母*/clrscr();printf("Please enter the deleted num: ");scanf("%s",s);p1=p2=head; /*给p1和p2赋初值头指针*/while(strcmp(p1->num,s) && p1 != NULL) /*当记录的学号不是要找的,或指针不为空时*/ {p2=p1; /*将p1指针值赋给p2作为p1的前驱指针*/ p1=p1->next; /*将p1指针指向下一条记录*/ }if(strcmp(p1->num,s)==0) /*学号找到了*/ {printf("**************************************FOUND************************************
"); printf("-------------------------------------------------------------------------------
"); printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |
"); printf("-------------------------------------------------------------------------------
"); printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|
", p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order); printf("-------------------------------------------------------------------------------
"); printf("***************************************END**************************************
"); printf("Are you sure to delete the student Y/N ?"); /*提示是否要删除,输入Y删除,N则退出*/ for(;;) {scanf("%c",&c); if(c=='n'||c=='N') break; /*如果不删除,则跳出本循环*/ if(c=='y'||c=='Y') { if(p1==head) /*若p1==head,说明被删结点是首结点*/ head=p1->next; /*把第二个结点地址赋予head*/ else p2->next=p1->next; /*否则将一下结点地址赋给前一结点地址*/ n=n-1; printf("
Num %s student have been deleted.
",s); printf("Don't forget to save.
");break; /*删除后就跳出循环*/ } } } else printf("
There is no num %s student on the list.
",s); /*找不到该结点*/return(head);}/*排序函数*/STUDENT *sort(STUDENT *head){int i=0; /*保存名次*/STUDENT *p1,*p2,*t,*temp; /*定义临时指针*/temp=head->next; /*将原表的头指针所指的下一个结点作头指针*/head->next=NULL; /*第一个结点为新表的头结点*/while(temp!=NULL) /*当原表不为空时,进行排序*/ { t=temp; /*取原表的头结点*/ temp=temp->next; /*原表头结点指针后移*/ p1=head; /*设定移动指针p1,从头指针开始*/ p2=head; /*设定移动指针p2做为p1的前驱,初值为头指针*/ while(t->averageaverage&&p1!=NULL) /*作成绩平均分比较*/ { p2=p1; /*待排序点值小,则新表指针后移*/ p1=p1->next; } if(p1==p2) /*p1==p2,说明待排序点值大,应排在首位*/ { t->next=p1; /*待排序点的后继为p*/ head=t; /*新头结点为待排序点*/ } else /*待排序点应插入在中间某个位置p2和p1之间,如p为空则是尾部*/ { t->next=p1; /*t的后继是p1*/ p2->next=t; /*p2的后继是t*/ } }p1=head; /*已排好序的头指针赋给p1,准备填写名次*/while(p1!=NULL) /*当p1不为空时,进行下列操作*/ { i++; /*结点序号*/ p1->order=i; /*将结点序号赋值给名次*/ p1=p1->next; /*指针后移*/ }printf("Sorting is sucessful.
"); /*排序成功*/return (head);}/*插入记录函数*/STUDENT *insert(STUDENT *head,STUDENT *newnode){STUDENT *p0,*p1,*p2;int n,sum1,i;p1=head; /*使p1指向第一个结点*/p0=newnode; /*p0指向要插入的结点*/printf("
Please enter a newnode record.
"); /*提示输入记录信息*/printf("Enter the num:");scanf("%s",newnode->num);printf("Enter the name:");scanf("%s",newnode->name);printf("Please enter the %d scores.
",3);sum1=0; /*保存新记录的总分,初值为0*/for(i=0;iscore[i]); if(newnode->score[i]>100||newnode->score[i]score[i]>100||newnode->score[i]score[i]; /*累加各门成绩*/ }newnode->sum=sum1; /*将总分存入新记录中*/newnode->average=(float)sum1/3;newnode->order=0;if(head==NULL) /*原来的链表是空表*/ {head=p0;p0->next=NULL;} /*使p0指向的结点作为头结点*/else {while((p0->averageaverage)&&(p1->next!=NULL)) {p2=p1; /*使p2指向刚才p1指向的结点*/ p1=p1->next; /*p1后移一个结点*/ } if(p0->average>=p1->average) {if(head==p1)head=p0; /*插到原来第一个结点之前*/ else p2->next=p0; /*插到p2指向的结点之后*/ p0->next=p1;} else {p1->next=p0;p0->next=NULL;} /*插到最后的结点之后*/ }n=n+1; /*结点数加1*/head=sort(head); /*调用排序的函数,将学生成绩重新排序*/printf("
Student %s have been inserted.
",newnode->name); printf("Don't forget to save the newnode file.
");return(head);}/*保存数据到文件函数*/void save(STUDENT *head){FILE *fp; /*定义指向文件的指针*/STUDENT *p; /* 定义移动指针*/char outfile[10];printf("Enter outfile name,for example c:\\score
");scanf("%s",outfile);if((fp=fopen(outfile,"wb"))==NULL) /*为输出打开一个二进制文件,为只写方式*/ { printf("Cannot open the file
"); return; /*若打不开则返回菜单*/ }printf("
Saving the file......
");p=head; /*移动指针从头指针开始*/while(p!=NULL) /*如p不为空*/ { fwrite(p,LEN,1,fp); /*写入一条记录*/ p=p->next; /*指针后移*/ }fclose(fp); /*关闭文件*/printf("Save the file successfully!
");}/* 从文件读数据函数*/STUDENT *load(){STUDENT *p1,*p2,*head=NULL; /*定义记录指针变量*/FILE *fp; /* 定义指向文件的指针*/char infile[10];printf("Enter infile name,for example c:\\score
");scanf("%s",infile);if((fp=fopen(infile,"rb"))==NULL) /*打开一个二进制文件,为只读方式*/ { printf("Can not open the file.
"); return(head); }printf("
Loading the file!
");p1=(STUDENT *)malloc(LEN); /*开辟一个新单元*/if(!p1) { printf("Out of memory!
"); return(head); }head=p1; /*申请到空间,将其作为头指针*/while(!feof(fp)) /*循环读数据直到文件尾结束*/ { if(fread(p1,LEN,1,fp)!=1) break; /*如果没读到数据,跳出循环*/ p1->next=(STUDENT *)malloc(LEN); /*为下一个结点开辟空间*/ if(!p1->next) { printf("Out of memory!
"); return (head); }p2=p1; /*使p2指向刚才p1指向的结点*/p1=p1->next; /*指针后移,新读入数据链到当前表尾*/ }p2->next=NULL; /*最后一个结点的后继指针为空*/fclose(fp);printf("You have success to read data from the file!
");return (head);}

//学生成绩管理系统C代码
/*头文件*/
#include <stdio.h>
#include<dos.h>
#include<stdlib.h> /*其它说明*/
#include<string.h> /*字符串函数*/
#include<mem.h> /*内存操作函数*/
#include<ctype.h> /*字符操作函数*/
#include<alloc.h> /*动态地址分配函数*/

#define LEN sizeof(STUDENT)

typedef struct stu /*定义结构体数组用于缓存数据*/
{
char num[6];
char name[5];
int score[3];
int sum;
float average;
int order;
struct stu *next;
}STUDENT;

/*函数原型*/
STUDENT *init(); /*初始化函数*/
int menu_select(); /*菜单函数*/
STUDENT *create(); /*创建链表*/
void print(STUDENT *head); /* 显示全部记录*/
void search(STUDENT *head); /*查找记录*/
STUDENT *delete(STUDENT *head); /*删除记录*/
STUDENT *sort(STUDENT *head); /*排序*/
STUDENT *insert(STUDENT *head,STUDENT *newnode); /*插入记录*/
void save(STUDENT *head); /*保存文件*/
STUDENT *load(); /*读文件*/

/*主函数界面*/
main()
{
STUDENT *head,newnode;
head=init(); /*链表初始化,使head的值为NULL*/
for(;;) /*循环无限次*/
{
switch(menu_select())
{
case 1:head=create();break;
case 2:print(head);break;
case 3:search(head);break;
case 4:head=delete(head);break;
case 5:head=sort(head);break;
case 6:head=insert(head,&newnode);break; /*&newnode表示返回地址*/
case 7:save(head);break;
case 8:head=load(); break;
case 9:exit(0); /*如菜单返回值为9则程序结束*/
}
}
}

/*初始化函数*/
STUDENT *init()
{
return NULL; /*返回空指针*/
}

/*菜单选择函数*/
menu_select()
{
int n;
struct date d; /*定义时间结构体*/
getdate(&d); /*读取系统日期并把它放到结构体d中*/
printf("press any key to enter the menu......"); /*按任一键进入主菜单*/
getch(); /*从键盘读取一个字符,但不显示于屏幕*/
clrscr(); /*清屏*/
printf("********************************************************************************\n");
printf("\t\t Welcome to\n");
printf("\n\t\t The student score manage system\n");
printf("*************************************MENU***************************************\n");
printf("\t\t\t1. Enter the record\n"); /*输入学生成绩记录*/
printf("\t\t\t2. Print the record\n"); /*显示*/
printf("\t\t\t3. Search record on name\n"); /*寻找*/
printf("\t\t\t4. Delete a record\n"); /*删除*/
printf("\t\t\t5. Sort to make new a file\n"); /*排序*/
printf("\t\t\t6. Insert record to list\n"); /*插入*/
printf("\t\t\t7. Save the file\n"); /*保存*/
printf("\t\t\t8. Load the file\n"); /*读取*/
printf("\t\t\t9. Quit\n"); /*退出*/
printf("\n\t\t Made by Hu Haihong.\n");
printf("********************************************************************************\n");
printf("\t\t\t\t%d\\%d\\%d\n",d.da_year,d.da_mon,d.da_day); /*显示当前系统日期*/
do{
printf("\n\t\t\tEnter your choice(1~9):");
scanf("%d",&n);
}while(n<1||n>9); /*如果选择项不在1~9之间则重输*/
return(n); /*返回选择项,主函数根据该数调用相应的函数*/
}

/*输入函数*/
STUDENT *create()
{
int i,s;
STUDENT *head=NULL,*p; /* 定义函数.此函数带回一个指向链表头的指针*/
clrscr();
for(;;)
{p=(STUDENT *)malloc(LEN); /*开辟一个新的单元*/
if(!p) /*如果指针p为空*/
{printf("\nOut of memory."); /*输出内存溢出*/
return (head); /*返回头指针,下同*/
}
printf("Enter the num(0:list end):");
scanf("%s",p->num);
if(p->num[0]=='0') break; /*如果学号首字符为0则结束输入*/
printf("Enter the name:");
scanf("%s",p->name);
printf("Please enter the %d scores\n",3); /*提示开始输入成绩*/
s=0; /*计算每个学生的总分,初值为0*/
for(i=0;i<3;i++) /*3门课程循环3次*/
{
do{
printf("score%d:",i+1);
scanf("%d",&p->score[i]);
if(p->score[i]<0 || p->score[i]>100) /*确保成绩在0~100之间*/
printf("Data error,please enter again.\n");
}while(p->score[i]<0 || p->score[i]>100);
s=s+p->score[i]; /*累加各门成绩*/
}
p->sum=s; /*将总分保存*/
p->average=(float)s/3; /*先用强制类型转换将s转换成float型,再求平均值*/
p->order=0; /*未排序前此值为0*/
p->next=head; /*将头结点做为新输入结点的后继结点*/
head=p; /*新输入结点为新的头结点*/
}
return(head);
}

/* 显示全部记录函数*/
void print(STUDENT *head)
{
int i=0; /* 统计记录条数*/
STUDENT *p; /*移动指针*/
clrscr();
p=head; /*初值为头指针*/
printf("\n************************************STUDENT************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Rec | Num | Name | Sc1 | Sc2 | Sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
while(p!=NULL)
{
i++;
printf("| %3d | %4s | %-4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
i, p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("-------------------------------------------------------------------------------\n");
printf("**************************************END**************************************\n");
}

/*查找记录函数*/
void search(STUDENT *head)
{
STUDENT *p; /* 移动指针*/
char s[5]; /*存放姓名用的字符数组*/
clrscr();
printf("Please enter name for searching.\n");
scanf("%s",s);
p=head; /*将头指针赋给p*/
while(strcmp(p->name,s) && p != NULL) /*当记录的姓名不是要找的,或指针不为空时*/
p=p->next; /*移动指针,指向下一结点*/
if(p!=NULL) /*如果指针不为空*/
{printf("\n*************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else
printf("\nThere is no num %s student on the list.\n",s); /*显示没有该学生*/
}

/*删除记录函数*/
STUDENT *delete(STUDENT *head)
{int n;
STUDENT *p1,*p2; /*p1为查找到要删除的结点指针,p2为其前驱指针*/
char c,s[6]; /*s[6]用来存放学号,c用来输入字母*/
clrscr();
printf("Please enter the deleted num: ");
scanf("%s",s);
p1=p2=head; /*给p1和p2赋初值头指针*/
while(strcmp(p1->num,s) && p1 != NULL) /*当记录的学号不是要找的,或指针不为空时*/
{p2=p1; /*将p1指针值赋给p2作为p1的前驱指针*/
p1=p1->next; /*将p1指针指向下一条记录*/
}
if(strcmp(p1->num,s)==0) /*学号找到了*/
{printf("**************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
printf("Are you sure to delete the student Y/N ?"); /*提示是否要删除,输入Y删除,N则退出*/
for(;;)
{scanf("%c",&c);
if(c=='n'||c=='N') break; /*如果不删除,则跳出本循环*/
if(c=='y'||c=='Y')
{
if(p1==head) /*若p1==head,说明被删结点是首结点*/
head=p1->next; /*把第二个结点地址赋予head*/
else
p2->next=p1->next; /*否则将一下结点地址赋给前一结点地址*/
n=n-1;
printf("\nNum %s student have been deleted.\n",s);
printf("Don't forget to save.\n");break; /*删除后就跳出循环*/
}
}
}
else
printf("\nThere is no num %s student on the list.\n",s); /*找不到该结点*/
return(head);
}

/*排序函数*/
STUDENT *sort(STUDENT *head)
{int i=0; /*保存名次*/
STUDENT *p1,*p2,*t,*temp; /*定义临时指针*/
temp=head->next; /*将原表的头指针所指的下一个结点作头指针*/
head->next=NULL; /*第一个结点为新表的头结点*/
while(temp!=NULL) /*当原表不为空时,进行排序*/
{
t=temp; /*取原表的头结点*/
temp=temp->next; /*原表头结点指针后移*/
p1=head; /*设定移动指针p1,从头指针开始*/
p2=head; /*设定移动指针p2做为p1的前驱,初值为头指针*/
while(t->average<p1->average&&p1!=NULL) /*作成绩平均分比较*/
{
p2=p1; /*待排序点值小,则新表指针后移*/
p1=p1->next;
}
if(p1==p2) /*p1==p2,说明待排序点值大,应排在首位*/
{
t->next=p1; /*待排序点的后继为p*/
head=t; /*新头结点为待排序点*/
}
else /*待排序点应插入在中间某个位置p2和p1之间,如p为空则是尾部*/
{
t->next=p1; /*t的后继是p1*/
p2->next=t; /*p2的后继是t*/
}
}
p1=head; /*已排好序的头指针赋给p1,准备填写名次*/
while(p1!=NULL) /*当p1不为空时,进行下列操作*/
{
i++; /*结点序号*/
p1->order=i; /*将结点序号赋值给名次*/
p1=p1->next; /*指针后移*/
}
printf("Sorting is sucessful.\n"); /*排序成功*/
return (head);
}

/*插入记录函数*/
STUDENT *insert(STUDENT *head,STUDENT *newnode)
{STUDENT *p0,*p1,*p2;
int n,sum1,i;
p1=head; /*使p1指向第一个结点*/
p0=newnode; /*p0指向要插入的结点*/
printf("\nPlease enter a newnode record.\n"); /*提示输入记录信息*/
printf("Enter the num:");
scanf("%s",newnode->num);
printf("Enter the name:");
scanf("%s",newnode->name);
printf("Please enter the %d scores.\n",3);
sum1=0; /*保存新记录的总分,初值为0*/
for(i=0;i<3;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",&newnode->score[i]);
if(newnode->score[i]>100||newnode->score[i]<0)
printf("Data error,please enter again.\n");
}while(newnode->score[i]>100||newnode->score[i]<0);
sum1=sum1+newnode->score[i]; /*累加各门成绩*/
}
newnode->sum=sum1; /*将总分存入新记录中*/
newnode->average=(float)sum1/3;
newnode->order=0;
if(head==NULL) /*原来的链表是空表*/
{head=p0;p0->next=NULL;} /*使p0指向的结点作为头结点*/
else
{while((p0->average<p1->average)&&(p1->next!=NULL))
{p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1->next; /*p1后移一个结点*/
}
if(p0->average>=p1->average)
{if(head==p1)head=p0; /*插到原来第一个结点之前*/
else p2->next=p0; /*插到p2指向的结点之后*/
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;} /*插到最后的结点之后*/
}
n=n+1; /*结点数加1*/
head=sort(head); /*调用排序的函数,将学生成绩重新排序*/
printf("\nStudent %s have been inserted.\n",newnode->name);
printf("Don't forget to save the newnode file.\n");
return(head);
}

/*保存数据到文件函数*/
void save(STUDENT *head)
{FILE *fp; /*定义指向文件的指针*/
STUDENT *p; /* 定义移动指针*/
char outfile[10];
printf("Enter outfile name,for example c:\\score\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL) /*为输出打开一个二进制文件,为只写方式*/
{
printf("Cannot open the file\n");
return; /*若打不开则返回菜单*/
}
printf("\nSaving the file......\n");
p=head; /*移动指针从头指针开始*/
while(p!=NULL) /*如p不为空*/
{
fwrite(p,LEN,1,fp); /*写入一条记录*/
p=p->next; /*指针后移*/
}
fclose(fp); /*关闭文件*/
printf("Save the file successfully!\n");
}

/* 从文件读数据函数*/
STUDENT *load()
{STUDENT *p1,*p2,*head=NULL; /*定义记录指针变量*/
FILE *fp; /* 定义指向文件的指针*/
char infile[10];
printf("Enter infile name,for example c:\\score\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL) /*打开一个二进制文件,为只读方式*/
{
printf("Can not open the file.\n");
return(head);
}
printf("\nLoading the file!\n");
p1=(STUDENT *)malloc(LEN); /*开辟一个新单元*/
if(!p1)
{
printf("Out of memory!\n");
return(head);
}
head=p1; /*申请到空间,将其作为头指针*/
while(!feof(fp)) /*循环读数据直到文件尾结束*/
{
if(fread(p1,LEN,1,fp)!=1) break; /*如果没读到数据,跳出循环*/
p1->next=(STUDENT *)malloc(LEN); /*为下一个结点开辟空间*/
if(!p1->next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1->next; /*指针后移,新读入数据链到当前表尾*/
}
p2->next=NULL; /*最后一个结点的后继指针为空*/
fclose(fp);
printf("You have success to read data from the file!\n");
return (head);
}
满意请采纳。

//学生成绩管理系统C代码
/*头文件*/
#include <stdio.h>
#include<dos.h>
#include<stdlib.h> /*其它说明*/
#include<string.h> /*字符串函数*/
#include<mem.h> /*内存操作函数*/
#include<ctype.h> /*字符操作函数*/
#include<alloc.h> /*动态地址分配函数*/

#define LEN sizeof(STUDENT)

typedef struct stu /*定义结构体数组用于缓存数据*/
{
char num[6];
char name[5];
int score[3];
int sum;
float average;
int order;
struct stu *next;
}STUDENT;

/*函数原型*/
STUDENT *init(); /*初始化函数*/
int menu_select(); /*菜单函数*/
STUDENT *create(); /*创建链表*/
void print(STUDENT *head); /* 显示全部记录*/
void search(STUDENT *head); /*查找记录*/
STUDENT *delete(STUDENT *head); /*删除记录*/
STUDENT *sort(STUDENT *head); /*排序*/
STUDENT *insert(STUDENT *head,STUDENT *newnode); /*插入记录*/
void save(STUDENT *head); /*保存文件*/
STUDENT *load(); /*读文件*/

/*主函数界面*/
main()
{
STUDENT *head,newnode;
head=init(); /*链表初始化,使head的值为NULL*/
for(;;) /*循环无限次*/
{
switch(menu_select())
{
case 1:head=create();break;
case 2:print(head);break;
case 3:search(head);break;
case 4:head=delete(head);break;
case 5:head=sort(head);break;
case 6:head=insert(head,&newnode);break; /*&newnode表示返回地址*/
case 7:save(head);break;
case 8:head=load(); break;
case 9:exit(0); /*如菜单返回值为9则程序结束*/
}
}
}

/*初始化函数*/
STUDENT *init()
{
return NULL; /*返回空指针*/
}

/*菜单选择函数*/
menu_select()
{
int n;
struct date d; /*定义时间结构体*/
getdate(&d); /*读取系统日期并把它放到结构体d中*/
printf("press any key to enter the menu......"); /*按任一键进入主菜单*/
getch(); /*从键盘读取一个字符,但不显示于屏幕*/
clrscr(); /*清屏*/
printf("********************************************************************************\n");
printf("\t\t Welcome to\n");
printf("\n\t\t The student score manage system\n");
printf("*************************************MENU***************************************\n");
printf("\t\t\t1. Enter the record\n"); /*输入学生成绩记录*/
printf("\t\t\t2. Print the record\n"); /*显示*/
printf("\t\t\t3. Search record on name\n"); /*寻找*/
printf("\t\t\t4. Delete a record\n"); /*删除*/
printf("\t\t\t5. Sort to make new a file\n"); /*排序*/
printf("\t\t\t6. Insert record to list\n"); /*插入*/
printf("\t\t\t7. Save the file\n"); /*保存*/
printf("\t\t\t8. Load the file\n"); /*读取*/
printf("\t\t\t9. Quit\n"); /*退出*/
printf("\n\t\t Made by Hu Haihong.\n");
printf("********************************************************************************\n");
printf("\t\t\t\t%d\\%d\\%d\n",d.da_year,d.da_mon,d.da_day); /*显示当前系统日期*/
do{
printf("\n\t\t\tEnter your choice(1~9):");
scanf("%d",&n);
}while(n<1||n>9); /*如果选择项不在1~9之间则重输*/
return(n); /*返回选择项,主函数根据该数调用相应的函数*/
}

/*输入函数*/
STUDENT *create()
{
int i,s;
STUDENT *head=NULL,*p; /* 定义函数.此函数带回一个指向链表头的指针*/
clrscr();
for(;;)
{p=(STUDENT *)malloc(LEN); /*开辟一个新的单元*/
if(!p) /*如果指针p为空*/
{printf("\nOut of memory."); /*输出内存溢出*/
return (head); /*返回头指针,下同*/
}
printf("Enter the num(0:list end):");
scanf("%s",p->num);
if(p->num[0]=='0') break; /*如果学号首字符为0则结束输入*/
printf("Enter the name:");
scanf("%s",p->name);
printf("Please enter the %d scores\n",3); /*提示开始输入成绩*/
s=0; /*计算每个学生的总分,初值为0*/
for(i=0;i<3;i++) /*3门课程循环3次*/
{
do{
printf("score%d:",i+1);
scanf("%d",&p->score[i]);
if(p->score[i]<0 || p->score[i]>100) /*确保成绩在0~100之间*/
printf("Data error,please enter again.\n");
}while(p->score[i]<0 || p->score[i]>100);
s=s+p->score[i]; /*累加各门成绩*/
}
p->sum=s; /*将总分保存*/
p->average=(float)s/3; /*先用强制类型转换将s转换成float型,再求平均值*/
p->order=0; /*未排序前此值为0*/
p->next=head; /*将头结点做为新输入结点的后继结点*/
head=p; /*新输入结点为新的头结点*/
}
return(head);
}

/* 显示全部记录函数*/
void print(STUDENT *head)
{
int i=0; /* 统计记录条数*/
STUDENT *p; /*移动指针*/
clrscr();
p=head; /*初值为头指针*/
printf("\n************************************STUDENT************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Rec | Num | Name | Sc1 | Sc2 | Sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
while(p!=NULL)
{
i++;
printf("| %3d | %4s | %-4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
i, p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("-------------------------------------------------------------------------------\n");
printf("**************************************END**************************************\n");
}

/*查找记录函数*/
void search(STUDENT *head)
{
STUDENT *p; /* 移动指针*/
char s[5]; /*存放姓名用的字符数组*/
clrscr();
printf("Please enter name for searching.\n");
scanf("%s",s);
p=head; /*将头指针赋给p*/
while(strcmp(p->name,s) && p != NULL) /*当记录的姓名不是要找的,或指针不为空时*/
p=p->next; /*移动指针,指向下一结点*/
if(p!=NULL) /*如果指针不为空*/
{printf("\n*************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else
printf("\nThere is no num %s student on the list.\n",s); /*显示没有该学生*/
}

/*删除记录函数*/
STUDENT *delete(STUDENT *head)
{int n;
STUDENT *p1,*p2; /*p1为查找到要删除的结点指针,p2为其前驱指针*/
char c,s[6]; /*s[6]用来存放学号,c用来输入字母*/
clrscr();
printf("Please enter the deleted num: ");
scanf("%s",s);
p1=p2=head; /*给p1和p2赋初值头指针*/
while(strcmp(p1->num,s) && p1 != NULL) /*当记录的学号不是要找的,或指针不为空时*/
{p2=p1; /*将p1指针值赋给p2作为p1的前驱指针*/
p1=p1->next; /*将p1指针指向下一条记录*/
}
if(strcmp(p1->num,s)==0) /*学号找到了*/
{printf("**************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
printf("Are you sure to delete the student Y/N ?"); /*提示是否要删除,输入Y删除,N则退出*/
for(;;)
{scanf("%c",&c);
if(c=='n'||c=='N') break; /*如果不删除,则跳出本循环*/
if(c=='y'||c=='Y')
{
if(p1==head) /*若p1==head,说明被删结点是首结点*/
head=p1->next; /*把第二个结点地址赋予head*/
else
p2->next=p1->next; /*否则将一下结点地址赋给前一结点地址*/
n=n-1;
printf("\nNum %s student have been deleted.\n",s);
printf("Don't forget to save.\n");break; /*删除后就跳出循环*/
}
}
}
else
printf("\nThere is no num %s student on the list.\n",s); /*找不到该结点*/
return(head);
}

/*排序函数*/
STUDENT *sort(STUDENT *head)
{int i=0; /*保存名次*/
STUDENT *p1,*p2,*t,*temp; /*定义临时指针*/
temp=head->next; /*将原表的头指针所指的下一个结点作头指针*/
head->next=NULL; /*第一个结点为新表的头结点*/
while(temp!=NULL) /*当原表不为空时,进行排序*/
{
t=temp; /*取原表的头结点*/
temp=temp->next; /*原表头结点指针后移*/
p1=head; /*设定移动指针p1,从头指针开始*/
p2=head; /*设定移动指针p2做为p1的前驱,初值为头指针*/
while(t->average<p1->average&&p1!=NULL) /*作成绩平均分比较*/
{
p2=p1; /*待排序点值小,则新表指针后移*/
p1=p1->next;
}
if(p1==p2) /*p1==p2,说明待排序点值大,应排在首位*/
{
t->next=p1; /*待排序点的后继为p*/
head=t; /*新头结点为待排序点*/
}
else /*待排序点应插入在中间某个位置p2和p1之间,如p为空则是尾部*/
{
t->next=p1; /*t的后继是p1*/
p2->next=t; /*p2的后继是t*/
}
}
p1=head; /*已排好序的头指针赋给p1,准备填写名次*/
while(p1!=NULL) /*当p1不为空时,进行下列操作*/
{
i++; /*结点序号*/
p1->order=i; /*将结点序号赋值给名次*/
p1=p1->next; /*指针后移*/
}
printf("Sorting is sucessful.\n"); /*排序成功*/
return (head);
}

/*插入记录函数*/
STUDENT *insert(STUDENT *head,STUDENT *newnode)
{STUDENT *p0,*p1,*p2;
int n,sum1,i;
p1=head; /*使p1指向第一个结点*/
p0=newnode; /*p0指向要插入的结点*/
printf("\nPlease enter a newnode record.\n"); /*提示输入记录信息*/
printf("Enter the num:");
scanf("%s",newnode->num);
printf("Enter the name:");
scanf("%s",newnode->name);
printf("Please enter the %d scores.\n",3);
sum1=0; /*保存新记录的总分,初值为0*/
for(i=0;i<3;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",&newnode->score[i]);
if(newnode->score[i]>100||newnode->score[i]<0)
printf("Data error,please enter again.\n");
}while(newnode->score[i]>100||newnode->score[i]<0);
sum1=sum1+newnode->score[i]; /*累加各门成绩*/
}
newnode->sum=sum1; /*将总分存入新记录中*/
newnode->average=(float)sum1/3;
newnode->order=0;
if(head==NULL) /*原来的链表是空表*/
{head=p0;p0->next=NULL;} /*使p0指向的结点作为头结点*/
else
{while((p0->average<p1->average)&&(p1->next!=NULL))
{p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1->next; /*p1后移一个结点*/
}
if(p0->average>=p1->average)
{if(head==p1)head=p0; /*插到原来第一个结点之前*/
else p2->next=p0; /*插到p2指向的结点之后*/
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;} /*插到最后的结点之后*/
}
n=n+1; /*结点数加1*/
head=sort(head); /*调用排序的函数,将学生成绩重新排序*/
printf("\nStudent %s have been inserted.\n",newnode->name);
printf("Don't forget to save the newnode file.\n");
return(head);
}

/*保存数据到文件函数*/
void save(STUDENT *head)
{FILE *fp; /*定义指向文件的指针*/
STUDENT *p; /* 定义移动指针*/
char outfile[10];
printf("Enter outfile name,for example c:\\score\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL) /*为输出打开一个二进制文件,为只写方式*/
{
printf("Cannot open the file\n");
return; /*若打不开则返回菜单*/
}
printf("\nSaving the file......\n");
p=head; /*移动指针从头指针开始*/
while(p!=NULL) /*如p不为空*/
{
fwrite(p,LEN,1,fp); /*写入一条记录*/
p=p->next; /*指针后移*/
}
fclose(fp); /*关闭文件*/
printf("Save the file successfully!\n");
}

/* 从文件读数据函数*/
STUDENT *load()
{STUDENT *p1,*p2,*head=NULL; /*定义记录指针变量*/
FILE *fp; /* 定义指向文件的指针*/
char infile[10];
printf("Enter infile name,for example c:\\score\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL) /*打开一个二进制文件,为只读方式*/
{
printf("Can not open the file.\n");
return(head);
}
printf("\nLoading the file!\n");
p1=(STUDENT *)malloc(LEN); /*开辟一个新单元*/
if(!p1)
{
printf("Out of memory!\n");
return(head);
}
head=p1; /*申请到空间,将其作为头指针*/
while(!feof(fp)) /*循环读数据直到文件尾结束*/
{
if(fread(p1,LEN,1,fp)!=1) break; /*如果没读到数据,跳出循环*/
p1->next=(STUDENT *)malloc(LEN); /*为下一个结点开辟空间*/
if(!p1->next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1->next; /*指针后移,新读入数据链到当前表尾*/
}
p2->next=NULL; /*最后一个结点的后继指针为空*/
fclose(fp);
printf("You have success to read data from the file!\n");
return (head);
}
以上回答你满意么?

//学生成绩管理系统C代码
/*头文件*/
#include <stdio.h>
#include<dos.h>
#include<stdlib.h> /*其它说明*/
#include<string.h> /*字符串函数*/
#include<mem.h> /*内存操作函数*/
#include<ctype.h> /*字符操作函数*/
#include<alloc.h> /*动态地址分配函数*/

#define LEN sizeof(STUDENT)

typedef struct stu /*定义结构体数组用于缓存数据*/
{
char num[6];
char name[5];
int score[3];
int sum;
float average;
int order;
struct stu *next;
}STUDENT;

/*函数原型*/
STUDENT *init(); /*初始化函数*/
int menu_select(); /*菜单函数*/
STUDENT *create(); /*创建链表*/
void print(STUDENT *head); /* 显示全部记录*/
void search(STUDENT *head); /*查找记录*/
STUDENT *delete(STUDENT *head); /*删除记录*/
STUDENT *sort(STUDENT *head); /*排序*/
STUDENT *insert(STUDENT *head,STUDENT *newnode); /*插入记录*/
void save(STUDENT *head); /*保存文件*/
STUDENT *load(); /*读文件*/

/*主函数界面*/
main()
{
STUDENT *head,newnode;
head=init(); /*链表初始化,使head的值为NULL*/
for(;;) /*循环无限次*/
{
switch(menu_select())
{
case 1:head=create();break;
case 2:print(head);break;
case 3:search(head);break;
case 4:head=delete(head);break;
case 5:head=sort(head);break;
case 6:head=insert(head,&newnode);break; /*&newnode表示返回地址*/
case 7:save(head);break;
case 8:head=load(); break;
case 9:exit(0); /*如菜单返回值为9则程序结束*/
}
}
}

/*初始化函数*/
STUDENT *init()
{
return NULL; /*返回空指针*/
}

/*菜单选择函数*/
menu_select()
{
int n;
struct date d; /*定义时间结构体*/
getdate(&d); /*读取系统日期并把它放到结构体d中*/
printf("press any key to enter the menu......"); /*按任一键进入主菜单*/
getch(); /*从键盘读取一个字符,但不显示于屏幕*/
clrscr(); /*清屏*/
printf("********************************************************************************\n");
printf("\t\t Welcome to\n");
printf("\n\t\t The student score manage system\n");
printf("*************************************MENU***************************************\n");
printf("\t\t\t1. Enter the record\n"); /*输入学生成绩记录*/
printf("\t\t\t2. Print the record\n"); /*显示*/
printf("\t\t\t3. Search record on name\n"); /*寻找*/
printf("\t\t\t4. Delete a record\n"); /*删除*/
printf("\t\t\t5. Sort to make new a file\n"); /*排序*/
printf("\t\t\t6. Insert record to list\n"); /*插入*/
printf("\t\t\t7. Save the file\n"); /*保存*/
printf("\t\t\t8. Load the file\n"); /*读取*/
printf("\t\t\t9. Quit\n"); /*退出*/
printf("\n\t\t Made by Hu Haihong.\n");
printf("********************************************************************************\n");
printf("\t\t\t\t%d\\%d\\%d\n",d.da_year,d.da_mon,d.da_day); /*显示当前系统日期*/
do{
printf("\n\t\t\tEnter your choice(1~9):");
scanf("%d",&n);
}while(n<1||n>9); /*如果选择项不在1~9之间则重输*/
return(n); /*返回选择项,主函数根据该数调用相应的函数*/
}

/*输入函数*/
STUDENT *create()
{
int i,s;
STUDENT *head=NULL,*p; /* 定义函数.此函数带回一个指向链表头的指针*/
clrscr();
for(;;)
{p=(STUDENT *)malloc(LEN); /*开辟一个新的单元*/
if(!p) /*如果指针p为空*/
{printf("\nOut of memory."); /*输出内存溢出*/
return (head); /*返回头指针,下同*/
}
printf("Enter the num(0:list end):");
scanf("%s",p->num);
if(p->num[0]=='0') break; /*如果学号首字符为0则结束输入*/
printf("Enter the name:");
scanf("%s",p->name);
printf("Please enter the %d scores\n",3); /*提示开始输入成绩*/
s=0; /*计算每个学生的总分,初值为0*/
for(i=0;i<3;i++) /*3门课程循环3次*/
{
do{
printf("score%d:",i+1);
scanf("%d",&p->score[i]);
if(p->score[i]<0 || p->score[i]>100) /*确保成绩在0~100之间*/
printf("Data error,please enter again.\n");
}while(p->score[i]<0 || p->score[i]>100);
s=s+p->score[i]; /*累加各门成绩*/
}
p->sum=s; /*将总分保存*/
p->average=(float)s/3; /*先用强制类型转换将s转换成float型,再求平均值*/
p->order=0; /*未排序前此值为0*/
p->next=head; /*将头结点做为新输入结点的后继结点*/
head=p; /*新输入结点为新的头结点*/
}
return(head);
}

/* 显示全部记录函数*/
void print(STUDENT *head)
{
int i=0; /* 统计记录条数*/
STUDENT *p; /*移动指针*/
clrscr();
p=head; /*初值为头指针*/
printf("\n************************************STUDENT************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Rec | Num | Name | Sc1 | Sc2 | Sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
while(p!=NULL)
{
i++;
printf("| %3d | %4s | %-4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
i, p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("-------------------------------------------------------------------------------\n");
printf("**************************************END**************************************\n");
}

/*查找记录函数*/
void search(STUDENT *head)
{
STUDENT *p; /* 移动指针*/
char s[5]; /*存放姓名用的字符数组*/
clrscr();
printf("Please enter name for searching.\n");
scanf("%s",s);
p=head; /*将头指针赋给p*/
while(strcmp(p->name,s) && p != NULL) /*当记录的姓名不是要找的,或指针不为空时*/
p=p->next; /*移动指针,指向下一结点*/
if(p!=NULL) /*如果指针不为空*/
{printf("\n*************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else
printf("\nThere is no num %s student on the list.\n",s); /*显示没有该学生*/
}

/*删除记录函数*/
STUDENT *delete(STUDENT *head)
{int n;
STUDENT *p1,*p2; /*p1为查找到要删除的结点指针,p2为其前驱指针*/
char c,s[6]; /*s[6]用来存放学号,c用来输入字母*/
clrscr();
printf("Please enter the deleted num: ");
scanf("%s",s);
p1=p2=head; /*给p1和p2赋初值头指针*/
while(strcmp(p1->num,s) && p1 != NULL) /*当记录的学号不是要找的,或指针不为空时*/
{p2=p1; /*将p1指针值赋给p2作为p1的前驱指针*/
p1=p1->next; /*将p1指针指向下一条记录*/
}
if(strcmp(p1->num,s)==0) /*学号找到了*/
{printf("**************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
printf("Are you sure to delete the student Y/N ?"); /*提示是否要删除,输入Y删除,N则退出*/
for(;;)
{scanf("%c",&c);
if(c=='n'||c=='N') break; /*如果不删除,则跳出本循环*/
if(c=='y'||c=='Y')
{
if(p1==head) /*若p1==head,说明被删结点是首结点*/
head=p1->next; /*把第二个结点地址赋予head*/
else
p2->next=p1->next; /*否则将一下结点地址赋给前一结点地址*/
n=n-1;
printf("\nNum %s student have been deleted.\n",s);
printf("Don't forget to save.\n");break; /*删除后就跳出循环*/
}
}
}
else
printf("\nThere is no num %s student on the list.\n",s); /*找不到该结点*/
return(head);
}

/*排序函数*/
STUDENT *sort(STUDENT *head)
{int i=0; /*保存名次*/
STUDENT *p1,*p2,*t,*temp; /*定义临时指针*/
temp=head->next; /*将原表的头指针所指的下一个结点作头指针*/
head->next=NULL; /*第一个结点为新表的头结点*/
while(temp!=NULL) /*当原表不为空时,进行排序*/
{
t=temp; /*取原表的头结点*/
temp=temp->next; /*原表头结点指针后移*/
p1=head; /*设定移动指针p1,从头指针开始*/
p2=head; /*设定移动指针p2做为p1的前驱,初值为头指针*/
while(t->average<p1->average&&p1!=NULL) /*作成绩平均分比较*/
{
p2=p1; /*待排序点值小,则新表指针后移*/
p1=p1->next;
}
if(p1==p2) /*p1==p2,说明待排序点值大,应排在首位*/
{
t->next=p1; /*待排序点的后继为p*/
head=t; /*新头结点为待排序点*/
}
else /*待排序点应插入在中间某个位置p2和p1之间,如p为空则是尾部*/
{
t->next=p1; /*t的后继是p1*/
p2->next=t; /*p2的后继是t*/
}
}
p1=head; /*已排好序的头指针赋给p1,准备填写名次*/
while(p1!=NULL) /*当p1不为空时,进行下列操作*/
{
i++; /*结点序号*/
p1->order=i; /*将结点序号赋值给名次*/
p1=p1->next; /*指针后移*/
}
printf("Sorting is sucessful.\n"); /*排序成功*/
return (head);
}

/*插入记录函数*/
STUDENT *insert(STUDENT *head,STUDENT *newnode)
{STUDENT *p0,*p1,*p2;
int n,sum1,i;
p1=head; /*使p1指向第一个结点*/
p0=newnode; /*p0指向要插入的结点*/
printf("\nPlease enter a newnode record.\n"); /*提示输入记录信息*/
printf("Enter the num:");
scanf("%s",newnode->num);
printf("Enter the name:");
scanf("%s",newnode->name);
printf("Please enter the %d scores.\n",3);
sum1=0; /*保存新记录的总分,初值为0*/
for(i=0;i<3;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",&newnode->score[i]);
if(newnode->score[i]>100||newnode->score[i]<0)
printf("Data error,please enter again.\n");
}while(newnode->score[i]>100||newnode->score[i]<0);
sum1=sum1+newnode->score[i]; /*累加各门成绩*/
}
newnode->sum=sum1; /*将总分存入新记录中*/
newnode->average=(float)sum1/3;
newnode->order=0;
if(head==NULL) /*原来的链表是空表*/
{head=p0;p0->next=NULL;} /*使p0指向的结点作为头结点*/
else
{while((p0->average<p1->average)&&(p1->next!=NULL))
{p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1->next; /*p1后移一个结点*/
}
if(p0->average>=p1->average)
{if(head==p1)head=p0; /*插到原来第一个结点之前*/
else p2->next=p0; /*插到p2指向的结点之后*/
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;} /*插到最后的结点之后*/
}
n=n+1; /*结点数加1*/
head=sort(head); /*调用排序的函数,将学生成绩重新排序*/
printf("\nStudent %s have been inserted.\n",newnode->name);
printf("Don't forget to save the newnode file.\n");
return(head);
}

/*保存数据到文件函数*/
void save(STUDENT *head)
{FILE *fp; /*定义指向文件的指针*/
STUDENT *p; /* 定义移动指针*/
char outfile[10];
printf("Enter outfile name,for example c:\\score\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL) /*为输出打开一个二进制文件,为只写方式*/
{
printf("Cannot open the file\n");
return; /*若打不开则返回菜单*/
}
printf("\nSaving the file......\n");
p=head; /*移动指针从头指针开始*/
while(p!=NULL) /*如p不为空*/
{
fwrite(p,LEN,1,fp); /*写入一条记录*/
p=p->next; /*指针后移*/
}
fclose(fp); /*关闭文件*/
printf("Save the file successfully!\n");
}

/* 从文件读数据函数*/
STUDENT *load()
{STUDENT *p1,*p2,*head=NULL; /*定义记录指针变量*/
FILE *fp; /* 定义指向文件的指针*/
char infile[10];
printf("Enter infile name,for example c:\\score\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL) /*打开一个二进制文件,为只读方式*/
{
printf("Can not open the file.\n");
return(head);
}
printf("\nLoading the file!\n");
p1=(STUDENT *)malloc(LEN); /*开辟一个新单元*/
if(!p1)
{
printf("Out of memory!\n");
return(head);
}
head=p1; /*申请到空间,将其作为头指针*/
while(!feof(fp)) /*循环读数据直到文件尾结束*/
{
if(fread(p1,LEN,1,fp)!=1) break; /*如果没读到数据,跳出循环*/
p1->next=(STUDENT *)malloc(LEN); /*为下一个结点开辟空间*/
if(!p1->next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1->next; /*指针后移,新读入数据链到当前表尾*/
}
p2->next=NULL; /*最后一个结点的后继指针为空*/
fclose(fp);
printf("You have success to read data from the file!\n");
return (head);
}

//学生成绩管理系统C代码
/*头文件*/
#include
#include
#include /*其它说明*/
#include /*字符串函数*/
#include /*内存操作函数*/
#include /*字符操作函数*/
#include /*动态地址分配函数*/

#define LEN sizeof(STUDENT)

typedef struct stu /*定义结构体数组用于缓存数据*/
{
char num[6];
char name[5];
int score[3];
int sum;
float average;
int order;
struct stu *next;
}STUDENT;

/*函数原型*/
STUDENT *init(); /*初始化函数*/
int menu_select(); /*菜单函数*/
STUDENT *create(); /*创建链表*/
void print(STUDENT *head); /* 显示全部记录*/
void search(STUDENT *head); /*查找记录*/
STUDENT *delete(STUDENT *head); /*删除记录*/
STUDENT *sort(STUDENT *head); /*排序*/
STUDENT *insert(STUDENT *head,STUDENT *newnode); /*插入记录*/
void save(STUDENT *head); /*保存文件*/
STUDENT *load(); /*读文件*/

/*主函数界面*/
main()
{
STUDENT *head,newnode;
head=init(); /*链表初始化,使head的值为NULL*/
for(;;) /*循环无限次*/
{
switch(menu_select())
{
case 1:head=create();break;
case 2:print(head);break;
case 3:search(head);break;
case 4:head=delete(head);break;
case 5:head=sort(head);break;
case 6:head=insert(head,&newnode);break; /*&newnode表示返回地址*/
case 7:save(head);break;
case 8:head=load(); break;
case 9:exit(0); /*如菜单返回值为9则程序结束*/
}
}
}

/*初始化函数*/
STUDENT *init()
{
return NULL; /*返回空指针*/
}

/*菜单选择函数*/
menu_select()
{
int n;
struct date d; /*定义时间结构体*/
getdate(&d); /*读取系统日期并把它放到结构体d中*/
printf("press any key to enter the menu......"); /*按任一键进入主菜单*/
getch(); /*从键盘读取一个字符,但不显示于屏幕*/
clrscr(); /*清屏*/
printf("********************************************************************************\n");
printf("\t\t Welcome to\n");
printf("\n\t\t The student score manage system\n");
printf("*************************************MENU***************************************\n");
printf("\t\t\t1. Enter the record\n"); /*输入学生成绩记录*/
printf("\t\t\t2. Print the record\n"); /*显示*/
printf("\t\t\t3. Search record on name\n"); /*寻找*/
printf("\t\t\t4. Delete a record\n"); /*删除*/
printf("\t\t\t5. Sort to make new a file\n"); /*排序*/
printf("\t\t\t6. Insert record to list\n"); /*插入*/
printf("\t\t\t7. Save the file\n"); /*保存*/
printf("\t\t\t8. Load the file\n"); /*读取*/
printf("\t\t\t9. Quit\n"); /*退出*/
printf("\n\t\t Made by Hu Haihong.\n");
printf("********************************************************************************\n");
printf("\t\t\t\t%d\\%d\\%d\n",d.da_year,d.da_mon,d.da_day); /*显示当前系统日期*/
do{
printf("\n\t\t\tEnter your choice(1~9):");
scanf("%d",&n);
}while(n9); /*如果选择项不在1~9之间则重输*/
return(n); /*返回选择项,主函数根据该数调用相应的函数*/
}

/*输入函数*/
STUDENT *create()
{
int i,s;
STUDENT *head=NULL,*p; /* 定义函数.此函数带回一个指向链表头的指针*/
clrscr();
for(;;)
{p=(STUDENT *)malloc(LEN); /*开辟一个新的单元*/
if(!p) /*如果指针p为空*/
{printf("\nOut of memory."); /*输出内存溢出*/
return (head); /*返回头指针,下同*/
}
printf("Enter the num(0:list end):");
scanf("%s",p->num);
if(p->num[0]=='0') break; /*如果学号首字符为0则结束输入*/
printf("Enter the name:");
scanf("%s",p->name);
printf("Please enter the %d scores\n",3); /*提示开始输入成绩*/
s=0; /*计算每个学生的总分,初值为0*/
for(i=0;i<3;i++) /*3门课程循环3次*/
{
do{
printf("score%d:",i+1);
scanf("%d",&p->score[i]);
if(p->score[i]score[i]>100) /*确保成绩在0~100之间*/
printf("Data error,please enter again.\n");
}while(p->score[i]score[i]>100);
s=s+p->score[i]; /*累加各门成绩*/
}
p->sum=s; /*将总分保存*/
p->average=(float)s/3; /*先用强制类型转换将s转换成float型,再求平均值*/
p->order=0; /*未排序前此值为0*/
p->next=head; /*将头结点做为新输入结点的后继结点*/
head=p; /*新输入结点为新的头结点*/
}
return(head);
}

/* 显示全部记录函数*/
void print(STUDENT *head)
{
int i=0; /* 统计记录条数*/
STUDENT *p; /*移动指针*/
clrscr();
p=head; /*初值为头指针*/
printf("\n************************************STUDENT************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Rec | Num | Name | Sc1 | Sc2 | Sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
while(p!=NULL)
{
i++;
printf("| %3d | %4s | %-4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
i, p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("-------------------------------------------------------------------------------\n");
printf("**************************************END**************************************\n");
}

/*查找记录函数*/
void search(STUDENT *head)
{
STUDENT *p; /* 移动指针*/
char s[5]; /*存放姓名用的字符数组*/
clrscr();
printf("Please enter name for searching.\n");
scanf("%s",s);
p=head; /*将头指针赋给p*/
while(strcmp(p->name,s) && p != NULL) /*当记录的姓名不是要找的,或指针不为空时*/
p=p->next; /*移动指针,指向下一结点*/
if(p!=NULL) /*如果指针不为空*/
{printf("\n*************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else
printf("\nThere is no num %s student on the list.\n",s); /*显示没有该学生*/
}

/*删除记录函数*/
STUDENT *delete(STUDENT *head)
{int n;
STUDENT *p1,*p2; /*p1为查找到要删除的结点指针,p2为其前驱指针*/
char c,s[6]; /*s[6]用来存放学号,c用来输入字母*/
clrscr();
printf("Please enter the deleted num: ");
scanf("%s",s);
p1=p2=head; /*给p1和p2赋初值头指针*/
while(strcmp(p1->num,s) && p1 != NULL) /*当记录的学号不是要找的,或指针不为空时*/
{p2=p1; /*将p1指针值赋给p2作为p1的前驱指针*/
p1=p1->next; /*将p1指针指向下一条记录*/
}
if(strcmp(p1->num,s)==0) /*学号找到了*/
{printf("**************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
printf("Are you sure to delete the student Y/N ?"); /*提示是否要删除,输入Y删除,N则退出*/
for(;;)
{scanf("%c",&c);
if(c=='n'||c=='N') break; /*如果不删除,则跳出本循环*/
if(c=='y'||c=='Y')
{
if(p1==head) /*若p1==head,说明被删结点是首结点*/
head=p1->next; /*把第二个结点地址赋予head*/
else
p2->next=p1->next; /*否则将一下结点地址赋给前一结点地址*/
n=n-1;
printf("\nNum %s student have been deleted.\n",s);
printf("Don't forget to save.\n");break; /*删除后就跳出循环*/
}
}
}
else
printf("\nThere is no num %s student on the list.\n",s); /*找不到该结点*/
return(head);
}

/*排序函数*/
STUDENT *sort(STUDENT *head)
{int i=0; /*保存名次*/
STUDENT *p1,*p2,*t,*temp; /*定义临时指针*/
temp=head->next; /*将原表的头指针所指的下一个结点作头指针*/
head->next=NULL; /*第一个结点为新表的头结点*/
while(temp!=NULL) /*当原表不为空时,进行排序*/
{
t=temp; /*取原表的头结点*/
temp=temp->next; /*原表头结点指针后移*/
p1=head; /*设定移动指针p1,从头指针开始*/
p2=head; /*设定移动指针p2做为p1的前驱,初值为头指针*/
while(t->averageaverage&&p1!=NULL) /*作成绩平均分比较*/
{
p2=p1; /*待排序点值小,则新表指针后移*/
p1=p1->next;
}
if(p1==p2) /*p1==p2,说明待排序点值大,应排在首位*/
{
t->next=p1; /*待排序点的后继为p*/
head=t; /*新头结点为待排序点*/
}
else /*待排序点应插入在中间某个位置p2和p1之间,如p为空则是尾部*/
{
t->next=p1; /*t的后继是p1*/
p2->next=t; /*p2的后继是t*/
}
}
p1=head; /*已排好序的头指针赋给p1,准备填写名次*/
while(p1!=NULL) /*当p1不为空时,进行下列操作*/
{
i++; /*结点序号*/
p1->order=i; /*将结点序号赋值给名次*/
p1=p1->next; /*指针后移*/
}
printf("Sorting is sucessful.\n"); /*排序成功*/
return (head);
}

/*插入记录函数*/
STUDENT *insert(STUDENT *head,STUDENT *newnode)
{STUDENT *p0,*p1,*p2;
int n,sum1,i;
p1=head; /*使p1指向第一个结点*/
p0=newnode; /*p0指向要插入的结点*/
printf("\nPlease enter a newnode record.\n"); /*提示输入记录信息*/
printf("Enter the num:");
scanf("%s",newnode->num);
printf("Enter the name:");
scanf("%s",newnode->name);
printf("Please enter the %d scores.\n",3);
sum1=0; /*保存新记录的总分,初值为0*/
for(i=0;i<3;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",&newnode->score[i]);
if(newnode->score[i]>100||newnode->score[i]<0)
printf("Data error,please enter again.\n");
}while(newnode->score[i]>100||newnode->score[i]<0);
sum1=sum1+newnode->score[i]; /*累加各门成绩*/
}
newnode->sum=sum1; /*将总分存入新记录中*/
newnode->average=(float)sum1/3;
newnode->order=0;
if(head==NULL) /*原来的链表是空表*/
{head=p0;p0->next=NULL;} /*使p0指向的结点作为头结点*/
else
{while((p0->averageaverage)&&(p1->next!=NULL))
{p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1->next; /*p1后移一个结点*/
}
if(p0->average>=p1->average)
{if(head==p1)head=p0; /*插到原来第一个结点之前*/
else p2->next=p0; /*插到p2指向的结点之后*/
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;} /*插到最后的结点之后*/
}
n=n+1; /*结点数加1*/
head=sort(head); /*调用排序的函数,将学生成绩重新排序*/
printf("\nStudent %s have been inserted.\n",newnode->name);
printf("Don't forget to save the newnode file.\n");
return(head);
}

/*保存数据到文件函数*/
void save(STUDENT *head)
{FILE *fp; /*定义指向文件的指针*/
STUDENT *p; /* 定义移动指针*/
char outfile[10];
printf("Enter outfile name,for example c:\\score\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL) /*为输出打开一个二进制文件,为只写方式*/
{
printf("Cannot open the file\n");
return; /*若打不开则返回菜单*/
}
printf("\nSaving the file......\n");
p=head; /*移动指针从头指针开始*/
while(p!=NULL) /*如p不为空*/
{
fwrite(p,LEN,1,fp); /*写入一条记录*/
p=p->next; /*指针后移*/
}
fclose(fp); /*关闭文件*/
printf("Save the file successfully!\n");
}

/* 从文件读数据函数*/
STUDENT *load()
{STUDENT *p1,*p2,*head=NULL; /*定义记录指针变量*/
FILE *fp; /* 定义指向文件的指针*/
char infile[10];
printf("Enter infile name,for example c:\\score\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL) /*打开一个二进制文件,为只读方式*/
{
printf("Can not open the file.\n");
return(head);
}
printf("\nLoading the file!\n");
p1=(STUDENT *)malloc(LEN); /*开辟一个新单元*/
if(!p1)
{
printf("Out of memory!\n");
return(head);
}
head=p1; /*申请到空间,将其作为头指针*/
while(!feof(fp)) /*循环读数据直到文件尾结束*/
{
if(fread(p1,LEN,1,fp)!=1) break; /*如果没读到数据,跳出循环*/
p1->next=(STUDENT *)malloc(LEN); /*为下一个结点开辟空间*/
if(!p1->next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1->next; /*指针后移,新读入数据链到当前表尾*/
}
p2->next=NULL; /*最后一个结点的后继指针为空*/
fclose(fp);
printf("You have success to read data from the file!\n");
return (head);
}
满意请采纳。


单片机C语言程序设计实训100例:基于PIC+Proteus仿真的目录
第1章PIC单片机C语言程序设计概述1.1 PIC单片机简介1.2 MPLAB+C语言程序开发环境安装及应用1.3 PICC\/PICC18\/MCC18程序设计基础1.4 PIC单片机内存结构1.5 PIC单片机配置位1.6 基本的I\/O端口编程1.7 中断服务程序设计1.8 PIC单片机外设相关寄存器1.9 C语言程序设计在PIC单片机应用系统开发中的优势...

c语言程序输出100~999之间个位是5且能被7整除的数,要求每行输出5个...
include<stdio.h>int main(void){ int n,i;\/\/少个;号 for(i=100;i<=999;i=i+1)\/\/第二个;号没用英文符号 if(i%10==5 && i%7==0)\/\/(号没用英文符号{ n++;printf("%5d",i);if(n%5==0)\/\/(号和)号都没用英文符号printf("\\n");\/\/(号和)号还有;号都没用英文符号} ...

用c语言程序输出100——200中不能被3整除的数,每四个换一行输出.并且计 ...
用for循环写,从100开始,除3取余不等于0的数就可以输出了,然后在循环外面声明一个整型变量,初始值为0,每输出1次加1,当计数器不为0并且除4取余为0时输出换行符,最后在循环外面声明一个整型变量,初始值为0,每次输出就进行累加,循环完成后输出总和 完 ...

编写c语言程序输出3--100中的所有素数,按每行5个输出
include<stdio.h> include<math.h> int shu(long unsigned num) \/\/判断素数函数,是,返回1.否,0 { int t,i;t=sqrt(num)+1;for(i=2;i<t;i++){ if(num%i==0)break;} return i==t;} int main(){ int i,k=0,n=300;for(i=3;i<n;i++)if(shu(i)) \/\/是素数 { k++;...

使用c语言多重循环编写程序搜索100以内的所有可能的勾股数组合,并将它...
include <stdio.h> define N 100 int main(){ int i,j,k;for(i=1;i<=N;i++)for(j=1;j<=N;j++)for(k=1;k<=N;k++)if(i*i+j*j==k*k&&i<j&&j<k)\/\/判断并且按从小到大的顺序输出 printf("\\t%d\\t%d\\t%d\\n",i,j,k);} ...

C语言编程:随机产生100个[10,80]范围整数,输出这100个数(每行10个...
代码文本:include "stdio.h"include <stdlib.h> include "time.h"int main(int argc,char *argv[]){ int n,s,k,t;k=s=n=0;srand((unsigned)time(NULL));while(n++<100){ printf(++k%10 ? "%3d" : "%3d\\n",t=rand()%71+10);s+=t;} printf("\\nThe SUM is %d\\nThe ...

求C语言程序设计,要200行左右的,急用
line(150+h*100,0,150+h*100,472);for(h=0;h<3;h++){ setlinestyle(3,0,1);line(200+h*100,0,200+h*100,472);settextstyle(1,HORIZ_DIR,3);} settextstyle(1,HORIZ_DIR,4);setcolor(RED);outtextxy(40,40,"PASS");outtextxy(40,80,"1");setcolor(10);setlinestyle(SOLID_...

写一个C语言程序,输出3~100之间的全部素数,每行显示6个素数
include<math.h>main(){ int i,j,a[101]; for(i=1;i<=100;i++) a[i]=i;for(i=2;i<sqrt(100);i++) for(j=i+1;j<=100;j++) { if(a[i]!=0&&a[j]!=0) if(a[j]%a[i]==0) a[j]=0; }printf("\\n");for(i=2,n=0;i<=100...

跪求c语言程序教程,先上100,搞定追加。
千锋Mars老师Java语言教程-2013年版,史上最牛java培训视频百度网盘免费资源在线学习 链接: https:\/\/pan.baidu.com\/s\/1115MNWjku3s1Wi2JHGecsw 提取码: vwi9 千锋Mars老师Java语言教程-2013年版,史上最牛java培训视频 千锋-java语言教程-43_类集框架(三).mp4 千锋-java语言教程-42_类集框...

C语言:从键盘输入一个不超过100个字符的字符串,其中字符串只包括字母...
1、首先需要打开编程软件。2、输入以下程序:#include <stdio.h>int main(){ char str[40]; scanf("%s",&str); printf("%s",str); return 0;}。3、然后按F5运行程序。4、然后输入想要输入的字符串。5、按回车键,就会弹出你输入的字符串。6、注意字符串的大小,改变中括号中的数字改变输入...

仙游县13788755501: 急需100行左右的C语言程序 -
木南利胆: /*-------------------------------------------------------*/ /* 俄罗斯方块 */ /* */ /*-------------------------------------------------------*/ /*------------- Compile ------------------------------ */ /* [in os mode:] */ /* bgiobj egavga */ /* tlib lib\graphics.lib+egavga */ /* [in TC: ] */ /* make exe */ ...

仙游县13788755501: 求一个100行左右的C语言程序
木南利胆:#include<stdio.h> #include<stdlib.h> #include<time.h> #include<conio.h> main() { int radomss; int num1; int jss=0; clrscr(); randomize(); radomss=random(100); while(1) { jss++; scanf("%d",&num1); if(num1==radomss) break; else if(num1<...

仙游县13788755501: 求个100行左右的c语言程序例子,配上中文注释. -
木南利胆: #include <stdio.h> int main(int argc, char *argv[]) { int n; printf("请输入方阵的阶数:"); scanf("%d", &n); int A[n][n], B[n][n], C[n][n], D[n][n], E[n][n], F[n][n]; printf("输入两个%d阶矩阵:\nA:\n", n); for(int i=0; i<n; i++) for(int j=0; j<n; j++)...

仙游县13788755501: 求一个大约100行的C语言程序,不要太复杂的 -
木南利胆: main( ) {int max(int,int); int min(int,int); int add(int,int); int process(int,int,int(*fun)( ); int a,b; printf("enter a and b:"); scanf("%d,%d",&a,&b); printf("max="); process(a,b,max); printf("min="); process(a,b,min); printf("sum="); process(a,b,...

仙游县13788755501: 求一个C语言100行的程序..要求在下面.. -
木南利胆: #include<stdio.h>//万年历 long int f(int year,int month) {//f(年,月)=年-月,如月<3;否则,f(年,月)=年 if (month <3) return year-1; else return year; } long int g(int month) {//g( 月)=月+13,如月<3;否则,g(月)=月+1 if(month<3) ...

仙游县13788755501: 速求100行左右的C语言程序 -
木南利胆: 答案 void main() { int sele=1,t; float x; system("cls");printf("欢迎使用简易菜单!本菜单在VC++平台编译通过\n"); printf("有何建议请联系本人!\n"); printf("成绩管理菜单\n"); printf(" \n"); printf(">> 1.输入成绩 2.计算总分 > 3.求平...

仙游县13788755501: 求100行的C语言程序啊 可以是计算器 通讯录之类的 哪位大神帮忙解决下咯!!! -
木南利胆: 计算器,自己编的#include<stdio.h>#include<stdlib.h>#include<string.h> double part_take1(char a[],int b,int c); double multiply(char h[]); void part_take2(char d[],char *e,int f,int g); int main(void) { int i; int j; int k; int length; char ch2[100]; char express[...

仙游县13788755501: 求助一个100行以上的C语言程序 -
木南利胆: 我给你一个吧,复数的四则运算./** 带有数据类型标志的复数四则运算.输入值可以是直角坐标或极坐标型,也 * 可以是它们的混合型.当进行加减运算时,输出值为直角坐标型;当进行乘 * 除运算时,输出值为极坐标型. */#include #include...

仙游县13788755501: c语言100句的程序, -
木南利胆: #include #include #define nmax 14 /*number的最大位数*/ #define norw 8 /*关键字个数*/ #define al 10 #define getchdo if(-1==getch()) return -1 #define getsymdo if(-1==getsym()) return -1 char ch; /*获取字符的缓冲区,getch使用*/ char word[norw...

仙游县13788755501: 求助!!!!!!!! 求一篇C语言程序,要求100行,每行带中文注释,内容随意(越简单越好)!!!!!!!!
木南利胆: #include<stdio.h> #include<Windows.h>/*后面关闭程序需要调用此头文件*/ #include<string.h> #define N 10/*定义学生的个数*/ typedef struct/*结构体*/ { char number[20];/*定义学生的学号*/ int grade[4];/*定义学生的四门成绩,这里用的整型,...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网