#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define MAX 10000
typedef struct astack *Stack;
typedef struct astack
{
int top;
int maxtop;
char*data;
}Astack;
int main()
{
int f(char* a[]);
Stack StackInit(int size);
int StacckFull(Stack S);
void Push(char x,Stack S);
char Pop (Stack S);
char a[10001];int t;
scanf("%s",a);
t= f( a);
return 0;
}
Stack StackInit(int size)
{
Stack S=malloc(sizeof*S);
S->data=malloc(size*sizeof(char));
S->maxtop=size;
S->top=-1;
return S;
}
int StackFull(Stack S)
{
return S->top=S->maxtop;
}
int StackEmpty(Stack S)
{
return S->top<0;}
void Push(char x,Stack S)
{
if(!StackFull(S))
S->data[++S->top]=x;
}
char Pop (Stack S)
{
if(!StackEmpty(S))
return S->data[S->top--];
}
int f(char *a[])
{
int n,i,j=0; char b[1000];
Stack S= StackInit(MAX);
n=strlen(a);
for(i=1;i<n;i++)
{
if(a[i-1]=='(')
{ Push('(',S);
j++;}
else if(a[i-1]==')')
{
if(StackEmpty(S))
{Push(')',S); j++;}
else if(!StackEmpty(S)) {Pop(S);j--;}
}
}
if(StackEmpty(S))
{for(i=0;i<n;i++)
printf("%c",a[i]);}
else {for(i=0;i<j;i++)
b[j-1-i]=Pop(S);
for(i=0;i<j;i++)
printf("%c",b[i]);
printf("\n");}return 0;
}
还是输出有问题啊
追答#include
#include
#include
#include
#define MAX 10000
typedef struct astack *Stack;
typedef struct astack
{
int top;
int maxtop;
char*data;
}
Astack;
int main()
{
int f(char* a);
Stack StackInit(int size);
int StacckFull(Stack S);
void Push(char x, Stack S);
char Pop(Stack S);
char a[10001];
int t;
scanf("%s", a);
t = f( a);
system("pause");
return 0;
}
Stack StackInit(int size)
{
Stack S = malloc(sizeof * S);
S->data = malloc(size * sizeof(char));
S->maxtop = size;
S->top = -1;
return S;
}
int StackFull(Stack S)
{
return S->top == S->maxtop;
}
int StackEmpty(Stack S)
{
return S->top data[++S->top] = x;
}
char Pop(Stack S)
{
if (!StackEmpty(S))
return S->data[S->top--];
}
char Top(Stack S)
{
if (!StackEmpty(S))
return S->data[S->top];
return 0;
}
int f(char *a)
{
int n, i, j = 0; char b[1000];
Stack S = StackInit(MAX);
n = strlen(a);
for (i = 1;i <= n;i++)
{
if (a[i - 1] == '(')
{
Push('(', S);
j++;
}
else if (a[i - 1] == ')')
{
if (StackEmpty(S) || Top(S) == ')')
{Push(')', S); j++;}
else if (!StackEmpty(S)) {Pop(S);j--;}
}
}
if (StackEmpty(S))
{
for (i = 0;i < n;i++)
printf("%c", a[i]);
}
else
{
for (i = 0;i < j;i++)
b[j - 1 - i] = Pop(S);
for (i = 0;i < j;i++)
printf("%c", b[i]);
printf("\n");
}
return 0;
}
还是输出有问题啊 要求若输入()()则输出()()若输入)()()))则输出)))
追答你那算法那有点问题, 这种算法上的事情找一个括号匹配的看看吧.
#include
#include
#include
#define MAX 10000
typedef struct astack
{
int top;
int maxtop;
char *data;
}Astack;
typedef struct astack *Stack;
Stack StackInit(int size)
{
Stack S = malloc(sizeof*S);
S->data = malloc(size*sizeof(char));
S->maxtop = size;
S->top = -1;
return S;
}
int StackFull(Stack S)
{
return S->top >= S->maxtop;
}
int StackEmpty(Stack S)
{
return S->top data[++S->top] = x;
}
char Pop(Stack S)
{
if(!StackEmpty(S))
return S->data[S->top--];
return '\0';
}
int f(char *a)
{
int n;
int i;
int j=0;
char b[1000];
Stack S = StackInit(MAX);
n=strlen(a);
for(i = 1; i <= n; i++) {
if (a[i-1] == '\0')
break;
if(a[i-1] == '(') {
Push('(', S);
j++;
} else if(a[i-1] == ')') {
if(!StackEmpty(S)) {
Push(')', S);
j++;
} else if(StackEmpty(S)) {
Pop(S);
j--;
}
}
}
if(StackEmpty(S)) {
for(i = 0; i < n; i++)
printf("%c", a[i]);
} else {
for(i = 0; i < j; i++)
b[j-1-i] = Pop(S);
for(i = 0; i < j; i++)
printf("%c", b[i]);
printf("\n");
}
return 0;
}
int main()
{
int t;
char a[10001];
scanf("%s", a);
t= f(a);
return 0;
}
不行输入什么输出还是什么
追答大哥,你不要全输入括号啊. 你输入一些其他的字符. 然后再看效果. 这段代码是不是你写的啊
本回答被提问者采纳