程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 程序設計訓練-能幫忙看一下Arrive和Out函數的問題嗎,謝謝

程序設計訓練-能幫忙看一下Arrive和Out函數的問題嗎,謝謝

編輯:編程解疑
能幫忙看一下Arrive和Out函數的問題嗎,謝謝

#include #include #include #include #define price 2//姣忓皬鏃舵敹璐� #define hprice 1//鍗婂皬鏃舵敹璐� #define INITSIZE 10 #define N 20 typedef struct{ int hour; int min; }Time; typedef struct{ int num[10];//杞︾墝鍙� Time time;//鍏ュ満鏃墮棿 int n;//鍏ュ満浣嶇疆 char data; }CarInform; typedef struct LNode{ char data; struct LNode next; }QNode,*QueuePtr; typedef struct { QueuePtr front; QueuePtr rear; }LQueue; typedef struct { char *base; char *top; int stacksize; }Stack; /**************棧******************/ int InitStack(Stack S) { S->base=NULL; //S->base=(char)malloc(INITSIZE*sizeof(char)); if(!S->base)return false; S->base=S->top; S->stacksize=INITSIZE; return true; } int StackEmpty(Stack S) { if(S->base==S->top)return true; else return false; } int PopStack(Stack *S,char *e) { if(!StackEmpty(S)) { e=S->top; S->top--; return true; } return 0; } int PushStack(Stack *S, char e) { if(S->top-S->base==S->stacksize)return false; *S->top=e; S->top++; return true; } void PrintStack(Stack *S) { while(S->base!=S->top) { printf("%2c",S->top); S->top++; } } /*******************闃熷垪******************************/ int InitQueue(LQueue Q) { Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode)); if(!Q->front)return false; Q->front->next=NULL; return true; } int EnQueue(LQueue *Q,char e) { QueuePtr P; P=(LNode)malloc(sizeof(QNode)); if(!P)return false; P->data=e; P->next=NULL; Q->rear->next=P; Q->rear=P; return true; } int DeQueue(LQueue Q,char e) { QueuePtr P; if(Q->front==Q->rear)return false; P=Q->front->next; e=P->data; Q->front->next=P->next; if(Q->rear==P)Q->rear=Q->front; free(P); return true; } /*************************鍋滆濺绯葷粺**********************/ int Arrive(CarInform e)//youwenti { Stack S; S=(Stack)malloc(sizeof(Stack)); InitStack(S); LQueue Q; Q=(LQueue)malloc(sizeof(LQueue)); InitQueue(Q); if(S->top-S->base<=INITSIZE) { PushStack(S,e.data);//鍋滆濺杩涘幓 S->top++; e.n=S->top-S->base; } else { EnQueue(Q,e.data); Q->rear++; } return e.n; } int Out(CarInform e) { Stack S; S=(Stack)malloc(sizeof(Stack)); InitStack(S); LQueue Q; Q=(LQueue)malloc(sizeof(LQueue)); InitQueue(Q); int j=0; printf("car number: ");//绂誨紑鏃剁殑浣嶇疆 scanf_s("%d",&e.n); while(S->top-S->base>=e.n) { PopStack(S,&e.data); EnQueue(Q,e.data); S->top--; j++; } while(j>1) { DeQueue(Q,e.data); PushStack(S,e.data); j--; S->top++; } return true; } void Charge(CarInform begin,CarInform end) { int total; int fee,half; total=(end.time.hour-begin.time.hour); half=(end.time.min-end.time.min)/30; fee=total*price+half*hprice; printf("$%d",fee); putchar('\n'); } int main() { CarInform e,start,end; printf("please enter the information:\n"); printf("************************************\n"); //the car go into parking lot int i=0; char a[N]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t'}; while(i<N) { if(i<N) { e.data=a[i]; printf("car:%c ",e.data); Arrive(e); printf("order:%d",i); i++; } printf("\nlicense plate number: ");
scanf_s("%d",&e.num[10]); printf("\nstart time: "); scanf_s("%d:%d",&start.time.hour,&start.time.min); printf("************************************\n"); } //the car go off the parking lot Out(e); printf("\nout time: "); scanf_s("%d:%d",&end.time.hour,&start.time.min); printf("\ncharge: "); Charge(start,end); printf("************************************\n"); }

每次輸出時a[N]裡面的字母全都輸出來了,按照正常來說,應該只輸出前10個

