// colddays.c -- 找出0℃如下的天數佔總天數的百分比 #include <stdio.h> int main(void) { const int FREEZING = 0; float temperature; int cold_days = 0; int all_days = 0; printf("Enter the list of daily low temperatures.\n"); printf("Use Celsius, and enter q to quit.\n"); while (scanf("%f", &temperature) == 1) { all_days++; if (temperature < FREEZING) cold_days++; } if (all_days != 0) printf("%d days total: %.1f%% were below freezing.\n", all_days, 100.0 * (float) cold_days / all_days); if (all_days == 0) printf("No data entered!\n"); return 0; }
// cypher1.c -- 更改輸入,空格不變 #include <stdio.h> #define SPACE ' ' // SPACE表示單引號-空格-單引號 int main(void) { char ch; ch = getchar(); // 讀取一個字符 while (ch != '\n') // w當一行未結束時 { if (ch == SPACE) // 留下空格 putchar(ch); //該字符不變 else putchar(ch + 1); // 改變其餘字符 ch = getchar(); // 獲取下一個字符 } putchar(ch); // 打印換行符 return 0; }
// cypher2.c -- 替換輸入的字母,非字母字符保持不變 #include <stdio.h> #include <ctype.h> // 包含isalpha()的函數原型 int main(void) { char ch; while ((ch = getchar()) != '\n') { if (isalpha(ch)) // 若是是一個字符 putchar(ch + 1); // 顯示該字符的下一個字符 else // 不然, putchar(ch); // 原樣顯示 } putchar(ch); //顯示換行符 return 0; }
#4 electric.c函數
// electric.c -- 計算電費 #include <stdio.h> #define RATE1 0.13230 // 首次使用 360 kwh 的費率 #define RATE2 0.15040 // 接着再使用 108 kwh 的費率 #define RATE3 0.30025 // 接着再使用 252 kwh 的費率 #define RATE4 0.34025 // 使用超過 720kwh 的費率 #define BREAK1 360.0 // 費率的第1個分界點 #define BREAK2 468.0 // 費率的第2個分界點 #define BREAK3 720.0 // 費率的第3個分界點 #define BASE1 (RATE1 * BREAK1) // 使用360kwh的費用 #define BASE2 (BASE1 + (RATE2 * (BREAK2 - BREAK1))) // 使用468kwh的費用 #define BASE3 (BASE1 + BASE2 + (RATE3 *(BREAK3 - BREAK2))) //使用720kwh的費用 int main(void) { double kwh; // 使用的千瓦時 double bill; // 電費 printf("Please enter the kwh used.\n"); scanf("%lf", &kwh); // %lf對應double類型 if (kwh <= BREAK1) bill = RATE1 * kwh; else if (kwh <= BREAK2) // 360~468 kwh bill = BASE1 + (RATE2 * (kwh - BREAK1)); else if (kwh <= BREAK3) // 468~720 kwh bill = BASE2 + (RATE3 * (kwh - BREAK2)); else // 超過 720 kwh bill = BASE3 + (RATE4 * (kwh - BREAK3)); printf("The charge for %.1f kwh is $%1.2f.\n", kwh, bill); return 0; }
// divisors.c -- 使用嵌套if語句顯示一個數的約數 #include <stdio.h> #include <stdbool.h> int main(void) { unsigned long num; // 待測試的數 unsigned long div; // 可能的約數 bool isPrime; // 素數標記 printf("Please enter an integer for analysis; "); printf("Enter q to quit.\n"); while (scanf("%lu", &num) == 1) { for (div = 2, isPrime = true; (div * div) <= num; div++) { if (num % div == 0) { if ((div * div) != num) printf("%lu is divisible by %lu and %lu.\n",num, div, num / div); else printf("%lu is divisible by %lu.\n",num, div); isPrime= false; // number is not prime } } if (isPrime) printf("%lu is prime.\n", num); printf("Please enter another integer for analysis; "); printf("Enter q to quit.\n"); } printf("Bye.\n"); return 0; }
// chcount.c -- 使用邏輯與運算符 #include <stdio.h> #define PERIOD '.' int main(void) { char ch; int charcount = 0; while ((ch = getchar()) != PERIOD) { if (ch != '"' && ch != '\'') charcount++; } printf("There are %d non-quote characters.\n", charcount); return 0; }
// wordcnt.c -- 統計字符數、單詞數、行數 #include <stdio.h> #include <ctype.h> // 爲isspace()函數提供原型 #include <stdbool.h> // 爲bool、true、false提供定義 #define STOP '|' int main(void) { char c; // 讀入字符 char prev; // 讀入的前一個字符 long n_chars = 0L; // 字符數 int n_lines = 0; // 行數 int n_words = 0; // 單詞數 int p_lines = 0; // 不完整的行數 bool inword = false; // 若是c在單詞中,inword 等於 true printf("Enter text to be analyzed (| to terminate):\n"); prev = '\n'; // 用於識別完整的行 while ((c = getchar()) != STOP) { n_chars++; // 統計字符 if (c == '\n') n_lines++; // 統計行 if (!isspace(c) && !inword) { inword = true; // 開始一個新的單詞 n_words++; // 統計單詞 } if (isspace(c) && inword) inword = false; // 打到單詞的末尾 prev = c; // 保存字符的值 } if (prev != '\n') p_lines = 1; printf("characters = %ld, words = %d, lines = %d, ", n_chars, n_words, n_lines); printf("partial lines = %d\n", p_lines); return 0; }
/* paint.c -- 使用條件運算符 */ #include <stdio.h> #define COVERAGE 350 // 每罐油漆可刷的面積(單位:平方英尺) int main(void) { int sq_feet; int cans; printf("Enter number of square feet to be painted:\n"); while (scanf("%d", &sq_feet) == 1) { cans = sq_feet / COVERAGE; cans += ((sq_feet % COVERAGE == 0)) ? 0 : 1; printf("You need %d %s of paint.\n", cans, cans == 1 ? "can" : "cans"); printf("Enter next value (q to quit):\n"); } return 0; }
/* skippart.c -- 使用continue跳過部分循環 */ #include <stdio.h> int main(void) { const float MIN = 0.0f; const float MAX = 100.0f; float score; float total = 0.0f; int n = 0; float min = MAX; float max = MIN; printf("Enter the first score (q to quit): "); while (scanf("%f", &score) == 1) { if (score < MIN || score > MAX) { printf("%0.1f is an invalid value. Try again: ",score); continue; // 跳轉至while循環的測試條件 } printf("Accepting %0.1f:\n", score); min = (score < min)? score: min; max = (score > max)? score: max; total += score; n++; printf("Enter next score (q to quit): "); } if (n > 0) { printf("Average of %d scores is %0.1f.\n", n, total / n); printf("Low = %0.1f, high = %0.1f\n", min, max); } else printf("No valid scores were entered.\n"); return 0; }
/* break.c -- 使用 break 退出循環 */ #include <stdio.h> int main(void) { float length, width; printf("Enter the length of the rectangle:\n"); while (scanf("%f", &length) == 1) { printf("Length = %0.2f:\n", length); printf("Enter its width:\n"); if (scanf("%f", &width) != 1) break; printf("Width = %0.2f:\n", width); printf("Area = %0.2f:\n", length * width); printf("Enter the length of the rectangle:\n"); } printf("Done.\n"); return 0; }
/* animals.c -- 使用switch語句 */ #include <stdio.h> #include <ctype.h> int main(void) { char ch; printf("Give me a letter of the alphabet, and I will give "); printf("an animal name\nbeginning with that letter.\n"); printf("Please type in a letter; type # to end my act.\n"); while ((ch = getchar()) != '#') { if('\n' == ch) continue; if (islower(ch)) /* 只接受小寫字母 */ switch (ch) { case 'a' : printf("argali, a wild sheep of Asia\n"); break; case 'b' : printf("babirusa, a wild pig of Malay\n"); break; case 'c' : printf("coati, racoonlike mammal\n"); break; case 'd' : printf("desman, aquatic, molelike critter\n"); break; case 'e' : printf("echidna, the spiny anteater\n"); break; case 'f' : printf("fisher, brownish marten\n"); break; default : printf("That's a stumper!\n"); } /* switch結束 */ else printf("I recognize only lowercase letters.\n"); while (getchar() != '\n') continue; /* 跳過輸入行的剩餘部分 */ printf("Please type another letter or a #.\n"); } /* while循環結束 */ printf("Bye!\n"); return 0; }
// vowels.c -- 使用多重標籤 #include <stdio.h> int main(void) { char ch; int a_ct, e_ct, i_ct, o_ct, u_ct; a_ct = e_ct = i_ct = o_ct = u_ct = 0; printf("Enter some text; enter # to quit.\n"); while ((ch = getchar()) != '#') { switch (ch) { case 'a' : case 'A' : a_ct++; break; case 'e' : case 'E' : e_ct++; break; case 'i' : case 'I' : i_ct++; break; case 'o' : case 'O' : o_ct++; break; case 'u' : case 'U' : u_ct++; break; default : break; } // switch結束 } // while循環結束 printf("number of vowels: A E I O U\n"); printf(" %4d %4d %4d %4d %4d\n", a_ct, e_ct, i_ct, o_ct, u_ct); return 0; }