1、編寫一個程序讀取輸入,讀到#字符中止,而後報告讀取的空格數、換行符數和全部其餘字符的數量。ios
#include<iostream> using namespace std; int main() { char x; int a=0; int b=0; int c=0; while((x=getchar())!= '#') { if(x == ' ') a++; else if(x == '\n') b++; else { c++; } } cout << a <<" "<< b << " "<< c; return 0; }
2、編寫一個程序讀取輸入,讀到#字符中止。程序要打印每一個輸入的字符以及對應的ASCII碼(十進制)。一行打印8個字符。建議:使用字符計數和求模運算符(%)在每8個循環週期時打印一個換行符。c++
#include <stdio.h> //#define STOP '#' int main(void) { char c; int n = 0; while((c = getchar()) != '#') { if (c == '\n') continue; printf("%2c:%-7d", c, c); n++; } return 0; }
3、編寫一個程序,讀取整數直到用戶輸入 0。輸入結束後,程序應報告用戶輸入的偶數(不包括 0)個數、這些偶數的平均值、輸入的奇數個數及其奇數的平均值。測試
#include <stdio.h> #include <stdlib.h> int main(void) { char ch; int os = 0, js = 0, oss = 0, jss = 0; double osa = 0.0, jsa = 0.0; int a; while(scanf("%d", &a) && a) { if(a % 2) { js++; jss += a; } else { os++; oss += a; } } if(os) { osa = double(oss) / double(os); } if(js) { jsa = double(jss) / double(js); } printf("%d %.2lf %d %.2lf\n", os, osa, js, jsa); return 0; }
4、使用if else語句編寫一個程序讀取輸入,讀到#中止。用感嘆號替換句號,用兩個感嘆號替換原來的感嘆號,最後報告進行了多少次替換。ui
#include <stdio.h> #include <stdlib.h> int main(void) { char ch; int n = 0; while((ch = getchar()) != '#') { if(ch == '.') { putchar('!'); n++; } else if(ch == '!') { putchar('!'); putchar('!'); n++; } else { putchar(ch); } } printf("\n%d\n", n); return 0; }
5、編寫程序讀取輸入,讀到#中止,報告ei出現的次數。 注意 該程序要記錄前一個字符和當前字符。用「Receive your eieio award」這樣的輸入來測試。spa
#include <stdio.h> int main(void) { char ch,sh; int i=0; ch=getchar(); while(ch!='#'&&(sh=getchar())!='#') { if(ch=='e'&&sh=='i') i++; ch=sh; } printf("%d",i); return 0; }
6、編寫一個程序,提示用戶輸入一週工做的小時數,而後打印工資總額、稅金和淨收入。作以下假設: a.基本工資 = 1000美圓/小時 b.加班(超過40小時) = 1.5倍的時間 c.稅率: 前300美圓爲15% 續150美圓爲20% 餘下的爲25% 用#define定義符號常量。不用在乎是否符合當前的稅法 #include <stdio.h> #include <stdlib.h> #define MAXT 40.0 #define GH 1000.0 #define FR 300.0 #define SR 450.0 #define FTR 0.15 #define STR 0.20 #define TTR 0.25code
int main(void) { float t, sum, tax, gain; scanf("%f", &t); if(t > MAXT) { t = MAXT + (t - MAXT) * 1.5; } sum = GH * t; if(sum <= FR) { tax = sum * FTR; gain = sum - tax; } else if(sum > FR && sum < SR) { tax = FR * FTR + (sum - FR) * STR; gain = sum - tax; } else if(sum > SR) { tax = FR * FTR + (SR - FR) * STR + (sum - SR) * TTR; gain = sum - tax; } printf("%-10.2f%-10.2f%-10.2f%-10.2f\n", t, sum, gain, tax); return 0; }
7、編寫一個程序,只接受正整數輸入,而後顯示全部小於或等於該數的素數。get
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { long long num; int x = 1; while(x) { if(scanf("%d", &num) == 1) { if(num < 1) { continue; } else { x = 0; const int len = num; int a[len + 1]; memset(a, 0, sizeof(int) * (len + 1)); for(int i = 2; i <= num; i++) { if(!a[i]) { for(int j = i + i; j <= num; j += i) { a[j] = 1; } } } for(int i = 2; i <= num; i++) { if(!a[i]) { printf("%d ", i); } } printf("\n"); } } } return 0; }
8、1988年的美國聯邦稅收計劃是近代最簡單的稅收方案。它分爲4個類別,每一個類別有兩個等級。下面是該稅收計劃的摘要(美圓數爲應徵稅的收入): 例如,一位工資爲20000美圓的單身納稅人,應繳納稅0.15×17850+0.28×(20000−17850)美圓。編寫一個程序,讓用戶指定繳納稅金的種類和應納稅收入,而後計算稅金。程序應經過循環讓用戶能夠屢次輸入input
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { float income, tax_r, tax; int type; printf("*******************************************************\n"); printf("1)單身 2)戶主\n3)已婚,共有 4)已婚,離異\n5)退出\n"); printf("*******************************************************\n"); printf("input type:"); while(scanf("%d", &type) == 1) { int x = 1; while(x) { switch(type) { case 1: tax_r = 17850.0; x = 0; break; case 2: tax_r = 23900.0; x = 0; break; case 3: tax_r = 29750.0; x = 0; break; case 4: tax_r = 14875.0; x = 0; break; case 5: return 0; default: printf("input from 1 to 5:"); scanf("%d", &type); } } printf("input income:"); scanf("%f", &income); if(income > tax_r) { tax = tax_r * 0.15 + (income - tax_r) * 0.28; } else { tax = income * 0.15; } printf("tax is %.2f\n", tax); printf("input from 1 to 4 to continue or input 5 to exit:"); } return 0; }
9、ABC 郵購雜貨店出售的洋薊售價爲 2.05 美圓/磅,甜菜售價爲 1.15美圓/磅,胡蘿蔔售價爲 1.09美圓/磅。在添加運費以前,100美圓的訂單有5%的打折優惠。少於或等於5磅的訂單收取6.5美圓的運費和包裝費,5磅~20磅的訂單收取14美圓的運費和包裝費,超過20磅的訂單在14美圓的基礎上每續重1磅增長0.5美圓。編寫一個程序,在循環中用switch語句實現用戶輸入不一樣的字母時有不一樣的響應,即輸入a的響應是讓用戶輸入洋薊的磅數,b是甜菜的磅數,c是胡蘿蔔的磅數,q 是退出訂購。程序要記錄累計的重量。即,若是用戶輸入 4 磅的甜菜,而後輸入 5磅的甜菜,程序應報告9磅的甜菜。而後,該程序要計算貨物總價、折扣(若是有的話)、運費和包裝費。隨後,程序應顯示全部的購買信息:物品售價、訂購的重量(單位:磅)、訂購的蔬菜費用、訂單的總費用、折扣(若是有的話)、運費和包裝費,以及全部的費用總額。string
#include <stdio.h> #include <stdlib.h> #include <string.h> const float ap = 2.05; const float bp = 1.15; const float cp = 1.09; int main(void) { char ch; float sum, discount, df; int an = 0, bn = 0, cn = 0, sn = 0, n; while((ch = getchar()) != 'q') { switch(ch) { case 'a': printf("\ninput n:"); scanf("%d", &n); an += n; sn += n; sum += ap * n; break; case 'b': printf("\ninput n:"); scanf("%d", &n); bn += n; sn += n; sum += bp * n; break; case 'c': printf("\ninput n:"); scanf("%d", &n); cn += n; sn += n; sum += cp * n; break; default: printf("input a or b or c or q:"); } getchar(); } if(sum > 100) { discount = 0.05 * sum; } else { discount = 0.0; } if(sn <= 5) { df = 6.5; } else if(sn < 20) { df = 14.0; } else { df = 14.0 + (sn - 20) * 0.5; } printf("ap = %.2f, bp = %.2f, cp = %.2f\n", ap, bp, cp); printf("an = %d, bn = %d, cn = %d\n", an, bn, cn); printf("a is %.2f, b is %.2f, c is %.2f\n", ap * an, bp * bn, cp * cn); printf("sum is %.2f, discount is %.2f, df is %.2f\n", sum, discount, df); return 0; }
10、使用switch重寫練習4it
#include <stdio.h> #include <stdlib.h> int main(void) { char ch; int n = 0; while((ch = getchar()) != '#') { switch(ch) { case '.': putchar('!'); n++; break; case '!': putchar('!'); putchar('!'); n++; break; default: putchar(ch); } } printf("\n%d\n", n); return 0; }
11、修改練習6的假設a,讓程序能夠給出一個供選擇的工資等級菜單。使 用switch完成工資等級選擇。運行程序後,顯示的菜單應該相似這樣:
Enter the number corresponding to the desired pay rate or action:
若是選擇 1~4 其中的一個數字,程序應該詢問用戶工做的小時數。程 序要經過循環運行,除非用戶輸入 5。若是輸入 1~5 之外的數字,程序應 提醒用戶輸入正確的選項,而後再重複顯示菜單提示用戶輸入。使用#define 建立符號常量表示各工資等級和稅率。 #include <stdio.h> #include <stdlib.h> #define MAXT 40.0 #define GH 10.0 #define FR 300.0 #define SR 450.0 #define FTR 0.15 #define STR 0.20 #define TTR 0.25
int main(void) { float t, sum, tax, gain; scanf("%f", &t); if(t > MAXT) { t = MAXT + (t - MAXT) * 1.5; } float gh; int num, f = 0, x = 1; printf("***************************************************************** \n"); printf("Enter the number corresponding to the desired pay rate or action: \n"); printf("1) $8.75/hr 2) $9.33/hr\n3) $10.00/hr 4) $11.20/hr\n5) quit \n"); printf("***************************************************************** \n"); scanf("%d", &num); while(x) { switch(num) { case 1: gh = 8.75; x = 0; break; case 2: gh = 9.33; x = 0; break; case 3: gh = 10.00; x = 0; break; case 4: gh = 11.20; x = 0; break; case 5: return 0; break; default: printf("input from 1 to 5:"); scanf("%d", &num); } } sum = gh * t; if(sum <= FR) { tax = sum * FTR; gain = sum - tax; } else if(sum > FR && sum < SR) { tax = FR * FTR + (sum - FR) * STR; gain = sum - tax; } else if(sum > SR) { tax = FR * FTR + (SR - FR) * STR + (sum - SR) * TTR; gain = sum - tax; } printf("%-10.2f%-10.2f%-10.2f%-10.2f\n", t, sum, gain, tax); return 0; }