C語言程式設計 練習題參考答案 第三章 (2) 選擇結構

來源:互聯網
上載者:User

/* 3.6 求3個數中最大值。類似於例 1.2*/
#include <stdio.h>
void main( )
{
    int a, b, c, max;
    printf("\n 請輸入3個整數,整數以空格分隔:\n");
    scanf("%d%d%d",&a,&b,&c);
    if(a>b)     max=a;
    else     max=b;
    if(max<c)  max=c;
    printf("最大值是%d\n",max);    
}

/*3.6 求3個數中最大值。類似於例 1.2*/
#include <stdio.h>
void main( )
{
    int a, b, c, max;
    printf("\n 請輸入3個整數,整數以空格分隔:\n");
    scanf("%d%d%d",&a,&b,&c);
    max=a>b?a:b;
    max=max>c?max:c;
    printf("最大值是%d\n",max);    
}

/*3.7 輸入x求y的值。類似於例 3.13, 也可以用switch語句*/
#include <stdio.h>
#include <math.h>
void main( )
{
 float x, y;
 printf("\n 請輸入一個實數:\n");
 scanf("%f",&x);
    if( x>=0 && x<10)
            y=sin(x);
    if( x>=10 && x<20)
            y=cos(x);
    if( x>=20 && x<30)
            y=exp(x)-1;
    if( x>=30 && x<40)
            y=log(x+1);
    if( x<0 || x>=40)
          printf("無定義");
    else
          printf("y=%f\n",y);    
}
/*3.7 輸入x求y的值。類似於例 3.13,解法二*/
#include <stdio.h>
#include <math.h>
void main( )
{
 float x, y;
 printf("\n 請輸入一個實數:\n");
 scanf("%f",&x);

if(x>0 && x<=40)
      switch( (int)(x/10))
      {
       case 0: y=sin(x); printf("y=%f\n",y); break;     
       case 1: y=cos(x); printf("y=%f\n",y); break;     
       case 2: y=exp(x)-1; printf("y=%f\n",y); break;     
       case 3: y=log(x+1); printf("y=%f\n",y); break;     
      } 

else

    printf("無定義");
}

/*3.7 輸入x求y的值。解法三*/

# include<stdio.h>
# include<math.h>
void main()
{
      float x,y;
      scanf("%f",&x);
       if(x<0 || x>=40)
             printf ("無定義\n");
        else if(x>30)
             y=log(x+1);
        else if(x>20) 
                y=exp(x)-1;
        else if(x>10) 
                y=cos(x);
        else
                y=sin(x);
    if(x>=0 && x<40)
     printf ("y=%f\n",y); 
}

 /*  3.8 輸入一個百分製成績,給出提示, 類似於例3.14,也可以用switch語句*/

#include "stdio.h"
void main()
{
    int score;
    printf("請輸入一個分數(整數),樣本 85\n");
    scanf("%d",&score);
    if(score>=90 && score<=100)
        printf("excellent\n");
    if(score>=80 && score<=89)
        printf("good\n");
    if(score>=70 && score<=79)
        printf("middle\n");
    if(score>=60 && score<=69)
        printf("pass\n");
    if(score<60)
        printf("fail\n");
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.