NYOJ257 depressed C small plus (a)

Source: Internet
Author: User

Depressed C small plus (a) time limit: +Ms | Memory Limit:65535KB Difficulty:3
Describe

we are familiar with expressions such asa+b,a+b* (c+d)and so all belong to infix expression. The infix expression is the (for binocular operator) operator in the middle of two operands:num1 operand num2. Similarly, a suffix expression is the operator after two operands:num1 num2 operand. ACMteam's "Csmall Plus "is depressed how to convert an infix expression to a suffix expression, now you have to design a program to helpCSmall plus converts infix expressions to suffix expressions. To simplify the problem, the operands are all single digits and the operator is only+-*/and parentheses.

Input
the first line, input T, indicates that there is a T set of test data (T<10).
Each set of test data has only one row, which is a string of not more than 1000 in length, representing the expression. This expression contains only +-*/and parentheses. Where parentheses can be used in a nested set. The data guarantees that no negative numbers appear in the operands entered. And the input data does not appear to be mismatched.
Output
each set of outputs is separated by an output conversion suffix expression.
Sample input
21+2 (1+2) *3+4*5
Sample output
12+12+3*45*+
This problem is similar to infix suffix: Specific ideas can be viewed http://blog.csdn.net/java_oracle_c/article/details/41986941
AC code: # include <stdio.h># include <stdlib.h># include <string.h># define False 0# define True 1
typedef struct Node {char data; struct node *next; }linkstacknode, *linkstack;
int Initstack (Linkstack *s)//initialization stack {(*s) = (linkstack) malloc (sizeof (Linkstacknode));  (*s)->next = NULL;  if ((*s) = NULL) return True; else return False; }
int Push (Linkstack S, char x)//into stack {Linkstack temp;  temp = (linkstack) malloc (sizeof (Linkstacknode));  if (temp = = NULL) return False;  Temp->data = x;  Temp->next = s->next;  S->next = temp; return True; }
int Pop (Linkstack S)//out stack {linkstack temp;  temp = s->next;  if (temp = = NULL) return False;  S->next = temp->next;  Free (temp); return True; }int Top (Linkstack S) {char e; e = s->next->data; return e;}
 //*************************************************************************int cmp (char ch) { switch ( CH)  { case ' + ':  case '-': return 1; case ' * ':  case '/': Return 2; default:return 0;  }}void Fun (char *a, char *b,linkstack s) { push (S, ' # ');  int i = 0,j  = 0; while (i < strlen (a)) &nbsp ; {  if (a[i] = = ' (')   {   push (S,a[i]);    i++;  }   else if (a[i] = = ') ')   {   while (top (s)!=  ' (')    { & NBSP;&NBSP;&NBSP;B[J] = top (s);     j++;    pop (s);    }     pop (s);    i++;  }  else if (a[i] = = ' + ' | | a[i] = = '-' | | a[i] = = ' * ' || A[i] = = '/')   {   while (CMP (top (s)) >= CMP (a[i))    {    &NBSP;B[J] = top (s);     j++;    pop (s);    }   push (S,a[i]);    i++;  }  else    {   while (' 0 ' <= a[i] &&a[i] <= ' 9 ' | | A[i] = = '. ') &NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;B[J] = a[i];    i++;j++;   }   } } while (Top (s)! = ' # ')  {  b[j] = top (s);   j++;  pop (s);  }
}int Main (void) {int n,i; char a[1001],b[2002]; Linkstack S; scanf ("%d", &n);  while (n--) {memset (b, ' a ', sizeof (b));  Initstack (&s);  scanf ("%s", a);  Fun (a,b,s);  i = 0;   while (b[i]! = ' a ') {printf ("%c", B[i]);  i++; } printf ("\ n"); } return 0;}

NYOJ257 depressed C small plus (a)

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.