1.編寫一個程式。該程式讀取輸入直到遇到#字元,然後報告讀取的空格數目、讀取的換行符數目以及讀取的所有其他字元數目。 2.編寫一個程式。該程式讀取輸入直到遇到#字元。使程式列印每個輸入的字元以及它的十進位ASCII 碼。每行列印8 個字元/編碼對。建議:利用字元計數和模運算符(%)在每8 個迴圈周期 ...
1.編寫一個程式。該程式讀取輸入直到遇到#字元,然後報告讀取的空格數目、讀取的換行符數目以及讀取的所有其他字元數目。
#include<stdio.h> #include<ctype.h> int main(void){ char ch; int count1,count2,count3; count1 = count2 = count3 = 0;
printf("Enter text to be analyzed(#to terminate):\n"); while((ch = getchar()) != '#'){ if ( ch == '\n'){ count1++; } else if(isspace(ch)){ count2++; } else if (!isspace(ch)){ count3++; } } printf("換行數量%d,空格數量%d,其他字元數量%d",count1,count2,count3); return 0; }
2.編寫一個程式。該程式讀取輸入直到遇到#字元。使程式列印每個輸入的字元以及它的十進位ASCII 碼。每行列印8 個字元/編碼對。建議:利用字元計數和模運算符(%)在每8 個迴圈周期時列印一個換行符。
#include<stdio.h> #define STOP '#' int main(void){ char ch; int length = 1; printf("Enter text to be analyzed(#to terminate):\n"); while((ch = getchar()) != STOP){ if(length%8==0){ printf("\n"); } printf("%c/%d ",ch,ch); length++; } return 0; }
3.編寫一個程式。該程式讀取整數,直到輸入0。輸入終止後,程式應該報告輸入的偶數(不包括0)總個數、偶數的平均值,輸入的奇數總個數以及奇數的平均值。
#include<stdio.h> int main(void){ int num,even_num,odd_num; double even_count,odd_count;//必需定義為double類型,否則列印平均值會出錯 even_num = odd_num = 0; even_count = odd_count = 0.0 ; printf("Enter int to be analyzed(0 to terminate):\n"); while (scanf("%d", &num) == 1 && num != 0){ if (num%2 == 0){ even_num++; even_count +=num; } else { odd_num++; odd_count +=num; } } if (even_count > 0) printf("Even have %d, even average is %.2f\n",even_num,even_count / even_num); if (odd_num > 0) printf("Odd have %d,odd average is %.2f",odd_num,odd_count/odd_num); return 0; }
4.利用if else 語句編寫程式讀取輸入,直到#。用一個感嘆號代替每個句號,將原有的每個感嘆號用兩個感嘆號代替,最後報告進行了多少次替代。
#include<stdio.h> #define STOP '#' int main(void){ char ch; int i; i = 0; while((ch = getchar()) != STOP){ if (ch == '.'){ putchar('!'); i++; } else if (ch == '!'){ putchar(ch); putchar(ch); i++; } else { putchar(ch); } } printf("\n"); printf("count %d",i); return 0; }
5.用switch 重做練習3。
#include<stdio.h> int main(void){ int num,even_num,odd_num; double even_count,odd_count;//必需定義為double類型,否則列印平均值會出錯 even_num = odd_num = 0; even_count = odd_count = 0.0 ; printf("Enter int to be analyzed(0 to terminate):\n"); while (scanf("%d", &num) == 1 && num != 0){ switch (num%2){ case 0: even_num++; even_count +=num; break; case 1: odd_num++; odd_count +=num; break; } } if (even_count > 0) printf("Even have %d, even average is %.2f\n",even_num,even_count / even_num); if (odd_num > 0) printf("Odd have %d,odd average is %.2f",odd_num,odd_count/odd_num); return 0; }
6.編寫一個程式讀取輸入,直到#,並報告序列ei 出現的次數。說明此程式必須要記住前一個字元和當前的字元。用諸如“Receive your eieio award.”的輸入測試它。
#include<stdio.h> #define STOP '#' #define PREV_CHAR 'e' #define CURRENT_CHAR 'i' int main(void){ char ch, prev_char; int i; i = 0; prev_char = 0; printf("Enter text to be analyzed(#to terminate):\n"); while((ch = getchar()) != STOP){ if (ch == PREV_CHAR){ prev_char = ch; } if (ch == CURRENT_CHAR && prev_char == PREV_CHAR){ i++; } } printf("Enter of 'ei' %d ",i); return 0; }
7.編寫程式,要求輸入一周中的工作小時數,然後列印工資總額、稅金以及凈工資。作如下假設:
a.基本工資等級=10.00 美元/小時
b.加班(超過40 小時)=1.5 倍的時間
c.稅率前300 美元為15%
下一個150 美元為20%餘下的為25%。
用#define 定義常量,不必關心本例是否符合當前的稅法。
#include<stdio.h> #define BASE 10.00 #define TAX_LEVEL1 300 #define TAX_LEVEL2 150 #define TAX_LEVEL1_RATE 0.15 #define TAX_LEVEL2_RATE 0.2 #define TAX_LEVEL3_RATE 0.25 #define BASE_HOUR 40 #define OVER_RATE 1.5 int main(void){ float num,wage,tax,real_wage,temp,real_hour; wage = tax = real_wage = real_hour = 0.0; printf("Please enter the number of hours you work a week:\n"); scanf("%f",&num); if (num > BASE_HOUR){ real_hour = num + (num - BASE_HOUR)*OVER_RATE; } else { real_hour = num; } wage = real_hour*BASE; temp = wage - TAX_LEVEL1; if (temp <= 0){ tax += wage*TAX_LEVEL1_RATE; } else { tax +=TAX_LEVEL1*TAX_LEVEL1_RATE; if (temp<= TAX_LEVEL2){ tax += temp*TAX_LEVEL2_RATE; } else { tax += TAX_LEVEL2*TAX_LEVEL2_RATE; tax += (temp - TAX_LEVEL2) * TAX_LEVEL3_RATE; } } real_wage = wage - tax; printf("hour:%.2f wage:%.2f tax:%.2f real wage :%.2f",num,real_hour,wage,tax,real_wage); return 0; }
8.修改練習7 中的假設a,使程式提供一個選擇工資等級的菜單。用switch 選擇工資等級。程式運行的開頭應該像這樣:
*****************************************************************
Enter the number corresponding to the desired pay rate or action:
1)$8.75/hr 2)$9.33/hr
3)$10.00/hr 4)$11.20/hr
5)quit
*****************************************************************
如果選擇1 到4,那麼程式應該請求輸入工作小時數。程式應該一直迴圈運行,直到輸入5。如果輸入1 到5 以外的選項,那麼程式應該提醒用戶合適的選項是哪些,然後再迴圈。用#define 為各種工資等級和稅率定義常量。
#include<stdio.h> #define LEVEL1 8.75 #define LEVEL2 9.33 #define LEVEL3 10.00 #define LEVEL4 11.20 #define TAX_LEVEL1 300 #define TAX_LEVEL2 150 #define TAX_LEVEL1_RATE 0.15 #define TAX_LEVEL2_RATE 0.2 #define TAX_LEVEL3_RATE 0.25 #define BASE_HOUR 40 #define OVER_RATE 1.5 int main(void){ int level; float fee,num,wage,tax,real_wage,temp,real_hour; wage = tax = real_wage = real_hour = fee = 0.0; printf("*****************************************************************\n"); printf("Enter the number corresponding to the desired pay rate or action:\n"); printf("1)$8.75/hr 2)$9.33/hr\n"); printf("3)$10.00/hr 4)$11.20/hr\n"); printf("5)quit\n"); printf("*****************************************************************\n"); while(scanf("%d",&level) == 1 && level != 5){ switch(level){ case 1: fee = LEVEL1; break; case 2: fee = LEVEL2; break; case 3: fee = LEVEL3; break; case 4: fee = LEVEL4; break; default: printf("You should enter the number between 1 to 4 (5 to quit).\n"); printf("Please enter the right number: \n"); continue; } printf("Please enter the number of hours you work a week:\n"); scanf("%f",&num); if (num > BASE_HOUR){ real_hour = num + (num - BASE_HOUR)*OVER_RATE; } else { real_hour = num; } wage = real_hour*fee; temp = wage - TAX_LEVEL1; if (temp <= 0){ tax += wage*TAX_LEVEL1_RATE; } else { tax +=TAX_LEVEL1*TAX_LEVEL1_RATE; if (temp<= TAX_LEVEL2){ tax += temp*TAX_LEVEL2_RATE; } else { tax += TAX_LEVEL2*TAX_LEVEL2_RATE; tax += (temp - TAX_LEVEL2) * TAX_LEVEL3_RATE; } } real_wage = wage - tax; printf("hour:%.2f wage:%.2f tax:%.2f real wage :%.2f\n",real_hour,wage,tax,real_wage); printf("Please enter next number:\n"); } printf("exit!"); return 0; }
沒有學透getchar和scanf的區別,一開始用getchar獲取輸入,結果出錯了。這也告訴自己,對學習要有敬畏之心,看都教材不是看小說,要瞭解的是細節不是大概,馬虎不得。
9.編寫一個程式,接受一個整數輸入,然後顯示所有小於或等於該數的素數。
#include<stdio.h> #include<stdbool.h> int main(void){ int num,i,k; bool isPrime; printf("Enter int number:\n"); scanf("%d",&num); for(i=2;i<=num;i++){ for(k=2,isPrime = true;(k*k)<=i;k++){ if (i%k==0){ isPrime = false; } } if (isPrime){ printf("%lu ",i); } } return 0; }
10.1988 年United States Federal Tax Schedule 是近期最基本的。它分為4 類,每類有兩個等級。下麵是其摘要;美元數為應徵稅的收入。
種類 | 稅金 |
單身 | 前17,850 美元按15%,超出部分按28% |
戶主 | 前23,900 美元按15%,超出部分按28% |
已婚,共有 | 前29,750 美元按15%,超出部分按28% |
已婚,離異 | 前14,875 美元按15%,超出部分按28% |
例如,有20 000 美元應徵稅收入的單身雇佣勞動者應繳稅金0.15×17 850 美元+0.28×(20 000美元–17 850 美元)。編寫一個程式,讓用戶指定稅金種類和應徵稅收入,然後計算稅金。使用迴圈以便用戶可以多次輸入。
#include<stdio.h> #define TYPE1 1 #define TYPE2 2 #define TYPE3 3 #define TYPE4 4 #define TAX_RATE1 0.15 #define TAX_RATE2 0.28 #define TYPE1_BASE 17850 #define TYPE2_BASE 23900 #define TYPE3_BASE 29750 #define TYPE4_BASE 14875 int main(void){ double tax,income,base; int type; printf("請選擇稅金種類:\n"); printf("1)單身 2)戶主 3)已婚,共有 4)已婚,離異 5)退出\n"); while(scanf("%d",&type) == 1 && type != 5){ switch(type){ case TYPE1: base = TYPE1_BASE; break; case TYPE2: base = TYPE2_BASE; break; case TYPE3: base = TYPE3_BASE; break; case TYPE4: base = TYPE4_BASE; break; default: printf("請輸入1到4之間的數字,或者輸入5為退出\n"); continue; } printf("請輸入您的應徵稅收入:\n"); scanf("%lf",&income); if(income>base){ tax = base*TAX_RATE1+(income-base)*TAX_RATE2; } else { tax = income*TAX_RATE1; } printf("您共要繳納%.2lf美元稅金\n",tax); printf("請輸入您稅金種類代碼:\n"); } printf("Exit."); return 0; }
感覺和第七、八題一樣,而且更沒有難度。
11.ABC Mail Order Grocery 北韓薊的售價是1.25 美元/磅,甜菜的售價是0.65 美元/磅,胡蘿蔔的售價是0.89 美元/磅。在添加運輸費用之前,他們為100 美元的訂單提供5%的打折優惠。對5磅或以下的定單收取3.50 美元的運輸和裝卸費用;超過5 磅而不足20 磅的定單收取10.00 美元的運輸和裝卸費用;20 磅或以上的運輸,在8 美元基礎上每磅加收0.1 美元。編寫程式,在迴圈中使用switch 語句,以便對輸入a 的響應是讓用戶輸入所需的北韓薊磅數,b 為甜菜的磅數,c 為胡蘿蔔的磅數,而q 允許用戶退出訂購過程。然後程式計算總費用、折扣和運輸費用(如果有運輸費的話),以及總數。隨後程式應該顯示所有的購買信息:每磅的費用、訂購的磅數、該訂單每種蔬菜的費用、訂單的總費用、折扣,如果有的話加上運輸費用,以及所有費用的總數。
#include<stdio.h> #define ARTICHOKE 'a' #define BEET 'b' #define CARROT 'c' #define ARTICHOKE_PRICE 1.25 #define BEET_PRICE 0.65 #define CARROT_PRICE 0.89 #define SHIP_FEE_LEVEL1 3.5 #define SHIP_FEE_LEVEL2 10.00 #define SHIP_FEE_LEVEL3 0.1 #define SHIP_FEE_LEVEL3_BASE 8.0 #define WEIGHT_LEVEL1 5 #define WEIGHT_LEVEL2 20 #define DISCOUNT 0.05 #define DISCOUNT_BASE 100 int main(void){ double fee_count,ship_fee,price,order_count,discount_count, pound_count,pound,artichoke_pound,beet_pound,carrot_pound,artichoke_fee,beet_fee,carrot_fee; char type; pound_count = discount_count = fee_count = ship_fee = order_count = artichoke_pound = beet_pound = carrot_pound = 0.0; artichoke_fee = beet_fee = carrot_fee = 0.0; printf("請選擇要訂購的蔬菜:\n"); printf("a)北韓薊 b)甜菜 c)胡蘿蔔 q)退出\n"); while(scanf("%c",&type) == 1 && type != 'q'){ if( type != ARTICHOKE && type != BEET && type != CARROT ){ printf("請選擇輸入a到c之間的字母,或者輸入q退出:\n"); continue; } printf("您輸入你要訂購的磅數:"); scanf("%lf",£); pound_count += pound; switch(type){ case ARTICHOKE: artichoke_pound = pound; artichoke_fee = pound*ARTICHOKE_PRICE; break; case BEET: beet_pound = pound; beet_fee = pound*BEET_PRICE; break; case CARROT: carrot_pound = pound; carrot_fee = pound*CARROT_PRICE; break; } } if (pound_count <= WEIGHT_LEVEL1){ ship_fee = SHIP_FEE_LEVEL1; } else if(pound_count > WEIGHT_LEVEL1 && pound_count < WEIGHT_LEVEL2){ ship_fee = SHIP_FEE_LEVEL2; } else { ship_fee = SHIP_FEE_LEVEL3_BASE+ (pound_count*0.1); } fee_count = ship_fee+artichoke_fee+beet_fee+carrot_fee; if (fee_count>DISCOUNT_BASE){ discount_count = DISCOUNT_BASE*DISCOUNT; } order_count = fee_count-discount_count+ship_fee; printf("你共訂購了%.2f磅蔬菜,平均每磅%.2f美元 \n",pound_count,order_count/pound_count); if (artichoke_fee>0){ printf("訂購了北韓薊%.2f磅 共%.2f美元\n",artichoke_pound,artichoke_fee); } if (beet_fee>0){ printf("訂購了甜菜%.2f磅 共%.2f美元\n",beet_pound,beet_fee); } if (carrot_fee>0){ printf("訂購了胡蘿蔔%.2f磅 共%.2f美元\n",carrot_pound,carrot_fee); } printf("共%.2f美元 折扣優惠%.2f \n",fee_count,discount_count); if (ship_fee>0){ printf("運費共%.2f \n",ship_fee); } printf("訂單金額%.2f\n",order_count); return 0; }