[Expression calculation] HDU hdoj 3000 a simple language

Source: Internet
Author: User

I haven't done any questions about expression operations for a long time. I 've been reading it for a long time ....

This question is somewhat different from the general expression operation. It is necessary to process the equal sign operation ..

 

In fact, the priority of the equal sign operation can be considered as the lowest except (...

For specific operator priorities, you can view books such as C ++. For more information, see ..

 

In addition, map can be used to store variable values...

 

Code comments are very detailed. Let's take a look...

 

# Pragma warning (Disable: 4786) <br/> # include <iostream> <br/> # include <string> <br/> # include <map> <br/> using namespace STD; </P> <p> # define strlen 1000 <br/> // obtain the priority <br/> int getpriority (char priority) <br/>{< br/> switch <br/>{< br/> case '(': <br/> return 0; <br/> break; <br/> case '=': <br/> return 1; <br/> break; <br/> case '+': <br/> return 2; <br/> break; <br/> case '-': <br/> return 2; <br/> break; <B R/> case '*': <br/> return 3; <br/> break; <br/> case '/': <br/> return 3; <br/> break; <br/>}< br/> // determine whether to calculate <br/> bool iscalc (char instack, char outstack) <br/>{< br/> return getpriority (instack)> = getpriority (outstack ); <br/>}< br/> // word segmentation mark <br/> bool issplit (char C) <br/> {<br/> If (C = '+' | <br/> C = '-' | <br/> C = '* '| <br/> C ='/'| <br/> C =' ('| <br/> C = ') '| <br/> C =') <br/> Return true; <br/> else <br/> return false; <br/>}</P> <p> // true is unknown, false is num <br/> bool getnext (char STR [], Int & S, char ans []) <br/>{< br/> int I = s, j = 0; <br/> int Len = strlen (STR); <br/> bool flag = false; <br/> for (; I <Len; I ++) <br/>{< br/> If (issplit (STR [I]) <br/>{< br/> If (j = 0) <br/>{< br/> ans [J ++] = STR [I ++]; <br/> flag = true; <br/>}< br/> break; <br/>} else {<br/> ans [J ++] = ST R [I]; <br/>}< br/> ans [J] = 0x00; <br/> S = I; <br/> return flag; <br/>}</P> <p> // determines whether it is a number. <br/> bool isnum (char s []) <br/> {<br/> If (s [0]> = '0' & S [0] <= '9' | s [0] =' -') <br/> return true; <br/> else <br/> return false; <br/>}</P> <p> int calc (int A, int B, char success) <br/>{< br/> switch (fail) <br/>{< br/> case '+': <br/> return A + B; <br/> break; <br/> case '-': <br/> return a-B; <br/> break; <br/> cas E '*': <br/> return a * B; <br/> break; <br/> case '/': <br/> return A/B; <br/> break; <br/>}</P> <p> void gettruestr (char STR []) <br/>{< br/> int Len = strlen (STR); <br/> int I; <br/> for (I = len-1; I> = 0; I --) <br/> If (STR [I] = ';') <br/> STR [I] = 0x00; <br/> else <br/> break; <br/>}</P> <p> int main () <br/>{< br/> char STR [strlen]; <br/> char operstack [strlen]; <br/> char numstack [strlen] [100]; <br/> char NS [strlen]; <br/> char puttonumstack [strlen]; <br/> int opertop, numtop, strpos; <br/> // elements retrieved from the symbol stack <br/> char ctopoper; <br/> // elements retrieved from the digital stack. <Br/> char a [strlen], B [strlen]; <br/> // for calculation, C is the result <br/> int NUMA, numb, numc; <br/> // int numc to string snumc <br/> char snumc [strlen]; <br/> // Map <br/> Map <string, int> t_map; <br/> int Len; <br/> bool isfushu; <br/> while (CIN. getline (& STR [1], strlen) <br/>{< br/> // memset <br/> memset (operstack, 0, sizeof (operstack )); <br/> memset (numstack, 0, sizeof (numstack); <br/> memset (ANS, 0, sizeof (ANS); <br/> I Sfushu = false; <br/> // Add (<br/> STR [0] = '('; <br/> // remove unnecessary values; <br/> gettruestr (STR); <br/> Len = strlen (STR); <br/> // Add) <br/> STR [Len ++] = ')'; <br/> STR [Len] = 0x00; <br/> // construct the 20 + 20 expression string to the form of (20 + 20) </P> <p> strpos = opertop = numtop = 0; <br/> while (strpos <Len) <br/> {<br/> // if it is an operator <br/> If (getnext (STR, strpos, ANS )) <br/>{< br/> // if this number is negative, that is (-100) this form <br/> If (ANS [0] = '-' & STR [StrPos-1] = '( ') <Br/>{< br/> isfushu = true; <br/> continue; <br/>}< br/> // if it is (then the stack is imported <br/> If (ANS [0] = '(') <br/>{< br/> operstack [opertop ++] = ans [0]; <br/>}< br/> // If the brackets are left, the value is calculated until the left parenthesis. <br/> else if (ANS [0] = ')') <br/>{< br/> // get the top symbol of the stack <br/> while (ctopoper = operstack [-- opertop])! = '(') <Br/>{< br/> // If the symbol is equal, assign a value <br/> If (ctopoper = ') <br/>{< br/> strcpy (B, numstack [-- numtop]); <br/> strcpy (A, numstack [-- numtop]); <br/> If (isnum (B) <br/> numb = atoi (B); <br/> else <br/> numb = t_map [B]; <br/> t_map [a] = numb; <br/> // The result is pushed back to the stack. <br/> strcpy (numstack [numtop ++], B ); <br/>} else {<br/> strcpy (B, numstack [-- numtop]); <br/> strcpy (A, numstack [-- numtop]); <br/> // determines whether a number is used. If it is not a number, set the value to map. <br/> If (isnum (B) <br/> numb = atoi (B ); <br/> else <br/> numb = t_map [B]; <br/> If (isnum (A) <br/> NUMA = atoi (); <br/> else <br/> NUMA = t_map [a]; <br/> // calculate <br/> numc = calc (NUMA, numb, ctopoper ); <br/> sprintf (snumc, "% d", numc); <br/> // The result is pushed back to the stack. <br/> strcpy (numstack [numtop ++], snumc); <br/>}< br/> // if it is a = sign, then the inbound stack <br/>} else if (ANS [0] = ') <br/>{< br/> operstack [opertop ++] = ans [0]; <br/> // if it is another symbol, determine the priority <br/>} else {<br/> // if the top symbol of the stack has a higher priority than or equal to the value of the outer symbol, then calculate <br/> while (iscalc (operstack [OperTop-1], ANS [0]) <br/>{< br/> strcpy (B, numstack [-- numtop]); <br/> strcpy (A, numstack [-- numtop]); <br/> // determines whether a number exists, if it is not a number, set the value to <br/> If (isnum (B) <br/> numb = atoi (B ); <br/> else <br/> numb = t_map [B]; <br/> If (isnum (A) <br/> NUMA = atoi (); <br/> else <br/> NUMA = t_map [a]; <br/> // retrieve the first symbol of the symbol stack <br/> ctopoper = operstack [-- opertop]; <br/> // perform operations <br/> numc = calc (NUMA, numb, ctopoper); <br/> sprintf (snumc, "% d", numc ); <br/> // The result is pushed back to the stack. <br/> strcpy (numstack [numtop ++], snumc ); <br/>}< br/> // if the priority of the symbol outside is greater than the top of the stack, press the stack <br/> If (iscalc (operstack [OperTop-1], ans [0]) = false) <br/> operstack [opertop ++] = ans [0]; <br/>}< br/>} else {<br/> // if not, add the operand to the stack. Negative numbers must be processed here. <br/> If (isfushu) <br/>{< br/> puttonumstack [0] = '-'; <br/> strcpy (& puttonumstack [1], ANS ); <br/>} else {<br/> strcpy (puttonumstack, ANS); <br/>}< br/> strcpy (numstack [numtop ++], puttonumstack ); <br/>}< br/> isfushu = false; <br/>}< br/> strcpy (A, numstack [-- numtop]); <br/> If (isnum (A) <br/> NUMA = atoi (a); <br/> else <br/> NUMA = t_map [a]; <br/> printf ("% d/N", numa); <br/>}< br/> return 0; <br/>}

 

 

=========== Hdoj 3000 a simple language ==================

A simple language <br/> time limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/others) <br/> total submission (s ): 152 accepted submission (s): 24 </P> <p> Problem description <br/> using sor x teaches the C programming language in college, but he finds it's too hard for his students and only a few students can pass the exam. so, he decide to invent a new language to reduce the burden on students. </P> <p> This new language only support four data type, but the syntax is an strict subset of C. it only support assignment operation, brackets operation, addition, subtration, multiplication and division between variables and numbers. the priority of operations is the same as C. </P> <p> In order to void the problem of forgetting the Eliminator ";", this new language allow to omit it. </P> <p> the variable naming rules is the same as C. </P> <p> comments is not allowed in this language. </P> <p> now prof. X need to impelment this language, and the variable part is done by himself. now prof. X need you, A execllent ACM coder's help: Given a section of this language's code, Please calculate it's return value. </P> <p> input <br/> the input contains lines, each line is a section of codes written in the language described above, you can assume that all variables have been declared as int and have been set to 0 initially. </P> <p> output <br/> to each line of input, output an integer indicate the return value of the input line. the semicolon will only appear in the end of line, you can assume that every literal, variable's value and the intermediate results of calculation wocould never bigger than a short integer. </P> <p> notice: The result may affect by assignment operation, if you don't know the exact return value of some statements such as a = 3, you can try run codes such as 'printf ("% d", A = 3); 'in C, and check the result. </P> <p> sample input <br/> A = 3 <br/> A + B <br/> A = A * (B + 2) + C; <br/> A + B <br/> A/4 <br/> _ Hello = 2 *; </P> <p> sample output <br/> 3 <br/> 3 <br/> 6 <br/> 6 <br/> 1 <br/> 12 </P> <p> source <br/> 2009 multi-university training contest 11-host by hrbeu </P> <p> Recommend <br/> gaojie <br/>

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.