Advertisement
Mishan150

all done

Apr 25th, 2019
985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string input = "25-((47*98/36*56)-sin(45-89)*0)";
  6. string output = "";
  7.  
  8. stack <char> oper;
  9. int i, length = 1;
  10. bool dell=0, was_num=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. #define minuse !((input[i-1]>='0' && input[i-1]<='9') || input[i-1]==')')
  60.  
  61. int main(){
  62.     for(i=0;i<input.size();i++){
  63.         if(dell){
  64.             if(was_num && !(num || skc)){
  65.                 output=output+"\n"+'/';
  66.                 dell=0;
  67.                 was_num=0;
  68.             }else if(was_num && i==input.size()-1){
  69.                 output=output+input[i]+"\n"+'/';
  70.                 dell=0;
  71.                 was_num=0;
  72.                 break;
  73.             }else if(num){
  74.                 was_num=1;
  75.             }
  76.         }//*/
  77.         if(input[i]=='-'){
  78.             if(i==0 || minuse) {
  79.                 output=output+input[i];
  80.             }else{
  81.                 output+="\n";
  82.                 out();
  83.                 oper.push(input[i]);
  84.             }
  85.         }else if(input[i]=='+'){
  86.             output+="\n";
  87.             out();
  88.             oper.push(input[i]);
  89.         }else if(input[i]=='*'){
  90.             oper.push(input[i]);
  91.             output+="\n";
  92.         }else if(input[i]=='/'){
  93.             dell=1;
  94.             output+="\n";
  95.             length+=2;
  96.         }else if(sko){     // open (
  97.             oper.push(input[i]);
  98.         }else if(sin){
  99.             oper.push('s');
  100.             i+=3;
  101.         }else if(cos){
  102.             oper.push('c');
  103.             i+=3;
  104.         }else if(tan){
  105.             oper.push('t');
  106.             i+=3;
  107.         }else if(skc){   // close )
  108.             out_with();
  109.         }else{
  110.             output=output+input[i];
  111.         }
  112.     }
  113.     output+="\n";
  114.     out();
  115.     cout<<input<<"\n\n";
  116.     cout<<output<<endl<<endl<<length;
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement