半夜又来求助各位大神了··程序是对的·但是要把括号匹配那段程序的从数组换成栈的,另外加上简单注释···麻烦各位了··
#include<malloc.h>
#include<stdio.h>
#define MaxSize 16
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
//初始化栈
void InitStack(SqStack **s)
{
*s=(SqStack *)malloc(sizeof(SqStack));
(*s)->top=-1;
}
//销毁栈
void ClearStack(SqStack **s)
{
free(*s);
}
//进栈
int Push(SqStack **s,ElemType e)
{
if((*s)->top==MaxSize-1)
return 0;
(*s)->top++;
(*s)->data[(*s)->top]=e;
return 1;
}
//出栈
int Pop(SqStack **s,ElemType *e)
{
if((*s)->top==-1)
return 0;
*e=(*s)->data[(*s)->top];
(*s)->top--;
return 1;
}
//取栈顶元素
int GetTop(SqStack *s,ElemType *e)
{
if(s->top==-1)
return 0;
*e=s->data[s->top];
return 1;
}
//显示栈中元素
void DispStack(SqStack *s)
{
int i;
for(i=s->top;i>=0;i--)
printf("%d",s->data[i]);
}
//十六进制判断
void Judge(SqStack *s)
{
int i;
for(i=s->top;i>=0;i--)
{
if(s->data[i]==10) printf("A");
else if(s->data[i]==11) printf("B");
else if(s->data[i]==12) printf("C");
else if(s->data[i]==13) printf("D");
else if(s->data[i]==14) printf("E");
else if(s->data[i]==15) printf("F");
else printf("%d",s->data[i]);
}
}
void function_1()
{
int i,t,j,m;//要转换的进制,j要被转换的数
SqStack *s;
InitStack(&s);
printf("请输入要转换的数(注意:j的范围):");
scanf("%d",&j);
printf("请输入要转换成的进制(二进制(输入2),八进制(输入8),十六进制(输入16)):");
scanf("%d",&i);
if(i==2||i==8)
{
if(j<0)
{ m=-j;
while(m!=0)
{
t=m%i;
Push(&s,t);
m=m/i;
}
Push(&s,1);
}
else if(j>0)
{
while(j!=0)
{
t=j%i;
Push(&s,t);
j=j/i;
}
Push(&s,0);
}
printf("转换成%d进制后的代码(首位为符号位):",i);
DispStack(s);
ClearStack(&s);
}
if(i==16)
{
if(j<0)
{ m=-j;
while(m!=0)
{
t=m%i;
Push(&s,t);
m=m/i;
}
Push(&s,1);
}
else if(j>0)
{
while(j!=0)
{
t=j%i;
Push(&s,t);
j=j/i;
}
Push(&s,0);
}
printf("转换成%d进制后的代码(首位为符号位):",i);
Judge(s);
ClearStack(&s);
}
printf("\n");
}
void getstr(char* p){
printf("input:\t");
scanf("%s", p);
return;
}
void function_3(){
char st[255];
int top = 0;
char str[255];
char k;
int i=0;
int st_error=0;
getstr(str);
while ( (k=str[i]) != 0)
{
if (k == '(' ) st[top++] = k;
if (k == ')' )
{
if (top == 0 )
{
st_error=1;
break;
}
else
top--;
}
i++;
}
if(st_error==0&&top==0) printf("匹配通过\n");
else
if(st_error==1) printf("缺少左括号!\n");
else
if(top>0) printf("缺少右括号!\n");
}
#include "stdio.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -1
//#define EOF -1
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 1000
#define MAXQSIZE 10
static int i=0;
typedef char ElemType1;
typedef struct StackNode//构造栈
{
ElemType1 *base;
ElemType1 *top;
int stacksize;
}SqStack1;
ElemType1 InitStack1(SqStack1 *S)//初始化栈
{
S->base=(ElemType1 *)malloc(STACK_INIT_SIZE*sizeof(ElemType1));
if(!S->base)
{
exit(OVERFLOW);
}
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
ElemType1 StackEmpty(SqStack1 *S)//判 断栈是否为空
{
if(S->top==S->base)
return OK;
else
return ERROR;
}
ElemType1 Push1(SqStack1 *S,ElemType1 e)//进栈操作
{
if(S->top-S->base>=S->stacksize)
{
S->base = (ElemType1 *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType1));
if(!S->base)
{
exit(OVERFLOW);
}
S->top = S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
return OK;
}
ElemType1 Pop1(SqStack1 *S,ElemType1 *e)//出栈操作
{
if(S->top==S->base)
{
return ERROR;
}
*e=*--S->top;
//printf("%d\n",e);
// return e;
return 0;
}
void ClearStack1(SqStack1 *S)//清空栈
{
S->top=S->base;
}
ElemType1 LineEdit(SqStack1 *S )//文本编译
{
char ch, e, a[30];
int i ;
fflush(stdin);//刷新标准输入
ch = getchar();
while(1)
{
while (ch!='\n')
{
switch(ch)
{
case '#': Pop1(S,&e); break; //遇到'#',前面个字符出栈
case '@': ClearStack1(S); break; //遇到'@',前面的所以字符出栈
default: Push1(S,ch); break; //其他字符进栈
}
ch = getchar();
}
i = 0;
while (!StackEmpty(S))
{
Pop1(S,&e);
a[i++]=e;
}
printf("循环输出结果为:");
for(--i; i>= 0; i--)
{
printf("%c",a[i]);
}
printf("\n");
printf("Exit?(Y/N)\n");
ch=getchar();
if(ch=='Y'||ch=='y')
break;
printf("\n请再输入几个字符吧:");
ClearStack1(S);
fflush(stdin);
ch = getchar();
}
return 0;
}
int function_2(void)
{
SqStack1 S;
printf("\n\t\t\t本程序演示数据结构中文本编辑\n\n");
printf("\t如果输入字符中包含'#',那么它前面的一个字符就会出栈,\n");
printf("\t如果输入的字符中包括'@',那么它前面的所有字符全部出栈(清空)!\n");
printf("请连续输入几个字符初始化栈(eg:abc):");
InitStack1(&S);
LineEdit(&S);
system("pause");
return 0;
}
int main()
{
int i;
loop:;
printf("\n\t\t|----------------------------------------------|");
printf("\n\t\t|--------- Please input ( 0 - 3) ------------|");
printf("\n\t\t|----------------------------------------------|");
printf("\n\t\t| 1.数值转换 |");
printf("\n\t\t| 2.文本编辑 |");
printf("\n\t\t| 3.括号匹配 |");
printf("\n\t\t|----------------------------------------------|");
printf("\n\t\t| 0. Exit |");
printf("\n\t\t|----------------------------------------------|");
scanf("%d",&i);
switch(i)
{
case 0:return 0;
case 1:function_1();goto loop;
case 2:function_2();goto loop;
case 3:function_3();goto loop;
default:printf("Input Error!!!");
}
return 0;
}
[解决办法]
楼主,按照你的思路给你改了括号匹配那一部分,已调试通过。自己看下吧
- C/C++ code
#include<malloc.h>#include<stdio.h>#define MaxSize 16typedef int ElemType;typedef struct{ ElemType data[MaxSize]; int top;}SqStack;//初始化栈void InitStack(SqStack **s){ *s=(SqStack *)malloc(sizeof(SqStack)); (*s)->top=-1;}//销毁栈void ClearStack(SqStack **s){ free(*s);}//进栈int Push(SqStack **s,ElemType e){ if((*s)->top==MaxSize-1) return 0; (*s)->top++; (*s)->data[(*s)->top]=e; return 1;}//出栈int Pop(SqStack **s,ElemType *e){ if((*s)->top==-1) return 0; *e=(*s)->data[(*s)->top]; (*s)->top--; return 1;}//取栈顶元素int GetTop(SqStack *s,ElemType *e){ if(s->top==-1) return 0; *e=s->data[s->top]; return 1;}//显示栈中元素void DispStack(SqStack *s){ int i; for(i=s->top;i>=0;i--) printf("%d",s->data[i]);}//十六进制判断void Judge(SqStack *s){ int i; for(i=s->top;i>=0;i--) { if(s->data[i]==10) printf("A"); else if(s->data[i]==11) printf("B"); else if(s->data[i]==12) printf("C"); else if(s->data[i]==13) printf("D"); else if(s->data[i]==14) printf("E"); else if(s->data[i]==15) printf("F"); else printf("%d",s->data[i]); }}void function_1(){ int i,t,j,m;//要转换的进制,j要被转换的数 SqStack *s; InitStack(&s); printf("请输入要转换的数(注意:j的范围):"); scanf("%d",&j); printf("请输入要转换成的进制(二进制(输入2),八进制(输入8),十六进制(输入16)):"); scanf("%d",&i); if(i==2||i==8) { if(j<0) { m=-j; while(m!=0) { t=m%i; Push(&s,t); m=m/i; } Push(&s,1); } else if(j>0) { while(j!=0) { t=j%i; Push(&s,t); j=j/i; } Push(&s,0); } printf("转换成%d进制后的代码(首位为符号位):",i); DispStack(s); ClearStack(&s); } if(i==16) { if(j<0) { m=-j; while(m!=0) { t=m%i; Push(&s,t); m=m/i; } Push(&s,1); } else if(j>0) { while(j!=0) { t=j%i; Push(&s,t); j=j/i; } Push(&s,0); } printf("转换成%d进制后的代码(首位为符号位):",i); Judge(s); ClearStack(&s); } printf("\n");}//*************************************************//括号匹配部分typedef struct { char str[255]; int top;}SqStack3;//初始化栈void InitStack3(SqStack3 **s){ *s=(SqStack3 *)malloc(sizeof(SqStack3)); (*s)->top=-1;}//销毁栈void ClearStack3(SqStack3 **s){ free(*s);}//进栈int Push3(SqStack3 **s,char e){ if((*s)->top==MaxSize-1) return 0; (*s)->top++; (*s)->str[(*s)->top]=e; return 1;}//出栈int Pop3(SqStack3 **s,char *e){ if((*s)->top==-1) return 0; *e=(*s)->str[(*s)->top]; (*s)->top--; return 1;}//输入字符串void getstr(char* p){ printf("input:\t"); scanf("%s", p); return;}void function_3(){ SqStack3 *seqstack; char str[255]; char k; int i=0; int st_error=0; InitStack3(&seqstack);//初始化栈 getstr(str); while ( (k=str[i]) != 0) { if (k == '(' ) { Push3(&seqstack,k);//是‘(’则入栈 } if (k == ')' ) { if (seqstack->top == -1 ) { st_error=1; break; } else { Pop3(&seqstack,&k);//是')'且栈中有'('则出栈 } } i++; } //匹配结果判断 if(st_error == 0&&seqstack->top == -1) { printf("匹配通过\n"); } else { if(st_error==1) { printf("缺少左括号!\n"); } else { if((seqstack->top) > -1) { printf("缺少右括号!\n"); } } } //销毁栈 ClearStack3(&seqstack); }//***********************************************************#include "stdio.h" #include "stdlib.h" #define OK 1 #define ERROR 0 #define OVERFLOW -1 //#define EOF -1 #define STACK_INIT_SIZE 10 #define STACKINCREMENT 1000 #define MAXQSIZE 10 static int i=0; typedef char ElemType1; typedef struct StackNode//构造栈 { ElemType1 *base; ElemType1 *top; int stacksize; }SqStack1; ElemType1 InitStack1(SqStack1 *S)//初始化栈 { S->base=(ElemType1 *)malloc(STACK_INIT_SIZE*sizeof(ElemType1)); if(!S->base) { exit(OVERFLOW); } S->top=S->base; S->stacksize=STACK_INIT_SIZE; return OK; } ElemType1 StackEmpty(SqStack1 *S)//判 断栈是否为空 { if(S->top==S->base) return OK; else return ERROR; } ElemType1 Push1(SqStack1 *S,ElemType1 e)//进栈操作 { if(S->top-S->base>=S->stacksize) { S->base = (ElemType1 *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType1)); if(!S->base) { exit(OVERFLOW); } S->top = S->base+S->stacksize; S->stacksize+=STACKINCREMENT; } *S->top++=e; return OK; } ElemType1 Pop1(SqStack1 *S,ElemType1 *e)//出栈操作 { if(S->top==S->base) { return ERROR; } *e=*--S->top; //printf("%d\n",e); // return e; return 0; } void ClearStack1(SqStack1 *S)//清空栈 { S->top=S->base; } ElemType1 LineEdit(SqStack1 *S )//文本编译 { char ch, e, a[30]; int i ; fflush(stdin);//刷新标准输入 ch = getchar(); while(1) { while (ch!='\n') { switch(ch) { case '#': Pop1(S,&e); break; //遇到'#',前面个字符出栈 case '@': ClearStack1(S); break; //遇到'@',前面的所以字符出栈 default: Push1(S,ch); break; //其他字符进栈 } ch = getchar(); } i = 0; while (!StackEmpty(S)) { Pop1(S,&e); a[i++]=e; } printf("循环输出结果为:"); for(--i; i>= 0; i--) { printf("%c",a[i]); } printf("\n"); printf("Exit?(Y/N)\n"); ch=getchar(); if(ch=='Y'||ch=='y') break; printf("\n请再输入几个字符吧:"); ClearStack1(S); fflush(stdin); ch = getchar(); } return 0; } int function_2(void) { SqStack1 S; printf("\n\t\t\t本程序演示数据结构中文本编辑\n\n"); printf("\t如果输入字符中包含'#',那么它前面的一个字符就会出栈,\n"); printf("\t如果输入的字符中包括'@',那么它前面的所有字符全部出栈(清空)!\n"); printf("请连续输入几个字符初始化栈(eg:abc):"); InitStack1(&S); LineEdit(&S); system("pause"); return 0; } int main(){ int i;loop:; printf("\n\t\t|----------------------------------------------|"); printf("\n\t\t|--------- Please input ( 0 - 3) ------------|"); printf("\n\t\t|----------------------------------------------|"); printf("\n\t\t| 1.数值转换 |"); printf("\n\t\t| 2.文本编辑 |"); printf("\n\t\t| 3.括号匹配 |"); printf("\n\t\t|----------------------------------------------|"); printf("\n\t\t| 0. Exit |"); printf("\n\t\t|----------------------------------------------|"); scanf("%d",&i); switch(i) { case 0:return 0; case 1:function_1();goto loop; case 2:function_2();goto loop; case 3:function_3();goto loop; default:printf("Input Error!!!"); } return 0;}