C primer plus (第6版)中文版 第七章答案

第七章 复习题3

//复习题3

int 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                                                 //改
			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;

编程练习

//第 1 题

#include <stdio.h>
int main(void)
{
	int space = 0, n_lines = 0, o_letter = 0;
	char ch;

	while ((ch = getchar()) != '#')
	{
		if (ch == ' ')
		{
			space++;
			continue;
		}
		if (ch == '\n')
		{
			n_lines++;
			continue;
		}
		o_letter++;
	}
	printf("space = %d, n_lines = %d, o_letter = %d", 
		space, n_lines, o_letter);
	return 0;
}
//第 2 题

#include <stdio.h>
int main(void)
{
	char ch;
	int n = 1;
	while ((ch = getchar()) != '#')
	{
		putchar(ch);
		printf(" %d ", ch);
		if (n++ % 8 == 0)
			printf("\n");
	}
		return 0;
}
//第 3 题

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
	int num, even_num = 0, odd_num = 0;
	int even_sum = 0, odd_sum = 0;
	while (scanf("%d", &num) != 0)
	{
		if (num == 0)		
			break;		
		if (num % 2 == 0)
		{
			even_num++;
			even_sum += num;
		}
		else 
		{
			odd_num++;
			odd_sum += num;
		}	
	}
	printf("Even number:%d \n", even_num);
	if (even_num > 0)
		printf("The average of even number is %d\n",
		even_sum / even_num);
	printf("odd number:%d \n", odd_num);
	if (odd_sum > 0)
	    printf("The average of odd number is %d\n",
		odd_sum / odd_num);

		return 0;
}
//第 4 题

#include <stdio.h>
int main(void)
{
	char ch;
	int sum = 0;
	while ((ch = getchar()) != '#')
	{
		if (ch == '.')
		{
			ch='!';
			sum++;
		}
		else if (ch == '!')
		{		
			putchar('!');
			sum++;
		}
		putchar(ch);
	}
	printf("\n%d", sum);
		return 0;
}
//第 5 题

#include <stdio.h>
int main(void)
{
	char ch;
	int sum = 0;
	while ((ch = getchar()) != '#')
	{
		switch (ch)
		{
		case '.':
			ch = '!';
			sum++;
			break;
		case'!':
			putchar('!');
			sum++;
			break;
		default:
			break;
		}
		 putchar(ch);
	}
	printf("\n%d", sum);
		return 0;
}
//第 6 题

