• 第7章,c语言控制语句:分支和跳转


    7.1  if语句

    通用形式:if(expression)

          statment

    7.2  if else语句

    通用形式:if(expression)

           startment

        else

           startment2

    7.2.1  字符输入,输出函数:getchar(),putchar()

    ch=getchar()类似scanf("%c",&ch)

    putchar(ch)类似printf("%c",ch)

    这两个函数定义在stdio.h文件中

    7.2.2  ctype.h系列字符函数

     字符映射函数不会修改原始参数。

     7.2.3  多重选择 else if

    7.2.4  else 与if配对

    规则else与离它最近的if匹配,除非最近的if被花括号括起来。

    7.2.5  多层嵌套if语句

    7.3  逻辑运算符

    与&&、或||、非!

    7.3.1  备选拼写:iso646.h

    and:&&、or:||、not:!。

    7.3.2

    !优先级只比圆括号低,

    &&比||优先级高,但是两者都比关系运算符低,比赋值运算符高。

    7.3.3  求值顺序

    逻辑表达式顺序从左往右,一旦发现有使整个表达式为假的的因素,便立即停止求值。

    7.5  条件运算符:?

    c语言使用条件表达式作为表达if else的一种快捷方式。

    该方式使用体哦阿建运算符,该运算符分为两部分,需要三个运算对象。

    通用表达形式:expression1?expression2;expression3

    如果expression1为真(非0),那么整个表达式的值和expression2相同,假和expression3相同。

    7.6  循环辅助:continue和break

    7.6.1  continue语句

    终止当前循环,进行到下一次循环。

    对于while而言,执行continue后,下一个被求值的是循环测试条件。

    对于for而言,执行continue后,下一个是对循环测试表达式求值。

    7.6.2  break 语句

    结束包含它的循环,执行下一阶段

    7.7  多重选择 switch和break

    7.7.1  switch语句

    switch(expression)

    {

      case label1:statement1

      case label2:startment2

    }

    程序根据expression的值跳入相应的case值处,执行剩下所有语句,除非执行break语句进行重定向。

    如果没有和case值相匹配的,控制则跳到标签带有default的语句,,否则将执行紧跟在switch语句后面的语句。

    default:标签行,如果上述没有匹配的标签,执行到该语句时,就跳转到指定的标签行。

    break:让程序离开switch语句

    7.8  goto语句

    goto 语句有两部分:goto 和标签名

    例:goto part2;

    part2:printf(“”);

    7.12  编程练习

    1.

    #include <stdio.h>
    int main(void)//报告读取的空格数,换行符数,和所有其他字符数
    {
        char ch;
        int a=0,b=0,c=0;
        printf("enter
    ");
        while((ch=getchar())!='#')
        {
            if(ch==' ')
                a++;
            else if (ch=='
    ')
                b++;
            else
                c++;
        }
        printf("空格数%d__换行符%d__其他字符%d",a,b,c);
        return 0;
    }

    2.

    #include <stdio.h>
    #include <ctype.h>
    int main(void)//将字符转化为ascll对应的十进制值
    {
        char ch;
        int i=0;
        printf("please enter one sentence
    ");
        while((ch=getchar())!='#')
        {
            if(isgraph(ch))
            {
            i++;
                if(i%9!=0)
                printf("%d ",ch);
                if(i%9==0)
                printf("
    %d ",ch);
            }
        }
        printf("
    %d",i);
        return 0;
    }

     3.

    #include<stdio.h>
    
    int main()
    {
        int integer=1;
        int even=0;
        int odd=0;
        int i=0,j=0;
        printf("please enter integer
    ");
        while ((scanf("%d",&integer)==1)&&integer!=0)
        {
            if((integer%2)==0)
           {    i++;
                even=even+integer;
           }
            else
            {
                j++;
                odd=odd+integer;
            }
        }
        printf("偶数平均值%d,偶数个数%d,奇数平均值%d,奇数个数%d
    ",even,i,odd,j);
        getchar();
        getchar();
        return 0;
    }

     4.注意字符替换

    #include<stdio.h>
    int main()
    {
        char ch=0;
        int i=0,j=0;
        printf("please enter integer
    ");
        while ((ch=getchar())!='#')
        {
            if(ch=='.')
           {   
            putchar('!');
            i++;
           }
            else if(ch=='!')
           {   
            putchar('!');
            putchar('!');
            j++;
           }
            else 
            {
                printf("%c",ch);
            }
        }
        printf("%d",i+j);
        getchar();
        getchar();
        return 0;
    }

    5.

    #include<stdio.h>
    int main()
    {
        char ch=0;
        int num=0;
        int i=0,j=0;
        printf("please enter integer
    ");
        while ((ch=getchar())!='#')
        {
            switch (num=ch)
            {
            case '.':
                 putchar('!');
                i++;
            break;
            case '!':
                putchar('!');
                putchar('!');
                j++;
            break;
            default:
                printf("%c",ch);
            }
        }
        printf("%d",i+j);
        getchar();
        getchar();
        return 0;
    }

    6.

    #include<stdio.h>
    int main()
    {
        char ch=0;
        int num=0;
        int i=0,j=0;
        printf("please enter integer
    ");
        while ((ch=getchar())!='#')
        {
         if(ch=='e')
         {
             i=1;
         }
         else if((i==1)&&(ch=='i'))
         {
             j++;
             i=0;
         }
         else
         {
             i=0;
         }
         
        }
        printf("%d",j);
        getchar();
        getchar();
        return 0;
    }

    7.

    #include<stdio.h>
    #define a 1000
    #define b 1.5
    #define c 0.15
    #define d 0.20
    #define e 0.25
    int main()
    {
        char ch=0;
        int num=0;
        int i=0,j=0;
        float total_wage=0.0;
        float taxes =0.0;
        float net_income=0.0;
        printf("please enter the number of working hours per week
    ");
        scanf("%d",&num);
        if(num>40)
        {
            total_wage=((num-40)*b+40)*a;
        }
        else
        {
            total_wage=num*a;
        }
        if(total_wage<300)
        {
            taxes=total_wage*c;
        }
        else if(total_wage<450)
        {
            taxes=45+(total_wage-300)*d;
        }
        else
        {
            taxes=75+(total_wage-450)*e;
        }
        printf("%.2f,%.2f,%.2f",total_wage,taxes,total_wage-taxes);
        getchar();
        getchar();
        return 0;
    }

    8.

    #include<stdio.h>
    #define b 1.5
    #define c 0.15
    #define d 0.20
    #define e 0.25
    int main()
    {
        char ch=0;
        int num=0;
        int gread=0;
        float a=0;
        float total_wage=0.0;
        float taxes =0.0;
        float net_income=0.0;
        do
        {
            printf("******************************
    ");
            printf("enter the number corresponding to the desired pay rate or action:
    ");
            printf("1) $8.75/hr		");
            printf("2) $9.33/hr
    ");
            printf("3) $10.00/hr		");
            printf("4) $11.20/hr
    ");
            printf("5)  quit
    ");
            printf("******************************
    ");
            scanf("%d",&gread);
            switch (gread)
            {
                case 1:
                a=8.75;
                break;
                case 2:
                a=9.33;
                break;
                case 3:
                a=10.00;
                break;
                case 4:
                a=11.20;
                break;
                case 5:
                break;
            }   
            if((gread>=1)&&(gread<=4))
            {
                printf("please enter the number of working hours per week
    ");
                scanf("%d",&num);
                if(num>40)
                {
                    total_wage=((num-40)*b+40)*a;
                }
                else
                {
                    total_wage=num*a;
                }
    
                if(total_wage<300)
                {
                    taxes=total_wage*c;
                }
                else if(total_wage<450)
                {
                    taxes=45+(total_wage-300)*d;
                }
                else
                {
                    taxes=75+(total_wage-450)*e;
                }
                printf("%.2f,%.2f,%.2f
    ",total_wage,taxes,total_wage-taxes);
            }
            else
            {
                printf("请重新选择正确的选项");
            }
        } while (gread!=5);
        getchar();
        getchar();
        return 0;
    }

    9

  • 相关阅读:
    0环与3环通信
    NACTF Encoder
    内核空间与内核模块
    Reserve ctf SSE_KEYGENME VAX2学习
    Inf2Cat Tool Output: ........................ Signability test failed.
    wustctf2020_closed
    ciscn_2019_final_5
    ciscn_2019_en_3 tcache
    内核编程基础
    保护模式阶段测试说明
  • 原文地址:https://www.cnblogs.com/suwencjp/p/12308582.html
Copyright © 2020-2023  润新知