Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream f("ecuatii.in");
  6. ofstream g("ecuatii.out");
  7.  
  8. char ecuatie[50],semn;
  9. int delta,k=1,a,b,c;
  10.  
  11. int main()
  12. {
  13.     f.getline(ecuatie,50);
  14.     strcat(ecuatie,"x");
  15.     if(ecuatie[0]=='-')
  16.     {
  17.         if(ecuatie[k]=='x') a=-1;
  18.         else
  19.         {
  20.             while(ecuatie[k]!='x')
  21.             {
  22.                 a=a*10+(ecuatie[k]-'0');
  23.                 k++;
  24.             }
  25.             a=-a;
  26.         }
  27.         k+=3;
  28.         if(ecuatie[k]=='+') semn='+';
  29.         else semn='-';
  30.         k++;
  31.         if(ecuatie[k]=='0') b=0,k+=2;
  32.         else if(ecuatie[k]=='x')
  33.         {
  34.             if(semn=='+') b=1;
  35.             else b=-1;
  36.             k++;
  37.         }
  38.         else
  39.         {
  40.             while(ecuatie[k]!='x')
  41.             {
  42.                 b=b*10+(ecuatie[k]-'0');
  43.                 k++;
  44.             }
  45.             if(semn=='-') b=-b;
  46.             k++;
  47.         }
  48.         if(ecuatie[k]=='+') semn='+';
  49.         else semn='-';
  50.         k++;
  51.         if(ecuatie[k]=='0') c=0;
  52.         else
  53.         {
  54.             while(ecuatie[k]!='x')
  55.             {
  56.                 c=c*10+(ecuatie[k]-'0');
  57.                 k++;
  58.             }
  59.             if(semn=='-') c=-c;
  60.         }
  61.     }
  62.     else
  63.     {
  64.         k=0;
  65.         if(ecuatie[k]=='x') a=1;
  66.         else
  67.         {
  68.             while(ecuatie[k]!='x')
  69.             {
  70.                 a=a*10+(ecuatie[k]-'0');
  71.                 k++;
  72.             }
  73.         }
  74.         k+=3;
  75.         if(ecuatie[k]=='+') semn='+';
  76.         else semn='-';
  77.         k++;
  78.         if(ecuatie[k]=='0') b=0,k+=2;
  79.         else if(ecuatie[k]=='x')
  80.         {
  81.             if(semn=='+') b=1;
  82.             else b=-1;
  83.             k++;
  84.         }
  85.         else
  86.         {
  87.             while(ecuatie[k]!='x')
  88.             {
  89.                 b=b*10+(ecuatie[k]-'0');
  90.                 k++;
  91.             }
  92.             if(semn=='-') b=-b;
  93.             k++;
  94.         }
  95.         if(ecuatie[k]=='+') semn='+';
  96.         else semn='-';
  97.         k++;
  98.         if(ecuatie[k]=='0') c=0;
  99.         else
  100.         {
  101.             while(ecuatie[k]!='x')
  102.             {
  103.                 c=c*10+(ecuatie[k]-'0');
  104.                 k++;
  105.             }
  106.             if(semn=='-') c=-c;
  107.         }
  108.     }
  109.     delta=b*b-4*a*c;
  110.     if(delta<0) g<<-1;
  111.     else
  112.     {
  113.         if(delta==0)
  114.         {
  115.             float radacina=-b/(2*a);
  116.             g<<fixed<<setprecision(2)<<radacina;
  117.         }
  118.         else
  119.         {
  120.             float radacina1, radacina2;
  121.             radacina1=(-b+sqrt(delta))/(2*a);
  122.             radacina2=(-b-sqrt(delta))/(2*a);
  123.             if(radacina1<radacina2) g<<fixed<<setprecision(2)<<radacina1<<" "<<fixed<<setprecision(2)<<radacina2;
  124.             else g<<fixed<<setprecision(2)<<radacina2<<" "<<fixed<<setprecision(2)<<radacina1;
  125.         }
  126.     }
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement