Квадратно уравнение

Да се напише програма, която решава квадратно уравнение от вида a.x2+b.x+c=0

Публикувано в 11а, 11в с етикети . Постоянна връзка.

5 Responses to Квадратно уравнение

  1. dreanor каза:
    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int a,b,c;
        float d;
        cout << "Wuwedi koeficientite na urawnenieto ax^2 + bx + c: ";
        cin >>a>>b>>c; 
        d=(b*b)-(4*a*c);
        if (d<0) cout << "Nqma realni koreni";
           else
           {
               d=sqrt(d);
               if (d==0) cout << "koren na urawnenieto e "<<((-b)/(2*a))<<endl;
               if (d>0) cout << "koreni na urawnenieto sa: "<<((-b+d)/(2*a))<<"; "<<((-b-d)/(2*a))<<endl;
               if (d<0) cout << "Nqma realni koreni";
           }
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
  2. marant каза:
    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int a,b,c,x;
        float D;
        cout<<"Vuvedi a,b i c za yravnenieto ax^2+bx+c=0 :"<<endl;
        cin>>a>>b>>c;
        D=(pow(b,2)-(4*a*c));
        if (D<0)
        cout<<"Yravnenieto nqma re6enie"<<endl;
        if (D>0)
         cout<<"x1= "<<((-b)+sqrt(D))/(2*a)<<" x2= "<<((-b)-sqrt(D))/(2*a)<<endl;
        if (D==0)
        cout<<"x1=x2="<<(-b)/2*a<<endl;
        system("PAUSE");
        return EXIT_SUCCESS;
    }
     
    • marant каза:

      Поправка

      #include <cstdlib>
      #include <iostream>
      #include <math.h>
      
      using namespace std;
      
      int main(int argc, char *argv[])
      {
          int a,b,c,x;
          float D;
          cout<<"Vuvedi a,b i c za yravnenieto ax^2+bx+c=0 :"<<endl;
          cin>>a>>b>>c;
          if (a==0)
           cout<<"nqma re6enie"<<endl;
           else {
          D=(pow(b,2)-(4*a*c));
          if (D<0)
          cout<<"Yravnenieto nqma re6enie"<<endl;
          if (D>0)
           cout<<"x1= "<<((-b)+sqrt(D))/(2*a)<<" x2= "<<((-b)-sqrt(D))/(2*a)<<endl;
          if (D==0)
          cout<<"x1=x2="<<(-b)/2*a<<endl;
      }
          system("PAUSE");
          return EXIT_SUCCESS;
      }
       
  3. Gadget каза:
     
    double a,b,c,x1,x2,D,x;
        cout<<"Vavedete a:"<<endl;
        cin>>a;
        cout<<"Vavedete b:"<<endl;
        cin>>b;
        cout<<"Vavedete c:"<<endl;
        cin>>c;
        D=(b*b)-(4*a*c);
           
        if(a!=0)
        {
               
           if((D>0)||(D==0))
           {
                if(D>0)
                {
                      x1=(-b-sqrt(D))/(2*a);
                      x2=(-b+sqrt(D))/(2*a);
                         cout<<"Otgovorut e: "<<"x1= "<<x1<<"  "<<"x2= "<<x2<<endl;
                }
                else
                    if(D==0)
                    {
               
                         x1=x2=(-b)/(2*a);
                         cout<<"Otgovorut e: "<<"x1=x2="<<x1<<endl;
                    }
                    else
                         cout<<"Nqma realni dvoini koreni!"<<endl;
    
                }
                else
                cout<<"Kvadratnoto uravnenie nqma realni koreni!"<<endl;  
         }
         else
         x=-(c/b);
         cout<<"Otgovorut e:"<<" "<<x<<endl;
     
  4. Данаил каза:

    Ето още две решения – първото не позволява а да е 0:

    #include <cstdlib>
    #include <iostream>
    #include <math.h>
     
    using namespace std;
     
    int main(int argc, char *argv[])
    {
        float a,b,c,D,x1,x2;
        cout<<"vuvedete a:";
        cin>>a;
        cout<<"vuvedete b:";
        cin>>b;
        cout<<"vuvedete c:";
        cin>>c;
        if (a==0) cout<<"tova ne e kvadratno uravnenie";
        else 
    	  {
            D=b*b-(4*a*c);
            cout<<"D e:"<<D<<endl;
            if (D>0)
               {
                   x1=((-b+sqrt(D))/(2*a));
                   cout<<"x1 e:"<<x1<<endl;
                   x2=((-b-sqrt(D))/(2*a));
                   cout<<"x2 e:"<<x2<<endl;
               }
            else
                if (D==0)
                { 
                  x1=-(b/(2*a));
                   cout<<"x1=x2="<<x1<<endl;
                }
                else
                   cout<<"Kvadratnoto uravnenie nqma reshenie";
        }
     
        system("PAUSE");
        return EXIT_SUCCESS;
    }
     

    Второто обработва и този случай:

     #include <cstdlib>
    #include <iostream>
    #include <math.h>
    using namespace std;
     
    int main(int argc, char *argv[])
    { 
    	double a,b,c,d;
    	cout <<"vuvedete parametrite na uravnenieto:";
    	cin>>a>>b>>c;
    	if (a==0)
    		if (b==0)
    			if(c==0)
    				cout<<"Vsqko x e re6enie \n";
    			else cout<<"Nqma re6enie \n";
    		else cout<<"reshenieto e"<<-c/b<<endl;
    	else {
    		d=(b*b)-(4*a*c);
    	  if (d==0)
    			cout<<"Korenite sa ravni  "<<(-b/(2*a))<<endl;
    		else if (d<0)
    			cout<<"nqma realni koreni"<<endl;
    		else
    			cout<<"Korenite sa"<<((-b-sqrt(d))/(2*a))<<"  "<<((-b+sqrt(d))/(2*a))<<endl;
    	}
     
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    

Вашият коментар