大侠们看看这个程序到底是什么问题?

来源:百度知道 编辑:UC知道 时间:2024/06/01 06:08:03
//程序可用编译通过,但是,输出有问题.
//输出学生数据时完了后,多输出了一写不必要的东西,不知道为什么
#include"stdio.h"
#include "stdafx.h"
#include"malloc.h"
#include"string.h"

#define LEN sizeof(struct stu)
#define NULL 0
//------------------------------------------
struct stu//定义一个学生的结构体
{
char num[10];//学号
char name[20];//姓名
float score;//成绩
struct stu *next;//链表
};
//------------------------------------------下面定义需要的全局变量
struct stu *head=NULL;//头指针
struct stu *foot=NULL;//链表末尾的指针
//------------------------------------------下面是添加学生的函数
void add(void)//添加学生
{
struct stu *p1;
p1=(struct stu*)malloc(LEN);
printf("学号,姓名,分数\n");
scanf("%s,%s,%f",p1->num,p1->name,p1->score);
if(head==NULL)//如果头指针为空,则说明没有数据
{
head=p1;//将第一个结构体的指针赋给head
p1->next=NULL;
foot=p1;//第一个,也是最后一个,所以将该结构体的地址赋给foot

p1->score前面加个&
scanf("%s %s %f",p1->num,p1->name,&(p1->score));,改成空格
比如你输入123,王二,65.7
系统会将整个123,王二,65.7赋给p1->name

"输出有问题"这五个字可以换成如下的:

"输出有问题,比如,我输入:*****************,程序输出@@@@@@@@@@@@@@@@@@@"

不用“,”分隔输入的字符串
并且接收输入信息的变量前要加&

scanf("%s,%s,%f",p1->num,p1->name,p1->score);
就是这句的问题,改成scanf("%s%s%f",p1->num,p1->name,&p1->score);
输入信息用空格隔开即可。