C primer plus第六版 第七章分支跳轉 答案

//7.1
#include<stdio.h>
int main(void)
{int countspace=0,countendl=0,countothers=0;
	char ch;
	ch=getchar();
while(ch!='#')
{
    if(ch==' ')
        countspace++;
    else if(ch=='\n')
        countendl++;
    else countothers++;
    ch=getchar();
}
printf("The numbers of space,endl and others are %d,%d and %d. ",countspace,countendl,countothers);

	return 0;
}

//7.2
#include<stdio.h>
int main(void)
{int n=0;
	char ch;
	ch=getchar();
	printf("The ASCII of char is  ASCII\n");
while(ch!='#')
{
printf("%c-%d\t",ch,ch);

    n++;
    if(n==8)
    {
        printf("\n");
        n=0;
    }
    ch=getchar();
}


	return 0;
}
//7.3
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    int sum1=0,sum2=0,count1=0,count2=0;
    int a;
    float av1,av2;
    printf("please enter !\n");
    while(scanf("%d",&a)==1&&a!=0)
    {




            if(a%2==0)
            {
                count1++;
                sum1+=a;
            }
            else
            {
                count2++;
                sum2+=a;
            }

    }
    av1=sum1/count1;
    av2=sum2/count2;
    printf("ou數的個數和平均數爲%d,%.2lf",count1,av1);
    printf("ji數的個數和平均數爲%d,%.2lf",count2,av2);
    return 0;
}
//7.4
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
	char ch;
	int n = 0;

	while ((ch = getchar()) != '#')
	{
		if (ch == '.')
		{
			ch = '!';
			putchar(ch);
			n++;
		}
		else if (ch == '!')
		{
			putchar('!');
			putchar('!');
			n++;
		}
		else
			putchar(ch);

	}
	printf("\n");
	printf("%d\n",n);

	return 0;
 }
//7.5
#include<stdio.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);
				 break;
		}
	}
	printf("\n%d\n",n);

	return 0;	
}
//7.6
#include <stdio.h>
 
int main(void)
{
	char ch;
	int n = 0;//n爲出現的次數
	ch = getchar();
	while(ch != '#')
	{
		if (ch == 'e')
		{
			ch = getchar();
			if (ch == 'i')
			{
				n++;
			}	
		}
		else
			ch = getchar();
	}
	printf("ei出現的次數爲%d\n", n);
}
/*      第七題     */
#include<stdio.h>
#define BASIC 10.00
#define OVER 1.5 
#define FIRST_RATE 0.15
#define NEXT_RATE 0.20
#define LAST_RATE 0.25
#define FIRST_SUM 300 * 0.85
#define NEXT_SUM 150*0.8

int main(void)
{
	int times;
	
	while (scanf("%d",&times) == 1)
	{
		if (times < 300/10)
			printf("%lf",(double)10*times*(1 - 0.15));
		else if (times < 400/10)
			printf("%lf",FIRST_SUM + (double)(times*10 - 300)*0.8);
		else
			printf("%lf",FIRST_SUM + NEXT_SUM + (double)(times - 40) * 1.5 * 10 * 0.75);
	}
	
	return 0;
}
/*    第八題   */
#include<stdio.h>
#define NON_OVERTIME 40.00
#define BASIC 10.00
#define FIRST_HOUR 300.00
#define NEXT_HOUR 450.00
#define FIRST_RATE 0.15
#define NEXT_RATE 0.20
#define LAST_RATE 0.25
void interface(void);
char star(void);
int main(void)
{
	int num, x = 1;
	float hour,sum,tax,gain,perhour;
	
	printf("Please enter you work hour a week.\n");
	scanf("%f",&hour);
	if (hour > NON_OVERTIME)
		hour = NON_OVERTIME + (hour - NON_OVERTIME) * 1.5;
		
	star();
	interface();
	star();
	
	printf("Which is your wages per hour.\n");
	scanf("%d",&num);
	while (x)
	{
		switch (num)
		{
			case 1:
				perhour = 8.75;
				x = 0;
				break;
			case 2:
				perhour = 9.33;
				x = 0;
				break;
			case 3:
				perhour = 10.00;
				x = 0;
				break;
			case 4:
				perhour = 11.20;
				x = 0;
				break;
			case 5:
				return 0;
				break;
			default:
				printf("please input number from 1 to 5: \n");
				scanf("%d",&num);
		}
	}
	sum = perhour * hour;
	if (sum <= FIRST_HOUR)
	{
		tax = sum * 0.15;
		gain = sum - tax;
	}
	else if (sum > FIRST_HOUR && sum <= NEXT_HOUR)
	{
		tax = FIRST_HOUR * 0.15 + (sum - FIRST_HOUR) * 0.20;
		gain = sum - tax;
	}
	else
	{
		tax = FIRST_HOUR * 0.15 + 150 * 0.2 + (hour - 450) * 0.25;
		gain = sum - tax;
	}
	printf("real hour:%.f  perhour:%.2f  tax:%.2f  gain:%.2f", hour, perhour, tax, gain);
	
	return 0;
}
 
