An algorithm designed to deal with computational equations

Source: Internet
Author: User

For example, when we do a calculator we will encounter 1+9+6/8 similar to this, then we can use this formula to become a suffix expression and then to calculate.


struct//set operator Precedence {char ch;//operator int pri;//priority}lpri[] = {' = ', 0}, {' (', 1}, {' * ', 5}, {'/', 5}, {' + ', 3}, {' -', 3}, {') ', 6}},rpri[] = {{' = ', 0}, {' (', 6}, {' * ', 4}, {' + ', 4}, {'-', 2}, {') ', 2}};int Leftpri (char op)//The priority of the left operator {int i;for (i = 0; i<7; i++) if (lpri[i].ch = = op) return lpri[i].pri;} int Rightpri (char op)//right operator precedence {int i;for (i = 0; i<7; i++) if (rpri[i].ch = = op) return rpri[i].pri;} BOOL Inop (char ch)//judgment CH is not an operator {if (ch = = ' (' | | ch = = ') ' | | ch = = ' + ' | | ch = = '-' | | | ch = = ' * ' | | ; Elsereturn false;} The precedence condition of the int precede (char OP1, char op2)//OP1 and OP2 operators {if (LEFTPRI (OP1) = = Rightpri (OP2)) return 0;if (Leftpri (OP1) < Rightpri (OP2)) Return-1;elsereturn 1;} void Trans (char *exp, char postexp[])//convert general expression to suffix expression {struct//store operator {char data[1000];int top;//stack pointer}op;int i = 0;op.top = -1;op.top++;op.data[op.top] = ' = '; while (*exp! = ')//exp expression is not scanned when the loop {if (! Inop (*EXP))//If the number {while (*exp >= ' 0 ' &&*exp <= ' 9 ')//determine the number {if (* (exp-2) = = ' (' && (* (exp-1) = = ' + ' | | * (exp-1) = = '-' | | * (exp-1) = = ' * ' | | * (EXP- 1) = = '/')//is a negative sign instead of a minus sign {postexp[i++] = ' & ';//This is to deal with negative cases, if it is a negative number will appear & and numbers such as &5 int a = i + 1;postexp[a++] = *exp;}  postexp[i++] = *exp;exp++;//store expression}postexp[i++] = ' # ';//Use a # number to separate}else//if it is an operator {if (*exp = = ' + ') || *exp = = '-' | | *exp = = ' * ' | | *exp = = '/') && * (exp-1) = = ' (' && (* (exp + 1) >= ' 0 ' &&* (exp + 1) <= ' 9 ')//get rid of negative number-No. {exp+ +;continue;} Else{switch (Precede (op.data[op.top], *exp)) {case-1://stack top operator low priority op.top++;op.data[op.top] = *exp;exp++;break;case 0 ://Only parentheses satisfy this situation op.top--;exp++;break;case 1://and enter into Postexp array postexp[i++] = Op.data[op.top];op.top--;break;}}} while (op.data[op.top]! = ' = ')//At this time has been scanned, the stack to ' = ' End {postexp[i++] = op.data[op.top];op.top--;} Postexp[i] = ' + ';//Add an End flag}float compvalue (char *postexp)//start with a suffix expression to calculate the value {struct{float data[1000];//the array int top that holds the number;} St;float D, A, B, c;st. top = -1;while (*postexp! =)//When the Postexp string is scanned {switch (*POSTEXP) {case ' + '://Judgment is ' + ' when a = st.data[st.top];st.top-- b = st.data[st.top];st.top--;c = a + b;st.top++;st.data[st.top] = c;//This is the calculation of the sum of two numbers when a, a, a, a, and then the stack break;case '-': a = St.da Ta[st.top];st.top--;b = St.data[st.top];st.top--;c = b-a;st.top++;st.data[st.top] = C;break;case ' * ': a = St.data[st.top ];st.top--;b = St.data[st.top];st.top--;c = b*a;st.top++;st.data[st.top] = C;break;case '/': a = st.data[st.top]; St.top--;b = St.data[st.top];st.top--;if (A! = 0) {c = b/a;st.top++;st.data[st.top] = c;} else{printf ("error (denominator 0) \ n"); exit (0);} Break;default:d = 0;int find = 0;while (*postexp >= ' 0 ' &&*postexp <= ' 9 ' &&*postexp! = ' & ') {if (* (postexp-1) = = ' & ') {find = 1;} Else{find =-1;} D = Ten * d + *postexp-' 0 ';p ostexp++;} if (find = = 1) {St.top++;st.data[st.top] =-D;} else if (find = =-1) {st.top++;st.data[st.top] = D;} break;} postexp++;} Return (St.data[st.top]);//The position of the last pointer refers to the value}

This is a C + + version, is now making this into a Java package after the dedicated use

An algorithm designed to deal with computational equations

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.