C + + Implementation Simple Calculator (regular expression calculation)

Source: Internet
Author: User

Description: Simple and efficient C + + code to implement a simple calculator (regular expression calculation), allowing decimals, parentheses. But there is no right-and-wrong test function, so make sure you are correct before testing.

Data structure: Stack

Demonstration Input:

((1.5+2.5) *3-4) +5

42/7-(12+3) *0.5

Standard output:

The answer is 13

The answer is-1.5

Source:

#include <iostream>
#include<stack>using namespaceStd;stack<Double> numbers;//number of storage operationsstack<Char> Operations;//Storage Operators/*prioritize operators with ' # ' as the operations of the stack (for later operation)*/intPriorityCharoperate) { Switch(operate) { Case '#': Case '\ n':return 0; Case '(': Case ')':return 1; Case '+': Case '-':return 2; Case '*': Case '/':return 3; }}voidCalculateChar);intMain () {cout<<"WELCOME CALCULATOR"<< Endl <<Endl<<"input an Algebrais expression to calculate or input ' Q ' to quit"<< Endl <<Endl; CharCommand = cin.Get(); while(Command! ='Q') {Calculate (command); Command= cin.Get(); } return 0;}voidCalculateCharcommand) { Doublenum, Leftnum, rightnum, result; Switch(command) {/*if the input is a number, the data of the double type is stored in the stack*/ Case '0': Case'1': Case '2': Case'3': Case '4': Case'5': Case '6': Case'7': Case '8': Case'9': Cin.putback (command); CIN>>num; Numbers.push (num); Break; Case '(': Case ')': Case '+': Case '-': Case '*': Case '/': Case '\ n': /*initialize the stack bottom element as ' # '*/ if(Operations.empty ()) Operations.push ('#'); /*If you now enter an operator with a higher precedence or input ' (', you should store the current operator and do not perform the previous operator*/ if(Command > Priority (Operations.top ()) | | command = ='(') operations.push (command); /*if the previously entered operator precedence is higher, the previous operator should be executed*/ Else { while(command) <=Priority (Operations.top ())) { /*when the operator is fully implemented, expose the stack bottom element ' # ' and enter ' \ n ' to print the result*/ if(Operations.top () = ='#'&& Command = ='\ n') {result=Numbers.top (); cout<<"The answer is"<< result << Endl <<Endl; Numbers.pop (); Operations.pop (); Break; } /*when the parentheses inside the operator are fully implemented, the parentheses are removed, and the next character is read into*/ Else if(Operations.top () = ='('&&command = =')') {operations.pop (); CIN>>command; } /*In either case, the previous operator, Operations.top () , is completed*/ Else{rightnum=Numbers.top (); Numbers.pop (); Leftnum=Numbers.top (); Numbers.pop (); Switch(Operations.top ()) { Case '+': Numbers.push (Leftnum+rightnum); Operations.pop (); Break; Case '-': Numbers.push (Leftnum-rightnum); Operations.pop (); Break; Case '*': Numbers.push (Leftnum*rightnum); Operations.pop (); Break; Case '/': Numbers.push (Leftnum/rightnum); Operations.pop (); Break; } } } /*once the previous high-priority operation is completed, the current operator (other than ' \ n ') becomes the highest priority, so it should be stored*/ if(command!='\ n') operations.push (command); } Break; }}

C + + Implementation Simple Calculator (regular expression calculation)

Related Article

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.