公 告

欢迎各位网友添加友情链接,在您添加本博客:http://linux0818.blogspot.com/ 做为链接后, E-mail:linux0818@gmail.com给我,我将将您的网址添加到本博客。

2008年10月14日星期二

前段时间写的电梯程序

这个电梯程序没有用到多进程调度,所以看起来比较简单,有兴趣的慢慢看吧,呵呵
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3//电梯数,暂未用
#define M 20//楼层数
void delay(void);
int main(int argc,char **argv)
{
char up[]="up",down[]="down",direction[10],quit[]="quit";
int i=0,floor;

printf("电梯初始化状态,停留一楼,总共 %d 层!\n",M);
printf("请输入(up/quit): ");//电梯外面的乘客输入"上"或者"下",quit为退出
scanf("%s",direction);
if(strcmp(direction,up)==0)
        {
        printf("请输入楼层: ");
        scanf("%d",&floor);

        while(floor>i&&floor<M){
        printf("电梯运行情况: %d 楼!\n",++i);
        delay();
        if(i>M) i=1;
        if(floor<i) printf("第 %d 层已到,电梯停在本层\n",floor);}
        printf("电梯状态,停留 %d 楼\n",i);
        }
else if(strcmp(direction,quit)==0)
        {
        exit(0);
        }
else
        {
        printf("输入错误,退出!\n");
        exit(0);
        }
while(strcmp(direction,quit)!=0)//比较输入的字符串,相等则为零
        {
        printf("请输入(up/down/quit): ");//电梯外面的乘客输入"上"或者"下",quit为退出
        scanf("%s",direction);
        if(strcmp(direction,quit)!=0){
                printf("请输入楼层: ");
                scanf("%d",&floor);}

        if(strcmp(direction,up)==0)
                {
                if(floor>i)
                        {
                        while(floor>i&&floor<M){
                                printf("电梯运行情况: %d 楼!\n",++i);
                                delay();
                                if(i>M) i=1;
                                if(floor<i) printf("第 %d 层已到,电梯停在本层\n",floor);}
                        printf("电梯状态,停留 %d 楼\n",i);
                        }
                else if(floor<i)
                        {
                        ;
                        }
                else
                        {
                        printf("电梯保持原先状态\n");
                        }
                }
        else if(strcmp(direction,down)==0)
                {
                if(floor>i)
                        {
                        ;
                        }
       else if(floor<i)
                        {
                        while(floor<i&&floor<M&&floor>=1){
                                printf("电梯运行情况: %d 楼!\n",--i);
                                delay();
                                if(i<1) i=1;
                                if(floor>i) printf("第 %d 层已到,电梯停在本层\n",floor);}
                        printf("电梯状态,停留 %d 楼\n",i);
                        }
                }
        else if(strcmp(direction,quit)==0)
                {
                printf("退出,谢谢使用!\n");
                }
        else
                {
                printf("输入错误,请重输!\n");
                }
        }
return 0;
}
void delay(void)
{
int i,j,k;
for(i=0;i<5000;i++)
        {
        for(j=0;j<600;j++)
                {
                        for(k=0;k<60;k++)
                        {
                        ;;;;;;;;;
                        }
                }
        }
}


--
/**************************************/
Name: Xiong Feng
E-mail:linux0818@gmail.com
MSN:linux0818@hotmail.com
QQ:23562033
Address: GuangZhou.China
/**************************************/

没有评论:

发表评论