2018-07-18
自己練習時手寫,難免會有些疏忽遺漏等各種各樣問題,錯誤之處還請指出
但這些代碼確實已通過編譯,實現(xiàn)了書上的輸出結(jié)果,還希望能給需要之人作為個小參考
8_1
- #include <stdio.h>
- #include <stdbool.h>
- int main (void)
- {
- bool digit_seen[10] = {false}, digit_wr[10] = {false}, flag = false;
- int digit;
- long n;
- printf ('Enter a number: ');
- scanf ('%ld', &n);
- while (n > 0) {
- digit = n % 10;
- if (digit_seen[digit] == true) {
- flag = true;
- digit_wr[digit] = true;
- }
- digit_seen[digit] = true;
- n /= 10;
- }
- if (flag) {
- printf ('Repeated digits(s): ');
- for (int i = 0; i < 10; i ) {
- if (digit_wr[i]) printf ('%d ', i);
- }
- }
- else
- printf ('No repeated digit\n');
- return 0;
- }
8_2
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- int main (void)
- {
- int digit_wr[10] = {0};
- int digit;
- int n;
- printf ('Enter a number:');
- scanf ('%d', &n);
- while (n > 0) {
- digit = n % 10;
- digit_wr[digit] ;
- n /= 10;
- }
- printf ('Digit:\t\t');
- for (int i = 0; i < 10; i ) {
- printf ('%3d', i);
- }
- printf ('\nOccurrences:\t');
- for (int i = 0; i < 10; i ) {
- printf ('%3d', digit_wr[i]);
- }
- printf('\n');
- system('pause');
- return 0;
- }
8_3
- #include <stdio.h>
- #include <stdbool.h>
- /* * * * * * * * * * * * * * * * * * * * * * * *
- * 連續(xù)輸入一百以內(nèi)數(shù),當輸入的數(shù)小于或等于0 *
- * 時程序終止。 *
- * * * * * * * * * * * * * * * * * * * * * * * */
- int main(void)
- {
- bool flag;
- int digit;
- int n;
- while (1) {
- //每次執(zhí)行前將標志數(shù)組和標志變量初始化
- bool digit_seen[10] = {false};
- flag = false;
- printf ('Enter a number: ');
- scanf ('%d', &n);
- //如果輸入是0跳出循環(huán),結(jié)束
- if (n <= 0) break;
- while (n > 0) {
- digit = n % 10;
- if (digit_seen[digit] == true) flag = true;
- digit_seen[digit] = true;
- n = n / 10;
- }
- if (flag) {
- printf('Repeated digit\n');
- }
- else {
- printf('No repeated digit\n');
- }
- }
- return 0;
- }
8_4
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.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');
- printf('\n');
- system('pause');
- return 0;
- }
8_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, num_years, year, month;
- double value[5];
- printf ('Enter interst 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 ');
- 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;
- }
8_6
- #include <stdio.h>
- #include <ctype.h>
- int main (void)
- {
- char mess[100];
- int i = 0, j;
- printf ('Enter message: ');
- while (((mess[i]) = getchar () ) != '\n') {
- i ;
- }
- printf ('In B1FF-speak: ');
- for (j = 0; j < i; j ) {
- if ((mess[j] <= 122) && (mess[j] >= 97)){
- mess[j] = toupper (mess[j]);
- }
- switch (mess[j]) {
- case 'A': printf ('4'); break;
- case 'B': printf ('8'); break;
- case 'E': printf ('3'); break;
- case 'I': printf ('1'); break;
- case 'O': printf ('0'); break;
- case 'S': printf ('5'); break;
- default : printf ('%c', mess[j]); break;
- }
- }
- printf ('!!!!!!!!!!');
- return 0;
- }
8_7
- #include <stdio.h>
- #define N 5
- int main(void)
- {
- int a[N][N] = {0};
- int i, j;
- for (i = 0; i < N; i ) {
- printf('Enter row %d: ', i 1);
- for (j = 0; j < N; j ) {
- scanf('%d', &a[i][j]);
- }
- }
- int sum;
- printf('Row totals: ');
- for (i = 0; i < N; i ) {
- for (j = 0; j < N; j ) {
- sum = a[i][j];
- }
- printf('%d ', sum);
- sum = 0;
- }
- printf('\nColumn totals: ');
- for (j = 0; j < N; j ) {
- for (i = 0; i < N; i ) {
- sum = a[i][j];
- }
- printf('%d ', sum);
- sum = 0;
- }
- return 0;
- }
8_8
- #include <stdio.h>
- #define N 5
- int main(void)
- {
- int a[N][N];
- int i, j;
- for (i = 0; i < N; i ) {
- printf('輸入學生%d的五門成績:', i 1);
- for (j = 0; j < N; j ) {
- scanf('%d', &a[i][j]);
- }
- }
- printf('\n');
- int sum = 0;
- float average = 0.0f;
- for (i = 0; i < N; i ) {
- for (j = 0; j < N; j ) {
- sum = a[i][j];
- }
- average = (float) sum / N;
- printf('學生%d的總成績和平均分分別為%d和%.2f\n', i, sum, average);
- sum = 0;
- }
- printf('\n');
- int max, mix;
- sum = 0;
- average = 0.0f;
- for (j = 0; j < N; j ) {
- max = a[0][j];
- mix = a[0][j];
- for (i = 0; i < N; i ) {
- if (max < a[i][j]) max = a[i][j];
- if (mix > a[i][j]) mix = a[i][j];
- sum = a[i][j];
- }
- average = (float) sum / N;
- printf('學科%d的平均分是%.2f,最高分是%d,最低分是%d\n', j, average, max, mix);
- sum = 0;
- }
- return 0;
- }
8_9
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #define N 10
- #define WAY 4
- int main(void)
- {
- bool a[N][N] = {false};
- char b[N][N];
- int i, j;
- //初始化表
- for (i = 0; i < N; i ) {
- for (j = 0; j < N; j ) {
- b[i][j] = '.';
- }
- }
- srand((unsigned) time(NULL));
- int ways;
- i = 0; j = 0;
- b[0][0] = 'A';
- a[0][0] = true;
- for (int m = 0; m < 25; ) {
- //余0表上, 1表下, 2表左, 3表右
- ways = rand() % WAY;
- if (ways == 0) {
- //如果往上走出表格了,continue重新循環(huán)試試別的方向
- if (i - 1 < 0) {
- continue;
- }
- //如果往上走有字母了, 進行判斷
- else if (a[i - 1][j] == true){
- //如果四面都是字母,退出
- if ((a[i 1][j] == true && a[i][j - 1] == true && a[i][j 1] == true)
- // 考慮左邊和左下角情況
- || j - 1 < 0 && a[i 1][j] == true && a[i][j 1] == true
- || j - 1 < 0 && i 1 > 9 && a[i][j 1] == true
- // 右邊和右下角
- || j 1 > 9 && a[i 1][j] == true && a[i][j - 1] == true
- || j - 1 < 0 && i 1 > 9 && a[i][j 1] == true
- //下邊
- || i 1 > 9 && a[i][j 1] == true && a[i][j - 1] == true) break;
- //如果都不是,重新選方向
- continue;
- } else {
- a[i - 1][j] = true;
- b[i - 1][j] = 'B' m;
- m ;
- i--;
- continue;
- }
- }
- if (ways == 1) {
- if (i 1 > 9) {
- continue;
- } else if (a[i 1][j] == true){
- if (a[i - 1][j] == true && a[i][j - 1] == true && a[i][j 1] == true
- //右邊和右上角
- || j 1 > 9 && a[i][j - 1] == true && a[i - 1][j] == true
- || j 1 > 9 && i - 1 < 0 && a[i][j - 1] == true
- //左邊
- || j - 1 < 0 && a[i - 1][j] == true && a[i][j 1] == true
- //上邊
- || i - 1 < 0 && a[i][j 1] == true && a[i][j - 1] == true) break;
- continue;
- } else {
- a[i 1][j] = true;
- b[i 1][j] = 'B' m;
- m ;
- i ;
- continue;
- }
- }
- if (ways == 2) {
- if (j - 1 < 0) {
- continue;
- } else if (a[i][j - 1] == true){
- if (a[i 1][j] == true && a[i - 1][j] == true && a[i][j 1] == true
- // 考慮下邊和右下角情況
- || i 1 > 9 && a[i - 1][j] == true && a[i][j 1] == true
- || j 1 > 9 && i 1 > 9 && a[i - 1][j] == true
- // 上邊和右上角
- || i - 1 < 0 && a[i 1][j] == true && a[i][j 1] == true
- || j 1 > 9 && i - 1 < 0 && a[i][j 1] == true
- // 右邊
- || j 1 > 9 && a[i - 1][j] == true && a[i 1][j] == true) break;
- continue;
- } else {
- a[i][j - 1] = true;
- b[i][j - 1] = 'B' m;
- m ;
- j--;
- continue;
- }
- }
- if (ways == 3) {
- if (j 1 > 9) {
- continue;
- } else if (a[i][j 1] == true){
- if (a[i 1][j] == true && a[i][j - 1] == true && a[i - 1][j] == true
- //左邊和左下角
- || j - 1 < 0 && a[i - 1][j] == true && a[i 1][j] == true
- || j - 1 < 0 && i 1 > 9 && a[i - 1][j] == true
- //下邊
- || i 1 > 9 && a[i - 1][j] == true && a[i][j - 1] == true
- //上邊
- || i - 1 < 0 && a[i][j - 1] == true && a[i 1][j] == true) break;
- continue;
- } else {
- a[i][j 1] = true;
- b[i][j 1] = 'B' m;
- m ;
- j ;
- continue;
- }
- }
- }
- for (i = 0; i < N; i ) {
- for (j = 0; j < N; j ) {
- printf('%c ', b[i][j]);
- }
- printf('\n');
- }
- return 0;
- }
8_10
- #include <stdio.h>
- #include <math.h>
- int main(void)
- {
- const int lea[8] = {480, 583, 679, 767, 840, 945, 1140, 1305};
- const int arr[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 (fabs(lea[i] - time) < mix) {
- mix = fabs(lea[i] - time);
- m = i;
- }
- }
- int hour, minute;
- //判斷是上午還是下午并輸出
- if (lea[m] / 60 > 12) {
- if (arr[m] / 60 > 12) {
- printf('CLosest departure time is %d:%.2d p.m.', lea[m] / 60 - 12, lea[m] % 60);
- printf(', arriving at %d:%.2d p.m.', arr[m] / 60 -12, arr[m] % 60);
- }
- } else {
- if (arr[m] / 60 > 12) {
- printf('CLosest departure time is %d:%.2d a.m.', lea[m] / 60, lea[m] % 60);
- printf(', arriving at %d:%.2d p.m.', arr[m] / 60 -12, arr[m] % 60);
- } else {
- printf('CLosest departure time is %d:%.2d a.m.', lea[m] / 60, lea[m] % 60);
- printf(', arriving at %d:%.2d p.m.', arr[m] / 60, arr[m] % 60);
- }
- }
- return 0;
- }
8_11
- #include <stdio.h>
- int main (void)
- {
- int ch, i = 0;
- char c[15];
- printf ('Enter phone number: ');
- //輸入字符直到為回車為止
- while ((ch = getchar ()) != '\n') {
- //輸入的如果是字母,換成數(shù)字
- 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;
- }
8_12
- #include <stdio.h>
- //頭文件ctpye中包含toupper()函數(shù)
- #include <ctype.h>
- int main(void)
- {
- //建立整數(shù)型數(shù)組模擬每個字母所代表的面值
- 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('Illegal input\n');
- break;
- } else {
- sum = a[toupper(ch) - 'A'];
- }
- }
- printf('Scrabble value: %d', sum);
- return 0;
- }
8_13
- #include <stdio.h>
- int main(void)
- {
- char a[20];
- char ch1, ch2;
- printf ('Enter a first and last name: ');
- scanf ('%c', &ch1);
- //循環(huán)輸入直到空格為止
- while ((getchar()) != ' ')
- ;
- int i = 0;
- while ((ch2 = getchar()) != '\n') {
- a[i] = ch2;
- i ;
- }
- printf('You enered the name: ');
- for (int j = 0; j < i; j ) {
- printf('%c', a[j]);
- }
- printf(', %c.', ch1);
- return 0;
- }
8_14
- #include <stdio.h>
- int main(void)
- {
- char sen[100];
- int i;
- printf('Enter a sentence: ');
- for (i = 0; i < 100; i ) { //建立字符數(shù)字sen并輸入句子
- sen[i] = getchar();
- if (sen[i] == '!' || sen[i] == '.' || sen[i] == '?') break; //遇到句末符號break跳出循環(huán)(i 并沒有執(zhí)行),此時i指向末尾的符號
- }
- int pri = i; //記錄下句子的長度,以后用于輸出
- char sen1[100]; //建立一個數(shù)組用于存改變順序后的句子 (一個一個單詞地復制)
- int j = 0;
- int over, start; //start為每次開始復制時的數(shù)組下標,over為復制結(jié)束時的
- for (i = i - 1; i >= 0; i--, j ) { //從i-1的位置往前找空格,i-1表示先不處理句末符號
- over = i; //下次一個單詞的復制就從i(i此時已經(jīng)i-1)處結(jié)束
- while (sen[i] != ' ' && i > 0) i--; //遇到空格或者i<=0時,停止尋找
- start = i 1; //開始一個單詞的復制,下標應(yīng)該在空格后一個
- if (i == 0) start--; //這個if只針對已經(jīng)復制到原句的第一個單詞的情況,start需要從零開始故減1
- for (; start <= over; start , j ) {
- sen1[j] = sen[start]; //復制單詞
- }
- sen1[j] = ' '; //每個單詞后加個空格
- }
- sen1[j - 1] = sen[pri]; //之后就是sen1數(shù)組的輸出了!
- printf('Reversal of sentence: ');
- for (int m = 0; m < pri 1; m ) {
- printf('%c', sen1[m]);
- }
- return 0;
- }
8_15
- #include <stdio.h>
- int main(void)
- {
- //sen為原句子
- char sen[80], sen1[80];
- int n;
- //初始化數(shù)組
- int i;
- printf('Enter message to be encrypted: ');
- for (i = 0; ; i ) {
- sen[i] = getchar();
- if (sen[i] == '\n') break;
- }
- printf('Enter shift amout (1~25): ');
- scanf('%d', &n);
- printf('Encrypted message: ');
- for (int j = 0; j < i 1; j ) {
- if (sen[j] <= 'z' && sen[j] >= 'a') {
- printf('%c', ((sen[j] - 'a') n) % 26 'a');
- } else if (sen[j] <= 'Z' && sen[j] >= 'A') {
- printf('%c', ((sen[j] - 'A') n) % 26 'A');
- } else {
- printf('%c', sen[j]);
- }
- }
- return 0;
- }
8_16
- #include <ctype.h>
- #include <stdio.h>
- #include <stdbool.h>
- int main(void)
- {
- //定義一個整數(shù)數(shù)組用來記錄每個字母出現(xiàn)的次數(shù)。第一輸入單詞,
- //對應(yīng)數(shù)組元素加一,第二次輸入減一,若最后每個元素都為零則 為變?yōu)樵~
- int num[26] = {0};
- char word[20];
- int i;
- printf('Enter first word: ');
- for (i = 0; i < 20; i ) {
- word[i] = getchar();
- if (!isalpha(word[i]) && word[i] != '\n') {
- printf('Illegal input!');
- return 0;
- }
- if (word[i] == '\n') break;
- word[i] = tolower(word[i]);
- num[word[i] - 'a'] ;
- }
- printf('Enter second word: ');
- for (i = 0; i < 20; i ) {
- word[i] = getchar();
- if (!isalpha(word[i]) && word[i] != '\n') {
- printf('Illegal input!');
- return 0;
- }
- if (word[i] == '\n') break;
- word[i] = tolower(word[i]);
- num[word[i] - 'a']--;
- }
- bool flag = false;
- for (i = 0; i < 26; i ) {
- if (num[i]) flag = true;
- }
- if (flag) printf('The words are not anagrams.');
- else printf('The words are anagrams.');
- return 0;
- }
8_17
- #include <stdio.h>
- int main(void)
- {
- 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: ');
- int size;
- scanf('%d', &size);
- int square[size][size];
- for (int i = 0; i < size; i ) {
- for (int j = 0; j < size; j ) {
- square[i][j] = 0;
- }
- }
- int i = 0, j = size / 2, num = 1;
- for (; ; ) {
- if (square[i][j] == 0) {
- square[i][j] = num;
- i = i size - 1; j ;
- i = i % size;
- j = j % size;
- } else {
- i ; i ; j = j size - 1;
- i = i % size;
- j = j % size;
- square[i][j] = num;
- i = i size - 1; j ;
- i = i % size;
- j = j % size;
- }
- num ;
- if (num > size * size) break;
- }
- for (i = 0; i < size; i ) {
- for (j = 0; j < size; j ) {
- printf('%3d', square[i][j]);
- }
- printf('\n');
- }
- return 0;
- }