- 编写一个程序读取输入,读到#字符停止,然后报告读取的空格数、 换行符数和所有其他字符的数量。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
int space=0;
int line_break=0;
int other=0;
while( (ch=getchar())!='#' )
{
if(ch=='\n')
line_break=line_break+1;
else if(ch==' ')
space=space+1;
else
other++;
}
printf("You enter %d space, %d line break and %d other character%s",
space, line_break, other, (other==0 || other==1)?".":"s.");
return 0;
}
2. 编写一个程序读取输入,读到#字符停止。程序要打印每个输入的字 符以及对应的ASCII码(十进制)。一行打印8个字符。建议:使用字符计数 和求模运算符(%)在每8个循环周期时打印一个换行符。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
int i=0;
while( (ch=getchar())!='#' ){
if(ch!='\n' && ch!=' ')
{ printf("%5c-%5d ",ch,ch);
i++;
if((i%8)==0)
printf("\n");}
else
continue;
}
return 0;
}
3. 编写一个程序,读取整数直到用户输入 0。输入结束后,程序应报告 用户输入的偶数(不包括 0)个数、这些偶数的平均值、输入的奇数个数及 其奇数的平均值
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number;
int odd_sum=0;
int odd_count=0;
int even_sum=0;
int even_count=0;
printf("Enter integers:\n");
scanf("%d",&number);
while(number!=0){
if( (number%2)==0 )
{
even_count++;
even_sum+=number;
}
else
{
odd_count++;
odd_sum+=number;
}
scanf("%d",&number);
}
if(odd_count==0)
printf("We have no odd integer\n");
else
printf("We have %d odd integer and the average value is %.2f\n",odd_count,(float)odd_sum/odd_count);
if(even_count==0)
printf("We have no even integer\n");
else
printf("We have %d even integer and the average value is %.2f\n",even_count,(float)even_sum/even_count);
return 0;
}
4. 使用if else语句编写一个程序读取输入,读到#停止。用感叹号替换句号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
int count=0;
printf("Enter characters:");
while((ch=getchar())!='#')
{
if(ch=='!')
{printf("%c%c",ch,ch);
count++;}
else if(ch=='.')
{ ch='!';
count++;
printf("%c",ch);}
else
printf("%c",ch);
}
printf("\n %d substitutions were made",count);
return 0;}
5. 使用switch重写练习4
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
int count=0;
printf("Enter characters:");
while((ch=getchar())!='#')
{
switch(ch){
case '!' :
printf("%c%c",ch,ch);
count++;
break;
case '.':
ch='!';
count++;
printf("%c",ch);
break;
default:
printf("%c",ch);
}
}
printf("\n %d substitutions were made",count);
return 0;}
6. 编写程序读取输入,读到#停止,报告ei出现的次数。注意该程序要记录前一个字符和当前字符。用“Receive your eieio award”这样的输入来测试。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
char pre_ch='#';
int count=0;
ch=getchar();
while(ch!='#')
{
if(pre_ch=='e' && ch=='i')
count++;
pre_ch=ch;
ch=getchar();
}
printf("%d ei is found.",count);
return 0;
}
7. .编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额,税金和净收入。做如下假设:
a.基本工资 = 10.00美元/小时
b.加班(超过40小时) = 1.5倍的时间
c.税率: 前300美元为15% 续150美元为20% ,余下的为25% 用#define定义符号常量。不用在意是否符合当前的税法。
#include <stdio.h>
#include <stdlib.h>
int main()
{
float wage;
float time;
float tax;
float income;
float base=10.00; //$ per h
float overtime_rate=1.5; //1.5h
float tax_300=0.15;
float tax_150=0.2;
float tax_left=0.25;
printf("input your work time:\n");
scanf("%f",&time);
if( time<=40&&time>0)
wage=base*time;
else
wage=400.0+overtime_rate*base*(time-40);
if(wage<=300)
tax=wage*tax_300;
else if(wage>300 && wage<=450)
tax=300*tax_300+(wage-300)*tax_150;
else if(wage>450)
tax=300*tax_300+150*tax_150+(wage-450)*tax_left;
income=wage-tax;
printf("wage=%.2f\ntax=%.2f\nincome=%.2f\n",wage,tax,income);
return 0;
}
8. 修改练习7的假设a,让程序可以给出一个供选择的工资等级菜单。使 用switch完成工资等级选择。运行程序后,显示的菜单应该类似这样:
如果选择 1~4 其中的一个数字,程序应该询问用户工作的小时数。程序要通过循环运行,除非用户输入 5。如果输入 1~5 以外的数字,程序应提醒用户输入正确的选项,然后再重复显示菜单提示用户输入。使用#define 创建符号常量表示各工资等级和税率。
#include <stdio.h>
#include <stdlib.h>
void calculate(float basic,int time);
int main()
{
int choice;
int time;
float basic_payment;
printf("Enter the number corresponding to the desired pay rate or action");
printf("\n1)$8.75/hr 2)$9.33/hr");
printf("\n3)$10.00/hr 2)$11.20/hr");
printf("\n5)quit\n\n");
while( ( scanf("%d",&choice) ) !=5 )//选
{
switch(choice){
case 1: basic_payment=8.75;
break;
case 2: basic_payment=9.33;
break;
case 3: basic_payment=10.00;
break;
case 4: basic_payment=11.20;
break;
case 5: goto quit;
default:
printf("\nPlease enter a valid number:\n");
printf("Enter the number corresponding to the desired pay rate or action");
printf("\n1)$8.75/hr 2)$9.33/hr");
printf("\n3)$10.00/hr 2)$11.20/hr");
printf("\n5)quit\n\n");
continue;
}
printf("Please enter your work time:\n ");
scanf("%d",&time);
calculate(basic_payment,time);
printf("Enter again?");
printf("\n1)$8.75/hr 2)$9.33/hr");
printf("\n3)$10.00/hr 2)$11.20/hr");
printf("\n5)quit\n\n");
}
quit: printf("\nDone!");
return 0;
}
//算
void calculate(float basic,int time){
float wage;
float tax;
float income;
float overtime_rate=1.5; //1.5h
float tax_300=0.15;
float tax_150=0.2;
float tax_left=0.25;
if( time<=40&&time>0)
wage=basic*time;
else
wage=400.0+overtime_rate*basic*(time-40);
if(wage<=300)
tax=wage*tax_300;
else if(wage>300 && wage<=450)
tax=300*tax_300+(wage-300)*tax_150;
else if(wage>450)
tax=300*tax_300+150*tax_150+(wage-450)*tax_left;
income=wage-tax;
printf("\nwage=%.2f tax=%.2f income=%.2f\n\n",wage,tax,income);
}
9.编写一个程序,只接受正整数输入,然后显示所有小于或等于该数的素数
#include <stdio.h>
#include <stdlib.h>
int main()
{
int isprime;
int count_prime=0;
int input;
printf("Enter an number:\n");
while(scanf("%d",&input)==1){
if(input<=0)
{printf("Continue input:\n");
continue;}
for(int num=2;num<=input;num++)
{
isprime=1;
for(int i=2;i*i<=num;i++)
{if(num%i==0)
isprime=0;}
if(isprime==1)
{ printf("%5d",num);
count_prime++;
if(count_prime%8==0)
printf("\n");}
}
}
printf("%d prime number in total.",count_prime);
return 0;
}
10. 美国税收,循环输入:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define BASIC 0.15
#define EXTRA 0.28
void show_tax(float base,float money);
int main()
{
char ch;
float base_line,money;
printf("请选择你的纳税类别:\n");
printf("a.单身 b.户主 c.已婚共有 d.已婚离异 q.退出\n");
while ( (ch=getchar())!='q' )
{
if(ch!='a' && ch!='b' && ch!='c' && ch!='d')
{printf("请输入a,b,c,d,q中的一个\n");
printf("a.单身 b.户主 c.已婚共有 d.已婚离异 q.退出\n");
while(getchar()!='\n')
continue;
continue;
}
else
switch (ch){
case 'a':
base_line=17850;
break;
case 'b':
base_line=23900;
break;
case 'c':
base_line=29750;
break;
case 'd':
base_line=14875;
break;
}
printf("请输入你的收入:\n");
scanf("%f",&money);
show_tax(base_line,money);
printf("\n");
printf("请选择你的纳税类别:\n");
printf("a.单身 b.户主 c.已婚共有 d.已婚离异 q.退出\n");
while(getchar()!='\n')
continue;
}
printf("结束!\n");
return 0;
}
void show_tax(float base,float money)
{
float tax;
if(money<=base)
tax=BASIC*money;
else
tax=BASIC*base+EXTRA*(money-base);
printf("你需要缴税%.2f美元\n",tax);
}
11. 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磅 的甜菜。然后,该程序要计算货物总价、折扣(如果有的话)、运费和包装 费。随后,程序应显示所有的购买信息:物品售价、订购的重量(单位: 磅)、订购的蔬菜费用、订单的总费用、折扣(如果有的话)、运费和包装 费,以及所有的费用总额。
#include <stdio.h>
#include <stdlib.h>
#define PRICE_CARROT 1.09
#define PRICE_BEET 1.15
#define PRICE_ARTICHOKE 2.05
int main(){
char ch;
float weight_carrot,weight_artichoke,weight_beet;
float total_c=0,total_a=0,total_b=0,total=0;
int discount=0;
float cost=0;
float cost_discounted=0;
float fare=0;
float pay=0;
printf("输入重量:A.洋蓟 B.甜菜 C.胡萝卜 ");
printf("q.退出\n");
while( (ch=getchar())!='q' )
{
switch (ch){
case 'c':
case 'C':
printf("胡萝卜重量:");
scanf("%f",&weight_carrot);
total_c+=weight_carrot;
printf("\n");
printf("输入重量:A.洋蓟 B.甜菜 C.胡萝卜 ");
printf("q.退出\n");
break;
case 'a':
case 'A':
printf("洋蓟重量:");
scanf("%f",&weight_artichoke);
total_a+=weight_artichoke;
printf("\n");
printf("输入重量:A.洋蓟 B.甜菜 C.胡萝卜 ");
printf("q.退出\n");
break;
case 'b':
case 'B':
printf("甜菜重量:");
scanf("%f",&weight_beet);
total_b+=weight_beet;
printf("\n");
printf("输入重量:A.洋蓟 B.甜菜 C.胡萝卜 ");
printf("q.退出\n");
break;
default:break;
}
}
printf(" 洋蓟总重量为%.2f 甜菜总重量为%.2f 胡萝卜总重量为:%.2f\n",
total_a, total_b, total_c);
total=total_b+total_a+total_c;
printf("总重量为%.2f\n",total);
cost=PRICE_CARROT*total_c+PRICE_ARTICHOKE*total_a+PRICE_BEET*total_b;
if(cost>=100)
{cost_discounted=0.95*cost;
discount=1;}
if(total<=5)
fare=6.5;
else if(total<=20)
fare=14;
else
fare=14+(total-20)*0.5;
pay=cost+fare;
printf("总价为%.2f,运费为%.2f,%s,需支付%.2f",
cost, fare, (discount==1)?"折扣95折":"无折扣" ,pay );
return 0;
}