目录
3.下面的程序关系表达式过于复杂,而且还有些错误,请简化并改正
10.重写复习题9,但这次不能使用continue和goto语句
1.编写一个程序读取输入,读到#字符停止,然后报告读取的空格数、换行符数和所有其他字符的数量
2.编写一个程序读取输入,读到#字符停止。程序要打印每个输入的字符以及对应的ASCII码(十进制)。一行打印8个字符。建议:使用字符计数和求模运算符(%)在每8个循环周期时打印一个换行符
3.编写一个程序,读取整数直到用户输入 0。输入结束后,程序应报告用户输入的偶数(不包括 0)个数、这些偶数的平均值、输入的奇数个数及其奇数的平均值
4.使用if else语句编写一个程序读取输入,读到#停止。用感叹号替换句号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换
7.编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额、税金和净收入。做如下假设:
c.税率: 前300美元为15% 续150美元为20% 余下的为25%
Enter the number corresponding to the desired pay rate or action:
*****************************************************************
9.编写一个程序,只接受正整数输入,然后显示所有小于或等于该数的素数
10.1988年的美国联邦税收计划是近代最简单的税收方案。它分为4个类别,每个类别有两个等级。 下面是该税收计划的摘要(美元数为应征税的收入):
超过20磅的订单在14美元的基础上每续重1磅增加0.5美元。
复习题
1.判断下列表达式是true还是false
a. 100 > 3 && 'a'>'c'
b. 100 > 3 || 'a'>'c'
c. !(100>3)
a. false
b. true
c. false
2.根据下列描述的条件,分别构造一个表达式:
a number等于或大于90,但是小于100
b ch不是字符q或k
c number在1~9之间(包括1和9),但不是5
d number不在1~9之间
a. 90 <= number && number < 100
b. ch != 'q' && ch != 'k'
c. number >= 1 && number <= 9 && number != 5
d. number < 1 && number > 9
3.下面的程序关系表达式过于复杂,而且还有些错误,请简化并改正
#include <stdio.h>
int main(void) /* 1 */ { /* 2 */
int weight, height; /* weight以磅为单位,height以英寸为单位 *//* 4 */
scanf("%d , weight, height); /* 5 */
if (weight < 100 && height > 64) /* 6 */
if (height >= 72) /* 7 */
printf("You are very tall for your weight.\n");
else if (height < 72 &&> 64) /* 9 */
printf("You are tall for your weight.\n");/* 10 */
else if (weight > 300 && !(weight <= 300) /* 11 */
&& height < 48) /* 12 */
if (!(height >= 48)) /* 13 */
printf(" You are quite short for your weight.\n");
else /* 15 */
printf("Your weight is ideal.\n"); /* 16 */ /* 17 */
return 0;
}
#include <stdio.h>
int main(void)
{
int weight, height; /* weight以磅为单位,height以英寸为单位 */
scanf("%d %d", &weight, &height);
if (weight < 100 && height > 64)
if (height >= 72)
printf("You are very tall for your weight.\n");
else /* 9 */
printf("You are tall for your weight.\n");
else if (weight > 300 && height < 48)
printf(" You are quite short for your weight.\n");
else
printf("Your weight is ideal.\n");
return 0;
}
4.下列个表达式的值是多少
a.5 > 2
b.3 + 4 > 2 && 3 < 2
c.x >= y || y > x
d.d = 5 + ( 6 > 2 )
e.'X' > 'T' ? 10 : 5
f.x > y ? y > x : x > y
a. 1 b. 0 c. 1 d. 6 e. 10 f. 0
5.下面的程序将打印什么
#include <stdio.h>
int main(void)
{
int num;
for (num = 1; num <= 11; num++)
{
if (num % 3 == 0)
putchar('$');
else
putchar('*');
putchar('#');
putchar('%');
}
putchar('\n');
return 0;
}
*#%*#%$#%*#%*#%$#%*#%*#%$#%*#%*#%
6.下面的程序将打印什么
#include <stdio.h>
int main(void)
{
int i = 0;
while (i < 3)
{
switch (i++)
{
case 0:
printf("fat ");
case 1:
printf("hat ");
case 2:
printf("cat ");
default:
printf("Oh no!");
}
putchar('\n');
}
return 0;
}
fat hat cat Oh no!
hat cat Oh no!
cat Oh no!
7.下面的程序有哪些错误
#include <stdio.h>
int main(void)
{
char ch;
int lc = 0; // 统计小写字母
int uc = 0; // 统计大写字母
int oc = 0; // 统计其他字母
while ((ch = getchar()) != '#')
{
if ('a' <= ch >= 'z')
lc++;
else if (!(ch < 'A') || !(ch > 'Z')
uc++;
oc++;
}
printf(%d lowercase, %d uppercase, %d other, lc, uc, oc);
return 0;
}
#include <stdio.h>
int main(void)
{
char ch;
int lc = 0; // 统计小写字母
int uc = 0; // 统计大写字母
int oc = 0; // 统计其他字母
while ((ch = getchar()) != '#')
{
if ('a' <= ch && ch <= 'z')
lc++;
else if (!(ch < 'A') && !(ch > 'Z')
uc++;
else:
oc++;
}
printf("%d lowercase, %d uppercase, %d other", lc, uc, oc);
return 0;
}
8.下面的程序将打印什么
#include <stdio.h>
int main(void)
{
int age = 20;
while (age++ <= 65)
{
if ((age % 20) == 0) /* age是否能被20整除? */
printf("You are %d.Here is a raise.\n", age);
if (age = 65)
printf("You are %d.Here is your gold watch.\n", age);
}
return 0;
}
先打印一次You are 20.Here is your gold watch.
再重复打印You are 65.Here is your gold watch.
【每一次赋值都会重置age为65】
9.给定下面的输入时,以下程序将打印什么
q
c
h
b
#include <stdio.h>
int main(void)
{
char ch;
while ((ch = getchar()) != '#')
{
if (ch == '\n')
continue;
printf("Step 1\n");
if (ch == 'c')
continue;
else if (ch == 'b')
break;
else if (ch == 'h')
goto laststep;
printf("Step 2\n");
laststep:
printf("Step 3\n");
}
printf("Done\n");
return 0;
}
Step 1
Step 2
Step 3
Step 1
Step 1
Step 3
Step 1
Done
10.重写复习题9,但这次不能使用continue和goto语句
#include <stdio.h>
int main(void)
{
char ch;
while ((ch = getchar()) != '#')
{
if (ch == '\n')
else
{
printf("Step 1\n");
if (ch == 'c')
else
{
if (ch == 'b')
break;
else if (ch == 'h')
else
printf("Step 2\n");
printf("Step 3\n");
}
}
}
printf("Done\n");
return 0;
}
编程练习
1.编写一个程序读取输入,读到#字符停止,然后报告读取的空格数、换行符数和所有其他字符的数量
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char ch;
int kg = 0, hh = 0, qt = 0;
while ((ch = getchar()) != '#')
{
switch(ch)
{
case ' ':
kg++;
break;
case '\n':
hh++;
break;
default:
qt++;
}
}
printf("%-5d %-5d %-5d\n", kg, hh, qt);
return 0;
}
2.编写一个程序读取输入,读到#字符停止。程序要打印每个输入的字符以及对应的ASCII码(十进制)。一行打印8个字符。建议:使用字符计数和求模运算符(%)在每8个循环周期时打印一个换行符
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char ch;
int t = 1;
while ((ch = getchar()) != '#')
{
putchar(ch);
printf("%d ", ch);
if(!(t++ % 8))
{
printf("\n");
}
}
return 0;
}
3.编写一个程序,读取整数直到用户输入 0。输入结束后,程序应报告用户输入的偶数(不包括 0)个数、这些偶数的平均值、输入的奇数个数及其奇数的平均值
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char ch;
int os = 0, js = 0, oss = 0, jss = 0;
double osa = 0.0, jsa = 0.0;
int a;
while(scanf("%d", &a) && a)
{
if(a % 2)
{
js++;
jss += a;
}
else
{
os++;
oss += a;
}
}
if(os)
{
osa = double(oss) / double(os);
}
if(js)
{
jsa = double(jss) / double(js);
}
printf("%d %.2lf %d %.2lf\n", os, osa, js, jsa);
return 0;
}
4.使用if else语句编写一个程序读取输入,读到#停止。用感叹号替换句号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char ch;
int n = 0;
while((ch = getchar()) != '#')
{
if(ch == '.')
{
putchar('!');
n++;
}
else if(ch == '!')
{
putchar('!');
putchar('!');
n++;
}
else
{
putchar(ch);
}
}
printf("\n%d\n", n);
return 0;
}
5.使用switch重写练习4
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char ch;
int n = 0;
while((ch = getchar()) != '#')
{
switch(ch)
{
case '.':
putchar('!');
n++;
break;
case '!':
putchar('!');
putchar('!');
n++;
break;
default:
putchar(ch);
}
}
printf("\n%d\n", n);
return 0;
}
6.编写程序读取输入,读到#停止,报告ei出现的次数
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char ch;
int n = 0, f = 0;
while((ch = getchar()) != '#')
{
switch(ch)
{
case 'e':
f = 1;
break;
case 'i':
if(f)
{
n++;
f = 0;
}
break;
default:
break;
}
}
printf("\n%d\n", n);
return 0;
}
7.编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额、税金和净收入。做如下假设:
a.基本工资 = 10美元/小时
b.加班(超过40小时) = 1.5倍的时间
c.税率: 前300美元为15% 续150美元为20% 余下的为25%
用#define定义符号常量。不用在意是否符合当前的税法
#include <stdio.h>
#include <stdlib.h>
#define MAXT 40.0
#define GH 10.0
#define FR 300.0
#define SR 450.0
#define FTR 0.15
#define STR 0.20
#define TTR 0.25
int main(void)
{
float t, sum, tax, gain;
scanf("%f", &t);
if(t > MAXT)
{
t = MAXT + (t - MAXT) * 1.5;
}
sum = GH * t;
if(sum <= FR)
{
tax = sum * FTR;
gain = sum - tax;
}
else if(sum > FR && sum < SR)
{
tax = FR * FTR + (sum - FR) * STR;
gain = sum - tax;
}
else if(sum > SR)
{
tax = FR * FTR + (SR - FR) * STR + (sum - SR) * TTR;
gain = sum - tax;
}
printf("%-10.2f%-10.2f%-10.2f%-10.2f\n", t, sum, gain, 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) $10.00/hr 4) $11.20/hr
5) quit
*****************************************************************
如果选择 1~4 其中的一个数字,程序应该询问用户工作的小时数。程序要通过循环运行,除非用户输入 5。如果输入 1~5 以外的数字,程序应提醒用户输入正确的选项,然后再重复显示菜单提示用户输入。使用#define创建符号常量表示各工资等级和税率
#include <stdio.h>
#include <stdlib.h>
#define MAXT 40.0
#define GH 10.0
#define FR 300.0
#define SR 450.0
#define FTR 0.15
#define STR 0.20
#define TTR 0.25
int main(void)
{
float t, sum, tax, gain;
scanf("%f", &t);
if(t > MAXT)
{
t = MAXT + (t - MAXT) * 1.5;
}
float gh;
int num, f = 0, x = 1;
printf("***************************************************************** \n");
printf("Enter the number corresponding to the desired pay rate or action: \n");
printf("1) $8.75/hr 2) $9.33/hr\n3) $10.00/hr 4) $11.20/hr\n5) quit \n");
printf("***************************************************************** \n");
scanf("%d", &num);
while(x)
{
switch(num)
{
case 1:
gh = 8.75;
x = 0;
break;
case 2:
gh = 9.33;
x = 0;
break;
case 3:
gh = 10.00;
x = 0;
break;
case 4:
gh = 11.20;
x = 0;
break;
case 5:
return 0;
break;
default:
printf("input from 1 to 5:");
scanf("%d", &num);
}
}
sum = gh * t;
if(sum <= FR)
{
tax = sum * FTR;
gain = sum - tax;
}
else if(sum > FR && sum < SR)
{
tax = FR * FTR + (sum - FR) * STR;
gain = sum - tax;
}
else if(sum > SR)
{
tax = FR * FTR + (SR - FR) * STR + (sum - SR) * TTR;
gain = sum - tax;
}
printf("%-10.2f%-10.2f%-10.2f%-10.2f\n", t, sum, gain, tax);
return 0;
}
9.编写一个程序,只接受正整数输入,然后显示所有小于或等于该数的素数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
long long num;
int x = 1;
while(x)
{
if(scanf("%d", &num) == 1)
{
if(num < 1)
{
continue;
}
else
{
x = 0;
const int len = num;
int a[len + 1];
memset(a, 0, sizeof(int) * (len + 1));
for(int i = 2; i <= num; i++)
{
if(!a[i])
{
for(int j = i + i; j <= num; j += i)
{
a[j] = 1;
}
}
}
for(int i = 2; i <= num; i++)
{
if(!a[i])
{
printf("%d ", i);
}
}
printf("\n");
}
}
}
return 0;
}
10.1988年的美国联邦税收计划是近代最简单的税收方案。它分为4个类别,每个类别有两个等级。 下面是该税收计划的摘要(美元数为应征税的收入):
例如,一位工资为20000美元的单身纳税人,应缴纳税0.15×17850+0.28×(20000−17850)美元。编写一个程序,让用户指定缴纳税金的种类和应纳税收入,然后计算税金。程序应通过循环让用户可以多次输入
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
float income, tax_r, tax;
int type;
printf("*******************************************************\n");
printf("1)单身 2)户主\n3)已婚,共有 4)已婚,离异\n5)退出\n");
printf("*******************************************************\n");
printf("input type:");
while(scanf("%d", &type) == 1)
{
int x = 1;
while(x)
{
switch(type)
{
case 1:
tax_r = 17850.0;
x = 0;
break;
case 2:
tax_r = 23900.0;
x = 0;
break;
case 3:
tax_r = 29750.0;
x = 0;
break;
case 4:
tax_r = 14875.0;
x = 0;
break;
case 5:
return 0;
default:
printf("input from 1 to 5:");
scanf("%d", &type);
}
}
printf("input income:");
scanf("%f", &income);
if(income > tax_r)
{
tax = tax_r * 0.15 + (income - tax_r) * 0.28;
}
else
{
tax = income * 0.15;
}
printf("tax is %.2f\n", tax);
printf("input from 1 to 4 to continue or input 5 to exit:");
}
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 是退出订购。程序要记录累计的重量。即,如果用户输入 4 磅的甜菜,然后输入 5磅的甜菜,程序应报告9磅的甜菜。然后,该程序要计算货物总价、折扣(如果有的话)、运费和包装费。随后,程序应显示所有的购买信息:物品售价、订购的重量(单位:磅)、订购的蔬菜费用、订单的总费用、折扣(如果有的话)、运费和包装费,以及所有的费用总额
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const float ap = 2.05;
const float bp = 1.15;
const float cp = 1.09;
int main(void)
{
char ch;
float sum, discount, df;
int an = 0, bn = 0, cn = 0, sn = 0, n;
while((ch = getchar()) != 'q')
{
switch(ch)
{
case 'a':
printf("\ninput n:");
scanf("%d", &n);
an += n;
sn += n;
sum += ap * n;
break;
case 'b':
printf("\ninput n:");
scanf("%d", &n);
bn += n;
sn += n;
sum += bp * n;
break;
case 'c':
printf("\ninput n:");
scanf("%d", &n);
cn += n;
sn += n;
sum += cp * n;
break;
default:
printf("input a or b or c or q:");
}
getchar();
}
if(sum > 100)
{
discount = 0.05 * sum;
}
else
{
discount = 0.0;
}
if(sn <= 5)
{
df = 6.5;
}
else if(sn < 20)
{
df = 14.0;
}
else
{
df = 14.0 + (sn - 20) * 0.5;
}
printf("ap = %.2f, bp = %.2f, cp = %.2f\n", ap, bp, cp);
printf("an = %d, bn = %d, cn = %d\n", an, bn, cn);
printf("a is %.2f, b is %.2f, c is %.2f\n", ap * an, bp * bn, cp * cn);
printf("sum is %.2f, discount is %.2f, df is %.2f\n", sum, discount, df);
return 0;
}