Advertisement
Mishan150

trigan && ()

Apr 25th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string input = "19-56*cos(45+16/98*20-49)*3";
  6. string output = "";
  7.  
  8. stack <char> oper;
  9. int i, length = 1;
  10. bool dell=0;
  11.  
  12. void out(){
  13.     while(oper.size()!=0){
  14.         if(oper.top()=='(' || oper.top()=='s' || oper.top()=='c' || oper.top()=='t') break;
  15.         else{
  16.             output=output+oper.top()+"\n";
  17.             oper.pop();
  18.             length+=2;
  19.         }
  20.     }
  21. }
  22.  
  23. void out_with(){
  24.     while(oper.size()!=0){
  25.         if(oper.top()=='('){
  26.             oper.pop();
  27.             break;
  28.         }else if(oper.top()=='s'){
  29.             output=output+"\n"+"sin";
  30.             oper.pop();
  31.             length++;
  32.             break;
  33.         }else if(oper.top()=='c'){
  34.             output=output+"\n"+"cos";
  35.             oper.pop();
  36.             length++;
  37.             break;
  38.         }else if(oper.top()=='t'){
  39.             output=output+"\n"+"tan";
  40.             oper.pop();
  41.             length++;
  42.             break;
  43.         }else{
  44.             output=output+"\n"+oper.top();
  45.             oper.pop();
  46.             length+=2;
  47.         }
  48.     }
  49. }
  50.  
  51. #define sin input[i]=='s' && input[i+1]=='i' && input[i+2]=='n' && input[i+3]=='('
  52. #define cos input[i]=='c' && input[i+1]=='o' && input[i+2]=='s' && input[i+3]=='('
  53. #define tan input[i]=='t' && input[i+1]=='a' && input[i+2]=='n' && input[i+3]=='('
  54. #define sko input[i]=='('
  55. #define skc input[i]==')'
  56. #define low input[i]=='+' || input[i]=='-'
  57. #define high input[i]=='*' ||input[i]=='/'
  58. #define num (input[i]>='0' && input[i]<='9')
  59.  
  60. int main(){
  61.     for(i=0;i<input.size();i++){
  62.         if(!num && dell){
  63.             output=output+"\n"+'/';
  64.             dell=0;
  65.         }
  66.         if(low){
  67.             output+="\n";
  68.             out();
  69.             oper.push(input[i]);
  70.         }else if(input[i]=='*'){
  71.             oper.push(input[i]);
  72.             output+="\n";
  73.         }else if(input[i]=='/'){
  74.             dell=1;
  75.             output+="\n";
  76.             length+=2;
  77.         }else if(sko){     // open (
  78.             oper.push(input[i]);
  79.         }else if(sin){
  80.             oper.push('s');
  81.             i+3;
  82.         }else if(cos){
  83.             oper.push('c');
  84.             i+=3;
  85.         }else if(tan){
  86.             oper.push('t');
  87.             i+=3;
  88.         }else if(skc){   // close )
  89.             out_with();
  90.         }else{
  91.             output=output+input[i];
  92.         }
  93.     }
  94.     output+="\n";
  95.     out();
  96.     cout<<input<<"\n\n";
  97.     cout<<output<<endl<<endl<<length;
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement