UVa 327:evaluating Simple C Expressions

Source: Internet
Author: User

Topic Link:

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_ problem&problem=263

Type of topic: data structure, two fork tree

Sample input:

A + b
b-z
a+b--+c++
c+f--+--a
   f--+ c--+ d-++e

Sample output:

Expression:a + b
    value = 3
    a = 1
    b = 2
expression:b-Z

The main effect of the topic:

To an expression, the variable of the expression is composed of 26 lowercase letters, which are divided into 1,2,3 in order of the initial value ... 26, and a variable in the expression does not recur. Operator by +,-, + +,--(self-added and self-subtraction has prefix and suffix).

And then output the value of the expression, and the value of each variable that appears after the calculation

Ideas for solving problems:

Because it is a data structure topic, at the beginning of natural thought is the method of achievement.

This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45529.htm

After you think of a good method, start knocking the code. When the code of achievement is finished, and the results are ready to be calculated, it turns out that the problem does not need to be accomplished, and it is much simpler.

Here is the non-build version of the code:

#include <iostream> #include <cstdio> #include <cctype> #include <cstring> #include <deque&  
Gt  
     
#include <vector> #include <algorithm> using namespace std;  
vector<char>var;  
deque<int>que;  
const int MAXN = 120;  
Char STR[MAXN]; int val[26];  
     
The initial value int increment to hold the a,b,...... Z;  
    Filter the input string to eliminate the space void Filter () {int pos=0;  
        for (int i=0; I<strlen (str); ++i) {if (Str[i]!= ') {str[pos++] = Str[i]; }} Str[pos] = 0; String end Flag '///whether there is a prefix inline bool haveprefix (int i) {if (str[i-1]== ' + ' &&str[i-2]== ' + ' | | str[  
    i-1]== '-' &&str[i-2]== '-') return true;  
return false; }//whether there are suffixes inline bool Havesuffix (int i) {if (str[i+1]== ' + ' &&str[i+2]== ' + ' | | str[i+1]== '-' &&str[  
    i+2]== '-') return true;  
return false;  
    } void Preprosess () {increment = 0; WHile (!que.empty ()) Que.pop_back ();  
    Var.clear ();  
            for (int i=0; I<strlen (str); ++i) {if (str[i]>= ' a ' && str[i]<= ' z ') {//have prefix  Var.push_back (Str[i]); Deposit the letter if (i>=2 && haveprefix (i)) {if (str[i-1]== ' + ') + +  
                Val[str[i]-' a '];  
                Else--val[str[i]-' a '];  
                int n = val[str[i]-' a '];  
                Que.push_back (n);   
            Str[i-1]=str[i-2] = ';  }//has suffix else if (i<=strlen (str)-3 && Havesuffix (i)) {int n  
                = Val[str[i]-' a '];  
                Que.push_back (n);  
                    if (str[i+1]== ' + ') {++val[str[i]-' a '];  
                --increment;  
                    } else{--val[str[i]-' a '];  
                ++increment; } sTR[I+1] = str[i+2] = ';  
                else {int n = val[str[i]-' a '];  
            Que.push_back (n); ()}} int getsum () {for (int i=0; I<strlen (str); ++i) {if (str[i]== ' + ' | | str[i]== ')  
            -') {int a=que.front ();  
            Que.pop_front ();  
            int B=que.front ();  
            Que.pop_front ();  
            if (str[i]== ' + ') Que.push_front (a+b);  
        else Que.push_front (a-b);  
} return Que.front ();  
    } void Solve () {//to A,b,c......z initial value for (int i=0; i<26; ++i) {val[i] = i+1;  
    } Filter ();  
    Preprosess ();  
  int sum = Getsum ();  
    Puts (str);  
    printf ("value =%d\n", sum);  
    Sort (Var.begin (), Var.begin () +var.size ());  
    for (int i=0; i<var.size (); ++i) {printf ("%c =%d\n", Var[i], val[var[i]-' a ']); }//printf ("\ n");  
    int main () {freopen ("Input.txt", "R", stdin);  
    Freopen ("Output.txt", "w", stdout);  
        while (gets (str)) {printf ("Expression:%s\n", str);  
    Solve ();  
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.