小弟做了一个学生成绩小系统,在win--tc运行,但是输入那里一直无限循环,问题小弟找不出,请各位高手指点一二,积分全给了
各位大神,求救啊。。小弟做了一个学生成绩小系统,在win--tc运行,但是输入那里一直无限循环,问题小弟找不出,请各位高手指点一二,积分全给了。谢谢,感激不尽。。#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#define N 3
typedef struct z1
{
char no[11];
char name[15];
int score[N];
float sum;
float average;
struct z1 *next;
}STUDENT;
STUDENT *init(); /*初始化*/
STUDENT *create();
STUDENT *delete(STUDENT *h);
void print(STUDENT *h);
void search(STUDENT *h);
void computer(STUDENT *h);
void save(STUDENT *h);
int menu_select();
main()
{
STUDENT *head;
head=init();
clrscr();
for(;;)
{
switch(menu_select())
{
case 0:head=init();break;
case 1:head=create();break;
case 2:head=delete(head);break;
case 3:print(head);break;
case 4:search(head);break;
case 5:computer(head);break;
case 6:save(head);break;
case 7:exit(0);
}
}
}
menu_select()
{
char *menu[]={"***************MENU***************",
" 0. init list",
" 1. Enter list",
" 2. Delete a record from list",
" 3. print list ",
" 4. Search record on name",
" 5. compute the score",
" 6. Save the file",
" 7. Quit"};
char s[3];
int c,i;
gotoxy(1,25);
printf("压任一键进入主菜单......\n");
getch();
clrscr();/*清屏函数*/
for(i=0;i<9;i++)
{
gotoxy(10,i+1); /*定位函数*/
cprintf("%s",menu[i]);
}
textbackground(BLACK); /*设置文本的背景颜色*/
window(1,1,80,25);
gotoxy(10,21); /*定位函数*/
do{
printf("\n 请输入选项(0~6):");
scanf("%s",s);
c=atoi(s);
}while(c<0||c>6);
return c;
}
STUDENT *init()
{
return NULL;
}
STUDENT *create()
{
int i; int s;
STUDENT *h=NULL,*info;
for(;;)
{
info=(STUDENT *)malloc(sizeof(STUDENT));
if(!info)
{
printf("\nout of memory"); /*如果没有空间就打印*/
return NULL; /*链表到此结束,不指向任何节点*/
}
inputs("请输入学号:",info->no,11);/* info指向no数组*/
if(info->no[0]=='@') break;
inputs("请输入姓名:",info->name,15);
printf("请输入 %d 科成绩 \n",N);
s=0;
for(i=0;i<N;i++)
{
do{
printf("请输入第%d科成绩:",i+1);
scanf("%d",&info->score[i]);
if(info->score[i]>100||info->score[i]<0)
printf("输入错误,请重新输入\n");
}while(info->score[i]>100||info->score[i]<0);
s=s+info->score[i]; /*把s+三科成绩赋值给s*/
}
info->sum=s; /*把成绩s赋值给info指向的总分sum*/
info->average=(float)s/N;
info->next=h; /*指针后移*/
h=info; /*把info赋值给h*/
}
return(h);
}
inputs(char *prompt, char *s, int count)
{
char p[255];
do{
printf(prompt);
scanf("%s",p);
if(strlen(p)>count)printf("\n输入字符过长 \n");
}while(strlen(p)>count);
strcpy(s,p);
}
void print(STUDENT *h)
{
STUDENT *p;
clrscr();
p=h;
printf("\n****************************STUDENT********************************\n");
printf("|nO | name | sc1| sc2| sc3| sum | ave |\n");
printf("|---|------|----|----|----|-----|--------|-------|\n");
while(p!=NULL)
{
printf("|%-10s|%-15s|%4d|%4d|%4d|%4.2f|%4.2f|\n",p->no,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average);
p=p->next; /*指针后移*/
}
printf("**********************************end*********************************\n");
}
STUDENT *delete(STUDENT *h)
{
STUDENT *p,*q;
char s[11];
clrscr();
printf("请输入要删除的学号\n");
scanf("%s",s);
q=p=h;
while(strcmp(p->no,s)&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==NULL)
printf("\n没有%s该学生\n",s);
else
{
printf("*****************************have found***************************\n");
printf("|no | name | sc1| sc2| sc3| sum | ave |\n");
printf("|----------|----|----|----|----|--------|-------|-----|\n");
printf("|%-10s|%-15s|%4d|%4d|%4d| %4.2f | %4.2f |\n", p->no,
p->name,p->score[0],p->score[1],p->score[2],p->sum,
p->average);
printf("********************************end*******************************\n");
getch();
if(p==h)
h=p->next;
else
q->next=p->next;
free(p); /*malloc是用来动态申请内存空间,而free用来释放malloc申请的空间*/
printf("\n 已删除No %s学生\n",s);
}
return(h);
}
/*查找记录*/
void search(STUDENT* h)
{
STUDENT *p;
char s[15];
int isFind = 0;
clrscr();
printf("请输入要查找的学生姓名\n");
scanf("%s",s);
p=h;
while(p!=NULL)
if(!strcmp(p->name,s))
{
printf("|nO | name | sc1| sc2| sc3| sum | ave |\n");
printf("|----------|----|----|----|----|--------|-------|-----|\n");
printf("|%-10s|%-15s|%4d|%4d|%4d| %4.2f | %4.2f | %3d |\n", p->no,
p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average);
printf("********************************end*******************************\n");
isFind = 1;
}
p = p->next;
}
if(!isFind) {
printf("\n没有 %s该学生\n",s);
}
}
void save(STUDENT *h)
{
FILE *fp; /*定义指向文件的指针*/
STUDENT *p; /* 定义移动指针*/
char outfile[10];
printf("请输入文件名格式,例如 c:\\f1\\te.txt:\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL)
{
printf("无法打开文件\n");
exit(1);/*就是退出,可以不论main()的返回值类型*/
}
printf("\n文件保存中......\n");
p=h;
while(p!=NULL)
{
fwrite(p,sizeof(STUDENT),1,fp);
p=p->next;
}
fclose(fp);
printf("-----保存成功!!-----\n");
}
/*计算总分和均值*/
void computer(STUDENT *h)
{
STUDENT *p;
int i=0;
long s=0;
float average=0;
p=h;
while(p!=NULL)
{
s+=p->sum;
i++;
p=p->next;
}
average=(float)s/i;
printf("\n所有学生的总分是:%ld 平均分是 %5.2f\n",s,average);
}