结构变量输入不正确的顺序可能会导致不正确的操作结果
2015-07-17 10:50
330 查看
写一个程序,需求:由...制作3单向动态列表学生数据节点配置,输入学生数据向每个节点
(每个学生的数据包含学号、全名、成就)。每个节点然后逐个输出数据。
正确的程序,如下面的:
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
float score;
struct student *next;
} ;
void main()
{
struct student *head,*p,*q;
head=p=(struct student*) malloc(LEN);
scanf("%d,%f,%s",&p->num,&p->score,p->name);
p=(struct student*) malloc(LEN);
scanf("%d,%f,%s",&p->num,&p->score,p->name);
q=(struct student*) malloc(LEN);
scanf("%d,%f,%s",&q->num,&q->score,q->name);
head->next=p;
p->next=q;
q->next=NULL;
p=head;
printf("\n结点 1:%d,%5.1f,%s",p->num,p->score,p->name);
p=p->next;
printf("\n结点 2:%d,%5.1f,%s",p->num,p->score,p->name);
q=p->next;
printf("\n结点 3:%d,%5.1f,%s\n",q->num,q->score,q->name);
}
执行结果为:
输入:
10101,98,li
10102,87,wang
10103,76,qi
输出:
结点 1:10101, 98.0,li
结点 2:10102, 87.0,wang
结点 3:10103, 76.0,qi
错误的程序例如以下:
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
int num;
char name[2];
float score;
struct student *next;
} ;
void main()
{
struct student *head,*p,*q;
head=p=(struct student*) malloc(LEN);
scanf("%d,%s,%f",&p->num,p->name,&p->score);
p=(struct student*) malloc(LEN);
scanf("%d,%s,%f",&p->num,p->name,&p->score);
q=(struct student*) malloc(LEN);
scanf("%d,%s,%f",&q->num,q->name,&p->score);
head->next=p;
p->next=q;
q->next=NULL;
p=head;
printf("\n结点 1:%d,%5.1f,%s",p->num,p->score,p->name);
p=p->next;
printf("\n结点 2:%d,%5.1f,%s",p->num,p->score,p->name);
q=p->next;
printf("\n结点 3:%d,%5.1f,%s\n",q->num,q->score,q->name);
}
执行结果为:
输入:
10101,wang,,98
10102,wani,,87
10103,wangx,,76
输出:
结点 1:10101, 0.0,wang,,98 p�
结点 2:10102, 0.0,wani,,878p�
结点 3:10103, 0.0,wangx,,7
想要得到的结果应该为:
结点 1:10101, 98.0,wang,
结点 2:10102, 87.0,wani,
结点 3:10103, 76.0,wangx,
假设将想要输入的字符串数组放在输入的中间,就极有可能导致
在输入完字符串之后,还会将接下来输入的内容输入到字符串中,
会导致输出值发生混乱,得不到想要的结果,因而,最好将字符串
输入參数放在參数列表的最后,这样它有可能避免输出错误。
(每个学生的数据包含学号、全名、成就)。每个节点然后逐个输出数据。
正确的程序,如下面的:
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
float score;
struct student *next;
} ;
void main()
{
struct student *head,*p,*q;
head=p=(struct student*) malloc(LEN);
scanf("%d,%f,%s",&p->num,&p->score,p->name);
p=(struct student*) malloc(LEN);
scanf("%d,%f,%s",&p->num,&p->score,p->name);
q=(struct student*) malloc(LEN);
scanf("%d,%f,%s",&q->num,&q->score,q->name);
head->next=p;
p->next=q;
q->next=NULL;
p=head;
printf("\n结点 1:%d,%5.1f,%s",p->num,p->score,p->name);
p=p->next;
printf("\n结点 2:%d,%5.1f,%s",p->num,p->score,p->name);
q=p->next;
printf("\n结点 3:%d,%5.1f,%s\n",q->num,q->score,q->name);
}
执行结果为:
输入:
10101,98,li
10102,87,wang
10103,76,qi
输出:
结点 1:10101, 98.0,li
结点 2:10102, 87.0,wang
结点 3:10103, 76.0,qi
错误的程序例如以下:
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
int num;
char name[2];
float score;
struct student *next;
} ;
void main()
{
struct student *head,*p,*q;
head=p=(struct student*) malloc(LEN);
scanf("%d,%s,%f",&p->num,p->name,&p->score);
p=(struct student*) malloc(LEN);
scanf("%d,%s,%f",&p->num,p->name,&p->score);
q=(struct student*) malloc(LEN);
scanf("%d,%s,%f",&q->num,q->name,&p->score);
head->next=p;
p->next=q;
q->next=NULL;
p=head;
printf("\n结点 1:%d,%5.1f,%s",p->num,p->score,p->name);
p=p->next;
printf("\n结点 2:%d,%5.1f,%s",p->num,p->score,p->name);
q=p->next;
printf("\n结点 3:%d,%5.1f,%s\n",q->num,q->score,q->name);
}
执行结果为:
输入:
10101,wang,,98
10102,wani,,87
10103,wangx,,76
输出:
结点 1:10101, 0.0,wang,,98 p�
结点 2:10102, 0.0,wani,,878p�
结点 3:10103, 0.0,wangx,,7
想要得到的结果应该为:
结点 1:10101, 98.0,wang,
结点 2:10102, 87.0,wani,
结点 3:10103, 76.0,wangx,
假设将想要输入的字符串数组放在输入的中间,就极有可能导致
在输入完字符串之后,还会将接下来输入的内容输入到字符串中,
会导致输出值发生混乱,得不到想要的结果,因而,最好将字符串
输入參数放在參数列表的最后,这样它有可能避免输出错误。
相关文章推荐
- QT 信号与槽connect
- 归并排序的算法实现
- 二分搜索的递归和循环实现
- HTML导入本地图片
- *Codeforces Round #305 (Div. 1) C. Mike and Foam(容斥原理)
- 汇编--在屏幕上显示彩色字体
- JAVA实现顺序表
- 界面化文件搜索器
- java编写一个文件查找器
- 用图片替换java程序的咖啡图标
- JFinal源码解析系列之三
- Andriod使用Intent实现拨号
- LeetCode--Majority Element II & 多数投票算法
- 养成好习惯,学好嵌入式(U-boot,内核烧录)
- Freemarker 各种格式化
- sublime text 3 常见问题总结 pyv8
- 简易画图板
- HashMap与ConcurrentHashMap的区别
- 基于spring3.0的采用注解配置的spring MVC项目
- Oracle 查看表空间的大小及使用情况sql语句