美文网首页
第7章 c控制语句分支和跳转

第7章 c控制语句分支和跳转

作者: 小风xf | 来源:发表于2018-11-24 11:09 被阅读0次

    求出温度第一零度的天数的百分率
    #include <stdio.h>

    void main()

    {

        const int FREEZING = 0;

        float temprature ;

        int cold_dat = 0;

        int all_days= 0;

        printf("Enter the list of daily low temperature \n");

        printf("Use celsius and enter q to quit .\n");

        while (scanf ("%f",&temprature) == 1)

        {

            all_days++;

            if (temprature < FREEZING) {

                cold_dat ++;

            }

        }

        if (all_days != 0) {

            printf("%d day total : %1.f  %% were below freezing .\n",all_days,100.0* (float)cold_dat/all_days);

        }

        if (all_days == 0) {

            printf("%d no data entered" );

        }

    }

    if只要不为0 就打开。

    #include <stdio.h>

    void main()

    {

        if (-1) {

            printf("hello =  -1");

        }

        if (0) {

            printf("hello =0");

        }

        if (-1) {

            printf("hello =  -1");

        }

         printf("hello =  0");

    }

    改变输入,只保留其中的空格

    #include <stdio.h>

    #define SPACE ' '

    void main()

    {

        char ch ;

        ch =getchar();

        while (ch != '\n') {

            if (ch == SPACE) {

                putchar(ch);

            }

            else  {

                putchar(ch +1);

            }

            ch =getchar();

        }

    }

    #include <stdio.h>

    #define SPACE ' '

    void main()

    {

        char ch ;

        while ( (ch = getchar() )!= '\n') {

            if (ch == SPACE) {

                putchar(ch);

            }

            else  {

                putchar(ch +1);

            }

        }

    }

    改变输入,只保留非字母字符

    #include <stdio.h>

    #include <ctype.h>

    void main()

    {

        char ch;

        while ((ch = getchar()) != '\n') {

            if (isalpha(ch)) {

                putchar(ch +1);

            }

            else

            {

                putchar(ch);

            }

        }

        putchar(ch);//打印换行字符

    }

    j计算用电账目

    #include <stdio.h>

    #define RATE1 0.12589

    #define RATE2 0.17901

    #define RATE3 0.20971

    #define BRESK1 360.0

    #define BRESK2 680.0

    #define BASE1 (RATE1*BRESK1)

    #define BASE2 ( BASE1 + (RATE2 * ( BRESK2 - BRESK2)))

    void main()

    {

        double kwh;

        double bill;

        printf("please  enter the kwh used .\n" );

        scanf("%lf",&kwh);

        if (kwh <= BRESK1) {

            bill =RATE1 * kwh;

        }

        else if (kwh <= BRESK2)

        {

            bill =BASE1 + (RATE2 * (kwh - BRESK1));

        }

        else bill = BASE2 + ( RATE3 * (kwh - BRESK2) );

        printf("the chare for %.1f kwh is $ %.2f .\n",kwh , bill);

    }

    使用嵌套if 现实一个数的约数
    #include <stdio.h>

    #include <stdbool.h>

    main()

    {

        unsigned longnum ,  div;

        bool isPrme;

        printf("Please enter an integer for analysis ");

        printf("enter q to qutie .\n");

        while (scanf("%lu",&num ) ==1  ) {

            for (div = 2 , isPrme = true ; (div * div) <= num ; div++)

            {

                if (num %div ==0) {

                    if ((div *div) != num)

                    {

                        printf("%lu is divisidle by %lu and %lu .\n",num,div ,num/div  );

                    }

                    else printf("%lu is divisivle %lu .\n ", num ,div);

                }

                isPrme =false;

            }

            if (isPrme) {

                printf("%lu is prime .\n",num);

            }

            printf("Please enter an integer for analysis ");

            printf("enter q to qutie .\n");

        }

        printf("bey.\n");

    }

    #include

    #define PERIOD '.'

    void main()

    {

        int ch , charcount = 0 ;

        while ((ch = getchar()) != PERIOD  ) {

            if (ch != '"' && ch != '\'')

                charcount ++ ;

        }

        printf("There are %d non-quote character .\n",charcount);

    }

    统计字符 单词和行

    #include <stdio.h>

    #include <ctype.h>

    #include <stdbool.h>

    #define STOP '|'

    void main()

    {

        char c , prev ;

        long n_chas = 0L;

        intn_lines  =0;

        int n_words = 0;

        int n_plines = 0;

        bool inword = false;

        printf ("Enter text to be analyzed \n") ;

        prev ='\n';

        while ((c = getchar() != STOP)) {

            n_chas ++;

            if (c == '\n') {

                n_lines ++;

            }

            if ( !isspace(c) && !inword)

            {

                inword =true;

                n_words ++;

            }

            if(    isspace(c) && inword)

            {

                inword =false;

            }

            prev = c;

        }

        if (prev != '\n') {

            n_lines =1;

        }

        printf("character = %ld , words  = %d , lines = %d ", n_chas,n_words ,n_lines );

        printf("partial lines = %d \n", n_lines);

    }

    三木运算符

    #include <stdio.h>

    #define COVERAGE 200

     void main()

    {

        int sq_feet ,cans ;

        printf("Enter number of square feet to be painted :\n");

        while (scanf("%d" , &sq_feet) == 1  ) {

            cans = sq_feet /COVERAGE;

            cans += ((sq_feet %COVERAGE == 0))? 0 : 1;

            printf(" your need %d %s of paint .\n",cans ,cans == 1 ? "can " : "cans");

            printf("enter next value (q to quit ) \n");

        }

    }

    continue 

    #include <stdio.h>

    #define COVERAGE 200

     void main()

    {

        const float MIN = 0.0f ;

        const float MAX = 100.0f;

        float score ;

        floattotal  =0.0f;

        int n = 0;

        float min = MAX;

        float max = min;

        printf("Enter the first score (q to quit )");

        while (scanf("%f",&score ) == 1) {

            if (score < MIN || score > MAX) {

                printf("%0.1f is an invalid value try again ", score );

                continue ;

            }

            printf("Accepting %0.1f \n",score);

        }

        if (n > 0) {

            printf("Average of %d scores is %0.1 f .\n",n,total/n);

            printf("low = %0.1f , high = %0.1f \n ",min ,MAX);

        }

        else

            printf(" no valid \n");

    }

    inu

    #include <stdio.h>

    #define COVERAGE 200

     void main()

    {

        const float MIN = 0.0f ;

        const float MAX = 100.0f;

        float score ;

        floattotal  =0.0f;

        int n = 0;

        float min = MAX;

        float max = min;

        printf("Enter the first score (q to quit )");

        while (scanf("%f",&score ) == 1) {

            if (score < MIN || score > MAX) {

                printf("%0.1f is an invalid value try again ", score );

                continue ;

            }

            printf("Accepting %0.1f \n",score);

            min = (score < min )? score : min ;

            max = (score > max )? score : max;

            total += score ;

            n ++ ;

            printf("enter netxt score (q to quit )");

        }

        if (n > 0) {

            printf("Average of %d scores is %0.1 f .\n",n,total/n);

            printf("low = %0.1f , high = %0.1f \n ",min ,MAX);

        }

        else

            printf(" no valid \n");

    }

    使用break 退出循环

    #include <stdio.h>

     void main()

    {

        float lengh , width ;

        printf("Length  the length of the rectangle \n");

        while (scanf("%f" , &lengh) == 1) {

            printf("length = %0.2f",lengh);

            printf("Enter its width \n");

            if (scanf("%f",&width) != 1) {

                break;

            }

            printf("Width = %0.2f \n",width);

            printf("Area = %0.2f :\n ",lengh * width);

            printf("enter the length of the rectangle \n");

        }

        printf("Done .\n");

    }

    GOTO 语句

    #include <stdio.h>

     void main()

    {

        int num;

        float cost = 12.0;

        scanf("%d",&num );

        if (num > 12 ) {

            goto a;

        }

        goto b;

    a:  cost = cost *1.05;

        printf("cost = %f \n " , cost);

    b:

        printf(" this is b  \n");

    }

    相关文章

      网友评论

          本文标题:第7章 c控制语句分支和跳转

          本文链接:https://www.haomeiwen.com/subject/nhugfqtx.html