主题:请大家指正我的程序中出现的错误
start4u
[专家分:70] 发布于 2006-08-14 20:17:00
我用栈写一个十进制转八进制的算法,可是编译的时候产生了很多错误,5楼是我用VC6.0编译后的debug 希望大家帮我看看这个程序到底错在那里, 谢谢大家.(明明没到10000字 ,为什么要我分割啊.)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
int r;
int c;
}PostType;
typedef struct
{
int ord;
PostType seat;
int di ;
}SElemType; //栈元素类型
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}Stack;
回复列表 (共7个回复)
沙发
start4u [专家分:70] 发布于 2006-08-14 20:17:00
void InitStack (Stack &S)
{
S.base=(SElemType*)malloc(INIT_SIZE *sizeof(SElemType));
if(!S.base)
exit (1);
S.top=S.base;
S.stacksize=INIT_SIZE;
}//InitStack 初始化栈
bool push(stack &S,SElemType e)
{
if (S.top==maxsize)
return false;
*(S.base+S.top)=e;
++S.top;
return true;
}//push
bool pop(stack &S,SElemType &e)
{
if (S.top==0)
return false;
e=*(S.base+S.top-1)
--S.top;
return true;
}//pop
板凳
start4u [专家分:70] 发布于 2006-08-14 20:17:00
bool StackEmpty(Stack &S)
{
if (S.top==0)
return true;
else
return false;
}//StackEmpty
Status DestroyStack(Stack &S){ //销毁栈S
free(S.base);
S.top=S.base;
return OK;
}//DestroyStack
3 楼
start4u [专家分:70] 发布于 2006-08-14 20:17:00
void conversion_10to8 (int m)
{ //将输入的十进制转为八进制
Stack S;
InitStack(S);
while (m)
{
m=m/8;
push(S,m%8);
}//while
while (!StackEmpty(S))
{
int e; pop(S,e);
printf ("%d",e);
}//while
DestroyStack(S);
}//conversion_10to8
main ()
{
int number;
printf ("Please input a num:\n");
scanf ("%d",&number);
conversion_10to8 (number);
}//main
4 楼
start4u [专家分:70] 发布于 2006-08-14 20:18:00
--------------------Configuration: 十进制转八进制 - Win32 Debug--------------------
Compiling...
十进制转八进制.c
C:\Documents and Settings\kobe\十进制转八进制.c(36) : error C2143: syntax error : missing ')' before '&'
C:\Documents and Settings\kobe\十进制转八进制.c(36) : error C2143: syntax error : missing '{' before '&'
C:\Documents and Settings\kobe\十进制转八进制.c(36) : error C2059: syntax error : '&'
C:\Documents and Settings\kobe\十进制转八进制.c(36) : error C2059: syntax error : ')'
C:\Documents and Settings\kobe\十进制转八进制.c(49) : error C2061: syntax error : identifier 'push'
C:\Documents and Settings\kobe\十进制转八进制.c(49) : error C2059: syntax error : ';'
C:\Documents and Settings\kobe\十进制转八进制.c(49) : error C2143: syntax error : missing ')' before '&'
C:\Documents and Settings\kobe\十进制转八进制.c(49) : error C2143: syntax error : missing '{' before '&'
C:\Documents and Settings\kobe\十进制转八进制.c(49) : error C2059: syntax error : '&'
C:\Documents and Settings\kobe\十进制转八进制.c(49) : error C2059: syntax error : ')'
C:\Documents and Settings\kobe\十进制转八进制.c(58) : error C2061: syntax error : identifier 'pop'
C:\Documents and Settings\kobe\十进制转八进制.c(58) : error C2059: syntax error : ';'
C:\Documents and Settings\kobe\十进制转八进制.c(58) : error C2143: syntax error : missing ')' before '&'
C:\Documents and Settings\kobe\十进制转八进制.c(58) : error C2143: syntax error : missing '{' before '&'
C:\Documents and Settings\kobe\十进制转八进制.c(58) : error C2059: syntax error : '&'
C:\Documents and Settings\kobe\十进制转八进制.c(58) : error C2059: syntax error : ')'
C:\Documents and Settings\kobe\十进制转八进制.c(70) : error C2061: syntax error : identifier 'StackEmpty'
C:\Documents and Settings\kobe\十进制转八进制.c(70) : error C2059: syntax error : ';'
C:\Documents and Settings\kobe\十进制转八进制.c(70) : error C2059: syntax error : 'type'
C:\Documents and Settings\kobe\十进制转八进制.c(79) : error C2061: syntax error : identifier 'DestroyStack'
C:\Documents and Settings\kobe\十进制转八进制.c(79) : error C2059: syntax error : ';'
C:\Documents and Settings\kobe\十进制转八进制.c(79) : error C2059: syntax error : 'type'
C:\Documents and Settings\kobe\十进制转八进制.c(93) : warning C4013: 'InitStack' undefined; assuming extern returning int
C:\Documents and Settings\kobe\十进制转八进制.c(100) : warning C4013: 'push' undefined; assuming extern returning int
C:\Documents and Settings\kobe\十进制转八进制.c(103) : warning C4013: 'StackEmpty' undefined; assuming extern returning int
C:\Documents and Settings\kobe\十进制转八进制.c(105) : warning C4013: 'pop' undefined; assuming extern returning int
C:\Documents and Settings\kobe\十进制转八进制.c(109) : warning C4013: 'DestroyStack' undefined; assuming extern returning int
Error executing cl.exe.
十进制转八进制.obj - 22 error(s), 5 warning(s)
5 楼
euc [专家分:4310] 发布于 2006-08-15 11:39:00
呵呵,那个... INIT_SIZE,maxsize没定义; 类型是Stack而不是stack; top是指针还是整数要搞清; 好象Stack名字和标准库冲突?换个名字试试.
6 楼
start4u [专家分:70] 发布于 2006-08-15 19:41:00
我试一下 谢谢楼上的回复.
7 楼
tgbtgb [专家分:490] 发布于 2006-08-19 12:29:00
#include"malloc.h"
#include"stdio.h"
#define MAX 225
typedef struct
{int q[MAX];
int top;
}list;
void creat(list *p)
{if(!p)
printf("错误!\n");
p->top=-1;
}
int into(list *j, int x)
{if(j->top<MAX-1)
{j->top=j->top+1;
j->q[j->top]=x;
}
else
printf("溢出。\n");
}
int put(list *r)
{int x;
if(r->top!=-1)
{x=r->q[r->top];
r->top=r->top-1;
return(x);
}
else
return(-1);
}
int yushu(int m)
{int s,h,n,k,y;list *p;
p=(list *)malloc(sizeof(list));
creat(p);
printf("请输入要转换的数:\n");
scanf("%d",&s);
do
{n=s;
s=s/m;
k=n%m;
into(p,k);
}while(s);
printf("10->%d进制数为:\n",m);
printf("\n");
do
{
y=put(p);
if(y>9)
printf("%c",y+87);
else if(y==-1)
break;
else
printf("%c",y+48);
}
while(y!=-1);
}
main()
{ int i;
int fage;
fage=1;
do{printf("\n");
printf("\t******主菜单******\n");
printf("\t1-----------10->16\n");
printf("\t2-----------10->8\n");
printf("\t3-----------10->2\n");
printf("\t0-----------finish.\n");
printf("\t===================\n");
printf("请选择:\n");
scanf("%d",&i);
switch(i)
{case 0:fage=0;break;
case 1:yushu(16);break;
case 2:yushu(8);break;
case 3:yushu(2);break;
default:printf("选择错误。请重新选择。\n");
}
}while(fage);
}
我来回复