我帮你稍微改了下,自己看看吧。写了注释了,对了如果你用TC2.0编译的话请把//注释行改成/* */注释行,否则无法运行。我怀疑这个程序是不是你自己写的?有许多概念错误,如果是你抄来的话,我只想说一句,学习不是别人的事情,是你自己的事情,如果是你自己写的,我觉得你基础很差,请好好看书。注意写程序的风格。我没怎么调试,不知道正确与否,错误全部改正了。总之自己看。。。
#include<stdio.h>
#include<string.h>
#include<malloc.h>
// To call the clrscr() function , include conio.h
#include <conio.h>
// NULL 0 is defined in stdio.h
//#define NULL 0
#define LEN sizeof(struct student)
/*
PROGRAMMING ADVICE: Notice the style of code you write, neat and tidy writing
style may help others to understand what you want to express in the program and
provide extra infomation of the principal of your program, somehow the habit you
formed may help you in finding the errors. Good habit will work well but a bad habit
like this will bring you trouble and destory all your work, even wasting time.
*/
struct student {
long int num;
char nam; /* ATTENTION: the name you defined is a character, using %c in scanf() function to retrieve a character. */
int Escore;
int Mscore;
int Cscore;
int Ascore;
struct student *next;
};
struct student *input(void) {
struct student *p1,*p2,*head;
int n=0;
printf("Enter the heo recores.\nAnd if you want to exit:please enter 0!\n");
// p1=p2=(struct student *)mallocl(LEN);
// You miss spelled the memory allocating function name, please remember it -
// the function prototype void *malloc(size_t)
p1=p2=(struct student *)malloc(LEN);
scanf("%ld %c %d %d %d",&p1->num,&p1->nam,&p1->Escore,&p1->Cscore,&p1->Mscore);
head=NULL;
while(p1->num!=0) {
n=n+1;
if(n==1)
head=p1; // head->null
else
p2->next=p1; // head->child1->child2->...
p2=p1; // move current needle to the new item you just added in order to make the list
p1=(struct student *)malloc(LEN);
scanf("%ld %c %d %d %d",&p1->num,&p1->nam,&p1->Escore,&p1->Cscore,&p1->Mscore);
p1->Ascore=p1->Escore+p1->Mscore+p1->Cscore;
// return head;
/*
sentence 'return' had better to put out of the while surrounding, it's only to
get rid of a warning message
*/
}
return head;
}
void output(struct student *head) {
/* ADVICE: Don't use sentence 'goto' in structure programming */
/* Invailed name of label : The initial alphabet of a varible , a label or a function
name cannot be numberic. Please review the concept of how to name a varible
*/
char k;
struct student *p1/*,*p2 No use at all */;
clrscr();
/* 1ab: NOTIC: BAD Habit */ printf("If you want exit,enter = !\n");
printf(" else press Enter \n");
p1=head;
do {
k=getchar();
//getchar(); // varible k has not set any value here
//if (strcmp(k,"="==0)) k is a character, cannot compare by using strcmp() function
// please read the reference of strcmp() function
if(k == '=') //using '' to surround a byte symbol means a character
return;
//if (strcmp(k,"\n"==0))
//p1=p2=head;
//p1->next=p1;
// if(head=NULL) NOTICE: Here "head=NULL" is not a relation expression, it's only
// a expression of new value setting.
if(p1 == NULL) {
printf("It is empty!\n");
break;
}
else {
printf("%ld %c %d %d %d %d\n",p1->num,p1->nam,p1->Escore,p1->Cscore,p1->Mscore,p1->Ascore);
p1 = p1 -> next; // needle moves to next
}
/* goto 1ab; Using loop surround to instead */
} while(k!='\n');
// else
//{printf("number name English Cmputer Math Total\n");
//while (p1->next!=NULL)
// printf("%ld %c %d %d %d %d\n",p1->num,p1->nam,p1->Escore,p1->Cscore,p1->Mscore,p1->Ascore);
//}
}
void find(struct student *head) {
int find;
struct student *p1/*,*p2 */;
// int i; Needn't using loop varible
/* 2ab: BAD Habit */
do {
printf("Enter the number which you want to find.If you want exit,enter 0 !\n");
scanf("%d",&find);
/* if(find==0)
return ; */
if(find) /* else */ { /* NOTICE: Mutiple sentences , don't miss '{' & '}' */
for(p1=head;/*p1->next!=NULL*/ p1;p1=p1->next /* p1++ It is a big fault I've ever seen */) {
if(p1->num==find) {
printf("Numer Name English Compruter Math Average\n");
printf("%ld %c %d %d %d %d\n",p1->num,p1->nam,p1->Escore,p1->Cscore,p1->Mscore,p1->Ascore);
return ; // Exit the function
}
}
printf("There is no the student which you want!\n");
return ;
}
} while(find != 0) ;
/*
if(p1->num!=find)
{printf("There is no the student which you want!\n");
goto 2ab;
} */
} /* don't miss the '}' to end function install */
/* HINT: You've got a spell mistake of insert function's name */
void /*inster*/insert(struct student *head) {
long num=0;
struct student *stu,*p2,/* *p0,*/*p1;
printf("Input the student infermation!\n");
printf("Number Name English Computer Math\n");
/* WARNING: the 'stu' is not allocated, the value setting action may cause fault. */
/* Allocate */ stu=(struct student *)malloc(LEN);
scanf("%ld %c %d %d %d",&stu->num,&stu->nam,&stu->Escore,&stu->/*Csore*/Cscore,&stu->Mscore);
stu->Ascore=stu->Escore+stu->Cscore+stu->Mscore/*)*/;
/* 3ab: BAD HABIT */
printf("which student do you want behind?\nAnd if you want exit,enter 0 !\n");
scanf("%ld",/* &p0->num */ &num);
if(/*p0->num==0*/num) {
// New item can be put behind the head
//return;
if(head != NULL) {
stu->next=head;
head=stu;
}
else {
head=stu;
head->next=NULL;
}
}
else {
p1=head;
//p2=p1=head;
if(head == NULL) {
head=stu;
head->next=NULL;
return ;
}
do {
if(p1->num == num) {
p2=p1->next; /* back up the next node of p1 */
p1->next = stu;
stu->next = p2;
return ;
}
p1 = p1->next;
} while(p1);
printf("There is no the student %ld\n",/*p0->*/num);
return ;
}
/*if(head==NULL)
{head=stu;
stu->next=NULL;
}
else
{while((p2->num!=p0->num)&&(p1->num!=NULL))
{p2=p1=head;
p1=p1->next;
}
if(p2->num==p0->num)
{p2->next=stu->num;
stu->next=p1->num;
}
else
{printf("There is no the student %ld\n",p0->num);
goto 3ab;
}
}*/
}
void del(struct student *head) {
long num=0;
struct student *p1,/**p2,*/*p0;
/* 4ab: BAD HABIT */
printf("Enter the number which you want to delete.If you want exit,enter 0 !\n");
/* Don't use an unallocated pointer to hold a value, it is dangerous */
scanf("%ld",&num/*p0->num*/);
p1=head;
p0=NULL;
if(!head) {
printf("The school report is empty!\n");
return ;
}
do {
if(p1 == NULL) break ;
if(p1->num == num) {
if(p0 == NULL) {
head = head -> next;
free(p1);
}
else {
if(p1->next)
p0->next=p1->next;
else
p0->next=NULL;
free(p1);
}
printf("The student had been deleted!\n");
return ;
}
p0=p1;
p1=p1->next;
} while(num);
printf("There is no the record which you want!\n");
/*
if(p0->num==0)
return;
while((p2->num!=p0->num)&&(p1->num!=NULL))
{p1=p2=head;
p1=p1->next;
}
if(p2->num!=p0->num)&&(head!=NULL)
{printf("There is no the record which you want!\n");
goto 4ab;
}
else if(head==NULL)
{printf("The school report is empty!\n");
goto 4ab;
}
else if(p2->num==p0->num)
{p2->next=p0->next;
("The student had been deleted!\n");
goto 4ab;
}
*/
}
main() {
/* long int *head; Do you know the meaning of 'head' ? */
struct student *head = NULL;
int a;
clrscr();
do {
printf("print a number from 1 to 6 !\n");
scanf("%d\n",&a);
/* abc: BAD HABIT */
switch(a) {
case 1: head=input(); break;
case 2: output(head); break;
case 3: find(head); break;
case 4: insert(head); break;
case 5: del/* delete may a keyword in c++ */(head); break;
//case 6:goto abc;break;
default: printf("Error common!\n"); break;
}
} while(a!=6);
}
[此贴子已经被作者于2005-9-1 9:40:41编辑过]