仅写了栈的相关实现函数和进制转换、括号匹配功能,行编辑有点问题,之后再进行补充 源代码:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<time.h> #include<string.h> #include<iostream> using namespace std; #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; typedef char SElemType; typedef struct SNode { SElemType data; struct SNode* next; }SNode,*LinkStack; //初始化栈 Status InitStack(LinkStack& S) { S = (LinkStack)malloc(sizeof(SNode)); if (!S) exit(OVERFLOW); S->next = NULL; S->data = NULL; return OK; } //销毁栈 Status DestroyStack(LinkStack& S) { LinkStack p = S->next, ptmp; while (p) { ptmp = p->next; free(p); p = ptmp; } free(S); return OK; } //把栈置为空栈 Status ClearStack(LinkStack& S) { LinkStack p = S->next, ptmp; while (p) { ptmp = p->next; free(p); p = ptmp; } S->next = NULL; return OK; } //判断栈是否为空 Status StackEmpty(LinkStack S) { if (S->next == NULL) return TRUE; else return FALSE; } //返回栈中的元素个数 Status StackLength(LinkStack S) { int n = 0; LinkStack p = S->next; while (p) { n++; p = p->next; } return n; } //栈不空的话返回栈顶元素 Status GetTop(LinkStack S, SElemType& e) { if (S->next == NULL) return ERROR; e = S->next->data; return OK; } //插入元素e作为栈顶元素 Status Push(LinkStack& S, SElemType e) { LinkStack p = (LinkStack)malloc(sizeof(SNode)); p->next = S->next; p->data = e; S->next = p; return OK; } //如果栈不为空删除S的栈顶元素并保存到e中 Status Pop(LinkStack& S, SElemType& e) { if (S->next == NULL) return ERROR; e = S->next->data; LinkStack ptmp = S->next->next; free(S->next); S->next = ptmp; return OK; } Status visit(SElemType e) { printf("%c", e); return OK; } //遍历输出,从栈底到栈顶 Status StackTraverse(LinkStack S) { if (S->next == NULL) { printf("栈为空!\n"); return ERROR; } for (int i = StackLength(S); i > 0; i--) { LinkStack p = S->next; int j = 1; while (p && j < i) { p = p->next; ++j; } visit(p->data); } printf("\n"); return OK; } //从栈顶到栈底遍历输出 Status StackTraverse_Top(LinkStack S) { if (S->next == NULL) { printf("栈为空!\n"); return ERROR; } LinkStack p = S->next; while (p) { visit(p->data); p = p->next; } printf("\n"); return OK; } //数值转换为8进制 void conversion() { int N,n; SElemType e; LinkStack S = NULL; InitStack(S); printf("请输入10进制数字以及进制数:"); scanf_s("%d%d", &N,&n); printf("转换为%d进制数字为:",n); while (N) { Push(S, N % n); N = N / n; } while (!StackEmpty(S)) { Pop(S, e); printf("%d ", e); } printf("\n"); DestroyStack(S); } //括号匹配的检查 bool check() { SElemType e,x; int i = 0; LinkStack S; InitStack(S); char word[50]; printf("请输入需要匹配的字符串:"); cin >> word; int length = strlen(word); for (i = 0; i < length; i++) { switch (word[i]) { case'(': case'[': case'{': case'<':Push(S, word[i]); break; case')': { GetTop(S, e); if (e == '(') Pop(S, x); else return FALSE; }break; case']': { GetTop(S, e); if (e == '[') Pop(S, x); else return FALSE; }break; case'}': { GetTop(S, e); if (e == '{') Pop(S, x); else return FALSE; }break; case'>': { GetTop(S, e); if (e == '<') Pop(S, x); else return FALSE; }break; default:break; } } if (StackEmpty(S)) return TRUE; else return FALSE; } //行编辑程序 /*void LineEdit() { LinkStack S; InitStack(S); SElemType ch = getchar(); SElemType c; while (ch != ' ') { if (ch != ' ' && ch != '\n') { switch (ch) { case'#': Pop(S, c); break; case'@': ClearStack(S); break; default: Push(S, ch); break; } } ch = getchar(); } StackTraverse(S); StackTraverse_Top(S); ClearStack(S); }*/ int main() { conversion(); if (check()) printf("括号匹配!\n"); else printf("括号不匹配!\n"); return 0; }