/** 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; }