C primer plus第4章(字符串和格式化输入/输出)习题

时间:2021-06-28 00:14:42

2

a: printf("%2.2f",2.345e2);打印结果234.50

b: printf("%c,%c,%c");打印结果H,i,!

c. #define Q "His Hamlet was funny without being vulgar."

printf("%s,%d");打印结果His Hamlet was funny without being vulgar. , 42

d. printf("%2.2e,%2.2f",1201.0,1201.0)打印结果 1.201e+003, 1201.00


4.

#include <stdio.h>
#define B "booboo"
#define X 10
int main(void)
{
int age,xp;
char name[50];
printf("Please enter your first name.\n");
scanf("%s",name);
printf("All right,%s.what`s your age?\n",name);
scanf("%d",&age);
xp = age + X;
printf("That`s a %s! You must be at least %d.\n",B,xp);
return 0;
}

5

#include <stdio.h>
#define BOOK "War and Peace"
int main (void)
{
float cost = 12.99;
float percent = 80.0;
printf("This copy of \"%s\" sells for $%.2f.\n",BOOK,cost);
printf("That is %.0f%% of list.",percent);
return 0;
}

6.

a.字段宽度等于数字位数的十进制整数 %d

b.形如8A、字段宽度为4的十六进制整数 %4x

c.形如232.346、字段宽度为10的浮点数 %10.3f

d.一个形如2.33e+002、字段宽度为12的浮点数 %12.2e

e.一个字段宽度为30、左对齐的字符串 %-30s

7.

a.一个字段宽度为15的unsigned long 整数 %15lu

b.一个形如0x8a/阻断宽度为4的十六进制整数 %#4x

c.一个形如2.33E+02、字段宽度为12、左对齐的浮点数 %-12.2e

d.一个形如+232.346、字段宽度为10的浮点数 %+10.3f

e.一个字符串的前8个字符,字段宽度为8字符 %8.8s

8.

a.一个字段宽度为6、最少有4位数字的十进制整数 %6.4d

b.一个字段宽度在参数列表中给定的八进制整数 %*o

c.一个字段宽度为2的字符 %2c

d.一个形如+3.13、字段宽度等于数字中字符个数的浮点数 %+0.2f

e.一个字符串的前5个字符,字段宽度为7、左对齐 %-7.5s


9.为下列每个输入行提供一个对其进行读取的scanf语句

a.101 scanf("%d",&num)

b.22.32 8.34e-09scanf("%f%f",&a,&b)

c.linguini scanf("%s",s)

d.catch 22 char a int bscanf("%s %d",a,&b)

e.catch 22(跳过catch) char ascanf("%s %d",a,&b)


1.

#include <stdio.h>
int main (void)
{
char firstName[20],lastName[20];
printf("请输入名字:\n");
scanf("%s",lastName);
printf("请输入姓氏:\n");
scanf("%s",firstName);
printf("%s,%s",lastName,firstName);
}

2.

#include <stdio.h>
#include <string.h>
int main (void)
{
char name[20];
printf("请输入名字:\n");
scanf("%s",name);
printf("\"%s\"\n",name);
printf("\"%20s\"\n",name);
printf("\"%-20s\"\n",name);
printf("\"%*s\"",strlen(name)+3,name);
}

3.

#include <stdio.h>
int main (void)
{
float num;
scanf("%f",&num);
printf("The input is %.1f or %.1e\n",num,num);
printf("The input is %+6.3f or %.3e",num,num);
}

4.

#include <stdio.h>
int main (void)
{
float num;
printf("请输入你的身高:\n");
scanf("%f",&num);
printf("Dabney, you are %.3f feet tall",num);
}
5.

#include <stdio.h>
#include <string.h>
int main (void)
{
char firstName[20],lastName[20];
int fl,ll;
printf("请输入名字:\n");
scanf("%s",lastName);
printf("请输入姓氏:\n");
scanf("%s",firstName);
ll = strlen(lastName);
fl = strlen(firstName);
printf("%s %s\n",lastName,firstName);
printf("%*d %*d\n",ll,ll,fl,fl);
printf("%s %s\n",lastName,firstName);
printf("%-*d %-*d",ll,ll,fl,fl);
}

6.

#include <stdio.h>
#include <float.h>
int main (void)
{
double d = 1.0/3.0;
float f = 1.0/3.0;
printf("%.4f,%.4f\n",d,f);
printf("%.12f,%.12f\n",d,f);
printf("%.16f.%.16f\n",d,f);
printf("%f,%f",FLT_DIG,DBL_DIG);
}

7.

#include <stdio.h>
#include <float.h>
#define ITK 1.609
int main (void)
{
const GTS = 3.785;
double i;
double g;
printf("请输入行驶英里数\n");
scanf("%f",&i);
printf("请输入消耗汽油加仑数\n");
scanf("%f",&g);
printf("%f",(i*ITK)/(g*GTS)*100);
}