• C控制语句--分支和跳转


    /*C控制语句--分支和跳转*/
    /*关键字 if else switch continue break case default goto
      运算符:&&(且) ||(或) ?:(三元运算符)
      函数 getchar() putchar()
      怎样使用if和if else 语句以及如何嵌套使用它们。
      使用逻辑运算符将关系表达式组合为更加复杂的判断表达式。
      C的条件运算符。
      swich语句。
      break、continue、和goto跳转。
      使用C的字符I/O函数 getchar()和putchar()。
      由ctype.h头文件提供的字符分析函数系列。
    */
    //求出温度低于零度的天数的百分率
    #include<stdio.h>
    
    int main(void)
    {
        const int FREEZING=0;  //定义一个整形常量
        float temperature;    //定义一个浮点型变量
        int cold_days=0;
        int all_days=0;
    
        printf("Enter the list of daily low temperatures.
    ");
        printf("Use Celsius, and enter q to quit.
    ");
        while(scanf("%f",&temperature)==1)  //判断输入的格式
        {
            all_days++;  //格式正确天数加1
            if(temperature<FREEZING)  //如果温度小于0
            {
                cold_days++;  //小于零度的天数加1
            }
        }
        if(all_days!=0) //天数不等于0输出
        {
            printf("%d days total; %.lf%% were below freezing.
    ",all_days,100.0*(float)cold_days/all_days);
    
        }
        if(all_days==0)//天数等于0输出
        {
            printf("No data entered!
    ");
        }
        system("pause"); //按任意键继续...
    
    
    
    }
    /*--统计字符、单词和行*/
    #include <stdio.h>
    #include <ctype.h>   //为isspace()提供函数原型
    #include <iso646.h>   
     
    #include<stdbool.h>
    
    #define STOP '|'
    
    int main(void)
    {
        char c;            //读入字符
        char prev;         //前一个读入字符
        long n_chars=0;    //字符数
        int n_lines=0;
        int n_words=0;
        int p_lines=0;
        bool inword=false;
    
        printf("输入一段文本用|分开
    ");
        prev='
    ';
        while(c=getchar()!=STOP)
        {
            n_chars++;
            if(c=='
    ')
            {
                n_lines++;
            }
            if(!isspace(c)&&!inword)
            {
                inword=true;
                n_words++;
            }
            if(isspance(c)&&inword)
            {
                inword=false;
            }
            prev=c;
        }
        if(prev!='
    ')
        {
            p_lines=1;
        }
        pirntf("characters=%1d,words=%d,lines=%d,",n_chars,n_words,n_lines);
        printf("partial lines=%d
    ",p_lines);
        system("pause");
    
    }
    /*--三元运算符*/
    #include <stdio.h>
    
    
    int main(void)
    {
        int n=0;
        int num;
        printf("请输入一个整数:
    ");
        scanf("%d",&n);
        num=(n<0)?-n:n;    //如果 n<0 那么num=-n 否则num=n;
        printf("绝对值为:%d",num);
        system("pause");
    
    }


    continue break goto  swich略。

  • 相关阅读:
    MVC初体验-过滤器(10)
    MVC阶段复习(一)
    搭建自己的视频文件网站
    答辩系统bug修改记录
    linux的pvtrace环境配置
    Linux使用期间命令积累
    php项目第三季
    分布式作业
    php第二季
    php错误记录
  • 原文地址:https://www.cnblogs.com/tangt/p/3734281.html
Copyright © 2020-2023  润新知