void interface(void)
{
	printf("Enter the number corresponding to the desired pay rate or action:\n");
	printf("1) $8.75/hr        2) $9.33/hr\n");
	printf("3) $10.00/hr       4) $11.20/hr\n");
	printf("5) quit\n");
}

char star(void)
{
	int i = 1;
	
	for (i;i < 100;i++)
		printf("*");
	printf("\n");
} 
/*    第九題   */
#include<stdio.h>
int main(void)
{
	int integer;
	int i, j;
	int sign = 0;
	
	printf("Please enter a postive integer: ");
	while (scanf("%d",&integer) != 0 && integer > 0)
	{
		for (i = 2;i <= integer;i++)
		{
			for (j = 2;j <= i;j++)
			{
				if (i % j == 0 && i != j)
				{
					sign = 1;
					break;
				}
			
			}
			if (sign == 0)
					printf("%d ",i);
			
			sign = 0;
		}
		printf("\n");
		printf("Please enter a postive integer: ");
	}
	
	return 0;
}
//7.10
/*    第十題   */
#include<stdio.h>
#define OR_RATE 0.15
#define OV_RATE 0.28
void star(void);
void interface(void);
int main(void)
{
	int choice;
	double money, Boundaries, tax;
	
	star();
	interface();
	star();
	
	while (scanf("%d",&choice) == 1 )
	{
		int x = 1;
		while (x)
		{
			switch(choice)
			{
				case 1:
					x = 0;
					Boundaries = 17850;
					break;
				case 2:
					x = 0;
					Boundaries = 23900;
					break;
				case 3:
					x = 0;
					Boundaries = 29750;
					break;
				case 4:
					x = 0;
					Boundaries = 14875;
					break;
				case 5:
					return 0;
				default:
					printf("please input number from 1 to 5: \n");
					scanf("%d",&choice);
			}
		}
		printf("Enter your salary: \n");
		scanf("%lf",&money);
		if (money <= Boundaries)
			tax = money * 0.15;
		else
			tax = Boundaries * 0.15 + (money - Boundaries) * 0.28;
		printf("Tax: %.2lf\n",tax);
		printf("Enter 1 to 4 to continue or 5 to quit.\n");
	}
		
	return 0;	
}
void star(void)
{
	int i;
	
	for (i = 0;i <= 100;i++)
		printf("*");
	printf("\n");
}
void interface(void)
{
	printf("Enter your category: \n");
	printf("1) Single     2) Household\n");
	printf("3) Married    4) Divorced\n");
	printf("5) Quit\n");
}
/*    第十一題    */
#include<stdio.h>
#define Artichokes 2.05 //洋薊
#define Sugarbeet 1.15  //甜菜
#define Carrots 1.09    //胡蘿蔔
void interface(void);
int main(void)
{
	char ch;
	int an, bn, cn, sn, n;
	float sum, count, other_count, cost, cost_vegtables;
	
	interface();
	
	while ((ch = getchar()) != 'q')
	{
		switch (ch)
		{
		 	case 'a':
				printf("How many pounds of artias do you want?\n");
				scanf("%f",&n);
				an += n;
				sn += n;
				sum += Artichokes * n;
				break;
			case 'b':
				printf("How many pounds of sugarbeet do you want?\n");
				scanf("%f",&n);
				bn += n;
				sn += n;
				sum += Sugarbeet * n;
				break;
			case 'c':
				printf("How many pounds of carrots do you want?\n");
				scanf("%f",&n);
				cn += n;
				sn += n;
				sum += Carrots * n;
				break;
			default:
				printf("Please input a or b or c (q to quit): \n");
		}
		printf("What else do you need?\n");
		getchar();
	}
	if (sum > 100)
		count = 0.05 * sum;
	else
		count = 0.0;
	if (sn <= 5)
		other_count = 6.5;
	else if (sn <= 20)
		other_count = 14;
	else
		other_count = 14 + (sn - 20) * 0.5;
	cost_vegtables = sum - count;
	cost = sum - count - other_count;
	
		
	printf("Artichokes: %.2f   Sugarbeet: %.2f   Carrots: %.2f\n",Artichokes,Sugarbeet,Carrots);
	printf("Weight: %.2f pounds  cost_vegtables: %.2f  Sum: %.2f \n",sn,cost_vegtables,sum);
	printf("discount: %.2f pounds Freight and packaging charges: %f\n",count,other_count);
	
	return 0;
} 
void interface(void)
{
	printf("*************************************\n");
	printf("Choose which vegtable you need: \n");
	printf("a) Artichokes   b) Sugarbeet\n");
	printf("c) Carrots      q) Quit\n");
	printf("*************************************\n");
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章