6.编写程序输入,读到#停止,报告ei出现的次数。
#include
#define STOP '#'
int main()
{
int count=0;
char ch,ch1;
while ((ch=getchar()) !=STOP)
{
if (ch=='e')
{
ch1=ch;
continue;
}
if (ch1=='e' && ch=='i') count++;
else break;
}
printf("%d",count);
}
9.编写一个程序,只接受正整数输入,然后显示所有小于等于该数的素数。
#include
#include
int main()
{
bool isPrime;
int i;
unsigned number;
printf("请输入一个正整数:\n");
while (scanf("%u",&number)==1)
{
for (;number>1;number--)
{
for (i=2,isPrime=true;i<=number-1;i++)
{
if (number%i==0)
isPrime=false;
}
if (isPrime)
printf("%u是一个素数。\n",number);
}
printf("输入非字符退出循环.\n");
}
printf("Bye.\n");
return 0;
}
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退出订购。程序要记录累计的重量。然后,该程序要计算货物总价、折扣(如果有的话)、运费和包装费。随后,程序要显示所有的购买信息:物品售价、订购的重量(磅)、订购的蔬菜费用、订单的总费用、折扣(if you)、运费和包装费、以及所有的费用总额。
#include
#include
#define thistle 2.05 //鲜蓟
#define sugar 1.15 //甜菜
#define carrots 1.09 //胡萝卜
#define RANGE1 5 //范围1
#define RANGE2 20 //..2
#define preferential 0.05 //优惠
#define money1 6.5
#define money2 14
#define over 0.5
int main()
{
double carriage,money,all,all_amount,discount,finally_money;
double cost_a,cost_b,cost_c;
char ch;
double pound,amount_a,amount_b,amount_c;
printf("请输入你要购买的蔬菜类型:\n");
while ((ch=getchar()) !='q' && ch !='Q')
{
if (isspace(ch))
continue;
while (getchar() !='\n')
continue;
ch=tolower(ch);
switch (ch)
{
case 'a':
{
printf("请输入鲜蓟的磅数:\n");
scanf("%lf",£);
amount_a +=pound;
break;
}
case 'b':
{
printf("请输入甜菜的磅数:\n");
scanf("%lf",£);
amount_b +=pound;
break;
}
case 'c':
{
printf("请输入胡萝卜的磅数:\n");
scanf("%lf",£);
amount_c +=pound;
break;
}
default :printf("请输入字符a,b,c,q。\n");break;
}
printf("a将购买鲜蓟,b将购买甜菜,c将购买胡萝卜.\n");
}
cost_a=thistle*amount_a;
cost_b=sugar*amount_b;
cost_c=carrots*amount_c;
all=cost_c+cost_b+cost_a;
all_amount=amount_a+amount_b+amount_c;
if (all_amount<=0)
carriage=0;
else if (all_amount<=RANGE1)
carriage=money1;
else if (all_amount<=RANGE2)
carriage=money2;
else
carriage=money2+over*all_amount;
if (all>100)
discount=preferential*all;
else
discount=0.0;
finally_money=all+carriage-discount;
printf("*****************************************\n");
printf("你的购物清单:\n");
printf("售价为%.2f的鲜蓟有%.2f 磅,总价为:%.2f\n",thistle,amount_a,cost_a);
printf("售价为%.2f的甜菜有%.2f 磅,总价为:%.2f\n",sugar,amount_b,cost_b);
printf("售价为%.2f的胡萝卜有%.2f 磅,总价为:%.2f\n",carrots,amount_c,cost_c);
printf("订单蔬菜的总费用:%.2f\n",all);
if (all>100)
printf("你的这次购物折扣有%.2f\n",discount);
printf("运费和包装费:%.2f\n",carriage);
printf("你的这次购物所有的费用总额%.2f\n",finally_money);
printf("*****************************************\n");
return 0;
}