Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- string input = "19-56*cos(45+16/98*20-49)*3";
- string output = "";
- stack <char> oper;
- int i, length = 1;
- bool dell=0;
- void out(){
- while(oper.size()!=0){
- if(oper.top()=='(' || oper.top()=='s' || oper.top()=='c' || oper.top()=='t') break;
- else{
- output=output+oper.top()+"\n";
- oper.pop();
- length+=2;
- }
- }
- }
- void out_with(){
- while(oper.size()!=0){
- if(oper.top()=='('){
- oper.pop();
- break;
- }else if(oper.top()=='s'){
- output=output+"\n"+"sin";
- oper.pop();
- length++;
- break;
- }else if(oper.top()=='c'){
- output=output+"\n"+"cos";
- oper.pop();
- length++;
- break;
- }else if(oper.top()=='t'){
- output=output+"\n"+"tan";
- oper.pop();
- length++;
- break;
- }else{
- output=output+"\n"+oper.top();
- oper.pop();
- length+=2;
- }
- }
- }
- #define sin input[i]=='s' && input[i+1]=='i' && input[i+2]=='n' && input[i+3]=='('
- #define cos input[i]=='c' && input[i+1]=='o' && input[i+2]=='s' && input[i+3]=='('
- #define tan input[i]=='t' && input[i+1]=='a' && input[i+2]=='n' && input[i+3]=='('
- #define sko input[i]=='('
- #define skc input[i]==')'
- #define low input[i]=='+' || input[i]=='-'
- #define high input[i]=='*' ||input[i]=='/'
- #define num (input[i]>='0' && input[i]<='9')
- int main(){
- for(i=0;i<input.size();i++){
- if(!num && dell){
- output=output+"\n"+'/';
- dell=0;
- }
- if(low){
- output+="\n";
- out();
- oper.push(input[i]);
- }else if(input[i]=='*'){
- oper.push(input[i]);
- output+="\n";
- }else if(input[i]=='/'){
- dell=1;
- output+="\n";
- length+=2;
- }else if(sko){ // open (
- oper.push(input[i]);
- }else if(sin){
- oper.push('s');
- i+3;
- }else if(cos){
- oper.push('c');
- i+=3;
- }else if(tan){
- oper.push('t');
- i+=3;
- }else if(skc){ // close )
- out_with();
- }else{
- output=output+input[i];
- }
- }
- output+="\n";
- out();
- cout<<input<<"\n\n";
- cout<<output<<endl<<endl<<length;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement