Analog calculator: evaluate an expression with addition, subtraction, multiplication, division, and parentheses

Source: Internet
Author: User

 

// Integer, add, subtract, multiply, divide, evaluate multiple parentheses, for example,-1 + (2-(3 + 4)/7) * 5, (without input check): # include <sstream> # include <iostream> # include <string> using namespace STD; int stoi (string S); string itos (INT Myint ); string scalculate (string S1, string S2, char OPR); string nobracket (string S); void main () {int N, start = 0, I; string S, S1; cout <"input the expression:" <Endl; CIN> S; // example-1 + (2-(3 + 4)/7) * 5 // processing brackets: While (start! =-1) {start =-1; n = S. length (); for (I = 0; I <n; I ++) {If (S. at (I) = '(') {start = I + 1;} else if (S. at (I) = ') {S1 = nobracket (S. substr (start, I-Start); // process the subexpression S = s without parentheses. substr (0, start-1) + S1 + S. substr (I + 1); // Replace the values in the brackets with the original brackets and their content. // cout <"s =" <S <Endl; break ;}}} S = nobracket (s); // process the entire cout expression without parentheses <S <Endl;} string nobracket (string S) {// process the entire expression int start = 0, N, I, j, m = 0; char C; string sub without parentheses S [200]; C = S. at (0); If (C = '+') s. replace (, ""); // if the expression starts with a positive sign, remove it else if (C = '-') s. replace (, "_"); // replace N = s with an underscore if the expression starts with a negative sign. length (); for (I = 0; I <n; I ++) {c = S. at (I); If (C = '+' | C = '-' | C = '*' | C = '/') {Subs [M ++] = S. substr (start, I-Start); // place each number and operator in the subs [] Unit of the array. Subs [M ++] = C; start = I + 1 ;}} subs [m] = S. substr (start); I = 0; while (I <m) {c = Subs [I]. at (0); If (C = '*' | C = '/') {// first multiply the subs [I-1] = Scalculate (subs [I-1], subs [I + 1], c); For (j = I; j <m-1; j ++) subs [J] = Subs [J + 2]; m-= 2;} else I ++;} for (I = 1; I <m; I = I + 2) {// add or subtract subs [0] = scalculate (subs [0], subs [I + 1], subs [I]. at (0);} return subs [0];} string scalculate (string S1, string S2, char OPR) {// calculate the two strings to obtain a string if (OPR = '+') return itos (stoi (S1) + stoi (S2 )); else if (OPR = '-') return itos (stoi (S1)-stoi (S2); else if (OPR = '*') return itos (stoi (S1) * St Oi (S2); else if (OPR = '/') return itos (stoi (S1)/stoi (S2); else return "error OPR! ";} Int stoi (string s) {// convert string to int if (S. at (0) = '_') s. replace (, "-"); // restores the underline to the negative istringstream buffer (s); int Myint; buffer> Myint; return Myint;} string itos (INT Myint) {// convert int to string ostringstream buffer; buffer <Myint; return buffer. STR ();}

 

Analog calculator: evaluate an expression with addition, subtraction, multiplication, division, and parentheses

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.