⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 forgive.cpp

📁 joseph的程序对很多C初学者有着很大的帮助
💻 CPP
字号:
#include<stdio.h>
 #include<stdlib.h>
 struct {
 char status;
 int num;
 int time;
 }a;                                                  /*命令的结构*/
 typedef struct{
 int num;
 int time;
 }Element;                                      
 struct {
 Element *base;
 Element *top;
 int stacksize;
 }S,VS;                                          /*S为栈,VS为辅助栈*/
 void main(){
 typedef struct{
  int num;
  struct QNode *next;
  }QNode,*QueuePtr;
  QueuePtr l;
 struct {
 QueuePtr front;
 QueuePtr rear;
 }Q;                                                 /*队列*/
    int n,x,m=0,order,money,b=0;
    printf("\nInput the size of the garage and the cost per hour:");
    scanf("%d%d",&n,&x);
    S.base=(Element*)malloc(n*sizeof(Element));
    S.top=S.base;
    S.stacksize=n;
    VS.base=(Element *)malloc((n-1)*sizeof(Element));
    VS.top=VS.base;
    VS.stacksize=n-1;
    Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
    Q.front->next=NULL;                                                       /*各结构的初始化*/
    while (b!=1){
  printf("\nInput the order!:");
  scanf("%c,%d,%d",&(a.status),&(a.num),&(a.time));
  switch(a.status)
        {
  case 'E':b=1;break;
  case 'A':
    if (S.top-S.base<S.stacksize){
        (*(S.top)).num=a.num;
        (*(S.top)).time=a.time;
        S.top++;
        order=S.top-S.base;
        printf("The %d car is in the %d of garage!\n",a.num,order);
    }
    else {
         Q.rear=(QueuePtr)malloc(sizeof(QNode));
                                                           Q.rear->next=NULL;
         Q.front->next=Q.rear;
         Q.rear->num=a.num;
         m++;
         printf("The %d car is in the %d of Queue!\n",a.num,m);
    }
    break;
  case 'D':
    while ((*(--S.top)).num!=a.num){
          (*(VS.top)).num=(*(S.top)).num;
          (*(VS.top)).time=(*(S.top)).time;
          VS.top++;
    }
    money=(a.time-(*(S.top)).time)*x;
    printf("The %d car is out of %d of garage and the cost is %d!\n",a.num,S.top-S.base+1,money);
    while (VS.top!=VS.base){
          (*(S.top)).num=(*(--VS.top)).num;
          (*(S.top)).time=(*(VS.top)).time;
          S.top++;
    }
    if (m!=0){
       l=Q.front->next;
       (*(S.top)).num=l->num;
       (*(S.top)).time=a.time;
       S.top++;
       printf("The %d car is in the %d of garage!\n",l->num,S.stacksize);
       l=Q.front->next;
       Q.front->next=Q.front->next->next;
       free(l);
       m--;
    }
    break;
  default: printf("The order is wrong!\n");break;
  }
    }
    printf("\nThe program has finished!\n");
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -