自己练习时手写,难免会有些疏忽遗漏等各种各样问题,错误之处还请指出
但这些代码确实已通过编译,实现了书上的输出结果,还希望能给抱有期待之人作为个小参考
7.1 这个直接粘上原答案吧!
7.2
#include <stdio.h>
int main (void)
{
int i, n;
printf ("This program prints a table of squares. \n");
printf ("Enter number of entries in table: ");
scanf ("%d", &n);
getchar (); //接收scanf留下的回车
for (i = 1; i <= n; i++) {
printf ("%10d%10d\n", i, i * i);
if (i % 24 == 0) {
printf ("Press Enter to continue...");
getchar ();
}
}
return 0;
}
7.3
#include <stdio.h>
int main (void)
{
double n, sum = 0.0;
printf ("This program sums a series of integers.\n");
printf ("Enter inetgers (0 to terminate): ");
scanf ("%lf", &n); //这里scanf是用的%lf, printf是用的%f
while (n != 0.0) {
sum += n;
scanf ("%lf", &n);
}
printf ("The sum is:%f\n", sum);
return 0;
}
7.4
#include <stdio.h>
int main (void)
{
int ch;
printf ("Enter phone number: ");
while ((ch = getchar ()) != '\n') {
if (ch <= 'Z' && ch >= 'A') {
switch (ch) {
case 65: case 66: case 67:
printf ("2");
break;
case 68: case 69: case 70:
printf ("3");
break;
case 71: case 72: case 73:
printf ("4");
break;
case 74: case 75: case 76:
printf ("5");
break;
case 77: case 78: case 79:
printf ("6");
break;
case 81: case 82: case 83: case 80:
printf ("7");
break;
case 84: case 85: case 86: case 87:
printf ("8");
break;
case 88: case 89: case 90:
printf ("9");
break;
}
continue;
}
printf