• c primer plus 习题答案(2)


    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.
    ");
    10     while(scanf("%d%d%d",&x, &y, &z)==3)
    11     {
    12     num=largest(x, y, z);
    13     printf("the largest number is %d
    ", num);
    14     printf("please enter three numbers.");
    15     printf("enter q to quit.
    ");
    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:
    ");
     8     printf("1) copy files 2) move files
    ");
     9     printf("3) remove files 4) quit
    ");
    10     printf("Enter the number of your choice:
    ");
    11     scanf("%d", &num);
    12     while(num>=1&&num<=4)
    13     {
    14         if(num==4)
    15         {
    16             printf("quit
    ");
    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("
    ");
    30         printf("Enter the number of your choice:
    ");
    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.
    ", x, y);
    10     larger_of(&x, &y);
    11     printf("Now x is %lf y is %lf.
    ", 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
    ", num);
    13         else
    14             printf("the char is not a letter.
    ");
    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.
    ");
    10     while(scanf("%lf%d", &n, &exp)==2)
    11     printf("%lf to the power %d is %lf
    ", 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 }
  • 相关阅读:
    在sql server中怎样获得正在执行的Sql查询
    在windows中使用VMWare安装Mac OS 10.7
    Scrspy 命令
    Windows Service 小品
    线程同步(一)
    线程基础必知必会(二)
    线程基础必知必会(一)
    准备工作与简介
    Python 正则表达式急速入门
    SQL Server 每日一题--每月销售额
  • 原文地址:https://www.cnblogs.com/coding-time/p/4526596.html
Copyright © 2020-2023  润新知