#include <stdio.h>
int main(void)
{
	char ch,ch1;
	int sum = 0;
	while ((ch = getchar()) != '#')
	{
		putchar(ch);
		if (ch == 'e')                         //这
		{                                      //一
			if ((ch1 = getchar()) == 'i')      //段
				sum++;                         //写
			putchar(ch1);                      //的
		}	                                   //真漂亮
	} 
	printf("\n%d", sum);
		return 0;
}
//第 7 题

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#define WAGE 10.00
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
int main(void)
{
	float total, taxes, income, time;
	printf("enter your work time:");
	scanf("%f", &time);
	if (time > 40)
		time = 1.5 * (time - 40) + 40;
	total = WAGE * time;
	if (total <= 300)
		taxes = total * RATE1;
	else if (total > 300 && total <= 450)
		taxes = (total - 300) * RATE2 + 300 * RATE1;
	else
		taxes = (total - 450) * RATE3 + 150 * RATE2 + 300 * RATE1;
	income = total - taxes;
	printf("total:%.2f, taxes:%.2f, income%.2f\n", total, taxes, income);
		return 0;
}
//第 8 题

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define WAGE1 8.75
#define WAGE2 9.33
#define WAGE3 10.00
#define WAGE4 11.20
int main(void)
{
	int ch;
	float total, taxes, income, time, wage = 0;
	printf("please choise (1~5):\n");
	printf("1) $%.2f/hr\t  2)$%.2f/hr\t\n", WAGE1, WAGE2);
	printf("3) $%.2f/hr\t  4)$.2f/hr\t\n", WAGE3, WAGE4);
	printf("5) quit\t\n");
	while (scanf("%d", &ch) == 1)
	{
		while (ch < 1 || ch>5)
		{
			printf("Open your eyes and choies again:\n");
			scanf("%d", &ch);
		}
		switch (ch)
		{
		case 1:
			wage = WAGE1;
			break;
		case 2:
			wage = WAGE2;
			break;
		case 3:
			wage = WAGE3;
			break;
		case 4:
			wage = WAGE4;
			break;
		case 5:
			printf("quit");
			return 0;
		default: break;
		}
		printf("enter your work time:\n");
		scanf("%f", &time);
		if (time > 40)
			time = 1.5 * (time - 40) + 40;
		total = wage * time;
		if (total <= 300)
			taxes = total * RATE1;
		else if (total > 300 && total <= 450)
			taxes = (total - 300) * RATE2 + 300 * RATE1;
		else
			taxes = (total - 450) * RATE3 + 150 * RATE2 + 300 * RATE1;
		income = total - taxes;
		printf("total:%.2f, taxes:%.2f, income%.2f\n", total, taxes, income);
		printf("choies other:\n");
			scanf("%d", &ch);
	}
	return 0;
}
//第 9 题

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
	int i, j, num,sign=0;
	printf("enter number:\n");
	while (scanf("%d", &num) != 0 && num > 0)
	{
		for (i = 2; i <= num; i++)
		{
			for (j = 2; j < i; j++)
			{
				if (i % j == 0 && i != j)
				{
					sign = 1;
					break;
				}
			}			
			if (sign == 0)
				printf("%d\n", i);
			sign = 0;
		}
	}
	return 0;
}
第 10 题

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#define FIRST_RATE 0.15
#define SCEOND_RATE 0.28
int main(void)
{
	int ch;
	float income, taxes, ex_amount, amount, x;
	printf("你是哪个类型?\n");
	printf("1)单身          2)户主\n");
	printf("3)已婚,共有    4)已婚,离异\n");
	while (scanf("%d", &ch) == 1)
	{
		if (ch < 1 || ch>4)
		{
			printf("请重新输入:\n");
			scanf("%d", &ch);
		}
		else
		{
			printf("请输入你的收入:\n");
			scanf("%f", &income);
		}
		switch (ch)
		{
		case 1:
			x = 17850;
			break;
		case 2:
			x = 23900;
			break;
		case 3:
			x = 29750;
			break;
		case 4:
			x = 14870;
			break;
		default:
			printf("请重新输入:\n");
			scanf("%d", &ch);
		}
		if (income > x)
		{
			amount = x;
			ex_amount = income - x;
		}
		else
		{
			amount = income;
			ex_amount = 0;
		}
		taxes = ex_amount * SCEOND_RATE + amount * FIRST_RATE;
		printf("Tax:%.2f", taxes);
		printf("请输入下一个类型:\n");
		scanf("%d", &ch);
	}
	return 0;
}
//第 11 题
//做了一半不想做了,存下再说

#define _crt_secure_no_warnings
#include <stdio.h>
#include <math.h>
#define discount 0.95
#define freight1 6.5
#define freight2 14
#define artichock 2.05
#define beet 1.15
#define carrot 1.09


int main(void)
{
	float price, weight = 0, money = 0, freight, weight1 = 0, money1 = 0;
	char ch;
	printf("please select the item you want to buy from a to c,(q:quie)\n");
	printf("a.artichock   b.beet   c.carrot  q(quit)\n");
	while (scanf("%c", &ch) == 1)
	{
		switch (ch)
		{
		case'a':
		case 'A':
			price = artichock;
			break;
		case'b':
		case 'B':
			price = beet;
			break;
		case'c':
		case 'C':
			price = carrot;
			break;
		case'q':
			goto flag;
		}
		printf("please enter weight\n");
		scanf("%f", &weight1);
		money1 = weight1 * price ;
		weight = weight1 + weight;
		money = money1 + money;
		printf("other item\n");
		scanf("%c", &ch);
	}
	flag:
	if (weight <= 5)
		freight = freight1;
	else if (weight <= 20)
		freight = freight2;
	else
		freight = (weight - 20) * 0.5 + freight2;
	money = money + freight;
	if (money > 100)
		money = money * discount;
	printf("total:%.2f", money);
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值