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

来源:互联网 发布:177pic info新域名 编辑:程序博客网 时间:2024/06/01 19:35
/*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.\n");    printf("Use Celsius, and enter q to quit.\n");    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.\n",all_days,100.0*(float)cold_days/all_days);    }    if(all_days==0)//天数等于0输出    {        printf("No data entered!\n");    }    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("输入一段文本用|分开\n");    prev='\n';    while(c=getchar()!=STOP)    {        n_chars++;        if(c=='\n')        {            n_lines++;        }        if(!isspace(c)&&!inword)        {            inword=true;            n_words++;        }        if(isspance(c)&&inword)        {            inword=false;        }        prev=c;    }    if(prev!='\n')    {        p_lines=1;    }    pirntf("characters=%1d,words=%d,lines=%d,",n_chars,n_words,n_lines);    printf("partial lines=%d\n",p_lines);    system("pause");}
/*--三元运算符*/#include <stdio.h>int main(void){    int n=0;    int num;    printf("请输入一个整数:\n");    scanf("%d",&n);    num=(n<0)?-n:n;    //如果 n<0 那么num=-n 否则num=n;    printf("绝对值为:%d",num);    system("pause");}


continue break goto  swich略。

0 0