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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章