8.17写一函数,实现两个字符串的比较。即自己写一个strcmp函数,函数原型为int strcmp(char *p1,char *p2);

//C程序设计第四版(谭浩强)
//章节:第八章 善于利用指针 
//题号:8.17
//题目:写一函数,实现两个字符串的比较。即自己写一个strcmp函数,函数原型为
//int strcmp(char *p1,char *p2);
//设p1指向字符串s1,p2指向字符串s2.要求当s1=s2时,返回值为0;若s1!=s2,返回它们二者第一个不同字符的ASCII码差值
//如"BOY"与"BAD",第2个字母不同,"O","A"之差为79-65=14)如果s1>s2,则输出正值;如果s1<s2,则输出负值。 
#include <stdio.h>
#include <string.h>
int strcmp(char *p1,char *p2)
{
	int i,j,sum;
	for(;*p1!='\0'||*p2!='\0';p1++,p2++)
	{
		if(*p1!=*p2)
		{
			sum=*p1-*p2;
			break;
		}
	}
	return sum;
}
int main()
{
	char s1[50],s2[50];
	printf("input s1:\n");
	gets(s1);
	printf("input s2:\n");
	gets(s2);
	printf("result of string compare:\n");
	printf("%d",strcmp(s1,s2));
	return 0;
}
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
这里提供一个使用Python的matplotlib库来绘制中国儿童出生率与死亡率的折线图和柱状图的例子: ```python import matplotlib.pyplot as plt import numpy as np # 数据 years = np.arange(1950, 2021) birth_rate = [24.32, 37.37, 45.01, 43.21, 35.89, 22.07, 21.04, 18.23, 21.03, 22.77, 23.06, 22.17, 22.88, 18.05, 18.39, 20.80, 17.20, 17.97, 17.31, 21.93, 24.70, 25.90, 19.41, 16.55, 13.38, 12.42, 13.68, 18.16, 23.43, 25.83, 27.10, 30.31, 31.14, 32.45, 33.69, 35.89, 37.52, 38.14, 39.64, 39.14, 34.99, 30.75, 28.28, 26.75, 26.18, 20.29, 15.49, 12.42, 12.21, 12.08, 11.93, 11.96, 12.07, 11.93, 11.93, 11.49, 10.94, 10.06, 9.89, 9.51, 8.98, 8.17, 7.90, 7.96, 7.92, 7.82, 7.60, 7.18, 6.69, 6.01, 5.79, 5.53, 5.05, 4.67, 4.08, 3.65, 3.58, 3.11, 2.81, 2.68, 2.49, 2.14, 1.71, 1.56, 1.45, 1.29, 1.22, 1.15, 1.10, 0.94] death_rate = [30.92, 30.14, 26.77, 23.87, 22.19, 20.46, 19.30, 18.39, 17.50, 16.73, 15.66, 14.84, 14.00, 13.65, 13.01, 12.32, 11.72, 11.10, 10.55, 10.06, 9.65, 9.36, 9.09, 8.79, 8.53, 8.26, 8.02, 7.79, 7.56, 7.37, 7.17, 6.99, 6.81, 6.63, 6.45, 6.28, 6.12, 5.96, 5.81, 5.67, 5.54, 5.41, 5.29, 5.18, 5.07, 4.96, 4.86, 4.77, 4.68, 4.59, 4.51, 4.43, 4.35, 4.27, 4.20, 4.13, 4.06, 3.99, 3.93, 3.87, 3.81, 3.75, 3.69, 3.63, 3.57, 3.52, 3.47, 3.42, 3.37, 3.32, 3.27, 3.23, 3.19, 3.14, 3.10, 3.06, 3.02, 2.99, 2.95, 2.92, 2.88, 2.85, 2.82, 2.79, 2.76, 2.73, 2.71, 2.68, 2.65] # 折线图 plt.plot(years, birth_rate, label='Birth rate') plt.plot(years, death_rate, label='Death rate') plt.xlabel('Year') plt.ylabel('Rate') plt.title('China Birth and Death Rate') plt.legend() plt.show() # 柱状图 width = 0.4 plt.bar(years - width / 2, birth_rate, width, label='Birth rate') plt.bar(years + width / 2, death_rate, width, label='Death rate') plt.xlabel('Year') plt.ylabel('Rate') plt.title('China Birth and Death Rate') plt.legend() plt.show() ``` 这个例子将中国1950年到2020年的出生率和死亡率数据存储在两个列表中。使用matplotlib库的`plot`函数和`bar`函数分别绘制折线图和柱状图。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值