1. 程式人生 > >三種基本程式結構

三種基本程式結構

一、順序結構

程式舉例:

1、財務人員給員工發工資時經常遇到這樣一個問題,即根據每個人的工資額(以元作為單位)計算出各種面值的鈔票的張數,且要求總張數最少。

例如,某職工工資為3436元,發放方案為:100元34張,20元1張,10元1張,5元1張,1元1張。

#include <stdio.h>

int main()

{

    int money;

    scanf("%d",&money);

    int m100;

    int m50;

    int m20;

    int m10;

    int m5;

    m100 = money / 100;

    money %= 100;

    m50 = money / 50;

    money %= 50;

    m20 = money / 20;

    money %= 20;

    m10 = money / 10;

    money %= 10;

    m5 = money / 5;

    money %= 5;

    printf("m100 = %d,m50 = %d,m20 = %d,m10 = %d,m5 = %d,m1 = %d\n",

    m100,m50,m20,m10,m5,money);

    return 0;

}

2、從鍵盤輸入一個大寫字母,要求改用小寫字母輸出。

#include <stdio.h>

int main()

{

    char ch;

    printf("please input ch = ");

    scanf("%c",&ch);

    if((ch >= 'A') && (ch <= 'Z'))

    {

        char ch_change = ch + 'a' - 'A';

        printf("%c\n",ch_change);         

    }

    else

    {

        printf("input is error\n");

    }

    return 0;

}

二、選擇結構

程式舉例:

1、判某一年是否閏年。輸入:年(year)計算:判是否閏年輸出:閏年或非閏年(leap)閏年條件:能被4整除,但不能被100整除。能被4整除,又能被400整除。

#include <stdio.h>

int main()

{

    int year;

    scanf("%d",&year);   

    int flag = 0;

    if(0 == year % 4)

    {

        if(0 == year % 100)

        {

            if(0 == year % 400)

            {

                flag = 1;

            }

        }

        else

        {

            flag = 1;

        }

    }

    if(1 == flag)

    {

        printf("this year = %d is leap\n",year);

    }

    else

    {

        printf("this year = %d is not leap\n",year);

    }

    return 0;

}

2、編寫一個能進行兩個運算元加減乘除四則運算的計數器模擬程式輸入:兩個運算元和運算子計算:根據運算子確定運算;輸出:運算結果。

#include <stdio.h>

int main()

{

    char op;

    float a,b;

    scanf("%f%c%f",&a,&op,&b);

    switch(op)

    {

        case '+':

            printf("=%5.2f\n",a+b);

            break;

        case '-':

            printf("=%5.2f\n",a-b);

            break;

        case '*':

            printf("=%5.2f\n",a*b);

            break;

        case '/':

            if(0 == b)

            {

                printf("b = 0\n");

            }

            else

            {

                printf("=%5.2f\n",a/b);

            }

        default:

            printf("op is error\n");

    }

    return 0;

}

三、迴圈結構

程式舉例:

1、計算一條語句中的空格、字母、數字的個數。

#include <stdio.h>

int main()

{

    int letter_count = 0;

    int space_count = 0;

    int num_count = 0;

    char ch;

    while(ch != '\n')

    {

        scanf("%c",&ch);

        if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))

        {

            letter_count++;

        }

        if(ch == ' ')

        {

            space_count++;

        }

        if(ch >= '0' && ch <= '9')

        {

            num_count++;

        }

    }

    printf("letter_count = %d,space_count = %d,num_count = %d\n",

            letter_count,space_count,num_count);

    return 0;

}

2、判斷數字m是否為素數(質數)。

#include <stdio.h>

int main()

{

    int num;

    scanf("%d",&num);

    int i;

    int count = 0;

    for(i = 2;i < num;i++)

    {

        if(0 == num % i)

        {

            count++;

        }

    }

    if(0 == count)

    {

        printf("True : %d\n",num);

    }

    else

    {

        printf("False : %d\n",num);

    }

    return 0;

}

3、題目:通過程式設計實現,統計0~n有多少個9 。提示:n通過引數傳入

#include <stdio.h>

int main()

{

    int i;

    int n;

    int count = 0;

    int temp;

    scanf("%d",&n);

    for(i = 0;i <= n;i++)

    {

        temp = i;

        while(0 != temp)

        {

            if(9 == temp % 10)

            {

                count++;

            }

            temp /= 10;

        }

    }

    printf("count = %d\n",count);

    return 0;

}