Advertisement
Guest User

qwertyj

a guest
Dec 14th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. ifstream fin("ecuatii2.in");
  5. ofstream fout("ecuatii2.out");
  6.  
  7. char s[255];
  8. int n;
  9.  
  10. void rezolvare(char s[])
  11. {
  12.     int i=0,coef1=0,coef2=0,semn=1,x=-1;
  13.     while(s[i]!='=')
  14.     {
  15.         if(s[i]=='-')
  16.         {
  17.             semn=-1;
  18.             i++;
  19.         }
  20.         else if(s[i]=='+')
  21.         {
  22.             semn=1;
  23.             i++;
  24.         }
  25.         else if(s[i]>='0' && s[i]<='9')
  26.         {
  27.             x=0;
  28.             while(s[i]>='0' && s[i]<='9')
  29.             {
  30.                 x=x*10+s[i]-'0';
  31.                 i++;
  32.             }
  33.             if(s[i]=='x')
  34.             {
  35.                 coef1+=semn*x;
  36.                 i++;
  37.             }
  38.             else
  39.                 coef2+=semn*x;
  40.             semn=1;
  41.             x=-1;
  42.         }
  43.         else if(s[i]=='x')
  44.         {
  45.             coef1+=semn;
  46.             i++;
  47.         }
  48.     }
  49.     i++;
  50.     semn=-1;
  51.     while(s[i])
  52.     {
  53.         if(s[i]=='-')
  54.         {
  55.             semn=1;
  56.             i++;
  57.         }
  58.         else if(s[i]=='+')
  59.         {
  60.             semn=-1;
  61.             i++;
  62.         }
  63.         else if(s[i]>='0' && s[i]<='9')
  64.         {
  65.             x=0;
  66.             while(s[i]>='0' && s[i]<='9')
  67.             {
  68.                 x=x*10+s[i]-'0';
  69.                 i++;
  70.             }
  71.             if(s[i]=='x')
  72.             {
  73.                 coef1+=semn*x;
  74.                 i++;
  75.             }
  76.             else
  77.                 coef2+=semn*x;
  78.             semn=-1;
  79.             x=1;
  80.         }
  81.         else if(s[i]=='x')
  82.         {
  83.             coef1+=semn;
  84.             i++;
  85.         }
  86.     }
  87.     if(coef1==coef2 && coef1==0)
  88.         fout<<"infinit\n";
  89.     else if(coef1==0)
  90.         fout<<"imposibil\n";
  91.     else
  92.     {
  93.         double sol=-1.0*coef2/coef1;
  94.         fout<<setprecision(4)<<fixed<<sol<<"\n";
  95.     }
  96. }
  97.  
  98. int main()
  99. {
  100.     fin>>n;
  101.     while(n)
  102.     {
  103.         fin>>s;
  104.         rezolvare(s);
  105.         n--;
  106.     }
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement