7.1-7.10的编译器为VS2010, 7.11用的VS2019。
每道题目都得花上不少时间,自身编程能力还是得提高啊。
大家想看比较全的答案的话,这里推荐xiaojimmychen的全部章节完整答案
我的运行结果如下:
7.1
#include "stdafx.h"
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char ch;
int space,enter,other;// 计算空格,换行,其他字符数
space=enter=other=0;
printf("请输入你想要输入的字符:(输入#退出)\n");
while((ch=getchar())!='#')
{
if(ch=='\n')
enter++;
else if(ch==' ')
space++;
else other++;
}
printf("\n你输入的字符中,空格数为%d,换行数为%d,其他字符数为%d.\n",space,enter,other);
getchar();
getchar();
return 0;
}
运行结果:
7.2
#include "stdafx.h"
#include <stdio.h>
int main(void)
{
char ch;
int i=0;
printf("请输入你想要输入的字符:\n");
while((ch=getchar())!='#')
{
if(i%8==0)
putchar('\n');
i++;
putchar(ch);
printf("%d",ch);
}
getchar();
getchar();
return 0;
}
运行结果:(空格的字符在ASCII码中是32)
7.3
#include "stdafx.h"
#include <stdio.h>
int main(void)
{
long long int number,n_odd,n_even,o_total,e_total;//设置好计数和用于存放所有偶数奇数的变量
n_odd=n_even=o_total=e_total=0;
printf("请输入想要输入的整数:(输入0就退出)\n");
scanf("%lld",&number);
while(number!=0)
{
if(number%2==0)
{
n_even++;
e_total+=number;
}
else
{
n_odd++;
o_total+=number;
}
//printf("请再输入要输入的整数:(0退出)\n"); //如果每次输入一个数的话,可以加这句
scanf("%lld",&number);
}
printf("用户输入的偶数个数为%lld,这些偶数平均值为%.3lf.\n",n_even,(long double)e_total/n_even);
printf("用户输入的奇数个数为%lld,这些奇数平均值为%.3lf.\n",n_odd,(long double)o_total/n_odd);
getchar();
getchar();
return 0;
}
运行结果:
7.4
#include "stdafx.h"
#include <stdio.h>
int main(void)
{
char ch;
int replace=0;
printf("请输入语句:(#退出)\n");
while((ch=getchar())!='#')
{
if(ch=='.')
{
ch='!';
replace++;
}
else if(ch=='!')
{
putchar('!');
replace++;
}
putchar(ch);
}
printf("总共进行了%d次替换",replace);
getchar();
getchar();
return 0;
}
运行结果:中文的句号和感叹号无法转换。
7.5
#include <stdio.h>
int main(void)
{
char ch;
int replace=0;
printf("请输入语句:(#退出)\n");
while((ch=getchar())!='#')
{
switch(ch)
{
case '.':
{
ch='!';
replace++;
}
break;
case '!':
{
putchar('!');
replace++;
}
break;
default:break;
}
putchar(ch);
}
printf("总共进行了%d次替换",replace);
getchar();
getchar();
return 0;
}
运行结果:
7.6
#include <stdio.h>
int main(void)
{
char ch,ch1;
ch=ch1=0;
int number = 0;
printf("请输入内容:(#退出)\n ");
while ((ch = getchar()) != '#')
{
if (ch=='i')
{
if (ch1=='e' )
{
number++;
}
}
ch1 = ch;
}
printf("ei总共出现了%d次",number);
getchar();
getchar();
return 0;
}
运行结果:
7.7
#include <stdio.h>
#define base_pay 10.00
#define base_hour 40
#define a 300
#define b 450
int main(void)
{
long double wage,hours,tax;
wage=hours=tax=0;
printf("请输入这一周工作的时间:(0退出)\n ");
scanf("%lf",&hours);
while (hours !=0&&hours>0)
{
if(hours<=40)
wage=hours*base_pay;
else wage=400+15*(hours-40);
if(wage<=a)
tax=wage*0.15;
else if(wage<=b)
tax=45+(wage-a)*0.2;
else tax=45+30+(wage-b)*0.25;
printf("这一周工资为%.2lf,所要缴纳的税费为%0.2lf,",wage,tax);
printf("净收入为%0.2lf\n",wage-tax);
wage=tax=0;
printf("请输入这一周工作的时间:(0退出)\n ");
scanf("%lf",&hours);
}
printf("这周没有上班.");
getchar();
getchar();
return 0;
}
运行结果:
7.8
#include <stdio.h>
#define base_hour 40
#define a 300
#define b 450
int main(void)
{
long double wage,hours,tax,base_pay;
wage=hours=tax=0;
int pick;
printf("请选择想要的工资:(数字1~4选择)\n");
printf("%-25s%-25s\n%-25s%-25s\n%-25s\n", "1) $8.75/hr", "2) $9.33/hr", "3) $10.00/hr", "4) $11.20/hr", "5) quit");//负号让字符串从左端开始输出,见书p84
while (scanf("%d",&pick)==1)
{
if (pick == '\n')
continue;
switch (pick)
{
case 1: base_pay = 8.75;
break;
case 2: base_pay = 9.33;
break;
case 3: base_pay = 10.00;
break;
case 4:base_pay = 11.20;
break;
case 5:printf("你没有选择工资,那么你就不能再这个公司上班:\n\n");
default:
printf("请选择想要的工资:(数字1~4选择)\n");
printf("%-25s%-25s\n%-25s%-25s\n%-25s\n", "1) $8.75/hr", "2) $9.33/hr", "3) $10.00/hr", "4) $11.20/hr", "5) quit");
continue;
}
break;
}
printf("请输入这一周工作的时间:(0退出)\n ");
scanf("%lf",&hours);
while (hours !=0&&hours>0)
{
if(hours<=40)
wage=hours*base_pay;
else wage=400+15*(hours-40);
if(wage<=a)
tax=wage*0.15;
else if(wage<=b)
tax=45+(wage-a)*0.2;
else tax=45+30+(wage-b)*0.25;
printf("这一周工资为%.2lf,所要缴纳的税费为%0.2lf,",wage,tax);
printf("净收入为%0.2lf\n",wage-tax);
wage=tax=0;
printf("请输入这一周工作的时间:(0退出)\n ");
scanf("%lf",&hours);
}
printf("这周没有上班.");
getchar();
getchar();
return 0;
}
运行结果:
7.9
#include <stdio.h>
#include <math.h>
int main(void)
{
int i,y,a,x;
long int number;
i=x=y=a=0;
printf("请输入整数:(0退出)\n");
while(a=scanf("%ld",&number)==1&&number>0)
{
if(number==1)
continue;
for(x=2;x<=number;x++)
{
for(i=2;i<sqrt((double)x);i++)//sqrt是double类型的函数
{ if(x%i == 0)
break;
}
if(i>sqrt((double)x))
printf("%3d",x);
}
printf("\n以上就是所有小于或等于该数的素数.\n");
printf("请输入整数:(0退出)\n");
}
getchar();
getchar();
return 0;
}
运行结果:
7.10
#include <stdio.h>
#include <math.h>
int main(void)
{
int pick,a;
double wage,limit,tax;
wage=limit=tax=0;
printf("%-30s%-50s\n%-30s%-50s\n","类别","税金","1) 单身","17850美元按15%计,超出部分按28%计");
printf("%-30s%-50s\n%-30s%-50s\n","2) 户主","23900美元按15%计,超出部分按28%计","3) 已婚,共有","29750美元按15%计,超出部分按28%计");
printf("%-30s%-50s\n","4) 已婚,离异","14875美元按15%计,超出部分按28%计");
printf("请输入你的税收类别:(#退出)\n");
while (a=scanf("%d", &pick)==1&&pick!='#')
{
switch (pick)
{
case 1:limit = 17850.0;
break;
case 2:limit = 23900.0;
break;
case 3:limit = 29750.0;
break;
case 4:limit = 14875.0;
break;
default:
printf("请重新输入你的类别:\n");
continue;
}
printf("请输入你的工资:\n");
scanf("%lf", &wage);
if (wage < limit)
{
tax=wage*0.15;
}
else
{
tax=limit*0.15+(wage-limit)*0.28;
}
printf("你的收入为%.2lf,要缴纳的税收为%.2lf\n\n",wage,tax);
printf("%-30s%-50s\n%-30s%-50s\n","类别","税金","1) 单身","17850美元按15%计,超出部分按28%计");
printf("%-30s%-50s\n%-30s%-50s\n","2) 户主","23900美元按15%计,超出部分按28%计","3) 已婚,共有","29750美元按15%计,超出部分按28%计");
printf("%-30s%-50s\n","4) 已婚,离异","14875美元按15%计,超出部分按28%计");
printf("请输入你的税收类别:(#退出)\n");
}
getchar();
getchar();
return 0;
}
运行结果:
7.11
#include<stdio.h>
#include<cwctype>
#define Yang 2.05
#define Tian 1.15
#define Hu 1.09
#define weight1 5
#define weight2 20
void menu(int);
void choice(int);
int main(void)
{
menu(1);
char ch;
double x, y, z, k;//3种蔬菜的重量
x = y = z = k = 0;
while ((ch = getchar()) != 'q')
{
if (ch == '\n')
continue;
if (iswlower(ch))
switch (ch)
{
case 'a':
puts("请输入你要购买洋蓟的磅数:");
scanf_s("%lf", &k);
x += k;
break;
case 'b':
puts("请输入你要购买的甜菜:");
scanf_s("%lf", &k);
y += k;
break;
case 'c':
puts("请输入你要购买的胡萝卜:");
scanf_s("%lf", &k);
z += k;
break;
default:puts("你的输入有误,请重新输入,请输入a,b,c中的字母");
break;
}
else
puts("我只能识别小写字母.");
while (getchar() != '\n')
continue;
puts("请输入再次输入要购买的蔬菜种类,或者输入q退出.");
}
double price_x, price_y, price_z;//开始计算运费,货物总价,折扣
printf("洋蓟售价为每磅2.05美元,甜菜售价为每磅1.15美元,胡萝卜售价为每磅1.09美元.\n");
printf("你选择购买的洋蓟的重量为%.2lf,甜菜的重量为%.2lf,胡萝卜的重量为%.2lf\n", x, y, z);
printf("请购的蔬菜费用总价为%.2lf\n", x * Yang + y * Tian + z * Hu);
price_x = x * Yang + y * Tian + z * Hu;
if ((x + y + z) < weight1)
{
printf("要收取的运费和包装费为6.5美元.\n");
price_y = 6.5;
}
else if ((x + y + z) < weight2)
{
printf("要收取的运费和包装费为14美元.\n");
price_y = 14;
}
else {
printf("要收取的运费和包装费为:%.2lf\n",14+(x+y+z-20)*0.5);
price_y = 14 + (x + y + z - 20) * 0.5;
}
printf("订单的总运费为%.2lf\n", price_x + price_y);
if ((price_x + price_y) > 100)
price_z = (price_x + price_y) * 0.05;
printf("折扣费用为%.2lf\n", price_z);
printf("所有费用总和为%.2lf\n", price_x+price_y-price_z);
return 0;
}
void menu(int)
{
for (int i = 0;i < 40;i++)
printf("*");
printf("\n");
puts("a.洋蓟 b.甜菜");
puts("c.胡萝卜 q.退出");
puts("请输入你要选择购买的蔬菜,按q退出");
for (int i = 0;i < 40;i++)
printf("*");
printf("\n");
}
运行结果: