C语言 电梯函数
#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;
}