C + + implementation of infix expression evaluation code __c++

Source: Internet
Author: User

The infix expression string is given and the value is calculated.

Main ideas:

1. The lexical analysis of the string, the analysis results stored to two-yuan array, with the two-element array storage infix expression.

2. Convert infix expression to suffix expression.

3. Use the stack to evaluate the suffix expression.

Characteristics:

A space that can be removed from a string.

can identify more than two digits of integer data calculation, the operator is mainly (+,-,x,/).

Code Run Results:

Infix expression examples are: 12+3* (8/2);


#include <iostream> #include <cstring> using namespace std;
#define the longest length char strtoken[maxsize] of the expression of maxSize-buffer[maxsize];//char;
	typedef struct line{unsigned short id;
Short indexoftable;
};
struct line data[maxsize];
int inttable[maxsize];
Short flagofinttable = 0;

Short flagofstruct = 0;
struct line stackofep[maxsize];//storage suffix expression short stackofid[maxsize];//store symbol short COUNTOFEP = 0;

Short Countofid = 0;
Const short INTEGER = 1;
Const short LEFT_B = 3;
Const short RIGHT_B = 4;
Const short PLUS = 6;

Const short Minux = 7;
Const short MULTIPLY = 9;

Const short DIVIDE = 10;
	void Deleblank (const char* str) {//clear string space for char *temp = buffer;
	if (0 = *str) {*temp = ' ";
				while (*STR) {if (*str = = ") {str++;
			Continue 
	} *temp++ = *str++;
} *temp = ' I ';
	} void Init (char *str) {for (Short i = 0;i <=strlen (str); i++) {*str = 0;
	} void Wordanalyse (const char* str) {const char *start = str;//character start pointer const char *search;//search pointer char *temp; WhilE (*start) {search = start;
		temp = strtoken;//loop once, the temp pointer is corrected to point to Strtoken first address.	
			if (IsDigit (*search)!= 0) {//is a number when the return value is 4 while (IsDigit (*search)!= 0) {*temp++ = *search++;
			} *temp = ' I ';
			Inttable[flagofinttable] = atoi (strtoken);
			Convert string to int, atof (); itoa (int value,char*s1,int radix) data[flagofstruct].id = INTEGER;
			data[flagofstruct].indexoftable = flagofinttable;
			Init (strtoken);//reinitialize flagofstruct++;
			flagofinttable++;
		start = search;
			else if (*start = = ' + ') {data[flagofstruct].id = PLUS;
			data[flagofstruct].indexoftable =-1;
			flagofstruct++;
			start++;
			else if (*start = = '-') {data[flagofstruct].id = Minux;
			data[flagofstruct].indexoftable =-1;
			flagofstruct++;
		start++;
			else if (*start = = ' * ') {data[flagofstruct].id = MULTIPLY;
			data[flagofstruct].indexoftable =-1;
			flagofstruct++;
			start++;
			else if (*start = = '/') {data[flagofstruct].id = DIVIDE;
			data[flagofstruct].indexoftable =-1;flagofstruct++;
		start++;
			else if (*start = = Data[flagofstruct].id = Left_b;
			data[flagofstruct].indexoftable =-1;
			flagofstruct++;
		start++;
			else if (*start = =) ') {data[flagofstruct].id = Right_b;
			data[flagofstruct].indexoftable =-1;
			flagofstruct++;
		start++;
} else{start++}} void Traverse () {//convert infix expression to suffix expression for (int i = 0;i <flagofstruct;i++) {if (data[i].id = = INTEGER) {Stackofe
		p[countofep++] = data[i];//If it is a number, direct pressure into the suffix expression.
					else if (data[i].id >=plus&&data[i].id <= DIVIDE) {//if is the operator symbol while (true) {if (Countofid = 0) {
					stackofid[countofid++] = data[i].id;
				Break
				else if (Data[i].id-stackofid[countofid] >= 2) {stackofid[countofid++] = data[i].id;//into stack break;
					} else{stackofep[countofep].id = stackofid[--countofid];//out Stack stackofep[countofep].indexoftable =-1;
				countofep++; else if (data[i].id = left_b) {//Stackofid[countofid++] = data[i].id; If else if (data[i].id = = Right_b) {while (true) {//until there is an opening parenthesis, the pop-up loop if (stackofid[countofid-1] = = Left_b) {//must be reduced by one, otherwise the logic
					Error.
				countofid--;//out stack break;
				} if (Countofid = = 0) {break;
				} stackofep[countofep].id = Stackofid[--countofid];
				stackofep[countofep].indexoftable =-1;
			countofep++;
				The contents of all the symbol stacks are released and added to the suffix while (countofid) {stackofep[countofep].id = Stackofid[--countofid] When the data entry ends;
				stackofep[countofep].indexoftable =-1;
			countofep++;
	COMPUTE suffix expression int compute () {short tempptr = 0;
	int number[120]; for (int i = 0;i <countofep;i++) {if (stackofep[i].id = INTEGER) {number[tempptr++] = Inttable[stackofep[i].indexof
		Table];
			else if (stackofep[i].id = = PLUS) {int a = Number[--tempptr];
			int b = Number[--tempptr];
		number[tempptr++] = B + A;
			else if (stackofep[i].id = = minux) {int a = Number[--tempptr];
			int b = Number[--tempptr];
		number[tempptr++] = b-a; } else if (stackofep[i].id = = MULTIPLY) {int a = Number[--tempptr];
			int b = Number[--tempptr];
		number[tempptr++] = b * A;
			else if (stackofep[i].id = = DIVIDE) {int a = Number[--tempptr];
			int b = Number[--tempptr];
		number[tempptr++] = b/a;
} return *number; void display () {for (int i = 0;i <flagofstruct;i++) {if (data[i].id = INTEGER) {printf ("%d"), Inttable[data[i].indexof
		Table]);
		else if (data[i].id = = PLUS) {printf ("+");}
		else if (data[i].id = = Minux) {printf ("-");}
		else if (data[i].id = = MULTIPLY) {printf ("*");}
		else if (data[i].id = = DIVIDE) {printf ("/");}
		else if (data[i].id = = left_b) {printf ("(");}
		else if (data[i].id = = right_b) {printf (")");}
	else{printf ("\ n");} } void Displayep () {for (int i = 0;i <countofep;i++) {if (stackofep[i].id = = INTEGER) {printf ("%d", INTTABLE[STACKOFEP
		[i].indexoftable]);}
		else if (stackofep[i].id = = PLUS) {printf ("+");}
		else if (stackofep[i].id = = Minux) {printf ("-");} else if (stackofep[i].id = = MULTIPLY) {printf ("*");}
		else if (stackofep[i].id = = DIVIDE) {printf ("/");}
		else if (stackofep[i].id = = left_b) {printf ("(");}
		else if (data[i].id = = right_b) {printf (")");}
	else{printf ("\ n");}
	int main () {//test Wordanalytse () char str[20] = "12+3* (8/2)";
	Deleblank (str);
	printf (buffer);
	Wordanalyse (buffer);
	Traverse ();
	Displayep ();
	printf ("\ n");
	printf ("%d\n", compute ());
return 0;
 }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.