1.如果 i*i 超出了int类型的最大取值范围,那么6.3节的程序 square2.c 将失败(通常会显示奇怪的答案)。运行该程序,并确定导致失败的 n 的最小值。尝试把变量的类型改成 short 并再次运行该程序。(不要忘记更新 printf 函数调用中的转换说明!)然后尝试改成long。从这些实验中,你能总结出在你的机器上用于存储整数类型的位数是多少吗?
习题解析
6.3节中的程序square2.c计算变量的平方,数据打印过程中,如果 i 的平方超过 int 类型的最大取值范围就会发生溢出,这时通常显示为一个负数。例如能够正确计算平方的数值是 46340,46341的平方将超过32位 int 类型的最大值2 147 483 647。如果修改为 short int , 则能够计算最大平方的数为181,即182的平方就会超过16位短整型的最大数32767。如果修改为1ong类型,则能计算最大平方的数为 46 340,结果和int类型一致。即整数类型32位,long 类型和 int 类型一致。
参考答案
在Windows操作系统下,使用GCC编译器,int 是32位,long int 也是32位。
\\ square3.c
#include<stdio.h>
int main()
{
int n;
printf("This program prints an table of squares.\n");
printf("Enter number of enries in table: ");
scanf("%d",&n);
for(int i=1,odd=3,square=1;i<=n;i++,square+=odd,odd+=2)
{
printf("%10d%10d\n",i,square);
}
return 0;
}
2.修改6.3节的程序 square2.c , 每24次平方后暂停并显示下列信息:
Press Enter to continue...
显示完上述消息后,程序应该使用getchar 函数读入一个字符。getchar 函数读到用户录入的
回车键才允许程序继续。
#include<stdio.h>
int main()
{
int n;
printf("This program prints an table of squares.\n");
printf("Enter number of enries in table: ");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
printf("%10d%10d\n",i,i*i);
if(i%24==0)
{
printf("Press Enter to continue...");
while(getchar()!='\n');
}
}
return 0;
}
3.修改7.1节的程序 sum2.c,对double型值组成的数列求和。
#include<stdio.h>
int main()
{
double lf, sum =0;
printf("This program sums a series of integers. \n");
printf("Enter integers (0 to terminate): ");
scanf("%lf",&lf);
while(lf!=0){
sum += lf;
scanf("%lf",&lf);
}
printf("The sum is : %lf\n",sum);
return 0;
}
4.编写程序把字母格式的电话号码翻译成数值格式:
Enter phone number: CALLATT
2255288
(如果没有电话在身边,参考这里给出的字母在键盘上的对应关系:2=ABC,3=DEE,4=GHI,5=JKL,6=MNO,7=PQRS,8=TUV,9=WXYZ。)原始电话号码中的非字母字符(例如数字或标点符号)保持不变:
Enter phone number: 1-800-COL-LECT
1-800-265-5328
可以假设任何用户输入的字母都是大写字母。
#include<stdio.h>
int main()
{
char c;
printf("Enter phone number: ");
while((c=getchar()) !='\n'){
if(c>='A' && c<='C') printf("%d",2);
else if(c>='D' && c<='F') printf("%d",3);
else if(c>='G' && c<='I') printf("%d",4);
else if(c>='J' && c<='L') printf("%d",5);
else if(c>='M' && c<='O') printf("%d",6);
else if(c>='Q' && c<='S') printf("%d",7);
else if(c>='T' && c<='V') printf("%d",8);
else if(c>='W' && c<='Z') printf("%d",9);
else printf("%c",c);
}
return 0;
}
5.在十字拼字游戏中,玩家利用小卡片组成单词,每个卡片包含字母和面值。面值根据字母稀缺
程度的不同而不同。(面值有:1AEILNORSTU,2一DG,3一BCMP,4一FHVWY,5一K,8一JX,10一QZ。)编写程序通过对单词中字母的面值求和来计算单词的值:
Enter a word: pitfall
Scrabble value: 12
编写的程序应该允许单词中混合出现大小写字母。提示:使用toupper库函数。
#include<stdio.h>
#include<ctype.h>
int main()
{
char c;
int a=0;
printf("Enter a word: ");
while( ( c=getchar() ) != '\n' ){
c=toupper(c);
if(c=='D' || c=='G') a+=2;
else if(c=='B' || c=='C' || c=='M' || c=='P') a+=3;
else if(c=='F' || c=='H' || c=='V' || c=='W' || c=='Y') a+=4;
else if(c=='K') a+=5;
else if(c=='J' || c=='X') a+=8;
else if(c=='Q' || c=='Z') a+=10;
else a+=1;
}
printf("Scrabble value: %d",a);
return 0;
}
6.编写程序显示 sizeof(int) 、sizeof(short) 、sizeof(long) 、sizeof(float) 、sizeof(double) 和 sizeof(long double)的值。
#include<stdio.h>
int main()
{
printf("Size of int: %zu\n",sizeof(int));
printf("Size of short: %zu\n",sizeof(short));
printf("Size of long: %zu\n",sizeof(long));
printf("Size of float: %zu\n",sizeof(float));
printf("Size of double: %zu\n",sizeof(double));
printf("Size of long double: %zu\n",sizeof(long double));
return 0;
}
7.修改第3章的编程题6,使得用户可以对两个分数进行加、减、乘、除运算(在两个分数之间输
入+、-、*或 / 符号)。
#include<stdio.h>
int main()
{
int num1,num2,result_num,demond1,demond2,result_demond;
char ch;
printf("Enter two fractions separated by a plus sign:");
scanf(" %d/%d%c%d/%d",&num1,&demond1,&ch,&num2,&demond2);
if(ch=='+'){
result_num=num1*demond2+num2*demond1;
result_demond=demond1*demond2;
printf("The sum is %d/%d",result_num,result_demond);
}
else if(ch=='-'){
result_num=num1*demond2-num2*demond1;
result_demond=demond1*demond2;
printf("The sum is %d/%d",result_num,result_demond);
}
else if(ch=='*'){
result_num=num1*num2;
result_demond=demond1*demond2;
printf("The sum is %d/%d",result_num,result_demond);
}
else if(ch=='-'){
result_num=num1*demond2;
result_demond=demond1*num2;
printf("The sum is %d/%d",result_num,result_demond);
}
return 0;
}
8.修改第5章的编程题8,要求用户输入12小时制的时间。输入时间的格式为 时:分 后跟A、P或AM、PM(大小写均可)。数值时间和 AM/PM 之间允许有空白(但不强制要求有空白)。有效输入的示例如下:
1:15P
1:15PM
1:15p
1:15pm
1:15 P
1:15 PM
1:15 p
1:15 pm
可以假定输入的格式就是上述之一,不需要进行错误判定。
#include<stdio.h>
#include<ctype.h>
int main()
{
int hour=0,min=0,time=0;
char ch;
printf("Enter a 12-hour time: ");
scanf("%d:%d %c",&hour,&min,&ch);
ch=toupper(ch);
if(ch=='A')time=hour*60+min;
else time=12*60+hour*60+min;
if( time < (480+583)/2)
printf("Closest departure time is 8:00 a.m., arriving at 10:06 a.m.");
else if (time < (583+679)/2)
printf("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
else if( time < (679+767)/2)
printf("Closest departure time is 11:19 a.m., arriving at 1:31 p.m.");
else if( time < (767+840)/2)
printf("Closest departure time is 12:47 a.m., arriving at 3:00 p.m.");
else if( time < (840+945)/2)
printf("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
else if( time < (945+1140)/2)
printf("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
else if( time < (1140+1305)/2)
printf("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
else if( time < (135+480)/2)
printf("Closest departure time is 9:45 p.m., arriving at 11:58 p.m.");
else
printf("Closest departure time is 9:45 p.m., arriving at 11:58 p.m.");
return 0;
}
9.编写程序要求用户输入12小时制的时间,然后用24小时制显示该时间:
Enter a 12-hour time: 9:11 PM
Equivalent 24-hour time: 21:11
参考编程题8中关于输入格式的描述。
#include<stdio.h>
#include<ctype.h>
int main()
{
int hour,min;
char ch;
printf("Enter a 12-hour time: ");
scanf("%d:%d %c",&hour,&min,&ch);
ch=toupper(ch);
if(ch=='P')hour=hour+12;
printf("Equivalent 24-hour time: %d:%d",hour,min);
return 0;
}
10.编写程序统计句子中元音字母(a、e、i、o、u)的个数:
Enter a sentence: And that's the way it is.
Your sentence contains 6 vowels.
#include<stdio.h>
#include<ctype.h>
int main()
{
int i=0;
char ch;
printf("Enter a sentence: ");
while( ( ch=getchar() )!='\n')
{
ch=toupper(ch);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')i++;
}
printf("Your sentence contains %d vowels.",i);
return 0;
}
11.编写一个程序,根据用户输入的英文名和姓先显示姓氏,其后跟一个逗号,然后显示名的首字母,最后加一个点:
Enter a first and last name: Llovd Fosdick
Fosdick, L.
用户的输入中可能包含空格(名之前、名和姓之间、姓氏之后)。
#include<stdio.h>
int main()
{
char a,b,c;
printf("Enter a first and last name: ");
while( ( a=getchar() ) == ' ' ) ;
while( ( b=getchar() ) != ' ' ) ;
while( ( c=getchar() ) !='\n')
{
if(c!=' ')putchar(c);
}
printf(", %c.",a);
return 0;
}
12.编写程序对表达式求值:
Enter an expression: 1+2.5*3
Value of expression: 10.5
表达式中的操作数是浮点数,运算符是+、-、*和/。表达式从左向右求值(所有运算符的优先级都一样)。
#include<stdio.h>
int main()
{
char ch;
float a=0,b=0;
printf("Enter an expression: ");
scanf("%f",&a);
while( ( ch=getchar() )!='\n')
{
scanf("%f",&b);
switch(ch)
{
case '+':a=a+b;break;
case '-':a=a-b;break;
case '*':a=a*b;break;
case '/':a=a/b;break;
deault:break;
}
}
printf("Value of expression %f",a);
return 0;
}
13.编写程序计算句子的平均词长:
Enter a sentence: It was deia vu all over again.
Average word length: 3.4
为简单起见,程序中把标点符号看作其前面单词的一部分。平均词长显示一个小数位。
#include<stdio.h>
int main()
{
char c;
float a=0,b=0;
printf("Enter a sentence: ");
while( ( c=getchar() ) != '\n')
{
if(c==' ')a++;
else b++;
}
printf("Average word length: %.1f",b/(a+1));
return 0;
}
14.编写程序,用牛顿方法计算正浮点数的平方根:
Enter a positive number: 3
Square root: 1.73205
设x是用户输人的数。牛顿方法需要先给出 x 平方根的猜测值 y (我们使用 1)。后续的猜测值通
过计算 y 和 x/y 的平均值得到。下表给出了求解 3 的平方根的过程。
注意,y 的值逐渐接近 x 的平方根。为了获得更高的精度,程序中应使用 double 类型的变量代替 float 类型的变量。当 y 的新旧值之差的绝对值小于0.000 01和 y 的乘积时程序终止。提示:调用fabs 函数求 double 类型数值的绝对值。(为了使用 fabs 函数,需要在程序的开头包含 <math.h>头。)
#include<stdio.h>
#include<math.h>
int main(void)
{
double x, newy, y=1.0;
printf("Enter a positive number: ");
scanf("%lf",&x);
do{
newy = (y + x/y)/2;
if( fabs(newy-y) < 0.00001*y ) break;
else y = newy;
}while(1);
printf("Square root: %f\n",y);
return 0;
}
15.编程计算正整数的阶乘:
Enter a positive integer: 6
Factorial of 6: 720
(a)用 short 类型变量存储阶乘的值。为了正确打印出n的阶乘,n的最大值是多少?
(b)用 int 类型变量重复(a)。
(c)用 long 类型变量重复(a)。
(d)如果你的编译器支持 long long 类型,用 long long 类型变量重复(a)。
(e)用 float 类型变量重复(a)。
(f )用 double 类型变量重复(a)。
(g)用 long double 类型变量重复(a)。
在(e)~(g)几种情况下,程序会显示阶乘的近似值,不一定是准确值。
#include<stdio.h>
int main()
{
int i=0,n=1;
printf("Enter a positive integer: ");
scanf("%d",&i);
for(int a=1;a<=i;a++)n*=a;
printf("Factorial of %d: %d",i,n);
return 0;
}