/** Module Name: Description:編寫一個程序讀取輸入,讀到 #字符中止,而後報告讀取的空格數、換行符數和全部其餘字符的數量。 Author:zhuyan Created:2019-11-8 Last Change:2019-11-8 Functions: */ #include<stdio.h> #include<ctype.h> int main(void) { char ch; int spaceCount=0,enterCount=0,otherCount=0; ch=getchar(); while(ch!='#'){ if(isspace(ch)){ spaceCount++; } else if(ch!='\n'){ enterCount++; } else{ otherCount++; } ch=getchar(); } printf("%d,%d,%d\n",spaceCount,enterCount,otherCount); return 0; }
/** Module Name: Description:編寫一個程序讀取輸入,讀到#字符中止。程序要打印每一個輸入的字符以及對應的ASCII碼(十進制)。一行打印8個字符。建議:使用字符計數和求模運算符(%)在每8個循環週期時打印一個換行符。 Author:zhuyan Created:2019-11-8 Last Change:2019-11-8 Functions: */ #include<stdio.h> int main(void) { char ch; int chCount=0; ch=getchar(); while(ch!='#'){ printf("ch:%c,Ascii:%d;",ch,ch); chCount++; if(chCount%8==0);{ printf("\n"); } ch=getchar(); } return 0; }
/** Module Name: Description:編寫一個程序,讀取整數直到用戶輸入0.輸入結束後,程序應報告用戶輸入的偶數(不包括0)個數、這些偶數的平均值、輸入的奇數個數及其奇數的平均數。 Author:zhuyan Created:2019-11-8 Last Change:2019-11-8 Functions: */ #include<stdio.h> int main(void) { char number,a=0,b,c=0,d,e,f; printf("please input some integers\n"); number=getchar(); while(number=!'0'){ if(number%2==0){ a++,b==b+number,e=b/a; } else{ c++,d==d+number,f=d/c; } number=getchar(); } printf("偶數的個數爲%d\n偶數的平均值爲%d\n奇數的個數爲%d\n奇數的平均值爲%d\n",a,b,e,f); return 0; }
/** Module Name: Description:使用if else語句編寫一個程序讀取輸入,讀到#中止。用感嘆號替換句號,用兩個感嘆號替換原來的感嘆號,最後報告進行了多少次替換。 Author:zhuyan Created:2019-11-8 Last Change:2019-11-8 Functions: */ #include<stdio.h> int main(void) { char ch; int ct1=0,ct2=0; while((ch=getchar())!='#'){ switch(ch){ case '.': putchar('!'); ct1++; break; case '!': putchar('!'); putchar('!'); ct2++; break; default: putchar(ch); } } printf("%d replacement(s) of . with !\n",ct1); printf("%d replacement(s) of !with !!\n",ct2); return 0; }
/** Module Name: Description:記錄ei的個數 Author:zhuyan Created:2019-11-8 Last Change:2019-11-8 Functions: */ #include<stdio.h> int main(void) { char ch; int j=0; char ch1="",ch2; ch=getchar(); while(ch!='#'){ ch2=ch; if(ch1=='e'&&ch2=='i'){ j++; } ch1=ch2; ch=getchar(); } printf("出現%d次ei",j); return 0; }
a.基本工資 = 1000美圓/小時 b.加班(超過40小時) = 1.5倍的時間 c.稅率: 前300美圓爲15% 續150美圓爲20% 餘下的爲25% 用#define定義符號常量。不用在乎是否符合當前的稅法。c++
/** Module Name: Description:編寫一個程序,提示用戶輸入一週工做的小時數,而後打印工資總額、稅金和淨收入。 Author:zhuyan Created:2019-11-8 Last Change:2019-11-8 Functions: */ #include<stdio.h> #define BASEPAY 10 #define BASEHRS 40 #define OVERTIME 1.5 #define AMT1 300 #define AMT2 150 #define RATE1 0.15 #define RATE2 0.20 #define RATE3 0.25 int main(void){ double hours,gross,net,taxes; printf("Enter the number of houres you worked this week\n"); scanf("%f",&hours); if(hours<=BASEHRS){ gross=hours*BASEPAY; } else{ gross=BASEHRS*BASEPAY+(hours-BASEHRS)*OVERTIME*BASEPAY; } if(gross<=AMT1){ taxes=gross*RATE1; } else if(gross<=AMT1+AMT2){ taxes=AMT1*RATE1+(gross-AMT1)*RATE2; } else{ taxes=AMT1*RATE1+AMT2*RATE2+(gross-AMT1-AMT2)*RATE3; } net=gross-taxes; printf("收入是%d\n稅收是%d\n淨收入是%d\n"); return 0; }
/** Module Name: Description:編寫一個程序,只接受正整數輸入,而後顯示全部小於或等於該數的素數。 Author:zhuyan Created:2019-11-8 Last Change:2019-11-8 Functions: */ #include<stdio.h> #include<math.h> #include<stdbool.h> int main(void) { int limit,num,div; bool numlsPrime; printf("please input a postive integer\n"); while(scanf("%d"&limit)==1&&limit>=0){ if(limit>1){ printf("here are the prime numbers upthought%d\n",limit); } else{ printf("no prine!\n"); } for (num=2;num<=limit;num++){ for(div=2;numlsPrime=true;div<=sqrt(num);div){ if(num%div==0){ numlsPrime=false; } } if(numlsPrime){ printf("%d is prime",limit); } } } return 0; }
/** Module Name: 稅金計算 Description: Author:xzb Created:2019-11-08 Last Change: Functions: */ #include <stdio.h> #define RATE1 0.15 #define RATE2 0.28 #define BASEHRS1 17850 #define BASEHRS2 23900 #define BASEHRS3 29750 #define BASEHRS4 14875 int main(void) { int gross,net; int type; printf("1)單身 2)戶主\n3)已婚,共有 4)已婚,離異\n"); scanf("%d",&type); printf("請輸入你的工資\n"); scanf("%d",&gross) ; while(type!=0){ switch(type){ case 1: if(gross<=BASEHRS1){ net=gross*RATE1; } else{ net=BASEHRS1*RATE1+(gross-BASEHRS1)*RATE2; } break; case 2: if(gross<=BASEHRS2){ net=gross*RATE1; } else{ net=BASEHRS2*RATE1+(gross-BASEHRS2)*RATE2; } break; case 3: if(gross<=BASEHRS3){ net=gross*RATE1; } else{ net=BASEHRS3*RATE1+(gross-BASEHRS3)*RATE2; } break; case 4: if(gross<=BASEHRS4){ net=gross*RATE1; } else{ net=BASEHRS4*RATE1+(gross-BASEHRS4)*RATE2; } break; } printf("應繳納稅金:%d\n",net); printf("1)單身 2)戶主\n3)已婚,共有 4)已婚,離異\n"); scanf("%d",&type); printf("請輸入你的工資\n"); scanf("%d",&gross) ; } return 0; }
/** Module Name: 菜價計算 Description: Author:xzb Created:2019-11-08 Last Change: Functions: */ #include <stdio.h> #define PRICE1 2.05 #define PRICE2 1.15 #define PRICE3 1.09 #define FREIGHT1 6.5 #define FREIGHT2 14 #define FREIGHT3 0.5 int main(void) { int net1=0,net2=0,net=0,discount=0,freight=0; int weight1=0,weight2=0,weight3=0,weight=0; int type; printf("1)洋薊 2)甜菜\n3)胡蘿蔔 4)退訂\n"); scanf("%d",&type); while(type!=4){ switch(type){ case 1: printf("輸入重量:\n"); scanf("%d",&weight1); break; case 2: printf("輸入重量:\n"); scanf("%d",&weight2); break; case 3: printf("輸入重量:\n"); scanf("%d",&weight3); break; } printf("1)洋薊 2)甜菜\n3)胡蘿蔔 4)退訂\n"); scanf("%d",&type); } net1=weight1*PRICE1+weight2*PRICE2+weight2*PRICE3; if(net1<=100){ net2=net2; } else{ discount=net1*0.05; net2=net1-discount; } weight=weight1+weight2+weight3; if(weight<=5){ freight=FREIGHT1; } else if(weight<=20){ freight=FREIGHT2; } else{ freight=FREIGHT2+(weight-20)*FREIGHT3; } net=net2+freight; printf("洋薊售價爲 2.05 美圓/磅,甜菜售價爲 1.15美圓/磅\n胡蘿蔔售價爲 1.09美圓/磅"); printf("洋薊:%d磅,甜菜:%d磅,胡蘿蔔:%d磅\n",weight1,weight2,weight3); printf("訂購的蔬菜費用%d,訂單的總費用%d,折扣%d\n",net1,net2,discount); printf("運費和包裝費%d,全部的費用總額%d\n",freight,net); return 0; }
/** Module Name: 同4 Description: Author:xzb Created:2019-11-08 Last Change: Functions: */ #include <stdio.h> int main(void) { char ch; int ct1=0,ct2=0; while((ch=getchar())!='#'){ switch(ch){ case'.': putchar('!'); ct1++; break; case'!': putchar('!'); putchar('!'); ct2++; break; default: putchar(ch); } } printf("\n%d replacement(s) of . with\n",ct1); printf("%d replacement(s) of ! with\n",ct2); return 0; }
Enter the number corresponding to the desired pay rate or action:post
$8.75/hr 2) $9.33/hrui
$10.00/hr 4) $11.20/hrthis
quitspa
若是選擇 1~4 其中的一個數字,程序應該詢問用戶工做的小時數。程 序要經過循環運行,除非用戶輸入 5。若是輸入 1~5 之外的數字,程序應 提醒用戶輸入正確的選項,而後再重複顯示菜單提示用戶輸入。使用#define 建立符號常量表示各工資等級和稅率。code
/** Module Name: 計算稅率 Description: Author:xzb Created:2019-11-08 Last Change: Functions: */ #include<stdio.h> #define BASEHRS 40 #define OVERTIME 1.5 #define AMT1 300 #define AMT2 150 #define RATE1 0.15 #define RATE2 0.20 #define RATE3 0.25 int main() { int hours; float gross,net,taxes,BASEPAY; int type; printf("1) $8.75/hr 2) $9.33/hr\n3) $10.00/hr 4) $11.20/hr\n5) quit\n"); scanf("%d",&type); while(type!=5){ switch(type){ case 1: BASEPAY=8.75; break; case 2: BASEPAY=9.33; break; case 3: BASEPAY=10.00; break; case 4: BASEPAY=11.20; break; } printf("Enter the number of hours you worked this week\n"); scanf("%d",&hours); if(hours<=BASEHRS){ gross=hours*BASEPAY; } else{ gross=BASEHRS*BASEPAY+(hours-BASEHRS)*OVERTIME*BASEPAY; } if(gross<=AMT1){ taxes=gross*RATE1; } else if(gross<=AMT1+AMT2){ taxes=AMT1*RATE1+(gross-AMT1)*RATE2; } else{ taxes=AMT1*RATE1+AMT2*RATE2+(gross-AMT1)*RATE3; } net=gross-taxes; printf("工資總額:%.2f;稅金:%.2f;淨收入:%.2f\n",gross,taxes,net); printf("1) $8.75/hr 2) $9.33/hr\n3) $10.00/hr 4) $11.20/hr\n5) quit\n"); scanf("%d",&type); } return 0; }