车厢调度
#include<stdio.h>#include<malloc.h>
#include<stdlib.h>
#include <conio.h>
#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
typedef char SElemType;
typedef int Status;
int n;//总车厢数
long total=0;
typedef struct //结构体定义
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//构建一个空栈
Status InitStack(SqStack &S)
{
S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base) exit(0);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return 0;
}
//输出栈顶元素
Status GetTop(SqStack &S,SElemType &e)
{
if(S.top==S.base)
return 1;
e= *(S.top-1);
return 0;
}
Status Full(SqStack &S)
{
if(S.top-S.base==n)
return 1;
return 0;
}
//元素进站
Status Push(SqStack &S,SElemType e)
{
if(S.top-S.base>=S.stacksize)
{
s.base=(SElemType*)realloc(s.base,(STACK_INIT_SIZE+STACKINCRESIZE)*sizeof(SElemType));
if(!S.base) exit(0);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return 0;
}
//元素出栈
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base)
return 1;
e=*--S.top;
return 0;
}
//空栈
Status StackEmpty(SqStack &S)
{
if(S.top==S.base)
return 1;
return 0;
}
//构建三个栈进行输出
void f(SqStack &s1,SqStack &s2,SqStack &s3)
{
int e;
if(!StackEmpty(s1))
{
Pop(s1,e);
Push(s2,e);
f(s1,s2,s3);
Pop(s2,e); Push(s1,e);
}
if(!StackEmpty(s2))
{
Pop(s2,e);Push(s3,e);
f(s1,s2,s3);
Pop(s3,e);Push(s2,e);
}
if(s3.Full())
{
total++;
s3.print();
}
}
Status print(SqStack &S)
{
int *p;
p=S.base;
printf("\t[%ld]",total);
for(;p!=S.top;)
{
printf("%d",*p++);
}
printf("\n");
}
void main()
{
SqStack s1,s2,s3;
printf("请输入车厢的长度");
InitStack(&s1);
InitStack(&s2);
InitStack(&s3);
for(int i=n;i>1;i--)
Push(&s1,i);
f(&s1,&s2,&s3);
}
--------------------Configuration: he - Win32 Debug--------------------
Compiling...
he.cpp
D:\Users\Administrator\Desktop\he.cpp(24) : error C2143: syntax error : missing ')' before ';'
D:\Users\Administrator\Desktop\he.cpp(24) : error C2059: syntax error : ')'
D:\Users\Administrator\Desktop\he.cpp(24) : error C2100: illegal indirection
D:\Users\Administrator\Desktop\he.cpp(49) : error C2065: 's' : undeclared identifier
D:\Users\Administrator\Desktop\he.cpp(49) : error C2228: left of '.base' must have class/struct/union type
D:\Users\Administrator\Desktop\he.cpp(49) : error C2228: left of '.base' must have class/struct/union type
D:\Users\Administrator\Desktop\he.cpp(49) : error C2143: syntax error : missing ')' before ';'
D:\Users\Administrator\Desktop\he.cpp(49) : error C2143: syntax error : missing ')' before ';'
D:\Users\Administrator\Desktop\he.cpp(49) : error C2065: 'STACKINCRESIZE' : undeclared identifier
D:\Users\Administrator\Desktop\he.cpp(49) : error C2059: syntax error : ')'
D:\Users\Administrator\Desktop\he.cpp(49) : error C2059: syntax error : ')'
D:\Users\Administrator\Desktop\he.cpp(49) : warning C4552: '*' : operator has no effect; expected operator with side-effect
D:\Users\Administrator\Desktop\he.cpp(79) : error C2664: 'Pop' : cannot convert parameter 2 from 'int' to 'char &'
A reference that is not to 'const' cannot be bound to a non-lvalue
D:\Users\Administrator\Desktop\he.cpp(82) : error C2664: 'Pop' : cannot convert parameter 2 from 'int' to 'char &'
A reference that is not to 'const' cannot be bound to a non-lvalue
D:\Users\Administrator\Desktop\he.cpp(86) : error C2664: 'Pop' : cannot convert parameter 2 from 'int' to 'char &'
A reference that is not to 'const' cannot be bound to a non-lvalue
D:\Users\Administrator\Desktop\he.cpp(88) : error C2664: 'Pop' : cannot convert parameter 2 from 'int' to 'char &'
A reference that is not to 'const' cannot be bound to a non-lvalue
D:\Users\Administrator\Desktop\he.cpp(90) : error C2039: 'Full' : is not a member of 'SqStack'
D:\Users\Administrator\Desktop\he.cpp(16) : see declaration of 'SqStack'
D:\Users\Administrator\Desktop\he.cpp(93) : error C2039: 'print' : is not a member of 'SqStack'
D:\Users\Administrator\Desktop\he.cpp(16) : see declaration of 'SqStack'
D:\Users\Administrator\Desktop\he.cpp(97) : error C2373: 'print' : redefinition; different type modifiers
D:\Users\Administrator\Desktop\he.cpp(99) : error C2440: '=' : cannot convert from 'char *' to 'int *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
D:\Users\Administrator\Desktop\he.cpp(101) : error C2446: '!=' : no conversion from 'char *' to 'int *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
D:\Users\Administrator\Desktop\he.cpp(101) : error C2230: '!=' : indirection to different types
D:\Users\Administrator\Desktop\he.cpp(111) : error C2664: 'InitStack' : cannot convert parameter 1 from 'SqStack *' to 'SqStack &'
A reference that is not to 'const' cannot be bound to a non-lvalue
D:\Users\Administrator\Desktop\he.cpp(112) : error C2664: 'InitStack' : cannot convert parameter 1 from 'SqStack *' to 'SqStack &'
A reference that is not to 'const' cannot be bound to a non-lvalue
D:\Users\Administrator\Desktop\he.cpp(113) : error C2664: 'InitStack' : cannot convert parameter 1 from 'SqStack *' to 'SqStack &'
A reference that is not to 'const' cannot be bound to a non-lvalue
D:\Users\Administrator\Desktop\he.cpp(115) : error C2664: 'Push' : cannot convert parameter 1 from 'SqStack *' to 'SqStack &'
A reference that is not to 'const' cannot be bound to a non-lvalue
D:\Users\Administrator\Desktop\he.cpp(116) : error C2664: 'f' : cannot convert parameter 1 from 'SqStack *' to 'SqStack &'
A reference that is not to 'const' cannot be bound to a non-lvalue
执行 cl.exe 时出错.
he.exe - 1 error(s), 0 warning(s)