c primer plus 习题答案(1)

这是我做的c primer plus(第五版)上的部分习题解答,一些类似的就不发了。我用的IDE是visual studio 2013。

p186.3

 1 #include<stdio.h>

 2 #include<stdlib.h>

 3 #define SIZE 30

 4 int main(void)

 5 {

 6     int o_num=0, e_num=0; 

 7     int i, j; 

 8     int num[SIZE];

 9     printf("please input number\n");

10     scanf("%d", &num[i]);

11     j=0;

12 

13     for(i=0;num[i++]!=0;)

14     {

15         if(num[i]%2==0)

16         {

17             j++;

18             e_num+=num[i];

19         }

20         else

21             o_num+=num[i];

22     }

23     printf("%d %d", j, e_num/j);

24     printf("%d %d", i-j, o_num/(i+1-j));

25 

26     system("pause");

27     return 0;

28 }

p187.6

A方案

 1 #include<stdio.h>

 2 #include<stdlib.h>

 3 

 4 int main(void)

 5 {

 6     int num=0;

 7     char ch;

 8 

 9     while(1)

10     {

11         if((ch=getchar())=='#')

12             break;

13         if(ch=='e'&&(ch=getchar())=='i')

14             num++;

15         else if(ch=='#')

16             break;

17     }

18 

19     printf("%d", num);

20 

21     system("pause");

22     return 0;

23 }

 B方案

 1 #include<stdio.h>

 2 #include<stdlib.h>

 3 

 4 int main(void)

 5 {

 6     int num=0;

 7     char ch;

 8 

 9     while(1)

10     {

11         if((ch=getchar())=='#')

12             break;

13         else if(ch=='e')

14         {

15             switch(ch=getchar())

16         {

17                 case 'i': num++;

18                 case '#': break;}

19         if(ch=='#')

20             break;}

21         if(ch=='#')

22         break;

23     }

24 

25     printf("%d", num);

26 

27     system("pause");

28     return 0;

29 }

 

 

p187.8

 1 #include<stdio.h>

 2 #include<stdlib.h>

 3 int get_int(void);

 4 #define BASETIME 40

 5 #define RATE1 0.15

 6 #define RATE2 0.2

 7 #define RATE3 0.25

 8 

 9 

10 int main(void)

11 {

12     float time, gross, net_income, tax, payrate, i;

13 

14     for(i=1;i<=48;i++)

15         printf("*");

16     printf("\n");

17     printf("Enter the number correspongding to the desired pay rate or action:\n");

18     printf("1)$%-.2f/hr%18c 2)$%-.2f/hr%18c\n", 8.75, ' ',9.33,' ');

19     printf("3)$%-.2f/hr%18c4)$%-.2f/hr%18c\n", 10.00, ' ',11.20,' ');

20     printf("5)quit");

21     printf("\n");

22     for(i=1;i<=48;i++)

23         printf("*");

24     printf("\n");

25 

26     while(1)

27     {

28     printf("Please enter your pay rate.\n");

29     switch(get_int())

30     {        

31         case 1: payrate=8.75;

32                 break;

33         case 2: payrate=9.33;

34                 break;

35         case 3: payrate=10.00;

36                 break;

37         case 4: payrate=11.20;

38                 break;

39         case 5: break;             

40     }

41     if(get_int()==5)

42         break;

43     printf("Please enter your working hours.\n");

44     while(scanf("%f", &time)==1)

45     {

46         if(time<=40)

47             if(time<=30)

48                 printf("the gross/tax/net_income is %f %f %f",time*payrate, time*payrate*RATE1, time*payrate-time*payrate*RATE1);

49             else

50                 printf("the gross/tax/net_income is %f %f %f",300+(time-30)*payrate, 300*RATE1+(time-30)*payrate*RATE2, (300+(time-30)*payrate)-(300*RATE1+(time-30)*payrate*RATE2));

51         else if(time>40)

52             printf("the gross/tax/net_income is %f %f %f",450+(time-40)*1.5*payrate, 300*RATE1+150*RATE2+(time-40)*1.5*payrate*RATE3, 450+(time-40)*1.5*payrate-300*RATE1-150*RATE2-(time-40)*1.5*payrate*RATE3);

53         printf("\n");

54         printf("enter q to quit.\n");

55     }

56     }    

57 

58     system("pause");

59     return 0;

60 }

61 

62 

63 int get_int(void)

64 {

65     int num;

66 

67         scanf("%d", &num);

68         switch(num)

69         {

70         case 1: return num;

71                 break;

72         case 2: return num;

73                 break;

74         case 3: return num;

75                 break;

76         case 4: return num;

77                 break;

78         case 5: return num;

79                 break;

80         default:

81             printf("Please input an integer range from 1~5\n");

82         }

83 }

 

 p187.10

 1 #include<stdio.h>

 2 #include<stdlib.h>

 3 int get_char();

 4 

 5 int main(void)

 6 {

 7     int bill;

 8     double tax;

 9 

10     while(1)

11     {

12         printf("Please input your taxable income.\n");

13         printf("enter q to quit.\n");

14         if(scanf("%d",&bill)!='1')

15             break;

16         switch(get_char())

17         {

18         case '1': if(bill<=17850)

19                       tax=0.15*bill;

20                   else

21                       tax=17850*0.15+(bill-17850)*0.28;

22             printf("The tax is %f\n", tax);

23             break;

24         case '2': if(bill<=23900)

25                       tax=0.15*bill;

26                   else

27                       tax=23900*0.15+(bill-23900)*0.28;

28             printf("The tax is %f\n", tax);

29             break;

30         case '3': if(bill<=29750)

31                       tax=0.15*bill;

32                   else

33                       tax=29750*0.15+(bill-29750)*0.28;

34             printf("The tax is %f\n",tax);

35             break;

36         case '4': if(bill<=14875)

37                       tax=bill*0.15;

38                   else

39                       tax=14875*0.15+(bill-14875)*0.28;

40             printf("The tax is %f\n",tax);

41             break;

42         }

43     }

44     system("pause");

45     return 0;

46 }

47 

48 int get_char()

49 {

50     int num;

51     printf("Please enter a number.\n");

52     

53     while(scanf("%d", &num))

54     if(num!=1&&num!=2&&num!=3&&num!=4)

55         printf("Please enter again.\n");

56     else    

57         return num;

58     

59 }

 

你可能感兴趣的:(Prim)