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.\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略。

转载于:https://www.cnblogs.com/tangt/p/3734281.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值