数据结构之正则表达式和栈
遇到问题不断补充中
先记录第一种算正则表达式的方法
该代码并未使用前缀表达式或者后缀表达式,直接处理字符串,按照字符串顺序进行处理表达式
注意 code from nekenet
该代码量比前缀表达式求正则 和后缀表达式求正则 要少很多
```c
//Regular expression evaluation
#if 1
#include<stdio.h>
int pos;
int compute(char* data)
{
int len = strlen(data);
int stack[1000]; //Just use one stack ,Here use array as stack
int top = -1; //The top of the stack is initialized to -1
char flag = '+';
int num = 0; //Get the numeric variable in the expression
while (pos < len) //Iterate over the string
{
if (data[pos] == '{' || data[pos] == '[' || data[pos] == '(')
{
pos++;
num = compute(data); //Recursion
}
/**
Convert string to number !!!it's a good skill to transform string to num
int isdigit(char a ); introduce how to use isdigit
#include<stdio.h>
#include<ctype.h>
int main() {
char a = '2';
char c = 'a';
if (isdigit(a)) { printf("a is a num\n"); }
if (isdigit(c)) { printf("c is a num\n"); }
return 0;}
**/
while (pos < len && isdigit(data[pos]))
{
num = 10*num + data[pos] - '0';
pos++;
}
//condition 3
switch (flag)
{
case '+':stack[++top] = num; printf("+:%d\n", num); break;
case '-':stack[++top] = -num; break;
case '*':stack[top] *= num; printf("*:%d\n", num); break;
case '/':stack[top] /= num; break;
}
num = 0;
flag = data[pos];
//condition 4
if (data[pos] == '}' || data[pos] == ']' || data[pos] == ')')
{
pos++;
break;
}
pos++;
}
int res = 0;
for (int i = 0; i <= top; i++)
{
printf("res:%d\n", stack[i]);
res += stack[i];
}
return res;
}
int main()
{
char data[1000]; //Create string array, get expression
while (gets(data)>0)
{
pos = 0;
int res = compute(data);
printf("%d\n", res);
}
return 0;
}
#endif
后缀表达式
``后缀表达式主要是使用栈的数据结构来实现,学习使用后缀表达式来求运算字符串时(例如:2+4*(3+5) ),需要对栈了解比较清楚,可以使用数组栈的形式,也可以使用链表栈的形式。建议最好画框架图来分析后缀表达式运算时对各种判断的优先顺序,加强对代码的理解。
code from niukenet, 由于牛客网的高赞回答使用的链表栈,而我使用的数组栈,所以代码可能有些差异。后期再补充链表栈的后缀表达式代码。
如果有不明白的地方请留言,我会在看到时进行恢复
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include"type.h"
#define maxN 100
extern in N;
extern ch M;
/*
description: use to judge the priority with c1,c2
if c1 is high level ,return 1
else return 0;
*/
int ishigh(char c1, char c2) { //c2>c1?
if (c1 == '+' || c1 == '-') {
if (c2 == '+' || c2 == '-') {
return 0;
}
else if (c2 == '*' || c2 == '/') {
return 1;
}
}
if (c1 == '*' || c1 == '/') {
return 0;
}
if (c1 == '(') {
return 1;
}
}
/*
operate + - * /
*/
int calcpro(int a, int b, int ch) {
switch (ch) {
case '+':return(a + b);
case '-':return(a - b);
case '*':return(a * b);
case '/':return(a / b);
}
}
/*
Convert infix expression to postfix expression
*/
int compute1(char* str) {
stackarrayinitin(maxN);//initialize integer stack;
stackarrayinitch(maxN);//initialize character stack;
int len = strlen(str);
int pos = 0; //the position in str array
/* Traverse str array,transform [ { to ( */
for (int i = 0; i < len; i++) {
if (str[pos] == '{' || str[pos] == '[')
str[pos] = '(';
if(str[pos] == '}' || str[pos] == ']')
str[pos] = ')';
}
pos = 0; //when input a new str, we need initialize pos
//while (pos < len) {
for (pos = 0; pos < len; pos++) {
if (str[pos] == '(') {
stackarraypushch(str[pos]);
continue;
}
/*
description
when str[pos] is ')' ,before pop chstack a '(' ,we need pop intstack n1 ,n2 ,and pop chstack ch1,compute the n1 ch n2,and push the temp into intstack.
*/
if (str[pos] == ')') {
ch ch1;
in n1, n2, temp, m, l=0;
while (ch1 = stackarrayelementch() != '(') {
ch1 = stackarraypopch();
n1 = stackarraypopin();
n2 = stackarraypopin();
printf("expresssion:%d %c %d\n", n1, ch1, n2);
temp = calcpro(n1, n2, ch1);
stackarraypushin(temp);
}
ch1 = stackarraypopch();
continue;
}
if (str[pos] == '+' || str[pos] == '-' || str[pos] == '*' || str[pos] == '/') {
ch ch1;
in n1, n2,temp;
if (stackarrayemptych()) {
stackarraypushch(str[pos]);
continue;
}
else {
ch1 = stackarrayelementch();
if (ishigh(ch1, str[pos]) == 1) {
stackarraypushch(ch1);
stackarraypushch(str[pos]);
}
else {
n1 = stackarraypopin();
n2 = stackarraypopin();
ch1 = stackarraypopch();
temp = calcpro(n1, n2, ch1);
stackarraypushin(temp);
//stackarraypushch(ch1);
pos--;
}
continue;
}
}
int n1=0;
while (isdigit(str[pos])) {
// char* p;
// n1 = strtoul(&str[pos], &p, 10);
n1 = n1 * 10 + str[pos] - '0';
pos++;
}
if(n1 != 0)
stackarraypushin(n1);
n1 = 0;
pos--;
}
while (stackarrayemptych()==0) {
in n1, n2, temp;
if (N!=1) {
printf("dev get in here\n");
ch ch;
ch = stackarraypopch();
n1 = stackarraypopin();
n2 = stackarraypopin();
temp = calcpro(n1, n2, ch);
stackarraypushin(temp);
}
else
return stackarraypopin();
}
int res;
res = stackarraypopin();
return res;
//}
}