Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- string input = "25-((47*98/36*56)-sin(45-89)*0)";
- string output = "";
- stack <char> oper;
- int i, length = 1;
- bool dell=0, was_num=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')
- #define minuse !((input[i-1]>='0' && input[i-1]<='9') || input[i-1]==')')
- int main(){
- for(i=0;i<input.size();i++){
- if(dell){
- if(was_num && !(num || skc)){
- output=output+"\n"+'/';
- dell=0;
- was_num=0;
- }else if(was_num && i==input.size()-1){
- output=output+input[i]+"\n"+'/';
- dell=0;
- was_num=0;
- break;
- }else if(num){
- was_num=1;
- }
- }//*/
- if(input[i]=='-'){
- if(i==0 || minuse) {
- output=output+input[i];
- }else{
- output+="\n";
- out();
- oper.push(input[i]);
- }
- }else if(input[i]=='+'){
- 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