最佳回答:


#include
#include
#include
#include

#define price 2//每小時收費
#define hprice 1//半小時收費
#define INITSIZE 10
#define N 20

typedef struct{
int hour;
int min;
}Time;
typedef struct{
int num[10];//車牌號
Time time;//入場時間
int n;//入場位置
char data;
}CarInform;
typedef struct LNode{
char data;
struct LNode next;
}QNode,*QueuePtr;
typedef struct {
QueuePtr front;
QueuePtr rear;
}LQueue;
typedef struct {
char *base;
char *top;
int stacksize;
}Stack;
/
**************ջ******************/
int InitStack(Stack S)
{
S->base=NULL;
//S->base=(char
)malloc(INITSIZE*sizeof(char));
if(!S->base)return false;
S->base=S->top;
S->stacksize=INITSIZE;
return true;
}
int StackEmpty(Stack S)
{
if(S->base==S->top)return true;
else return false;
}
int PopStack(Stack *S,char *e)
{
if(!StackEmpty(S))
{
e=S->top;
S->top--;
return true;
}
return 0;
}
int PushStack(Stack *S, char e)
{
if(S->top-S->base==S->stacksize)return false;
*S->top=e;
S->top++;
return true;
}
void PrintStack(Stack *S)
{
while(S->base!=S->top)
{
printf("%2c",S->top);
S->top++;
}
}
/
*******************隊列******************************/
int InitQueue(LQueue Q)
{
Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q->front)return false;
Q->front->next=NULL;
return true;
}
int EnQueue(LQueue *Q,char e)
{
QueuePtr P;
P=(LNode
)malloc(sizeof(QNode));
if(!P)return false;
P->data=e;
P->next=NULL;
Q->rear->next=P;
Q->rear=P;
return true;
}
int DeQueue(LQueue Q,char e)
{
QueuePtr P;
if(Q->front==Q->rear)return false;
P=Q->front->next;
e=P->data;
Q->front->next=P->next;
if(Q->rear==P)Q->rear=Q->front;
free(P);
return true;
}
/
*************************停車系統**********************/
int Arrive(CarInform e)//youwenti
{
Stack S;
S=(Stack
)malloc(sizeof(Stack));
InitStack(S);
LQueue Q;
Q=(LQueue
)malloc(sizeof(LQueue));
InitQueue(Q);
if(S->top-S->base<=INITSIZE)
{
PushStack(S,e.data);//停車進去
S->top++;
e.n=S->top-S->base;

}
else
{
    EnQueue(Q,e.data);
    Q->rear++;
}
return e.n;

}
int Out(CarInform e)
{
Stack S;
S=(Stack
)malloc(sizeof(Stack));
InitStack(S);
LQueue Q;
Q=(LQueue
)malloc(sizeof(LQueue));
InitQueue(Q);
int j=0;
printf("car number: ");//離開時的位置
scanf_s("%d",&e.n);
while(S->top-S->base>=e.n)
{
PopStack(S,&e.data);
EnQueue(Q,e.data);
S->top--;
j++;
}
while(j>1)
{
DeQueue(Q,e.data);
PushStack(S,e.data);
j--;
S->top++;

}
return true;

}
void Charge(CarInform begin,CarInform end)
{

int total;
int fee,half;
total=(end.time.hour-begin.time.hour);
half=(end.time.min-end.time.min)/30;
fee=total*price+half*hprice;
printf("$%d",fee);
putchar('\n');

}

int main()
{
CarInform e,start,end;
printf("please enter the information:\n");
printf("************************************\n");
//the car go into parking lot
int i=0;
char a[N]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t'};
while(i<N)
{
if(i<N)
{

e.data=a[i];
printf("car:%c ",e.data);
Arrive(e);
printf("order:%d",i);
i++;
}
printf("\nlicense plate number: ");//車牌號 入場時間 離開時間 應付費用
scanf_s("%d",&e.num[10]);
printf("\nstart time: ");
scanf_s("%d:%d",&start.time.hour,&start.time.min);
printf("************************************\n");
}
//the car go off the parking lot
Out(e);
printf("\nout time: ");
scanf_s("%d:%d",&end.time.hour,&start.time.min);
printf("\ncharge: ");
Charge(start,end);

printf("************************************\n");

}

這樣看著舒服點圖片說明

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved