|
我的C程序有时正常又有时输出乱码,为什么?
几日前搞好的C课程设计(当时没问题),刚才试了试,有时会输出乱码。如果老师看的时候乱码就惨啦,大家试过吗?
感谢 毛啦啦的提点。creat函数是改书本例子的。
可是为什么会有时正常呢?
代码:
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
char name[10];
float mat,eng,com,sum,aver;
struct student *next;
};
int n;
struct student *head=NULL;
struct student *creat(void)
{
struct student *p1,*p2;
n=0;
clrscr();
printf("*************************CREAT*******************************\n");
printf("(if you finish inputting,please input name=0)\n\n");
p1=p2=(struct student *)malloc(LEN);
printf("please input name:");
scanf("%s",p1->name);
printf("please input number:");
scanf("%ld",&p1->num);
printf("please input math:");
scanf("%f",&p1->mat);
printf("please input english:");
scanf("%f",&p1->eng);
printf("please input computer:");
scanf("%f",&p1->com);
p1->sum=p1->mat+p1->eng+p1->com;
p1->aver=p1->sum/3;
while(1)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
printf("please input name:");
scanf("%s",p1->name);
if((strcmp(p1->name,"0")==0))
{
free(p1);
return(head);}
printf("please input number:");
scanf("%ld",&p1->num);
printf("please input math:");
scanf("%f",&p1->mat);
printf("please input english:");
scanf("%f",&p1->eng);
printf("please input computer:");
scanf("%f",&p1->com);
p1->sum=p1->mat+p1->eng+p1->com;
p1->aver=p1->sum/3;
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
clrscr();
printf("****************************ALL STUDENTS****************************\n");
printf("number name math english computer sum aver\n");
p=head;
if(p!=NULL)
do
{
printf("%-10ld %-10s %-5.1f %-5.1f %-5.1f %-5.1f %-6.2f\n",p->num,p->name,p->mat,p->eng,p->com,p->sum,p->aver);
p=p->next;
}while(p!=NULL);
printf("press anykey out!\n");
getch();
}
main()
{float aa,ff=9;
aa=sqrt(aa);
head=creat();
print(head);
}
没格式的代码看起来很麻烦,改了下格式,PS:{dd 在大括号下不换行这种教材上的风格很不合我的代码风格... - -# 不知有没人和我一样...
[ 本帖最后由 Sai999 于 2007-1-19 16:45 编辑 ] |
|