日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

结构变量输入不正确的顺序可能会导致不正确的操作结果

發布時間:2023/12/14 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 结构变量输入不正确的顺序可能会导致不正确的操作结果 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寫一個程序,需求:由...制作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,


假設將想要輸入的字符串數組放在輸入的中間,就極有可能導致

在輸入完字符串之后,還會將接下來輸入的內容輸入到字符串中,

會導致輸出值發生混亂,得不到想要的結果,因而,最好將字符串

輸入參數放在參數列表的最后,這樣它有可能避免輸出錯誤。


版權聲明:本文博客原創文章。博客,未經同意,不得轉載。

轉載于:https://www.cnblogs.com/lcchuguo/p/4653771.html

總結

以上是生活随笔為你收集整理的结构变量输入不正确的顺序可能会导致不正确的操作结果的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。