C语言关于数据类型的编程练习


1.编写一个程序,要求提示输入一个ASCII码值(如66),然后打印输入的字符。

2.编写一个程序,发出一声警报,然后打印下面的文本:

Startled by the sudden sound, Sally shouted,

"By the Great Pumpkin, what was that!"


3.编写一个程序,读取一个浮点数,先打印成小数点形式,再打印成指数形式。然后,如果系统支持,再打印成p记数法(即十六进制记数法)。按以下格式输出(实际显示的指数位数因系统而异):Enter a floating-point value: 64.25
fixed-point notation: 64.250000
exponential notation: 6.425000e+01
p notation: 0x1.01p+6


4.一年大约有3.156×10⁷秒。编写一个程序,提示用户输入年龄,然后显示该年龄对应的秒数。

5.1个水分子的质量约为3.0×10ˉ²³克。1夸脱水大约是950克。编写一个程序,提示用户输入水的夸
脱数,并显示水分子的数量。

6.1英寸相当于2.54厘米。编写一个程序,提示用户输入身高(/英寸),然后以厘米为单位显示身高。

7.在美国的体积测量系统中,1品脱等于2杯,1杯等于8盎司,1盎司等于2大汤勺,1大汤勺等于3茶勺。编写一个程序,提示用户输入杯数,并以品脱、盎司、汤勺、茶勺为单位显示等价容量。思考对于该程序,为何使用浮点类型比整数类型更合适?
 

参考代码

1.

#include <stdio.h>
int main(void)
{
    int input;
    printf("请输入一个ASCII码值: ");
    scanf_s("%d", &input);
    printf("该ASCII码值对应的字符是%c\n", input);
    return 0;
}

2.

#include <stdio.h>
int main(void)
{
	char ch = '\a';
	printf("%c", ch);
	printf("Startled by the sudden sound,Sally shouted,\n");
	printf("\"By the Great Pumpkin, what was that!\"\n");
	return 0;
}

3.

#include <stdio.h>
int main(void)
{
	float input;
	printf("Enter a floating-point value:");
	scanf_s("%f", &input);
	printf("fixed-point notation: %f\n",input);
	printf("exponentinal notation: %e\n",input);
	printf("p notation: %a\n", input);	//打印p记数法形式,转换说明符使用 %a
	return 0;
}

4.

#include <stdio.h>
#define year_s 3.156e7	//预编译指令定义每年的秒数
int main(void)
{
	float s,age;
	printf("请输入你的年龄:");
	scanf_s("%f", &age);
	s = age * year_s;
	printf("你的年龄是%f岁,已经度过了%f秒",age,s);
	return 0;
}

5.

#include <stdio.h>
#define water_weight 3.0e-23
#define water_quart 950
int main(void)
{
	float quart,num;
	printf("请输入水的夸脱:");
	scanf_s("%f", &quart);
	num = quart * water_quart / water_weight;	//	计算水分子数量
	printf("这里有%f夸脱的水,这些水有%e的数量的水分子",quart,num);
	return 0;
}

6.

#include <stdio.h>
#define yc 2.54
int main(void)
{
	float high,num;
	printf("请输入你的身高:");
	scanf_s("%f", &high);
	num = high * yc;
	printf("你的身高是%.2f,大概%.2f英寸.\n",high,num);
	return 0;
}

7.

#include <stdio.h>
#define pt_b 2	//1品脱=2杯
#define b_as 8	//1杯等于8盎司
#define as_ts 2	//1盎司等于2大汤勺
#define ts_cs 3	//1大汤勺等于3茶勺
int main(void)
{
	float b,pt,as,ts,cs;
	printf("请输入杯数:");
	scanf_s("%f", &b);
	pt = b / pt_b;
	as = b * b_as;
	ts = as * as_ts;
	cs = ts * ts_cs;
	printf("%.1f杯等于%.1f品脱等于%.1f盎司等于%.1f汤勺等于%.1f茶勺\n",b,pt,as,ts,cs);
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值