C Primer Plus chap 4 编程练习

这些C语言代码示例展示了如何从用户获取输入,包括姓名、浮点数等,并进行格式化输出。程序涵盖了基本的字符串处理、浮点数格式控制以及单位转换。此外,还涉及到了精度控制和内存占用的相关概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

test1

#include <stdio.h>
int main(void)
{
	char c_fist_name[20],c_last_name[20];
	printf("please enter your first name:");
	scanf("%19s",c_fist_name);
	printf("please enter your last name:");
	scanf("%19s",c_last_name);
	printf("your name is:%s,%s",c_fist_name,c_last_name);
	return 0;
}

test2

#include <stdio.h>
#include <string.h>
int main(void)
{
	char c_first_name[20], c_last_name[20];
	printf("please enter your first name:");
	scanf("%19s", c_first_name);
	printf("please enter your last name:");
	scanf("%19s", c_last_name);
	printf("your name is:\"%s,%s\"\n", c_first_name, c_last_name);
	printf("your name is:\"%10s,%10s\"\n", c_first_name, c_last_name);
	printf("your name is:\"%-10s,%-10s\"\n", c_first_name, c_last_name);
	printf("your name is:\"%*s,%s\"\n", strlen(c_first_name) + 3, c_first_name, c_last_name);//%*s在printf中使用, 表示用后面的形参替代的位置,实现动态格式输出

	return 0;
}

test3

#include <stdio.h>
int main(void)
{
	float f_a;
	printf("please enter a float number:");
	scanf("%f", &f_a);
	printf("a.输入%.1f或%e\n",f_a,f_a);
	printf("b.输入%+.3f或%E\n",f_a,f_a);

	return 0;
}

test4

#include <stdio.h>

int main(void)
{
	char c_name[20];
	float f_height;
	printf("please enter your name:");
	scanf("%19s", c_name);
	printf("please enter your height(cm):");
	scanf("%f", &f_height);
	printf("%s,you are %.2fm tall ", c_name, f_height/100);

	return 0;
}

test5

#include <stdio.h>

int main(void)
{
	float f_megabits, f_megabytes;
	printf("please enter megabits per second:");
	scanf("%f", &f_megabits);
	printf("please enter your file's megabytes:");
	scanf("%f", &f_megabytes);
	printf("At %.2f megabits per second,a file of %.2f megabytes\ndownloads in %.2f seconds.",
		f_megabits, f_megabytes, f_megabytes*8/ f_megabits);

	return 0;
}

test6

#include <stdio.h>
#include <string.h>
int main(void)
{
	char c_first_name[20], c_last_name[20];
	printf("please enter your first name:");
	scanf("%19s", c_first_name);
	printf("please enter your last name:");
	scanf("%19s", c_last_name);
	printf("your name is:\"%s %s\"\n", c_first_name, c_last_name);
	printf("your name is:\"%*d %*d\"\n", 
		strlen(c_first_name), strlen(c_first_name), strlen(c_last_name),strlen(c_last_name));
	printf("your name is:\"%s %s\"\n", c_first_name, c_last_name);
	printf("your name is:\"%-*d %-*d\"\n",
		strlen(c_first_name), strlen(c_first_name), strlen(c_last_name),strlen(c_last_name));
	
	return 0;
}

test7

#include <stdio.h>
#include <float.h>

int main(void)
{
	double d_a = 1.0/3.0;
	float f_a = 1.0/3.0;
	printf("d_a:%.6f,f_a:%.6f\n", d_a, f_a);
	printf("d_a:%.12f,f_a:%.12f\n", d_a, f_a);
	printf("d_a:%.16f,f_a:%.16f\n", d_a, f_a);
	printf("FLT_DIG:%d,DBL_DIG:%d",FLT_DIG,DBL_DIG);
	return 0;
}

test8

#include <stdio.h>
#define S 3.785/1.609

int main(void)
{
	float f_m, f_y;
	printf("请分别输入旅行的里程数和消耗的汽油量(加仑)\n");
	scanf("%f",&f_m);
	scanf("%f",&f_y);
	float f_a = (f_m) / (f_y);
	printf("消耗每加仑汽油行驶:%.1f\n", f_a);
	printf("转换为升/100公里为:%.1f",0.01*(S/f_a));

	return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值