Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.3 kB
2
Indexable
Never
#include <iostream>
#include <cstring>
#include <vector>

using namespace std;

char s[2550]="2x-4+5x+300=98x";
int n, nr1, nr2, nr1x, nr2x, cnt;

int main(){
  
  //ifstream cin("ecuatii.in");
  //ofstream cout("ecuatii.out");
  
  cin>>n;
  
  for(int i=0; i<n; i++){
      if(cnt==0){
        if(int(s[i])>=48 && int(s[i])<=57){
            if(s[i+1]!='x' && int(s[i+1])<48 || int(s[i+1])>57){
                  if(s[i-1]=='+'){
                      nr1+=s[i];
                  }
                  else if(s[i-1]=='-'){
                      nr1-=s[i];
                  }
                  else if(i==0){
                      nr1+=s[i];
                  }
              }
              else if(s[i+1]=='x' && int(s[i+1])<48 || int(s[i+1])>57){
                 if(s[i-1]=='+'){
                      nr1x+=s[i];
                 }
                 else if(s[i-1]=='-'){
                      nr1x-=s[i];
                 }
                 else if(i==0){
                      nr1+=s[i];
                  }
              }
          }
      }
      
      if(s[i]=='='){
          cnt++;
      }
      
      if(cnt==1){
          if(s[i]>='0' && s[i]<='9'){
                if(s[i+1]!='x'){
                    if(s[i-1]=='+'){
                         nr2+=s[i];
                     }
                     else if(s[i-1]=='-'){
                          nr2-=s[i];
                     }
                     else if(i==0 || s[i-1]=='='){
                         nr2+=s[i];
                     }
                 }
                 else{
                    if(s[i-1]=='+'){
                         nr2x+=s[i];
                    }
                    else if(s[i-1]=='-'){
                         nr2x-=s[i];
                    }
                    else if(i==0 || s[i-1]=='='){
                         nr2x+=s[i];
                     }
                }
            }
        }
  }
    //cout<<nr1<<" "<<nr1x<<endl<<nr2<<" "<<nr2x;
    if(nr1x!=nr2x){
        cout<<(float)(nr2-nr1)/(nr1x-nr2x)<<endl;
    }
    else if(nr1x==nr2x){
        if(nr1!=nr2){
            cout<<"imposibil\n";
        }
        else if(nr1==nr2){
            cout<<"infinit\n";
        }
    }

    return 0;
}