码迷,mamicode.com
首页 > 其他好文 > 详细

c primer plus 习题答案(2)

时间:2015-05-24 23:22:00      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

p242.8

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int main()
 5 {
 6     int x, y, z, num;
 7     int largest(int a, int b, int c);
 8     printf("please enter three numbers.");
 9     printf("enter q to quit.\n");
10     while(scanf("%d%d%d",&x, &y, &z)==3)
11     {
12     num=largest(x, y, z);
13     printf("the largest number is %d\n", num);
14     printf("please enter three numbers.");
15     printf("enter q to quit.\n");
16     }
17 
18     system("pause");
19     return 0;
20 }
21 
22 int largest(int a, int b, int c)
23 {
24     if(a>b)
25     {
26         if(a>c)
27             return a;
28         else return c;
29     }
30     else
31     {
32         if(b>c)
33             return b;
34         else return c;
35     }
36 }

 p242.9

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int main()
 5 {
 6     int num;
 7     printf("Please choose one of the following:\n");
 8     printf("1) copy files 2) move files\n");
 9     printf("3) remove files 4) quit\n");
10     printf("Enter the number of your choice:\n");
11     scanf("%d", &num);
12     while(num>=1&&num<=4)
13     {
14         if(num==4)
15         {
16             printf("quit\n");
17             break;
18         }
19         switch(num)
20         {            
21             case 1: printf("copy files");
22                 break;
23             case 2: printf("move files");
24                 break;
25             case 3: printf("remove files");
26                 break;
27             default:printf("please input number only from 1~4");
28         }
29         printf("\n");
30         printf("Enter the number of your choice:\n");
31         scanf("%d", &num);
32     }
33                 
34 
35     system("pause");
36     return 0;
37 }

p243.5

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 void larger_of(double *, double *);
 4 
 5 int main(void)    
 6 {
 7     double x, y;
 8     scanf("%lf%lf", &x, &y);
 9     printf("x is %lf y is %lf.\n", x, y);
10     larger_of(&x, &y);
11     printf("Now x is %lf y is %lf.\n", x, y);
12     system("pause");
13     return 0;
14 }
15 
16 void larger_of(double *x, double *y)
17 {
18     if(*x>*y)
19         *y=*x;
20     else *x=*y;
21 }

p243.6

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int get_ch(int);
 4 
 5 int main(void)    
 6 {
 7     int ch, num;
 8     while((ch=getchar())!=‘#‘)
 9     {
10         num=get_ch(ch);
11         if(num>0)
12             printf("the char is a letter. number is %d\n", num);
13         else
14             printf("the char is not a letter.\n");
15     }
16 
17     system("pause");
18     return 0;
19 }
20 
21 int get_ch(int ch)
22 {
23     int i;
24     if(ch>=‘A‘&&ch<=‘Z‘)
25     {
26         i=ch-‘A‘+1;
27         return i;
28     }
29     else if(ch>=‘a‘&&ch<=‘z‘)
30     {
31         i=ch-‘a‘+1;
32         return i;
33     }
34     else return -1;
35 }

p243.7

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 double power(double, int);
 4 
 5 int main(void)    
 6 {
 7     double n;
 8     int exp;
 9     printf("please enter a number and a exp.\n");
10     while(scanf("%lf%d", &n, &exp)==2)
11     printf("%lf to the power %d is %lf\n", n, exp, power(n, exp));
12     
13     system("pause");
14     return 0;
15 }
16 
17 double power(double n, int p)
18 {
19     int i; 
20     double pow=1;
21     
22     if(n!=0,p>0)
23     {
24         for(i=1;i<=p;i++)
25             pow*=n;
26         return pow;
27     }
28     else if(n!=0,p<0)
29     {
30         for(i=1;i<=-p; i++)
31             pow*=n;
32         return 1.0/pow;
33     }
34     else if(n!=0,p=1)
35         return 1;
36     else if(n=0)
37         return 0;
38 }

 

c primer plus 习题答案(2)

标签:

原文地址:http://www.cnblogs.com/coding-time/p/4526596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!