CH8_1
#include <stdio.h>
int main(void)
{
int digit_count[10] = { 0 };
int digit;
long long n;
printf("Enter a number: ");
scanf("%lld", &n);
while (n > 0) {
digit = n % 10;
digit_count[digit]++;
n /= 10;
}
printf("Repeated digit(s): ");
for (digit = 0; digit <= 9; digit++) {
if (digit_count[digit] > 0) {
printf("%2d", digit);
}
}
printf("\n");
return 0;
}
CH8_2
#include <stdio.h>
int main(void)
{
int digit_count[10] = { 0 };
int digit;
long long n;
printf("Enter a number: ");
scanf("%lld", &n);
while (n > 0) {
digit = n % 10;
digit_count[digit]++;
n /= 10;
}
printf("Digit: ");
for (digit = 0; digit <= 9; digit++)
printf("%3d", digit);
printf("\nOccurrences:");
for (digit = 0; digit <= 9; digit++)
printf("%3d", digit_count[digit]);
printf("\n");
return 0;
}
CH8_3
#include <stdio.h>
int main(void)
{
int digit_count[10] = { 0 };
int digit;
long long n;
while (1) {
printf("Enter a number: ");
scanf("%lld", &n);
if (n <= 0) {
break;
}
while (n > 0) {
digit = n % 10;
digit_count[digit]++;
n /= 10;
}
printf("Digit: ");
for (digit = 0; digit <= 9; digit++)
printf("%3d", digit);
printf("\nOccurrences: ");
for (digit = 0; digit <= 9; digit++)
printf("%3d", digit_count[digit]);
printf("\n");
for (int i = 0; i <= 9; i++) {
digit_count[i] = 0;
}
}
return 0;
}
CH8_4
#include <stdio.h>
#define SIZE (int)(sizeof (a) / sizeof (a[0]))
#define N 10
int main(void)
{
int a[N], i;
printf("Enter %d numbers: ", N);
for (i = 0; i < SIZE; i++)
scanf("%d", &a[i]);
printf("In reverse order: ");
for (i = SIZE - 1; i >= 0; i--)
printf(" %d", a[i]);
printf("\n");
return 0;
}
CH8_5
#include <stdio.h>
#define NUM_RATES ((int) (sizeof(value) / sizeof(value[0])))
#define INITIAL_BALANCE 100.00
int main(void)
{
int i, low_rate, month, num_years, year;
double value[5];
printf("Enter interest rate: ");
scanf("%d", &low_rate);
printf("Enter number of years: ");
scanf("%d", &num_years);
printf("\nYears");
for (i = 0; i < NUM_RATES; i++) {
printf("%6d%%", low_rate + i);
value[i] = INITIAL_BALANCE;
}
printf("\n");
for (year = 1; year <= num_years; year++) {
printf("%3d ", year);
for (i = 0; i < NUM_RATES; i++) {
for (month = 1; month <= 12; month++)
value[i] += ((double)(low_rate + i) / 12) / 100.0 * value[i];
printf("%7.2f", value[i]);
}
printf("\n");
}
return 0;
}
CH8_6
#include <stdio.h>
#include <ctype.h>
#define N 100
int main(void)
{
int i = 0, n = 0;
char a_ch[N];
char ch;
printf("Enter message: ");
for (i = 0; (ch = getchar()) != '\n'; i++, n++) {
ch = toupper(ch);
switch (ch) {
case 'A':ch = '4'; break;
case 'B':ch = '8'; break;
case 'E':ch = '3'; break;
case 'I':ch = '1'; break;
case 'O':ch = '0'; break;
case 'S':ch = '5'; break;
}
a_ch[i] = ch;
}
printf("IN B1FF-speak: ");
for (i = 0; i <= n - 1; i++) {
printf("%c", a_ch[i]);
}
printf("!!!!!\n");
return 0;
}
CH8_7
#include <stdio.h>
int main(void)
{
int a[5][5] = { 0 };
int o[10] = { 0 };
int i, j;
for (i = 0; i <= 4; i++) {
printf("Enter row %d: ", i + 1);
for (j = 0; j <= 4; j++) {
scanf("%d", &a[i][j]);
}
}
for (i = 0; i <= 4; i++) {
for (j = 0; j <= 4; j++) {
o[i] = a[i][j] + o[i];
o[j + 5] = a[i][j] + o[j + 5];
}
}
printf("Row totals: %d %d %d %d %d\n", o[0], o[1], o[2], o[3], o[4]);
printf("Column totals: %d %d %d %d %d\n", o[5], o[6], o[7], o[8], o[9]);
return 0;
}
CH8_8
#include <stdio.h>
int main(void)
{
double a[5][5] = { 0 };
double o[10] = { 0 };
int i, j;
printf(" A B C D E\n"); //ABCDE代表科目名称
for (i = 0; i <= 4; i++) {
printf("Enter student %d: ", i + 1);
for (j = 0; j <= 4; j++) {
scanf("%lf", &a[i][j]);
}
}
for (i = 0; i <= 4; i++) {
for (j = 0; j <= 4; j++) {
o[i] = a[i][j] + o[i];
o[j + 5] = a[i][j] + o[j + 5];
}
}
int t;
int k;
for (j = 0; j <= 4; j++) {
for (k = 3; k >= 0; k--) {
for (i = 0; i <= k; i++) {
if (a[i][j] > a[i + 1][j]) {
t = a[i][j];
a[i][j] = a[i + 1][j];
a[i + 1][j] = t;
}
}
}
}
printf("Row totals: %.0f %.0f %.0f %.0f %.0f;the Average: %.2f %.2f %.2f %.2f %.2f\n",
o[0], o[1], o[2], o[3], o[4], o[0] / 5, o[1] / 5, o[2] / 5, o[3] / 5, o[4] / 5);
printf("Column totals: %.0f %.0f %.0f %.0f %.0f;the Average: %.2f %.2f %.2f %.2f %.2f\n",
o[5], o[6], o[7], o[8], o[9], o[5] / 5, o[6] / 5, o[7] / 5, o[8] / 5, o[9] / 5);
printf("A max: %.0f; min: %.0f\n", a[4][0], a[0][0]);
printf("B max: %.0f; min: %.0f\n", a[4][1], a[0][1]);
printf("C max: %.0f; min: %.0f\n", a[4][2], a[0][2]);
printf("D max: %.0f; min: %.0f\n", a[4][3], a[0][3]);
printf("E max: %.0f; min: %.0f\n", a[4][4], a[0][4]);
return 0;
}
CH8_9
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#define N 10 //方形矩阵边长
int main(void)
{
char a[N][N];
bool key[N][N] = { true };
int i, j, it, jt;
int way;
int step = 0;
int temp;
bool full = false;
for (i = 0; i <= N - 1; i++) { //初始化方阵
for (j = 0; j <= N - 1; j++) {
a[i][j] = 46;
}
}
i = j = 0;
temp = a[i][j] = 'A';
srand((unsigned)time(NULL));
while (1) {
step++;
it = i; //i,j缓存变量;试错之后方便回到原位
jt = j;
do {
if (key[it][jt + 1] == 1 && key[it][jt - 1] == 1 && key[it + 1][jt] == 1 && key[it - 1][jt] == 1 ||
key[it][jt + 1] == 1 && key[it][jt - 1] == 1 && key[it + 1][jt] == 1 && it == 0 ||
key[it][jt + 1] == 1 && key[it][jt - 1] == 1 && it == N - 1 && key[it - 1][jt] == 1 ||
key[it][jt + 1] == 1 && jt == 0 && key[it + 1][jt] == 1 && key[it - 1][jt] == 1 ||
jt == N - 1 && key[it][jt - 1] == 1 && key[it + 1][jt] == 1 && key[it - 1][jt] == 1 ||
key[it][jt + 1] == 1 && key[it + 1][jt] == 1 && it == 0 && jt == 0 ||
key[it][jt - 1] == 1 && key[it - 1][jt] == 1 && it == N - 1 && jt == N - 1 ||
key[it][jt + 1] == 1 && key[it - 1][jt] == 1 && it == N - 1 && jt == 0 ||
key[it][jt - 1] == 1 && key[it + 1][jt] == 1 && it == 0 && jt == N - 1)
{ //if判断此时是否处于被包围状态
full = true;
break;
}
i = it;
j = jt;
way = rand() % 4;
switch (way) {
case 0: ++i; break;
case 1: ++j; break;
case 2: --i; break;
case 3: --j; break;
default: printf("ERROR"); return 0; break; //测试语句,可删去
}
} while ((i < 0) || (i > N - 1) || (j < 0) || (j > N - 1) || (key[i][j] == true));
if (step >= 26 || full == true) { //满足退出条件时,打印已经走完的step
for (i = 0; i <= N - 1; i++) {
for (j = 0; j <= N - 1; j++) {
printf("%-2c", a[i][j]);
}
printf("\n");
}
break;
}
key[i][j] = true;
temp = a[i][j] = temp + 1;
}
printf("%d step\n", step); //测试语句,显示共走了几步
return 0;
}
CH_10
#include <stdio.h>
#include <math.h>
int main(void)
{
const int leave[8] = { 480, 583, 679, 767, 840, 945, 1140, 1305 };
const int arrive[8] = { 616, 712, 811, 900, 968, 1075, 1280, 1438 };
int hours, minutes;
int time;
printf("Enter a 24-hour time: ");
scanf("%d:%d", &hours, &minutes);
time = hours * 60 + minutes;
int mix = 1440, m;
for (int i = 0; i < 8; i++) {
if (abs(leave[i] - time) < mix) {
mix = abs(leave[i] - time);
m = i;
}
}
if (leave[m] / 60 > 12) {
if (arrive[m] / 60 > 12) {
printf("CLosest departure time is %d:%.2d p.m.", leave[m] / 60 - 12, leave[m] % 60);
printf(", arriving at %d:%.2d p.m.", arrive[m] / 60 - 12, arrive[m] % 60);
}
}
else {
if (arrive[m] / 60 > 12) {
printf("CLosest departure time is %d:%.2d a.m.", leave[m] / 60, leave[m] % 60);
printf(", arriving at %d:%.2d p.m.", arrive[m] / 60 - 12, arrive[m] % 60);
}
else {
printf("CLosest departure time is %d:%.2d a.m.", leave[m] / 60, leave[m] % 60);
printf(", arriving at %d:%.2d p.m.", arrive[m] / 60, arrive[m] % 60);
}
}
return 0;
}
CH_11
#include <stdio.h>
int main(void)
{
int ch, i = 0;
char c[15];
printf("Enter phone number: ");
while ((ch = getchar()) != '\n') {
if (ch <= 'Z' && ch >= 'A') {
switch (ch) {
case 65: case 66: case 67:
c[i] = '2';
break;
case 68: case 69: case 70:
c[i] = '3';
break;
case 71: case 72: case 73:
c[i] = '4';
break;
case 74: case 75: case 76:
c[i] = '5';
break;
case 77: case 78: case 79:
c[i] = '6';
break;
case 81: case 82: case 83: case 80:
c[i] = '7';
break;
case 84: case 85: case 86: case 87:
c[i] = '8';
break;
case 88: case 89: case 90:
c[i] = '9';
break;
}
i++;
continue;
}
c[i] = ch;
i++;
}
printf("In numeric form: ");
for (int j = 0; j < i; j++) {
printf("%c", c[j]);
}
return 0;
}
CH_12
#include <stdio.h>
#include <ctype.h>
int main(void)
{
const int a[26] = { 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10 };
int sum = 0;
char ch;
printf("Enter a word: ");
while ((ch = getchar()) != '\n') {
if (toupper(ch) < 'A' || toupper(ch) > 'Z') {
printf("ERROR\n");
return 0;
}
else {
sum += a[toupper(ch) - 'A'];
}
}
printf("Scrabble value: %d", sum);
return 0;
}
CH_13
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char ch[20];
char ch_;
int i = 0;
printf("Enter the name: ");
ch_ = toupper(getchar());
while (getchar() != ' ')
;
while ((ch[i] = getchar()) != '\n') {
i++;
}
printf("You enered the name: ");
for (int j = 0; j < i; j++) {
printf("%c", ch[j]);
}
printf(", %c.\n", ch_);
return 0;
}
CH_14
#include <stdio.h>
int main(void)
{
char ch[100];
int i = 0;
int t1;
int t2;
printf("Enter a sentence: ");
while ((ch[i] = getchar()) != '.' && ch[i] != '?' && ch[i] != '!') {
t1 = ++i;
}
int sign = ch[i];
if (i == 0) {
printf("Reversal of sentence: %c\n", sign);
return 0;
}
ch[i] = ' ';
t2 = t1; //t2表示一个单词左方指示,t1表示该单词右方指示,并将t2至t1单词输出
//然后再重复移动t1,t2。具体见下面循环
printf("Reversal of sentence: ");
while (1) {
while (ch[t2] != ' ') {
t2--;
if (t2 == -1) {
break;
}
}
for (int j = t2 + 1; j <= t1; j++) {
printf("%c", ch[j]);
}
t1 = t2--;
if (t2 == -2) {
break;
}
}
printf("\b%c\n", sign);
return 0;
}
CH8_15
#include <stdio.h>
int main(void)
{
char ch[100];
int i = 0;
int n;
printf("Enter message to be encrypted: ");
while ((ch[i] = getchar()) != '\n') {
i++;
}
printf("Enter shift amout (1~25): ");
scanf("%d", &n);
for (int j = 0; j < i; j++) {
if (ch[j] >= 'A' && ch[j] <= 'Z') {
ch[j] = (ch[j] - 'A' + n) % 26 + 'A';
}
else if (ch[j] >= 'a' && ch[j] <= 'z') {
ch[j] = (ch[j] - 'a' + n) % 26 + 'a';
}
}
printf("Encrypted message: ");
for (int j = 0; j < i; j++) {
printf("%c", ch[j]);
}
return 0;
}
CH8_16
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int ch[26] = { 0 };
char CH;
printf("Enter first word: ");
while ((CH = getchar()) != '\n') {
if (isalpha(CH)) {
CH = toupper(CH);
ch[CH - 'A']++;
}
}
printf("Enter Second word: ");
while ((CH = getchar()) != '\n') {
if (isalpha(CH)) {
CH = toupper(CH);
ch[CH - 'A']--;
}
}
int key = 0;
for (int i = 0; i < 26; i++) {
if (ch[i] == 0) {
key++;
}
}
if (key == 26) {
printf("The words are anagrams.");
}
else {
printf("The words are not anagrams.");
}
return 0;
}
CH8_17
#include <stdio.h>
#define N 5 //VS2019不支持变长数组,利用宏给出大小
int main(void)
{
int n, m;
int n_next, m_next;
printf("This is program creates a magic square of a specified size.\n");
printf("The size must be an odd number between 1 and 99.\n");
printf("Enter size of magic square: %d\n", N);
int a[N][N] = { 0 };
m = 0;
n = (N - 1) / 2;
for (int i = 1; i <= N * N; i++) {
a[m][n] = i;
if (m == 0) {
m_next = N - 1;
}
else {
m_next = m - 1;
}
if (n == N - 1) {
n_next = 0;
}
else {
n_next = n + 1;
}
if (a[m_next][n_next] != 0) {
m++;
}
else {
m = m_next;
n = n_next;
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%5d", a[i][j]);
}
printf("\n");
}
return 0;
}