赞
踩
a. East Simpleton的人口
b. DVD影碟的价格
c. 本章出现次数最多的字母
d. 本章出现次数最多的字母次数
答:a.int、short或unsigned short类型。人口是一个整数。
b.float类型。价格通常不是一个整数。
c.char类型。
d.int或unsigned类型。
答:原因之一:在系统中要表示的数超过了 int 可表示的范围,这时要使用 long 类型。
原因之二:如果要处理更大的值,那么使用一种在所有系统上都保证至少是32位的类型,可提高程序的可移植性。
答:如果要正好获得32位的整数,可以使用 int32_t 类型。
如果要获得可储存至少32位整数的最小类型,可以使用 int_least32_t 类型。
如果要为32位整数提供最快的计算速度,可以使用 int_fast32_t类型。
(假设你的系统已定义了上述类型)
a. ‘\b’ char类型常量(但是存储为int类型)
b. 1066 int类型常量
c. 99.44 double类型常量
d. 0XAA unsigned int 类型常量,十六进制格式
e. 2.0e30 double类型常量
include <stdio.h>
main
(
float g; h;
float tax, rate;
g = e21;
tax = rate*g;
)
答:
#includ <stdio.h>
int main(void)
{
float g, h;
float tax, rate;
rate = 0.08; //rate变量应赋值
g = 1.0e5; //在e前面至少有一个数字
tax = rate*g;
h = g + tax;
printf("You owe $%f plus $%f in taxes for a total of $%f.\n", g, tax, h);
return 0;
}
| 常量 | 类型 | 转换说明(%转换字符) |
|–|–|–|–|
| 12 | int | %d |
| 0X3 | unsigned int | %#X |
| ‘C’ | char | %c |
| 2.34E07 | double | %e |
| ‘\ 040’ |char | %c |
| 7.0 | double | %f |
| 6L | long | %ld |
| 6.0f | float | %f |
| 0x5.b6p12 | double| %a |
| 常量 | 类型 | 转换说明(%转换字符) |
|–|–|–|–|
| 012 | unsigned int | %#o |
| 2.9e05L | long double | %Le |
| ‘s’ | char | %c |
| 100000| int | %d |
| ‘\ n’ | char | %c |
| 20.0f | float | %f |
| 0x44 | unsigned int | %x |
| - 40| int | %d |
int imate = 2;
long shot = 53456;
char grade = ‘A’;
float log = 2.71828;
把下面的printf()语句中的转换字符补充完整:
printf(“The odds against the %d were %ld to 1.\n”, imate, shot);
printf(“A score of %f is not an %c grade.\n”, log, grade);
ch = '\r ';
ch = 13;
ch = ‘\015’;
ch = ‘\xd’;
void main(int) / this program is perfect/
{
cows, legs integer;
printf("How many cow legs did you count?\n");
scanf("%c",legs);
cows = legs / 4;
printf("That implies there are %f cows.\n",cows)
}
答:
#include <stdio.h>
int main(void) /* this program is perfect */
{
int cows, legs;
printf("How many cow legs did you count?\n");
scanf("%d", &legs);
cows = legs / 4;
printf("That implies there are %d cows.\n",cows);
return 0;
}
a. \n 换行字符
b. \ \ 反斜杠字符
c. \ " 双引号字符
d. \ t 制表字符
#include <stdio.h>
int main(void)
{
int a = 2147483647;
float b = 3.4E38 * 100.0f;
float c = 0.1234E-10/10E100;
printf("整数上溢:%d\n", a+1);
printf("浮点数上溢:%e\n", b);
printf("浮点数下溢:%e\n", c);
return 0;
}
运行结果:
程序分析:
整数数值小于最小值为下溢,大于最大值为上溢。
浮点数绝对值小于浮点数所能表示的最小值,为下溢,当作 0;绝对值大于浮点数所能表示的最大范围,为上溢,当作 INF。
可参考书P49和P58
#include <stdio.h>
int main(void)
{
int a;
printf("输入一个ASCII码值:");
scanf("%d",&a);
printf("ASCII码值对应字符:%c", a);
return 0;
}
运行结果:
Startled by the sudden sound,Sally shouted,
"By the Great Pumpkin,what was that ! "
#include <stdio.h>
int main(void)
{
printf("\aStartled by the sudden sound,Sally shouted,\n");
printf("\"By the Great Pumpkin,what was that ! \"");
return 0;
}
运行结果:
Enter a floating-point value: 64.25
fixed-point notation: 64.250000
exponential notation: 6.425000e+01
p notation: 0x1.01p+6
#include <stdio.h>
int main(void)
{
float i;
printf("Enter a floating-point value:");
scanf("%f", &i);
printf("fixed-point notation:%f\n", i);
printf("exponential notation:%e\n", i);
printf("p notation:%a\n", i);
return 0;
}
运行结果:
#include <stdio.h>
int main(void)
{
int age;
float minutes;
printf("提示用户输入年龄:");
scanf("%d", &age);
minutes = age * 3.156E7;
printf("该年龄对应的秒数: %f",minutes);
return 0;
}
运行结果:
#include <stdio.h>
int main(void)
{
float quarts;
float water_member;
printf("输入水的夸脱数: ");
scanf("%f", &quart);
water_member = quart*950/3.0E-23;
printf("水分子的数量: %e", water_member);
return 0;
}
运行结果:
#include <stdio.h>
int main(void)
{
float height;
float cm;
printf("输入身高(英寸):");
scanf("%f", &height);
cm = height * 2.54;
printf("以厘米为单位显示身高:%f",cm);
return 0;
}
运行结果:
#include <stdio.h> int main(void) { float cups, pint, ounce, soup_ladle, sppon; printf("输入杯数: "); scanf("%f", &cups); pint = cups / 2; printf("品脱:%f\n", pint); ounce = cups * 8; printf("盎司:%f\n", ounce); soup_ladle = cups * 8 * 2; printf("汤勺:%f\n", soup_ladle); sppon = cups * 8 * 2 * 3; printf("茶勺:%f\n", sppon); return 0; }
运行结果:
答:如果品脱设置为整数类型, 用户输入的杯数不是2的倍数的话,会导致品脱输出结果小数位的数字被舍去,结果不够精确。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。