day1:关于if判断中的字符串比较问题

        

  这是我发布的第一个文章。。。

  本人计算机大一新手,想把自己近来遇到的一些问题分享给大家,避免踩坑。

在这道题中,其实关于链表的建立是比较基础的,但是在最后有一个当输入“#”时结束。

由于姓名都是以字符串的形式输入的,所以这里的“#”应该是字符串,但是如何判断是否为“#”呢?

刚开始我想的是用“==”,但是发现错误,后来经过网上查询明白,字符串的比较不能用“==”,而应该用strcmp函数。此函数在两者相等时返回“0”,不等时返回非0值。所以后面就比较轻松了。

#include<stdio.h>
#include<stdlib.h>
struct stu
{
	double xh;
	char name[80];
	int cj;
	struct stu* next;
};
struct stu* list(struct stu *p)
{
	struct stu *head,*q;
	head=NULL;
	q=(struct stu*)malloc(sizeof(struct stu));
			while(p->xh)
		{	
			if(head==NULL)
				head=p;
			else
				q->next=p;
				q=p;
			p=(struct stu*)malloc(sizeof(struct stu));
			scanf("%lf%s%d",&p->xh,p->name,&p->cj);
		}
		q->next=NULL;
		free(p);
			return head;
}
void print(struct stu* h)
{
	int i=0;
	double count=0;
	while(h!=NULL)
	{
		printf("%.0lf %s %d\n",h->xh,h->name,h->cj);
		count=count+h->cj;
		i++;
		h=h->next;
	}
	printf("%.2lf\n",count/i);
}
int main()
{
	struct stu* h;
	struct stu *p;
	p=(struct stu*)malloc(sizeof(struct stu));
	while(scanf("%lf%s%d",&p->xh,p->name,&p->cj)!=EOF)
	{
		h=list(p);
	 	print(h);
	 	p=(struct stu*)malloc(sizeof(struct stu));
	}
}

以上为代码,欢迎大家有更优解分享。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值