1、编写一个程序。该程序读取输入直到遇到#字符,然后报告读取的空格数目、读取的换行符数目以及读取的所有其他字符数目。
#include <stdio.h>
#define STOP '#'
int main(void)
{
int space=0,line=0,other=0;
char a;
printf("Please input a string end by #:");
while ((a=getchar())!= STOP)
{
if (a==' ')
space++;
else if (a=='\n')
line++;
else
other++;
}
printf("space: %d,line: %d,other: %d",space,line,other);
return 0;
}
2、编写一个程序 ,该程序读取输入直到遇到#字符。使程序打印每个输入的字符以及它的十进制ASCII码。每行打印8个字符/编码对。建议:利用字符计数和模运算符(%)在每8个循环周期时打印一个换行符。
#include <stdio.h>
#define STOP '#'
int main(void)
{
char ch ;
int i,count;
while ((ch=getchar())!=STOP)
{
count++;
printf("%c--%d ",ch,ch);
if(count%8==0)
printf("\n");
}
printf("\n");
return 0;
}
3、编写一个程序,程序读取整数,直到输入0。输入终止后,程序应该报告输入的偶数(不包括0)总个数、偶数平均值,输入的奇数总个数以及奇数平均值。
#include<stdio.h>
int main(void)
{
int i_even = 0, sum_even = 0, i_odd = 0, sum_odd = 0, num;
printf("Please input numbers (0 to quit):");
while(scanf("%d",&num)==1 && num!=0)
{
if (num % 2 == 0) {i_even++; sum_even += num;}
else {i_odd++; sum_odd += num;}
}
printf("even number's count: %d\n",i_even);
printf("even number's sum : %d\n",sum_even);
printf("odd number's count: %d\n",i_odd);
printf("odd number's sum : %d\n",sum_odd);
return(0);
}
4、利用if else 语句编写程序读取输入,直到#。用一个感叹号代替每个句号,将原有的感叹号用两个感叹号代替,最后报告进行了多少次替代。
#include<stdio.h>
int main(void)
{
char ch ;
int a=0,b=0;
printf("Please input a string end by #:");
while ((ch=getchar())!='#')
{
if(ch=='.')
{
putchar('!');
a++;
}
else if(ch=='!')
{
putchar('!');
putchar('!');
b++;
}
else putchar(ch);
}
printf("\nthe times of '.' replaced by '!': %d\n",a);
printf("the times of '!' replaced by '!!': %d\n",b);
return(0);
}
5、利用switch重写题3。
#include<stdio.h>
int main(void)
{
int i_even = 0, sum_even = 0, i_odd = 0, sum_odd = 0, num;
printf("Please input numbers (0 to quit):");
while(scanf("%d",&num)==1 && num!=0)
{
switch (num % 2)
{ --switch下面一定要加大括号!!!
case 0:
i_even++; sum_even += num;
break;
default:
i_odd++;
sum_odd += num;
}
}
printf("even number's count: %d\n",i_even);
printf("even number's sum : %d\n",sum_even);
printf("odd number's count: %d\n",i_odd);
printf("odd number's sum : %d\n",sum_odd);
return(0);
}
6、编写一个程序读取输入,直到#,并报告序列ei出现的次数。
#include<stdio.h>
int main(void)
{
int count=0;
char ch,prev=0;
printf("Please input a string end by #:");
while ((ch=getchar())!='#')
{
if(prev=='e'&&ch=='i') /*前一个值和当前值同时满足*/
count++;
prev=ch; /*记住前一个的值*/
}
printf("ei has appeared %d times\n",count);
return 0;
}
7、编写程序要求输入一周中的工作小时数,然后打印工资、税金以及净工资。作如下假设:
a.基本工资等级=10.00美元/小时
b.加班(超过40小时)=1.5倍的时间
c.税率 前300美元为15%
下一个150美元为20%
余下的为25%
用#define定义常量,不必关心本例是否符合当前的税法。
#include<stdio.h>
//a.基本工资
#define BASIC 10.00 //基本工资等级=BASIC美元/小时
//b.加班
#define TIME 40 //加班(超过TIME小时) =
#define ADD 1.5 //ADD倍的时间
//c.税率
#define LIMIT1 300 //前LIMIT1美元为RATE1
#define RATE1 0.15
#define LIMIT2 150 //下一个LIMIT2美元为RATE2
#define RATE2 0.20
#define RATE3 0.25 //余下的位RATE3
int main(void)
{
double hours,gross,tax;
printf("input the work hours of a week:");
scanf("%lf",&hours);
if (hours > 40) hours = 40 + (hours - 40) * 1.5;
gross = hours * BASIC;
printf("gross income:\t\t%lf\n",gross);
if (gross <= LIMIT1) tax = gross * RATE1;
else if (gross <= LIMIT2) tax = LIMIT1 * RATE1 + (gross - LIMIT1) * RATE2;
else tax = LIMIT1 * RATE1 + LIMIT2 * RATE2 + (gross - LIMIT1 - LIMIT2) * RATE3;
printf("tax:\t\t\t%lf\n",tax);
printf("net income:\t\t%lf\n",gross - tax);
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) $lO.OO/hr 4) $11.20/hr
5) quit
*********************************************************************************
如果选择1-4,那么程序应该请求输入工作小时数。程序应该一直循环运行,直到输入5.如果输入1-5以外的选项,那么程序应该提醒用户合适的选项是哪些,然后再循环。用#define定义工资等级和税率常量。
#include<stdio.h>
int get_int(void);
//b.加班
#define TIME 40 //加班(超过TIME小时) =
#define ADD 1.5 //ADD倍的时间
//c.税率
#define LIMIT1 300 //前LIMIT1美元为RATE1
#define RATE1 0.15
#define LIMIT2 150 //下一个LIMIT2美元为RATE2
#define RATE2 0.20
#define RATE3 0.25 //余下的位RATE3
int main(void)
{
double basic,hours,gross,tax;
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");
printf("3) $10.00/hr\t\t\t4) $11.20/hr\n");
printf("5) quit\n");
switch(get_int())
{
case 1: basic=8.75; break;
case 2: basic=9.33; break;
case 3: basic=10.00; break;
case 4: basic=11.20; break;
default: printf("quit.\n"); return 0; //退出程序
}
printf("You select $%.2lf.\n",basic);
printf("Input the work hours of a week:");
scanf("%lf",&hours);
if (hours>40) hours=TIME+(hours-TIME)*1.5;
gross=hours*basic;
printf("Gross income:\t\t%.lf\n",gross);
if(gross<LIMIT1) tax=gross*RATE1;
else if(gross<LIMIT2) tax=LIMIT1*RATE1+(gross-LIMIT1)*RATE2;
else tax=LIMIT1*RATE1+LIMIT2*RATE2+(gross-LIMIT1-LIMIT2)*RATE3;
printf("tax:\t\t\t%lf\n",tax);
printf("net:\t\t\t%lf\n",gross-tax);
return 0;
}
int get_int(void) //得到一个合法的整数,滤除非法数
{
int num;
char str[40];
while(scanf("%d",&num)!=1)
{
gets(str);
printf("error! %s is not a number,please input a number.",str);
}
while(getchar()!='\n');
return num;
}
9、编写一个程序,接受一个整数输入,然后显示所有小于或等于这个数的素数。
#include <stdio.h>
int isprime(int);
int main(void)
{
int num,i;
printf("input a positive number:");
scanf("%d",&num);
printf("all the prime <= %d:\n",num);
for (i=2;i<=num;i++)
if(isprime(i))
printf("%d",i);
printf("\n");
return 0;
}
int isprime(int n) //如果n是素数,返回1,否则返回0
{
int div;
for (div=2;div*div<=n;div++)
if(n % div==0)
return 0;
return 1;
}
10、
10. 1988年United States Federal Tax Schedule是近期最基本的。它分为4类,每类有两个等级。下面是其摘要;美元数为应征税的收入。
┏━━━━━━┳━━━━━━━━━━━━━━━━━━┓
┃ 种 类 ┃ 税 金 ┃
┣━━━━━━╋━━━━━━━━━━━━━━━━━━┫
┃单身 ┃前17·850美元按15%,超出部分按28% ┃
┣━━━━━━╋━━━━━━━━━━━━━━━━━━┫
┃户主 ┃前23,900美元按15%,超出部分按28%┃
┣━━━━━━╋━━━━━━━━━━━━━━━━━━┫
┃已婚,共有 ┃前29,750美元按15%,超出部分按28% ┃
┣━━━━━━╋━━━━━━━━━━━━━━━━━━┫
┃已婚,离异 ┃前14,875美元按l5%,超出部分按28% ┃
┗━━━━━━┻━━━━━━━━━━━━━━━━━━┛
例如,有20 000美元应征税收入的单身雇佣劳动者应缴税金0.15×17 850美元+0.28×(20 000美元-17 850美元)。编写一个程序,让用户指定税金种类和应征税收入,然后计算税金。使用循环以便用户可以多次输入。
#include<stdio.h>
#define SINGLE 17850
#define HOST 23900
#define MARRIED_SHARE 29750
#define MARRIED_DIVORCE 14875
#define RATE1 0.15
#define RATE2 0.28
int main(void)
{
double type,pay,tax;
char ch;
while(1)
{
printf("Select the type of marriage:\n");
printf("1)SINGLE\n2)HOST\n3)MARRIED_SHARE\n4)MARRIED_DIVORCE\n5)quit\n");
while((ch = getchar()) == '\n') continue; //滤掉回车
switch(ch)
{
case '1': type = SINGLE; break;
case '2': type = HOST; break;
case '3': type = MARRIED_SHARE; break;
case '4': type = MARRIED_DIVORCE; break;
case '5': printf("quit\n"); return(0); //退出程序
default : printf("input error\n"); continue;
}
printf("you have select %c\n",ch);
printf("input the pay:");
scanf("%lf",&pay);
if (pay <= type) tax = pay * RATE1;
else tax = type * RATE1 + (pay - type) * RATE2;
printf("wax is %.2lf\n",tax);
}
}
11. ABC Mail Order Grocery朝鲜蓟的售价是1.25美元/磅,甜菜的售价是0.65美元/磅,胡萝卜的售价是0.89美元/磅。在添加运输费用之前,他们为100美元的订单提供5%的打折优惠。对5磅或以下的定单收取3.50美元的运输和装卸费用;超过5磅而不足20磅的定单收取1O.OO美元的运输和装卸费用:加磅或以上的运输,在8美元基础上每磅加收0.1美元。编写程序,在循环中使用switch语句,以便对输入a的响应是让用户输入所需的朝鲜蓟磅数,b为甜菜的磅数,c为胡萝卜的磅数,而q允许用户退出订购过程。然后程序计算总费用、折扣和运输费用(如果有运输费的话),以及总数。随后程序应该显示所有的购买信息:每磅的费用、订购的磅数、该订单每种蔬菜的费用、订单的总费用、折扣,如果有的话加上运输费用,以及所有费用的总数。
#include <stdio.h>
#include <ctype.h> //需要调用tolower()函数
#define ARTICHOKE 1.25
#define BEET 0.65
#define CARROT 0.89
#define DISCOUNT_LIMIT 100
#define DISCOUNT_RATE 0.05
#define FREIGHT_FEE1 3.50
#define FREIGHT_LIMIT1 5
#define FREIGHT_FEE2 10.00
#define FREIGHT_LIMIT2 20
#define FREIGHT_FEE3 8
#define FREIGHT_RATE 0.1
int main(void)
{
char ch ;
double artichoke=0,beet=0,carrot=0; //磅数
double sum,discount,freight;
//列示单价
printf("Please select your vegetable :a,b,c,q\n");
printf("a.artichoke price:$%.2f\n",ARTICHOKE);
printf("b.beet price:$%.2f\n",BEET);
printf("c.carrot price:$%.2f\n",CARROT);
printf("q.end\n");
printf("(price as dollars per pound)\n");
//选购菜品
while(tolower(ch=getchar())!='q')
{
switch(ch)
{
case 'a':
printf("How many pounds of artichokes do you want?");
scanf("%lf",&artichoke);
printf("Please select your vegetable :a,b,c,q:");
continue; //continue使得while循环得以继续
case 'b':
printf("How many pounds of beets do you want?");
scanf("%lf",&beet);
printf("Please select your vegetable: a,b,c,q:");
continue;
case 'c':
printf("How many pounds of carrots do you want?");
scanf("%lf",&carrot);
printf("Please select your vegetable: a,b,c,q:");
continue;
default:break;
}
}
//列示清单
printf("%10s%10s%10s%10s\n"," ","artichoke","beet","carrot");
printf("%10s%10.2lf%10.2lf%10.2lf\n","price",ARTICHOKE,BEET,CARROT);
printf("%10s%10.2lf%10.2lf%10.2lf\n","pound",artichoke,beet,carrot);
printf("%10s%10.2lf%10.2lf%10.2lf\n","charge",ARTICHOKE * artichoke,
BEET * beet,CARROT * carrot);
//合计金额
sum=ARTICHOKE * artichoke+BEET * beet+CARROT * carrot;
//计算折扣
if(sum>=DISCOUNT_LIMIT) discount=sum*DISCOUNT_RATE;
else discount=0;
printf("discount : %.2f\n",discount);
//计算运费
if(artichoke+beet+carrot<=5) freight=FREIGHT_FEE1;
else if (artichoke+beet+carrot<20) freight=FREIGHT_FEE2;
else freight = 8 + (artichoke + beet + carrot) * 0.1;
printf("freight : %.2f\n",freight);
//合计部费用
sum=sum-discount+freight;
printf("sum : %2f\n",sum);
return 0;
}