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;

}

原文地址:https://www.cnblogs.com/zlszls3113373723/p/9912460.html