代码仅供参考,愿大家多多思考
7-4:
翻译(TRANSLATION) 编写程序可以把字⺟格式的电话号码翻译成数值格式: ENTER PHONE NUMBER: CALLATT 2255288
如果没有电话在身边,参考这⾥给出的字⺟在键盘上的对应关系:(2=ABC,3=DEF,4=GHI,5=JKL,6=MNO,7=PQRS,8=TUV,9=WXYZ)原始电话号码中的⾮字⺟字符(例如数字或标点符号)保持不变:
Enter phone number: 1-800-COL-LECT1-800-265-5328
可以假设任何⽤户输⼊的字⺟都是⼤写字⺟。
输出范例:
Enter phone number: 1-DCKS-A2D-OLED
1-3257-223-6533
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
printf(" Enter phone number:");
while ((ch = getchar()) != '\n') {
if (ch == 'A' || ch == 'B' || ch == 'C')
printf("2");
else if (ch == 'D' || ch == 'E' || ch == 'F')
printf("3");
else if (ch == 'G' || ch == 'H' || ch == 'I')
printf("4");
else if (ch == 'J' || ch == 'K' || ch == 'L')
printf("5");
else if (ch == 'M' || ch == 'N' || ch == 'O')
printf("6");
else if (ch == 'P' || ch == 'Q' || ch == 'R' || ch == 'S')
printf("7");
else if (ch == 'T' || ch == 'U' || ch == 'V')
printf("8");
else if (ch == 'W' || ch == 'X' || ch == 'Y' || ch == 'Z')
printf("9");
else
printf("%c", ch);
}
return 0;
}
7-12:
表达式求值(EXPRESSION EVALUATION) 编写程序对表达式求值。
Enter an expression: 1+2.53
Value of expression: 10.5
表达式中的操作数是浮点数,运算符是+、-、和/。表达式从左向右求值(所有运算符的优先级都⼀样)。
输出范例:
Enter an expression: 2+3*4-5/2
Value of expression: 7.5
#include <stdio.h>
int main()
{
double a, d;
char ch;
printf("Enter an expression: ");
scanf("%lf", &a);
while ((ch = getchar()) != '\n') {
scanf("%lf", &d);
switch (ch) {
case '+':
a = a + d;
break;
case '-':
a = a - d;
break;
case '*':
a = a * d;
break;
case '/':
a = a / d;
break;
}
}
printf("Value of expression: %f", a);
}
8-2:
出现次数(NUMBER OCCURRENCES) 修改如下程序(8.1 节的 REPDIGIT.C),使其打印⼀份列表,显示出每个数字在数中出现的次数。
输出范例:
Enter a number: 41271092
Digit: | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|---|
Occurrences: | 1 | 2 | 2 | 0 | 1 | 0 | 0 | 1 | 0 | 1 |
#include <stdbool.h>
#include <stdio.h>
int main()
{
int digit_seen[10] = { 0 };
int digit, x;
long n;
x = 0;
printf("enter a number:");
scanf("%ld", &n);
printf(" 0 1 2 3 4 5 6 7 8 9\n");
if (n == 0)
printf(" 1");
while (n > 0) {
digit = n % 10;
digit_seen[digit] += 1;
n /= 10;
}
while (x <= 9) {
printf("%2d", digit_seen[x]);
x++;
}
return 0;
}
8-9:
随机步法(RANDOM WALK) 编写程序,⽣成⼀种贯穿10×10字符数组(初始时全为字符'.')的“随机步法”。程序必须随机地从⼀个元素“⾛到”另⼀个元素,每次都向上、向下、向左或向右移动⼀个元素位置。已访问过的元素按访问顺序⽤字⺟A到Z进⾏标记。
下⾯是⼀个输出示例:
A | . | . | . | . | . | . | . | . | . |
---|---|---|---|---|---|---|---|---|---|
B | C | D | . | . | . | . | . | . | . |
. | F | E | . | . | . | . | . | . | . |
H | G | . | . | . | . | . | . | . | . |
I | . | . | . | . | . | . | . | . | . |
J | . | . | . | . | . | . | . | Z | . |
K | . | . | R | S | T | U | V | Y | . |
L | M | P | Q | . | . | . | W | X | . |
. | N | O | . | . | . | . | . | . | . |
利⽤srand函数和rand函数(⻅程序deal.c)产⽣随机数,然后查看次数除以4的余数。余数⼀共有4种可能的值(0、1、2和3),指示下⼀次移动的4种可能⽅向。在执⾏移动之前,需要检查两项内容:⼀是不能⾛到数组外⾯,⼆是不能⾛到已有字⺟标记的位置。只要⼀个条件不满⾜,就得尝试换⼀个⽅向移动。如果4个⽅向都堵住了,程序就必须终⽌了。下⾯是提前结束的⼀个示例:
A | B | G | H | I | . | . | . | . | . |
---|---|---|---|---|---|---|---|---|---|
. | C | F | . | J | K | . | . | . | . |
. | D | E | . | M | L | . | . | . | . |
. | . | W | X | Y | P | Q | . | . | . |
. | . | V | U | T | S | R | . | . | . |
. | . | . | . | . | . | . | . | . | . |
. | . | . | . | . | . | . | . | . | . |
. | . | . | . | . | . | . | . | . | . |
. | . | . | . | . | . | . | . | . | . |
因为Y的4个⽅向都堵住了,所以没有地⽅可以放置下⼀步的Z了。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10
#define S 25
int main()
{
char f[N][N], ch = 'A';
srand((unsigned)time(NULL));
int i = 0, x, y = 0, z, n = 0, m = 0;
//初始化数组
int h, j;
for (h = 0; h < N; h++) {
for (j = 0; j < 10; j++) {
f[h][j] = '.';
}
}
f[0][0] = ch;
i = 0;
while (i < S) {
x = n; //还原
y = m;
z = rand() % 4;
switch (z) {
case 0:
x += 1;
break;
case 1:
y += 1;
break;
case 2:
x -= 1;
break;
case 3:
y -= 1;
break;
default:
break;
}
if (x < 0 || x > 9 || y < 0 || y > 9) //检验越界
continue;
if (f[x][y] != '.') //检验占位
continue;
n = x;
m = y;
ch++;
f[n][m] = ch;
if (n > 0 && m > 0) {
if (f[n - 1][m] != '.' && f[n + 1][m] != '.' && f[n][m - 1] != '.' && f[n][m + 1] != '.') //检验四面是否堵死
{
printf("wrong!\n");
break;
}
}
if (n == 0 && f[n + 1][m] != '.' && f[n][m - 1] != '.' && f[n][m + 1] != '.')
break;
if (n == 9 && f[n + 1][m] != '.' && f[n][m - 1] != '.' && f[n][m + 1] != '.')
break;
if (m == 0 && f[n - 1][m] != '.' && f[n + 1][m] != '.' && f[n][m + 1] != '.')
break;
if (m == 0 && f[n - 1][m] != '.' && f[n + 1][m] != '.' && f[n][m - 1] != '.')
break;
i++;
}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
printf("%c ", f[i][j]);
}
printf("\n");
}
// printf("Done!");
return 0;
}
8-15:
加密(ENCRYPTION) 已知的最古⽼的⼀种加密技术是凯撒加密(得名于 JULIUS CAESAR)。该⽅法把⼀条消息中的每个字⺟⽤字⺟表中固定距离之后的那个字⺟来替代。(如果越过了字⺟Z,会绕回到字⺟表的起始位置。例如,如果每个字⺟都⽤字⺟表中两个位置之后的字⺟代替,那么Y就被替换为A,Z就被替换为B。)
编写程序⽤凯撒加密⽅法对消息进⾏加密。⽤户输⼊待加密的消息和移位计数(字⺟移动的位置数⽬):
Enter message to be encrypted: Go ahead, make my day.
Enter shift amount (1-25): 3
Encrypted message: Jr dkhdg, pdnh pb gdb.
注意,当⽤户输⼊26与移位计数的差值时,程序可以对消息进⾏解密:
Enter message to be encrypted: Jr dkhdg, pdnh pb gdb.
Enter shift amount (1-25): 23
Encrypted message: Go ahead, make my day
不是字⺟的那些字符不要改动。此外,加密时不要改变字⺟的⼤⼩写。
输出范例:
Enter message to be encrypted: Hello, world!
Enter shift amount (1-25): 3
Encrypted message: Khoor, zruog!
Enter message to be encrypted: Khoor, zruog!
Enter shift amount (1-25): 23
Encrypted message: Hello, world!
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x, y, z;
char ch[100];
z = 0;
for (y = 0; y < 100; y++)
ch[y] = 0;
printf("Enter message to be encrypted:\n");
for (;; z++) {
ch[z] = getchar();
if (ch[z] == '\n')
break;
}
printf("Enter shift amount (1-25):\n");
scanf("%d", &x);
y = 0;
printf("Encrypted message: ");
while (y < z) {
if (ch[y] >= 'a' && ch[y] <= 'z') {
ch[y] = ((ch[y] - 'a') + x) % 26 + 'a';
if (ch[y] > 'z')
ch[y] = ((ch[y] - 'A') + x) % 26 + 'A';
}
else if (ch[y] >= 'A' && ch[y] <= 'Z') {
ch[y] += x;
if (ch[y] > 'Z')
ch[y] -= 26;
}
printf("%c", ch[y]);
y++;
}
return 0;
}