首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

十进制变换 用栈操作

2012-11-15 
十进制转换用栈操作我编程如下,但编译错误:#includestdio.h#includestdlib.h#define Stackincrement 1

十进制转换 用栈操作
我编程如下,但编译错误:
#include<stdio.h>
#include<stdlib.h>
#define Stackincrement 10;
typedef int SElemType;
typedef struct {
SElemType *top;
SElemType *base;
int stacksize;
}SqStack;
void creat_stack(SqStack &S)
{
S.base=(SElemType*)malloc(Stackincrement*sizeof(SElemType));
S.top=S.base;
S.stacksize=0;
int n;
 printf("Please input the number you want to change:\n"); 
scanf("%d",&n);
while(n){
  if(S.top-S.base>=S.stacksize){
S.base=(SElemType*)realloc(S.base,(S.stacksize+Stackincrement)*sizeof(SElemType));
S.top=S.base+S.stacksize;
S.stacksize+=Stackincrement;
}
*S.top++=(n%2);
n/=2;
}
}
int show_afchange(SqStack &S)
{
if(S.top==S.base){
printf("wrong!");
  return 0;
}
printf("Now the number change into:\n%d\n",*--S.top);
  return 1;
}
void main()
{
SqStack S;
creat_stack(S);
show_afchange(S);
}


错误如下,求指正!!!

--------------------Configuration: 12 - Win32 Debug--------------------
Compiling...
12.cpp
F:\上机文件\004\12.cpp(12) : error C2143: syntax error : missing ')' before ';'
F:\上机文件\004\12.cpp(12) : error C2059: syntax error : ')'
F:\上机文件\004\12.cpp(12) : error C2100: illegal indirection
F:\上机文件\004\12.cpp(20) : error C2143: syntax error : missing ')' before ';'
F:\上机文件\004\12.cpp(20) : error C2143: syntax error : missing ')' before ';'
F:\上机文件\004\12.cpp(20) : error C2059: syntax error : ')'
F:\上机文件\004\12.cpp(20) : error C2059: syntax error : ')'
F:\上机文件\004\12.cpp(20) : error C2100: illegal indirection
执行 cl.exe 时出错.

12.obj - 1 error(s), 0 warning(s)


[解决办法]
又是这种奇怪的写法 参考下

C/C++ code
#include <stdio.h>#include<stdlib.h>#define MAXSIZE  20typedef int ElemType;typedef struct {   ElemType  elem[MAXSIZE];   int  top; }SqStack;void InitStack( SqStack *S);int  Empty( SqStack *S);int  Push(SqStack *S, ElemType x);int  Pop(SqStack *S ); void fun(int n,int R);void main(){  int n;  char i;  while(1)  { printf("10进制转换为R(大小写都可以)进制\n");     printf("b-------2\n");    printf("o-------8\n");    printf("x-------16\n");    printf("e-------退出\n");    printf("请输入10进制数n和转换的i进制:\n");    scanf("%d,%c",&n,&i);    switch(i)    {  case'B':       case'b': printf("转换为2进制后为:\n");                fun(n,2);                break;       case'O':       case'o': printf("转换为8进制后为:\n");               fun(n,8);               break;       case'X':       case'x': printf("转换为16进制后为:\n");                printf("0x");                fun(n,16);                break;        case'E':       case'e': printf("退出系统\n");            exit(0);       default: printf("input error,input again");     }  }}void InitStack( SqStack *S){  S->top=0;}int  Empty( SqStack *S){return ( S->top==0);}int  Push(SqStack *S, ElemType x){  if( S->top==MAXSIZE )  return 0;         S->elem[S->top]=x; S->top++;         return 1;}int  Pop(SqStack *S ){ --S->top;  return (S->elem[S->top]);}void fun(int n,int R){  SqStack S;   InitStack(&S);   char m;   int p;   while(n!=0)   {      Push(&S,n%R);     n=n/R;   }   while(!Empty(&S))   { p=Pop(&S);      if(p>=10)      { m=p%10+'A';        printf("%c",m);      }      else                printf("%d",p);    }          printf("\n");}
[解决办法]
把文件名该成xx.cpp
C编译器识别不了引用!
C/C++ code

void creat_stack(SqStack &S) 

热点排行