一
// 编写一个程序读取输入,读到#字符停止,然后报告读取的空格数、换行数
// 和所有其他字符的数量。
#include"stdio.h"
#include"ctype.h"//字符函数
int main(void)
{
int la=0,ua=0,ss=0,other=0,speas=0,nest=0;//大写、小写、数字、其他、空格、换行
printf("please enter everything of you like\n");
char ch;
while((ch=getchar())!='#')
{
if(islower(ch))
la++;
else if(isupper(ch))
ua++;
else if(isdigit(ch))
ss++;
else if(ch==' ')
speas++;
else if(ch=='\n')
nest++;
else
other++;
二
// 编写一个程序读取输入,读到#字符停止。程序要打印每个输入的字符以及对应
// 的ASCII码(十进制)。每行打印8个“字符-ASCII码”组合,建议:使用字符计数
// 和求模运算符(%)在每8个循环周期时打印一个换行符。
#include"stdio.h"
int main(void)
{
int a=0;
char ch;
printf("please enter some character\n");
while((ch=getchar())!='#')
{
printf("%5c",ch);
printf("%5d",ch);
a++;
if(a%8==0)
printf("\n");
}
return 0;
}
三
// 编写一个程序,读取整数直到用户输入0.输入结束后,程序应报告用户输入
// 的偶数(不包括0)个数、这些偶数的平均值、输入的奇数个数及其奇数的
// 平均数。
#include<stdio.h>
int main(void)
{
int a;
int o=0,j=0;
int sumo=0;
int sumj=0;
printf("Please enter some number.\n");
while(scanf("%d",&a)==1)
{
if (a==0)
{
break;
}
if(a % 2 ==0)
{ o++;
sumo=sumo+a;
}
if(a % 2 ==1)
{ j++;
sumj=sumj+a;
}
}
printf("偶数个数有%d,平均数为%f\n",o,sumo/(float)o);
printf("奇数个数有%d,平均数为%f",j,sumj/(float)j);
return 0;
}
四
// 使用if else语句编写一个程序读取输入,读到#停止。用感叹号替换句号
// 用两个感叹号替换原来的感叹号。最后报告进行了多少次替换。
#include <stdio.h>
int main(void)
{
int a=0;
char characters;
printf("Please enter some characters.\n");
scanf("%c",&characters);
while(characters!='#')
{
if (characters=='.')
{
putchar(characters-13);
a++;
}
else if (characters=='!')
{
putchar('!');
putchar('!');
a++;
}
else
putchar(characters);
scanf("%c",&characters);
}
printf("It was replaced %d times\n",a);
return 0;
}
五
// 使用switch重写4.
#include <stdio.h>
int main(void)
{
int a;
char ch;
ch=getchar();
while(ch!='#')
{
switch (ch)
{
case '.':
putchar('!');
a++;
break;
case '!':
putchar('!');
putchar('!');
a++;
break;
default :
putchar(ch);
}
ch=getchar();
}
printf("It was replaced %d times\n",a);
return 0;
}
六
// 编写程序读取输入,读到#停止,报告ei出现次数。
// 注意:该程序要记录前一个字符和当前字符,用“Receive your eieio award”
// 这样的输入来测试;
#include <stdio.h>
int main(void)
{
char ch,chs;
int a=0;
ch=getchar();
while(ch!='#')
{
if (ch=='i'&&chs=='e')
{
a++;
}
chs=ch;
ch=getchar();
}
printf("It was replaced %d times\n",a);
return 0;
}
七
// 编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额
// 税金和净收入。做如下假设:
// a.基本工资 = 10.00美元/小时
// b.加班(超过40小时)= 1.5背时间
// c.税率:前300美元为15%
// 续150美元为20%
// 余下的为25%
// 用#define定义符号常量。不用在意是否符号当前的税法。
#include <stdio.h>
#define BASE 10
#define UPTIME 40
#define EIMES 1.5
#define TAX1 0.15
#define TAX2 0.20
#define TAX3 0.25
int main(void)
{
float Total,time,tax,income;
printf("Please enter your working hours.\n");
scanf("%f",&time);
if (time<=40)
{
Total=time*BASE;
if (Total<=300)
tax=Total*TAX1;
else
tax=300*TAX1+(Total-300)*0.2;
income=Total-tax;
}
else if (time>40)
{
Total=(time-UPTIME)*BASE;
Total+=(time-UPTIME)*1.5*BASE;
if (Total<=450)
tax=300*TAX1+(Total-300)*0.2;
else
tax=300*TAX1+150*TAX3+(Total-450)*0.25;
income=Total-tax;
}
printf("Total wages:%f\n",Total);
printf("Taxes:%f\n",tax);
printf("Net income:%f\n",income);
return 0;
}
八
这一题虽然没有错误,但是代码写的不好,仅供参考
// 修改练习7的假设啊,让程序可以给出一个供选择的工资等级菜单,
// 使用switch完成工资等级选择。运行程序后,显示的菜单应该类似这样:
// *********************************************************************
// Enter the number corresponding to the desired pay rate or action:
// 1)$8.75/hr 2)$9.33/hr
// 3)$10.00/hr 4)$11.20/hr
// 5)quit
// **********************************************************************
// 如果选择1~4其中的一个数字,程序应该询问用户工作的小时数。程序要通过循环运行
// 除非用户输入5。如果1~5以外的数字,程序应该提醒用户输入正确的选项,然后重复
// 显示菜单提示用户输入。使用#define创建符号常量表示各工资等级和税率。
#include <stdio.h>
#define UPTIME 40
#define EIMES 1.5
#define TAX1 0.15
#define TAX2 0.20
#define TAX3 0.25
int main(void)
{
float base,Total,time,tax,income;
int num;
printf("Please select according to the menu below.\n");
printf("*****************************************\n\n");
printf("1)$8.75/hr 2)$9.33/hr\n");
printf("3)$10.00/hr 4)$11.20/hr\n5)quit\n");
printf("*****************************************\n");
while(scanf("%d",&num)==1)
{
if(num > 5 || num < 1)
{
printf("Please enter the correct option\n");
printf("*****************************************\n\n");
printf("1)$8.75/hr 2)$9.33/hr\n");
printf("3)$10.00/hr 4)$11.20/hr\n5)quit\n");
printf("*****************************************\n");
}
switch(num)
{
case 1: base=8.75;
printf("Please enter your working hours.\n");
break;
case 2: base=9.33;
printf("Please enter your working hours.\n");
break;
case 3: base=10.00;
printf("Please enter your working hours.\n");
break;
case 4: base=11.20;
printf("Please enter your working hours.\n");
break;
case 5: goto bye;
}
scanf("%f",&time);
if (time<=40)
{
Total=time*base;
if (Total<=300)
tax=Total*TAX1;
else
tax=300*TAX1+(Total-300)*0.2;
income=Total-tax;
}
else if (time>40)
{
Total=(time-UPTIME)*base;
Total+=(time-UPTIME)*1.5*base;
if (Total<=450)
tax=300*TAX1+(Total-300)*0.2;
else
tax=300*TAX1+150*TAX3+(Total-450)*0.25;
income=Total-tax;
}
printf("Total wages:%f\n",Total);
printf("Taxes:%f\n",tax);
printf("Net income:%f\n",income);
printf("Please select according to the menu below again.\n");
printf("*****************************************\n\n");
printf("1)$8.75/hr 2)$9.33/hr\n");
printf("3)$10.00/hr 4)$11.20/hr\n5)quit\n");
printf("*****************************************\n");
}
bye:printf("Thank you.\n");
return 0;
}
九
// 编写一个程序,只接受正整数输入,然后显示所有小于或等于该数的素数。
#include <stdio.h>
int main(void)
{
int primer,num,div,i;
printf("Please enter a number.\n");
while((scanf("%d",&primer)==1)&&primer>0)
{
if (primer == 1)
{
printf("1 is not primer!\n");
continue;
}
printf("%d以内的素数有。\n",primer);
for(num=2;num<=primer;num++)
{
i=1;
for (div=2;div<num;div++)
{
if (num % div==0)
{
i=0;
break;
}
}
if(i)
printf("%3d",num);
}
}
return 0;
}
十
// 1988年的美国联邦税收计划是近代最简单的税收方案。它分为4各类别
// 每个类别有两个等级。下面是该税收计划的摘要(美元数为应征税的收入):
// 类别 税金
// 单身 17850美元按15%计,超出部分按28%计
// 户主 23900美元按15%计,超出部分按28%计
// 已婚,共有 29750美元按15%计,超出部分按28%计
// 已婚,离异 14875美元按15%计,超出部分按28%计
// 例如,一位工资为20000美元的单身纳税人,应缴纳税费
// 0.15×17850+0.25×(20000-17850)美元。编写一个程序,让用户指定缴纳税金
// 的种类和应收税收入,然后计算税金。程序应通过循环让用户可以多次输入。
#include "stdio.h"
int main(void)
{
int type;
float tax,total;
do
{
printf("Please select the tax collection type.\n");
printf("1.单身 2.户主\n");
printf("3.已婚,共有 4.已婚,离异\n5离开\n");
scanf("%d",&type);
if (type==5)
break;
printf("Please enter the income to be taxed\n");
scanf("%f",&total);
switch(type)
{
case 1: printf("For this category, 28%% is taxed"
"for those over $17850, and 15%% for the rest.\n");
break;
case 2: printf("For this category, 28%% is taxed"
"for those over $23900, and 15%% for the rest.\n");
break;
case 3: printf("For this category, 28%% is taxed"
"for those over $29750, and 15%% for the rest.\n");
break;
case 4: printf("For this category, 28%% is taxed"
"for those over $14875, and 15%% for the rest.\n");
break;
}
if (type==1)
{
if (total<=17850)
tax=total*0.15;
else
tax=17850*0.15+(total-17850)*0.28;
}
else if (type==2)
{
if (total<=23900)
tax=total*0.15;
else
tax=23900*0.15+(total-23900)*0.28;
}
else if (type==3)
{
if (total<=29750)
tax=total*0.15;
else
tax=29750*0.15+(total-29750)*0.28;
}
else if (type==4)
{
if (total<=14875)
tax=total*0.15;
else
tax=14875*0.15+(total-14875)*0.28;
}
printf("You should pay %f dollars in tax\n",tax);
} while (type<5);
printf("Thank you\n");
return 0;
}
十一
// 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"
#define TIAN 1.15
#define YANG 2.05
#define HU 1.09
int main(void)
{
float weight,sumweight=0,sumprice,caipr,jipr,bupr;
float caiwe,buwe,jiwe;
float yunfei;
float discount=0;
char ch;
printf("Please order according to the form below and press q to exit.\n");
do
{
printf("a.洋蓟 b.甜菜\n");
printf("c.胡罗卜 q.退出\n");
scanf("%c",&ch);
if (ch=='q')
break;
switch(ch)
{
case 'a': printf("The price of 洋蓟 is $2.05/pound "
"Please enter the weight ordered\n");
scanf("%f",&jiwe);
jipr=jiwe*YANG;
sumweight+=jiwe;
break;
case 'b': printf("The price of 甜菜 is $1.15/pound "
"Please enter the weight ordered\n");
scanf("%f",&caiwe);
caipr=caiwe*TIAN;
sumweight+=caiwe;
break;
case 'c': printf("The price of 胡罗卜 is $1.09/pound "
"Please enter the weight ordered\n");
scanf("%f",&buwe);
bupr=buwe*HU;
sumweight+=buwe;
break;
case 'q':
break;
}
printf("What else do you need to order\n");
}while(scanf("%c",&ch)!='q');
sumprice=bupr+caipr+jipr;
if (sumprice>100)
{
discount*=0.05;
sumprice-=discount;
}
if (sumweight<=5)
{
yunfei=6.5;
sumprice+=yunfei;
}
else if (sumprice>5&&sumprice<=20)
{
yunfei=14;
sumprice+=yunfei;
}
else
{
yunfei=14+(sumprice-20)*0.5;
sumprice+=yunfei;
}
printf("Here is your purchase information:\n");
printf("洋蓟$2.05/pound,order%.3fpound,prise is%.3f,\n",jiwe,jipr);
printf("甜菜$1.15/pound,order%.3fpound,prise is%.3f,\n", caiwe,caipr);
printf("胡罗卜$1.09/pound,order%.3fpound,prise is%.3f,\n",buwe,bupr);
printf("Cost of ordering vegetables %.3f\n",bupr+caipr+jipr);
if (discount>0)
{ printf("Because the cost of vegetables is more than 100,"
"the discount is 0.5%.\n");
printf("So cost of ordering vegetables %.3f\n",bupr+caipr+jipr-discount);
}
printf("Freight and packing %.3f\n",yunfei);
printf("Total cost %.3f\n",sumprice);
return 0;
}