/** Module Name: Description:记录空格数、换行符数和全部其余字符的数量。 Author:miaoyuting Created:2019-11-08 Last Change: Functions: */ #include <stdio.h> int main(void) { char ch; int i=0,j=0,k=0; ch=getchar(); while(ch!='#'){ if(ch==' '){ i++; } else if(ch=='\n'){ j++; } else{ k++; } ch=getchar(); } printf("%d,%d,%d",i,j,k); return 0; }
/** Module Name: 字符转ASCII码 Description: Author:miaoyuting Created:2019-11-08 Last Change: 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: Author:miaoyuting Created:20191108 Last Change: Functions: */ #include <stdio.h> int main(void) { int number; int os=0,js=0; float osave=0,jsave=0; printf("please input nimber\n"); scanf("%d",&number); while(number!=0){ if(number%2==0){ os++; osave+=number; }else{ js++; jsave+=number; } printf("please input number\n"); scanf("%d",&number); } osave/=os; jsave/=js; printf("you have input %d 偶数,average is %f\n",os,osave); printf("you have input %d 奇数,average is %f\n",js,jsave); return 0; }
/** Module Name: 感叹号替换句号,用两个感叹号替换原来的感叹号 Description: Author:miaoyuting Created:2019-11-08 Last Change: Functions: */ #include <stdio.h> int main(void) { char ch; int i=0,j=0; ch=getchar(); while(ch!='#'){ if(ch=='.'){ putchar('!'); i++; } else if(ch=='!'){ putchar('!'); putchar('!'); j++; } else{ putchar(ch); } ch=getchar(); } printf("\n"); printf(".替换了%d次,!替换了%d次",i,j); return 0; }
注意 该程序要记录前一个字符和当前字符。用“Receive your eieio award”这样的输入来测试。post
/** Module Name: 记录ei的个数 Description: Author:miaoyuting Created:2019-11-08 Last Change: 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定义符号常量。不用在乎是否符合当前的税法。测试
/** Module Name: 计算税率 Description: Author:miaoyuting Created:2019-11-08 Last Change: 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() { int hours,gross,net,taxes; 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("工资总额:%d;税金:%d;净收入:%d",gross,taxes,net); return 0; }
/** Module Name: Description:程序,只接受正整数输入,显示小于或等于该数的全部素数 Author:miaoyuting Created:2019-11-08 Last Change: Functions: */ #include <stdio.h> #include <math.h> #include <stdbool.h> int main(void) { int limit,num,div; bool numIsPrime; printf("please input a postive interger\n"); while(scanf("%d",&limit)==1 && limit>=0){ if(limit>1){ printf("here are the prime number upthrough%d\n",limit); } else{ printf("no prime!\n"); } for(num=2;num<=limit;num++){ for(div=2,numIsPrime=true;div<=sqrt(num);div++){ if(num%div==0){ numIsPrime=false; } } if(numIsPrime){ printf("%d is prime\n",num); } } } return 0; }
/** Module Name: 税金计算 Description: Author:miaoyuting 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:miaoyuting 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:miaoyuting 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:ui
$8.75/hr 2) $9.33/hrthis
$10.00/hr 4) $11.20/hrcode
quitip
若是选择 1~4 其中的一个数字,程序应该询问用户工做的小时数。程 序要经过循环运行,除非用户输入 5。若是输入 1~5 之外的数字,程序应 提醒用户输入正确的选项,而后再重复显示菜单提示用户输入。使用#define 建立符号常量表示各工资等级和税率。ci
/** Module Name: 计算税率 Description: Author:miaoyuting 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; }