Four arithmetic operations, simple, only single-digit addition and subtraction

Source: Internet
Author: User

Arithmetic arithmetic time limit: 1000 MS case time limit: 1000 MS memory limit: 64 MB
Total submission: 82 submission accepted: 24description a given four arithmetic expressions, including + -*/().


There are n rows of input. Each row has four arithmetic expressions, and no more than 10 digits of AI (1 <= I <= 10 ).
For ease of setting 0 <= AI <= 9


Output has n rows of output, one operation result per line


Sample Input

Original Transformed
1+15/2


Sample output

Original Transformed
22.5

# Include <stdio. h>

# Include <string. h>
# Deprecision max 200
Typedef struct Node
{
Int tail;
Int head;
Char A [200];
} Queue;
Void create (queue &);
Void enqueue (char, queue &);
Char dequeue (queue &);
Int empty (Queue );
Int full (Queue );
Void change (char *);
Double Suan ();
Queue Q;
Char stack [200];
Double stack [2, 200];
Int top;
Int main ()
{
Char s [200];
Int N, SL;
While (scanf ("% s", & S )! = EOF)
{
SL = strlen (s );
S [Sl] = ';';
S [SL + 1] = '\ 0 ';
Change (s );
Printf ("% G \ n", SUAN ());
}
Return 0;
}
Void change (char * s)
{
Int SL, I, T;
Int privilege (char );
SL = strlen (s );
Create (Q );
Top ++;
Stack [Top] = '(';
For (I = 0; I <SL; I ++)
{
Switch (s [I])
{
Case '0 ':
Case '1 ':
Case '2 ':
Case '3 ':
Case '4 ':
Case '5 ':
Case '6 ':
Case '7 ':
Case '8 ':
Case '9': enqueue (s [I], q); break;
Case '(': Top ++; stack [Top] = s [I]; break;
Case ')':
Case ';':
Do {
T = stack [Top];
Top --;
If (T! = '(')
Enqueue (t, q );
} While (T! = '(' & Top! =-1); break;
Case '+ ':
Case '-':
Case '*':
Case '/':
While (privilege (s [I]) <= privilege (stack [Top])
{
T = stack [Top];
Top --;
Enqueue (t, q );
}
Top ++;
Stack [Top] = s [I]; break;
}
}
}
Int privilege (char C)
{
Switch (c)
{
Case '(': Return 0; break;
Case '-':
Case '+': return 1; break;
Case '*':
Case '/': return 2; break;
}
}
Int full (queue q)
{
If (Q. Tail + 1) % max = Q. Head) return 1;
Else return 0;
}
Int empty (queue q)
{
If (Q. Head = Q. Tail) return 1;
Else return 0;
}
Char dequeue (queue & Q)
{
Char C;
If (empty (q ))
{
Printf ("underflow! \ N ");
}
C = Q. A [q. Head];
Q. Head = (Q. Head + 1) % Max;
Return C;
}
Void enqueue (char C, queue & Q)
{
If (full (q ))
{
Printf ("over flow! \ N ");
Return;
}
Q. A [q. Tail] = C;
Q. Tail = (Q. Tail + 1) % Max;
}
Void create (queue & Q)
{
Q. Head = Q. Tail = 0;
}
Double Suan ()
{
Char T;
Double X, Y;
Int Top =-1;
While (! Empty (q ))
{
T = dequeue (Q );
If (T> = '0' & T <= '9 ')
{
Top ++;
Stack [Top] = (INT) T-'0 ';
}
Else
{
Y = stack [Top];
Top --;
X = stack [Top];
Top --;
Switch (t)
{
Case '+': Top ++; stack [Top] = x + y; break;
Case '*': Top ++; stack [Top] = x * Y; break;
Case '/': Top ++; stack [Top] = x/y; break;
Case '-': Top ++; stack [Top] = x-y; break;
}
}
}
Return stack [Top];
}

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.