| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 770 人关注过本帖
标题:哪里有错,不明白
取消只看楼主 加入收藏
emcyk
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-1-7
收藏
 问题点数:0 回复次数:2 
哪里有错,不明白

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#define NULL 0
#define LEN sizeof(struct student)

struct student /*定义一个学生考试信息的结构体*/
{char num[10];
char name[10];
int Chinese;
int math;
int English;
int sum;
int aver;
struct student *next;/*定义指向下一个结构体的指针*/
};
int n; /*用来统计考生人数的*/
char ch[10]; /*用来存放姓名的*/
struct student *creat(void) /*定义一个指向struct student的结构体指针函数*creat()用来存储考生信息.*/
{
struct student *head,*p1,*p2;/*定义三个指向struct student的结构体指针*/
n=0;
printf("input student informations:\n");
printf("input 'stop' to end:\n");
printf("input name:");
scanf("%s",ch);
head=NULL; /*给指针head赋初值*/
p1=p2=(struct student *)malloc(LEN);/*调用malloc()函数用来开辟一个新的存储单元*/
while (strcmp(ch,"stop")!=0) /*调用字符比较函数strcmp()用来判断是否继续输入*/
{p1=(struct student*)malloc(LEN);/*调用malloc()函数用来开辟一个新的存储单元*/
strcpy(p1->name,ch); /*将循环结构前面输入的姓名复制到结构体名为p1的数组name中*/
printf("input number:");
scanf("%s",p1->num);
printf("input Chinese:");
scanf("%d",&p1->Chinese);/*将输入的中文成绩存放到p1所指结构体的数组score中*/
printf("input math:");
scanf("%d",&p1->math);/*将输入的数学成绩存放到p1所指结构体的数组score中*/
printf("input English:");
scanf("%d",&p1->English);/*将输入的英语成绩存放到p1所指结构体的数组score中*/
p1->sum=p1->Chinese+p1->math+p1->English;/*计算总分 */
p1->aver=p1->sum/3;
if(n==0) head=p1;/*如果是输入第一组学生考试信息就将指针p1赋给指针head*/
else p2->next=p1;/*否则将p1赋给p2所指结构体的next指针*/
p2=p1;/*将指针p1赋给指针p2*/
n++; /*将n的值加1*/
printf("name:");
scanf("%s",ch);/*将输入的姓名存放到字符数组ch中*/
}
p2->next=NULL;/*将p2所指结构体的next指针重新赋空值*/
return (head);/*将输入的第一组学生考试信息返回*/
}
void output(struct student *head) /*定义output()函数将考生的信息从头指针所指内容开始输出*/
{struct student *p;/*定义一个指向struct student的结构体指针p*/
printf("-------------------------------------------------------------------------\n"); /*73个"-"*/
printf("---------------------* students's information *--------------------------\n");
printf("-------------------------------------------------------------------------\n");
printf("-----num------name------Chinese------math------English------sum------aver\n");
printf("-------------------------------------------------------------------------\n");
p=head;/*将头指针赋给p*/
if(head!=NULL)/*如果头指针非空则继续*/
do{printf("%8s%10s---------%4d------%4d---------%4d----%5d------%4d\n",p->num,p->name,p->Chinese,p->math,p->English,p->sum,p->aver);
printf("-------------------------------------------------------------------------\n");
p=p->next;/*将下一组考生信息的next指针赋给p*/
}while(p!=NULL);/*若指针p非空则继续,目的是把所有的考生信息都传给指针p然后输出.*/
}

void modify(char target[],struct student *mhead)
{struct student *mp;
mp=mhead;
for(;mp!=NULL;mp=mp->next)
if(strcmp(mp->num,target)==0)
{printf("%s,%s,%d,%d,%d,%d,%d\n",mp->num,mp->name,mp->Chinese,mp->math,mp->English,mp->sum,mp->aver);
printf("Reinputs the student information:");
scanf ("%s,%s,%d,%d,%d,%d,%d",mp->num,mp->name,&mp->Chinese,&mp->math,&mp->English,&mp->sum,&mp->aver);
printf("%s,%s,%d,%d,%d,%d,%d\n",mp->num,mp->name,mp->Chinese,mp->math,mp->English,mp->sum,mp->aver);
}
}
main()
{struct student *z;
char number[10];
z=creat();
output(z);
printf("input a student's number:");
scanf("%s",number);
modify(number,z);
getch();
}
最后一个输出怎么有问题啊?

搜索更多相关主题的帖子: 信息 姓名 include 结构体 
2007-01-07 14:24
emcyk
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-1-7
收藏
得分:0 
以下是引用neverTheSame在2007-1-7 14:39:42的发言:
我运行了一下,没什么不合理的问题。

最后输出的时候 ,会多出几个数啊!我运行的时候

2007-01-07 15:12
emcyk
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-1-7
收藏
得分:0 
对3楼万分感谢,都想加一下你QQ
2007-01-07 15:17
快速回复:哪里有错,不明白
数据加载中...
 
   



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

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