
关于c语言和数据结构的问题。用顺序栈,将十进制转化为二进制
#include"stdio.h"#include"stdlib.h"#include"malloc.h"#defineMaxSize50typedefintElemTy...
#include"stdio.h"#include"stdlib.h"
#include"malloc.h"
#define MaxSize 50
typedef int ElemType
typedef struct
{
ElemType data[MaxSize];
int top;
} Stack;
void InitStack(Stack *S) { S->top=-1;
}
int Push(Stack *S,ElemType x)
{ if(S->top==MaxSize-1){
printf("\n Stack is full!");
return 0; }
S->top++;
S->data[S->top]=x;
return 1;
}
int Pop(Stack *S,ElemType *x) { if(Empty(S)){
printf("\n Stack is free!");
return 0;
}
*x=S->data[S->top];
S->top--;
return 1;
}
int Empty(Stack *S)
{
return (S->top==0);
}
void conversion(int N)
{
int e;
Stack *S;
InitStack(S); while(N){
Push(S,N%2);
N=N/2;
}
while(!Empty(S)){
Pop(S,e);
printf("%d ",e)
}
}
void main(){
int n;
printf("请输入待转换的值n:\n");
scanf("%d",&n);
conversion(n);
}
vc++ 6.0 报错
G:\数据结构\实验\listwork2.cpp(7) : error C2143: syntax error : missing ';' before '<class-head>'
G:\数据结构\实验\listwork2.cpp(7) : fatal error C1004: unexpected end of file found
执行 cl.exe 时出错. 展开
#include"malloc.h"
#define MaxSize 50
typedef int ElemType
typedef struct
{
ElemType data[MaxSize];
int top;
} Stack;
void InitStack(Stack *S) { S->top=-1;
}
int Push(Stack *S,ElemType x)
{ if(S->top==MaxSize-1){
printf("\n Stack is full!");
return 0; }
S->top++;
S->data[S->top]=x;
return 1;
}
int Pop(Stack *S,ElemType *x) { if(Empty(S)){
printf("\n Stack is free!");
return 0;
}
*x=S->data[S->top];
S->top--;
return 1;
}
int Empty(Stack *S)
{
return (S->top==0);
}
void conversion(int N)
{
int e;
Stack *S;
InitStack(S); while(N){
Push(S,N%2);
N=N/2;
}
while(!Empty(S)){
Pop(S,e);
printf("%d ",e)
}
}
void main(){
int n;
printf("请输入待转换的值n:\n");
scanf("%d",&n);
conversion(n);
}
vc++ 6.0 报错
G:\数据结构\实验\listwork2.cpp(7) : error C2143: syntax error : missing ';' before '<class-head>'
G:\数据结构\实验\listwork2.cpp(7) : fatal error C1004: unexpected end of file found
执行 cl.exe 时出错. 展开
展开全部
//已改正,自己对照一下
#include"stdio.h"
#include"stdlib.h"
#include"malloc.h"
#define MaxSize 50
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
} Stack;
void InitStack(Stack *S) { S->top=-1;
}
int Push(Stack *S,ElemType x){ if(S->top==MaxSize-1){
printf("\n Stack is full!");
return 0; }
S->top++;
S->data[S->top]=x;
return 1;
}
int Empty(Stack *S)
{
return (S->top==-1);
}
int Pop(Stack *S,ElemType *x)
{ if(Empty(S)){
printf("\n Stack is free!");
return 0;
}
*x=S->data[S->top];
S->top--;
return 1;
}
void conversion(int N){
int e;
Stack *S=(Stack*)malloc(sizeof(Stack));
InitStack(S); while(N){
Push(S,N%2);
N=N/2;
}
while(!Empty(S)){
Pop(S,&e);
printf("%d ",e);
}
}
void main(){ int n;
printf("请输入待转换的值n:\n");
scanf("%d",&n);
conversion(n);
}
#include"stdio.h"
#include"stdlib.h"
#include"malloc.h"
#define MaxSize 50
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
} Stack;
void InitStack(Stack *S) { S->top=-1;
}
int Push(Stack *S,ElemType x){ if(S->top==MaxSize-1){
printf("\n Stack is full!");
return 0; }
S->top++;
S->data[S->top]=x;
return 1;
}
int Empty(Stack *S)
{
return (S->top==-1);
}
int Pop(Stack *S,ElemType *x)
{ if(Empty(S)){
printf("\n Stack is free!");
return 0;
}
*x=S->data[S->top];
S->top--;
return 1;
}
void conversion(int N){
int e;
Stack *S=(Stack*)malloc(sizeof(Stack));
InitStack(S); while(N){
Push(S,N%2);
N=N/2;
}
while(!Empty(S)){
Pop(S,&e);
printf("%d ",e);
}
}
void main(){ int n;
printf("请输入待转换的值n:\n");
scanf("%d",&n);
conversion(n);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询