1. 程式人生 > >c程學習記錄

c程學習記錄

打算來記錄一下c程的幾個小思維

1.將一個數的個十百千萬依次輸出

規律:除10再模10

while()

{

  a = num % 10;

  num /= 10;

}

2.不用break的switch函式

如果我們在計算利率稅率等問題時,有時可以不用break來寫程式:倒著寫

#include<stdio.h>

#include<stdlib.h>

#define num 100000

 

int main()

{

    double profit,bonus,temp;

    bonus=0;

    printf("請輸入利潤:");

    scanf("%lf",&profit);

    temp=profit;

    int c;

 

    c=temp/num;

    if(c>10)c=10;

    switch(c)

    {

    case 10:bonus=bonus+(temp-1e6)*0.01;

            temp=1e6;

    case 9 :

    case 8 :

    case 7 :

    case 6 :bonus=bonus+(temp-6e5)*0.015;

            temp=6e5;

    case 5 :

    case 4 :bonus=bonus+(temp-4e5)*0.03;

            temp=4e5;

    case 3 :

    case 2 :bonus=bonus+(temp-2e5)*0.05;

            temp=2e5;

    case 1 :bonus=bonus+(temp-1e5)*0.075;

            temp=1e5;

    case 0 :bonus=bonus+temp*0.1;

 

    }

    printf("獎金總數為:%.2f",bonus);

    return 0;

}

3.列印菱形

注意行和列的控制選擇,找到他們之間的對應關係

#include<stdio.h>

#include<stdlib.h>

 

 

int main()

{

    int i,j;

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

    {

        for(j = 0;j <= 2-i;j++)

        {

            printf(" ");

        }

        for(j=0;j <= 2*i;j++)

        {

            printf("*");

        }

        printf("\n");

    }

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

    {

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

        {

            printf(" ");

        }

        for(j=0;j <= -2*i + 4;j++)

        {

            printf("*");

        }

        printf("\n");

    }

    return 0;

}