C語言 電梯函數

來源:互聯網
上載者:User

標籤:

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

void test(){//漢字輸出

    printf("THIS IS TEST\n");

    printf("My age is %d\n",26);

    printf("My age is %4d  發現沒?26前面多了兩個空格\n",26);

    printf("My age is %d,heighe is %f,name is %s,sex is %c\n",26,1.55,"李明傑",‘A‘ );//雙引號字串(漢字屬於字串)用S,單引號字元用C

    printf("My age is %d,heighe is %.2f,name is %s,sex is ‘%c‘\n",26,1.55,"李明傑",‘A‘ );// %.2f代表2位小數,‘%c‘輸出時才會帶上單引號,輸出不會幫你單上單引號的

    printf("sex is %s\n","男");

 

}

 

void test1(){//?:條件陳述式運用

    printf("THIS IS TEST1\n");

    int a,b,c;

    scanf("%d %d %d",&a,&b,&c);

    int d=(a>b?a:b)>(c)?(a>b?a:b):(c);

    printf("%d\n",d);

}

 

 

void test2(){//電梯函數

    printf("THIS IS TEST2\n");

    int z=1,b,c;//預設為了TEST1中的第一個輸入的數。

leap: {

       printf("Welcome to take the elevator\ninput the floor you want to go,please...\n");

    scanf("%d",&c);

    srand((unsigned int)time(0));

    b=rand()%103+1;//產生客戶所在層數

    printf("You are located in the first layer of %d,now.\n You will go to the %d layer\n The elevator is now located in the first layer of %d\n Wait a moment,please...\n",b,c,z);

    if (z>b)

    {//電梯靠近

        for (int i=1; i<=z-b; i++)

        {

            printf("%d\n",z-i);

        }

    }

    else if (z<b){

        for (int i=1; i<=b-z; i++)

        {

            printf("%d\n",z+i);

        }

    }

    else{

        printf("電梯就在這一層\n");

    }

    printf("將要開門,請注意安全!!!\n");//開關門函數

    printf("將要關門,請注意安全!!!\n");

    if (b>c)//乘坐電梯到達目的地

    {

        for (int i=1; i<=b-c; i++)

        {

            printf("%d\n",b-i);

        }

    }

    else if (b<c){

        for (int i=1; i<=c-b; i++)

        {

            printf("%d\n",b+i);

        }

    }

    else{

        printf("電梯就在這一層,你運氣真好");

    }

    printf("將要開門,請注意安全!!!\n歡迎你再次乘坐\n");//開關門函數

    printf("將要關門,請注意安全!!!\n");

    z=c;//讓程式記住當前電梯所在樓層

}

    goto leap;

}

 

void test3(){//電梯函數已解決

    printf("THIS IS TEST3\n");

    int a=1,b,c;//預設為了TEST1中的第一個輸入的數。

    printf("恭喜你成為此程式此次啟動並執行第一個乘客!!!!\n");

leap: {

    printf("Welcome to take the elevator\ninput the floor you want to go,please...\n");

    scanf("%d",&c);

    if (c>103) {

        printf("輸入錯誤!!!\n請重新輸入\n");

    }

    else {

    srand((unsigned int)time(0));

    b=rand()%103+1;//產生客戶所在層數

    printf("You are located in the first layer of %d,now.\n You will go to the %d layer\n The elevator is now located in the first layer of %d\n Wait a moment,please...\n",b,c,a);

    if (a>b)

    {//電梯靠近

        for (int i=1; i<=a-b; i++)

        {

            printf("%d\n",a-i);

        }

    }

    else if (a<b){

        for (int i=1; i<=b-a; i++)

        {

            printf("%d\n",a+i);

        }

    }

    else{

        printf("電梯就在這一層\n");

    }

    printf("將要開門,請注意安全!!!\n");//開關門函數

    printf("將要關門,請注意安全!!!\n");

    if (b>c)//乘坐電梯到達目的地

    {

        for (int i=1; i<=b-c; i++)

        {

            printf("%d\n",b-i);

        }

    }

    else if (b<c){

        for (int i=1; i<=c-b; i++)

        {

            printf("%d\n",b+i);

        }

    }

    else{

        printf("電梯就在這一層,你運氣真好");

    }

    printf("將要開門,請注意安全!!!\n歡迎你再次乘坐\n");//開關門函數

    printf("將要關門,請注意安全!!!\n");

    a=c;

    }

}

    goto leap;

}int main(int argc, const char * argv[]) {

    

    printf("Hello, World!\n");

    test();

    test1();

    test2();//已解決電梯停留層數隨機。

    test3();//已解決不記錄當前樓層問題。

    return 0;

}

C語言 電梯函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.