例题8.4 从键盘上读10个值,每个值从一新行开始读起,以字符I或C开头,指出此值是整数还是字符。把这些值保存在链表中,重新显示。
#include "stdio.h"
#define ERROR {printf("errorn");return;}
#define SKIP while(getchar()!='n');
main()
{int i;
struct item
{union
{char c;
int a;
}val;
int what;
struct item *next;
}*p,*temp;
if((p=(struct item*)malloc(sizeof(struct item)))==NULL)
ERROR
temp=p;
for(i=0;;i++)
{switch(getchar())
{case 'i'
->what=0;scanf("%d",&(p->val.a));break;
case 'c'
->what=1;scanf("%c",&(p->val.c));break;
default:ERROR
}
SKIP
if(i==9) break;
else
{if((p->next=(struct item*)malloc(sizeof(struct item)))==NULL)
ERROR
p=p->next;
}
}
for(i=0,p=temp;i<10;p=p->next,i++)
if(p->what)
printf("%c",p->val.c);
else printf("%d",p->val.a);
}
为何字符没有输出?
各位如果下不到的话
就下这个地址ftp://210.51.180.252/bbs/computer/吉林大学C语言视频教程_第01集[www.TopSage.com].zip
把01换成01-51就好了,这个用迅雷比较方便
或者直接去http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=177517
| ed2k: |