字符串和格式化输入/输出 编程练习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串和格式化输入/输出 编程练习相关的知识,希望对你有一定的参考价值。
1、
#include <stdio.h> int main(void) { char first_name[40]; char last_name[40]; printf("请输入您的名字: "); scanf("%s", last_name); printf("请输入您的姓氏: "); scanf("%s", first_name); printf("%s, %s", last_name, first_name); return 0; }
2、
#include <stdio.h> #include <string.h> int main(void) { char name[40]; printf("请输入您的名字: \n"); scanf("%s", name); printf("\"%s\"\n", name); printf("\"%20s\"\n", name); printf("\"%-20s\"\n", name); printf("\"%*s\"\n", strlen(name) + 3, name); return 0; }
3、
#include <stdio.h> int main(void) { float number; printf("请输入一个浮点数: \n"); scanf("%f", &number); printf("The input is %.1f or %.1e\n", number, number); printf("The input is %+.3f or %.3e\n", number, number); return 0; }
4、
#include <stdio.h> int main(void) { float feet; char name[40]; printf("请输入您的身高: \n"); scanf("%f", &feet); printf("请输入您的名字: \n"); scanf("%s", name); printf("%s, you are %.3f m tall.\n", name, feet / 100); return 0; }
5、
#include <stdio.h> #include <string.h> int main(void) { char lname[30]; char fname[30]; printf("请输入名字: \n"); scanf("%s", lname); printf("请输入姓氏: \n"); scanf("%s", fname); printf("%10s %10s\n", lname, fname); printf("%10d %10d\n", strlen(lname), strlen(fname)); printf("%-10s %-10s\n", lname, fname); printf("%-10d %-10d\n", strlen(lname), strlen(fname)); return 0; }
6、
#include <stdio.h> #include <float.h> int main(void) { double n1 = 1.0 / 3.0; float n2 = 1.0 / 3.0; printf("n1 = %.4f\n", n1); printf("n1 = %.12f\n", n1); printf("n1 = %.16f\n", n1); printf("n2 = %.4f\n", n2); printf("n2 = %.12f\n", n2); printf("n2 = %.16f\n", n2); printf("FLT_DIG = %d, DBL_DIG = %d\n", FLT_DIG, DBL_DIG); return 0; }
7、
#include <stdio.h> #define CAST1 3.785 #define CAST2 1.609 int main(void) { float miles; float gallons; printf("请输入行驶的英里数: \n"); scanf("%f", &miles); printf("请输入消耗汽油的加仑数: \n"); scanf("%f", &gallons); printf("消耗每加仑汽油行驶的英里数: %.1f\n", miles / gallons); printf("每100公里的升数: %.1f\n", gallons * CAST1 / (miles * CAST2 / 100)); return 0; }
以上是关于字符串和格式化输入/输出 编程练习的主要内容,如果未能解决你的问题,请参考以下文章