标题:看看我这程序的毛病,大哥大姐们帮帮忙
只看楼主
taopwen
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-8-31
 问题点数:0 回复次数:3 
看看我这程序的毛病,大哥大姐们帮帮忙

看看我这程序的毛病,大哥大姐们帮帮忙 #include<stdio.h> #include<string.h> #include<malloc.h> #define NULL 0 #define LEN sizeof(struct student) struct student {long int num; char nam; 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); scanf("%ld %c %d %d %d\n",&p1->num,&p1->nam,&p1->Escore,&p1->Cscore,&p1->Mscore); head=NULL; while(p1->num!=0) {n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(struct student *)malloc(LEN); scanf("%ld %s %d %d %d \n",&p1->num,&p1->nam,&p1->Escore,&p1->Cscore,&p1->Mscore); p1->Ascore=p1->Escore+p1->Mscore+p1->Cscore; return head; } }

void output(struct student *head) {char k; struct student *p1,*p2; clrscr(); 1ab: printf("If you want exit,enter = !\n"); printf(" else press Enter \n"); getchar(); if (strcmp(k,"="==0)) return; if (strcmp(k,"\n"==0)) {p1=p2=head; p1->next=p1; if(head=NULL) {printf("It is empty!\n"); goto 1ab;} } 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; 2ab: printf("Enter the number which you want to find.If you want exit,enter 0 !\n"); scanf("%d",&find); if(find==0) return; else for(p1=p2=head;p1->next!=NULL;p1++) 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); } if(p1->num!=find) {printf("There is no the student which you want!\n"); goto 2ab; }

void inster(struct student *head) {struct student *stu,*p2,*p0,*p1; printf("Input the student infermation!\n"); printf("Number Name English Computer Math\n"); scanf("%ld %c %d %d %d\n",&stu->num,&stu->nam,&stu->Escore,&stu->Csore,&stu->Mscore); stu->Ascore=stu->Escore+stu->Cscore+stu->Mscore); 3ab:printf("which student do you want behind?\nAnd if you want exit,enter 0 !\n"); scanf("%ld\n",&p0->num); if(p0->num==0) return; else {p2=p1=head; 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 delete (struct student *head) {struct student *p1,*p2,*p0; 4ab:printf("Enter the number which you want to delete.Ifyou want exit,enter 0 !\n"0; scanf("%ld\n",&p0->num); 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; int a; clrscr(); printf("print a number from 1 to 6 !\n"); scanf("%d\n",&a); abc:switch(a) {case"1":head=input();break; case"2":output(head);break; case"3":find(head);break; case"4":insert(head);break; case"5":delete(head);break; case"6":goto abc;break; default:printf("Error common!\n");break;

} }

搜索更多相关主题的帖子: 大哥大姐 student int struct 毛病 
2005-08-31 16:13
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
得分:0 

我帮你稍微改了下,自己看看吧。写了注释了,对了如果你用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编辑过]


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2005-08-31 23:26
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
得分:0 
对了以后写程序时,不要像我一样写注释,注释需要统一规范,整个程序中只能出现一种格式的注视符号,要么//当行注释要么/*..*/多行注释,不要出现混合注释,我只是贪图方便罢了。

我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2005-08-31 23:28
likk
Rank: 1
等 级:新手上路
帖 子:65
专家分:0
注 册:2005-9-15
得分:0 
新手同志:
       你的基础实在是太差了
        建议你再好好看看书
           有一本书对你比较好--------------谭浩强主编的c程序设计
谢谢
2005-09-16 12:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-26418-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.134787 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved