第四周:選擇結構的程式設計的習題(北理)

Carb_5683發表於2016-10-26

對於輸入的a、b、c三個整數求最小值。
以下程式有錯,如何改呢?

int main()
{
    int  a,b,c;
    scanf(”%d%d%d”,a,b,c);
    if((a>b)&&(a>c))
     if(b<c)
printf(”min=%d\n”,b);
else 
printf(”min=%d\n”,c);
if((a<b)&&(a<c))
printf(”min=%d\n”,a);
}

下列程式執行後t 的值為4,則執行時輸入a,b的值範圍。

#include <stdio.h>

int main()
{
    int a, b, s = 1,t = 1;
    scanf("%d ,%d",&a,&b);
    if (a > 0)
        s += 1;
    if (a > b)
        t += s;
    else if (a == b)
        t = 5;
    else
        t = 2 * s;
    printf("s=%d,t=%d\n",s,t);
}

下列程式當a的值為014和ox14時執行結果分別是:

#include <stdio.h>

int main()
{
    int a;

    scanf("%o", a);
    if (a = 0xA | a > 12)
        if (011 & 10 == a)
            printf("%d!\n", a);
        else
            printf("Right!%d\n", a);
    else
        printf("Wrong!%d\n",a);
    return 0;
}

3.1 實現路徑中的程式選擇

執行下列程式段後,x、y和z的值分別是 20 30 30

int x=10,y=20,z=30;
if(x>y)  z=x;x=y;y=z;
************************
int x = 10, y = 20, z = 30;
if (x > y)
z = x;
x = y;
y = z;
不執行第一個語句,繼續執行之後的語句

求解一元二次方程

#include <stdio.h>
#include <math.h>

int main()
{
    float a, b, c, d, x1, x2, q, m, n;
    printf("Please input a,b,c\n");
    scanf("%f,%f,%f",&a,&b,&c);

    d = b*b - 4*a*c;
    if (d >= 0)
    {
        q = sqrt(d);
        x1 = (-b + q) / (2 * a);
        x2 = (-b - q) / (2 * a);
        printf("x1 =%.2f,x2=%.2f\n",x1,x2);
    }
    else
    {
        m = b / (2 * a);
        n = sqrt(-d) / (2 * a);
        printf("x1 = %.2f + %.2fi",m,n);
        printf("x1 = %.2f + %.2fi",m,n);
    }
}

3.2路徑中的再選擇—-巢狀判斷

任意的三個數從大到小的排序

#include <stdio.h>

int main()
{
    int a, b, c;
    printf("請輸入三個整數 ");
    scanf("%d %d %d",&a,&b,&c);

    if (a > b)
    {
        if (c > a)
            printf("%d %d %d\n",c,a,b);
        else //c和b
        {
            if (b > c)
                printf("%d %d %d\n", a, b, c);
            else
                printf("%d %d %d\n", a, c, b);
        }

    }
    else//b>a
    {
        if (c > b)
            printf("%d %d %d\n",c,b,a);
        else
        {
            if (a > c)
                printf("%d %d %d\n", b, a, c);
            else
                printf("%d %d %d\n",b,c,a);
        }
    }
}

三個數排序之後的優化程式

#include <stdio.h>

int main()
{
    int a, b, c,t;
    printf("請輸入三個數 :");
    scanf("%d %d %d %d",&a,&b,&c);

    if (a < b)
    {
        t = a;
        a = b;
        b = t;
    }
    if (b < c)
    {
        t = b;
        b = c;
        c = t;
        if (a > b)
        {
            t = a;
            a = b;
            b = t;
        }
    }
    printf("從大到小:%d,%d,%d",a,b,c);

    return 0;
}

自己編的錯的程式,如何更改呢?

#include <stdio.h>

int main()
{
    int a, b, c,t;
    printf("請輸入三個整數:");
    scanf("%d %d %d",&a,&b,&c);

    if (a > b)//交換a,b
    {
        t = a;
        b = a;
        a = t;
    }
    else
    {
        if (b > c)
        {
            t = b;
            b = a;
            a = t;
        }
        else
        {
            if (a > b)
            {
                t = a;
                a = b;
                b = t;
            }
        }
    }
}

3.3 多級選擇問題的C程式

多級選擇
多級選擇和巢狀選擇有什麼區別?
來自課件“討論題2”

關於switch
switch可以巢狀使用嗎?
來自課件“討論題3”

#include <stdio.h>

int main()
{
    int year;
    float money, rate, total;

    printf("Input money and year: ");
    scanf("%f %d",&money,&year);

    if (year == 1)
        rate = 0.0032;
    else if (year == 2)
        rate = 0.0041;
    else if (year == 3)
        rate = 0.005;
    else if (year == 5)
        rate = 0.0055;
    else if (year == 6)
        rate = 0.0065;
    else
        rate = 0.0;
    total = money + money*rate * 12 * year;
    printf("Total = %.2f\n",total);

    return 0;
}

3.4 多分支問題的程式選擇

(一)用switch解決多分支問題

(二)實現加減練習題
編寫一個程式。實現簡單的加減乘除

#include <stdio.h>

int main()
{
    float d1, d2;//定義運算元
    char op; //操作符變數
    scanf("%f %c %f",&d1,&op,&d2);

    switch (op)//根據操作符分別進行處理
    {
    case '+':
        printf("%.2f +%.2f = %.2f\n",d1,d2,d1+d2);
        break;
    case '-':
        printf("%.2f - %.2f =%.2f\n",d1,d2,d1-d2);
        break;
    case '*':
        printf("%.2f - %.2f =%.2f\n",d1,d2,d1*d2);
        break;
    case '/':
        if (d2 == 0)//判斷除數是否為0
            printf("Division by zero.\n");
        else
            printf("%.2f/%.2f=%.2f\n",d1,d2,d1/d2);
        break;
    }
}

(三)switch的結構

型變數,以下正確的switch語句是      
A. switch (a/b)                       
    {   case 1: case 3.2: y=a+b; break ;     
        case 0: case 5:   y=a-b;              
    }
B. switch (a*a+b*b);
    {case 3:
     case 1: y=a+b; break ;
     case 0: y=b-a; break; 
    }
C. switch a                            
    { default : x=a+b;                     
      case 10 : y=a-b;break;         
      case 11 : y=a*d; break; 
    }
D.  switch(a+b)
    {case 10: x=a+b; break;
     case 11: y=a-b; break;
    }

3.5 goto 語句的適當使用

不提倡使用goto語句

goto 語句的簡單例項
簡單例項

#include <stdio.h>

int main()
{
    int n = 0;
    printf("input a string: \n");

loop:if (getchar() != '\n')
   {
    n++; 
    goto loop;
   }
     printf("%d",n);
}
#include <stdio.h>

int main()
{
    int n = 0;
    printf("input a string: \n");

    while (getchar() != '\n')
    {
        n++;
    }

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

3.6 選擇結構的程式例項

計算稅金
按目前國家稅收標準,如果月收入1.58萬元,應交國家的稅金是多少?

來自課件“討論題4”

輸出奇數
能正確表達“當x的值是[-1,100]或[200,210]範圍內的奇數時,輸出x”的if語句應該怎樣寫?
來自課件“討論題5”

輸入某年某月某日,判斷並輸出這一天是這一年的第幾天
問題分析:
輸入年月日,計算當前月份之前的天數 + 當月的天數
注意:如何判斷閏年
是閏年而且月份超過2月則給總天數再加1.

#include <stdio.h>

int main()
{
    int day, month, year,sum;
    printf("請輸入某年某月某日:");
    scanf("%d %d %d",&year,&month,&day);

    switch (month)//計算當前月之前的月份的天數
    {
        case 1:
            sum = 0;
            break;
        case 2:
            sum = 31;
            break;
        case 3:
            sum = 59;
            break;
        case 4 :
            sum = 90;
            break;
        case 5:
            sum = 120;
            break;
        case 6:
            sum = 151;
            break;
        case 7:
            sum = 181;
            break;
        case 8:
            sum = 212;
            break;
        case 9:
            sum = 243;
            break;
        case 10:
            sum = 273;
            break;
        case 11:
            sum = 304;
            break;
        case 12:
            sum = 334;
            break;

    }
    //判斷是否是閏年
    if (((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) && month >2)
        sum = sum + 1;如果是閏年的話,多加一天
    sum = sum + day;加上當前月的天數

    printf("總共%d day",sum);
}
#include <stdio.h>

int main()
{
    int year;
    float money, rate, total;
    printf("Input money and year:");
    scanf("%f %d",&money,&year);

    switch (year)
    {
    case 1:
        rate = 0.0032;

    case 2:
        rate = 0.0041;
            break;
    case 3:
        rate = 0.005;
            break;
    case 5:
        rate = 0.0055;
            break;
    case 8:
        rate = 0.0065;
            break;
    default:
        rate = 0.0;
    }
    total = money + money*rate * 12 * year;
    printf("Total = %.2f\n",total);
}

相關文章