標籤:
#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語言 電梯函數