c ++ simple calculator

  Try to write a calculator using c ++, support addition, subtraction and brackets.

  I was done step by step, to write a simple does not support brackets, support brackets and then changed version.

 

A. Supports only addition, subtraction

  Handwritten with two stacks (not recommended stl stack, the stack is not difficult to write because, stl feel too slow), a stack storage symbol, a stack storage has been read into the number (unsigned).

  Sweep over expression, the symbols and numerals in sequence stored into two stacks (Note, if the first number is a positive number, then the first symbol into the stack if the + sign; if it is negative, a push - number, storage stack number is stored into a positive number). Stack storage symbol only need to store the + and - on the line, or if read * \ number, the number of direct multiplication and division to the top of the stack, so you can ensure the priority of multiplication and division.

  After Saowan order them over the stack on the line.

#include <cstdio> 
#include <CString>
 the using  namespace STD;
 const  int MAXN = 10000 ; 

int n-;
 char CH [+ MAXN . 5 ]; 

int read_pos;     // Read () reads the position of the last character fashionable + 1'd 
inline int Read ( int S) {     // returns ch [s] to the beginning of the number 
    int ANS = 0 ;
     for (; (ch [s]> = ' 0 ' && ch [s] <= ' . 9 ' ); S ++ ) 
        ANS ANS * = 10CH + [S] - ' 0 ' ; 
    read_pos = S;
     return ANS; 
} 

int Solve ( int l, int r) {     // expression to a value of r l 
    int Stack [ 2 ] [+ MAXN . 5 ], len = 0 ;     // handwriting stack 
    IF (! CH [L] = ' - ' ) 
        stack [ 0 ] [len ++] = . 1 , stack [ . 1 ] [len] = Read (L);
     the else stack [ 0 ] [++ len] = - . 1 , Stack [1][len]=read(l+1);
    l=read_pos;
    int temp;
    while (l<=r){
        if (ch[l]=='+')
            stack[0][++len]=1,stack[1][len]=read(l+1);
        else if (ch[l]=='-')
            stack[0][++len]=-1,stack[1][len]=read(l+1);
        else if (ch[l]=='*')
            temp=read(l+1),stack[1][len]*=temp;
        else temp=read(l+1),stack[1][len]/=temp;
        l=read_pos;
    }
    int result=0;
    for (int i=1;i<=len;i++)    //处理掉栈中的数 
        result+=(stack[0][i]*stack[1][i]);
    return result;
}

int main(){
//    freopen("test2.in","r",stdin);
    scanf("%s",ch);
    int tt=strlen(ch);
    printf("%d",solve(0,tt-1));
    return 0;
}

 

II. Support brackets

  There are many methods should simulate, when I was deposited into each pre-position the brackets, and the brackets on the line recursive computation (the brackets when the number of view).

#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=10000;

int n;
char ch[maxn+5];
int to[maxn+5];

int que[maxn+5],size;

int read_pos;

int read(int s);

int solve(int l,int r){
    int stack[2][maxn+5],len=0;
    if (ch[l]!='-')
        stack[0][++len]=1,stack[1][len]=read(l);
    else stack[0][++len]=-1,stack[1][len]=read(l+1);
    l=read_pos;
    int temp;
    while (l<=r){
        if (ch[l]=='+')
            stack[0][++len]=1,stack[1][len]=read(l+1);
        else if (ch[l]=='-')
            stack[0][++len]=-1,stack[1][len]=read(l+1);
        else if (ch[l]=='*')
            temp=read(l+1),stack[1][len]*=temp;
        else    temp=read(l+1),stack[1][len]/=temp;
        l=read_pos;
    }
    int result=0;
    for (int i=1;i<=len;i++)
        result+=(stack[0][i]*stack[1][i]);
    return result;
}

int main(){
//    freopen("test2.in","r",stdin);
    scanf("%s",ch);
    int tt=strlen(ch);
    for (int i=0;i<tt;i++)
        if (ch[i]=='(')
            que[++size]=i;
        else if (ch[i]==')')
            to[que[size]]=i,size-=1;
    printf("%d",solve(0,tt-1));
    return 0;
}


inline int read(int s){
    int ans=0,f=1;
    if (ch[s]=='('){
        ans=solve(s+1,to[s]-1);
        read_pos=to[s]+1;
        return ans;
    }
    else if (ch[s]=='-') f=-1,s+=1;
    for (;(ch[s]>='0'&&ch[s]<='9');s++)
        ans=ans*10+ch[s]-'0';
    read_pos=s;
    return ans*f;
}

 

Guess you like

Origin www.cnblogs.com/awakening-orz/p/11265694.html