fork(2) download
  1. #include<iostream>
  2. #include<cmath>
  3. #include<complex>
  4. using namespace std;
  5.  
  6. int main() {
  7. //This program will take a quadratic in the form Ax^2+Bx+c and solve for the roots of the quadratic using the quadratic formula
  8. //Quad Form: -b+-sqrt((b^2-4ac)/(2a))
  9. //Working on adding imaginary(i) numbers
  10. //(2,1) in complex c++ is the same as 2+i in math
  11. // Lots of help in complex c++ in this thread {http://w...content-available-to-author-only...b.com/software-development/cpp/threads/311699}
  12.  
  13. double a, b, c;
  14. double Sol1, Sol2;
  15. complex<double> cSol1, cSol2, discr;
  16.  
  17.  
  18. cout << "Enter A,B, and C. From the general form of a quadratic. Ax^2+Bx+c";
  19. cout << "\nEnter A ";
  20. cin >> a;
  21. cout << "\nEnter B ";
  22. cin >> b;
  23. cout << "\nEnter C ";
  24. cin >> c;
  25.  
  26. if (a==0){
  27. cout<< "Not a Quadratic\n";
  28. }
  29.  
  30.  
  31. else if ((b*b-4*a*c) < 0) {
  32. cout << "\nImaginary Roots";
  33. discr =sqrt(complex<double>(b*b-4*a*c));
  34. complex<double> cSol1= ((-1*b)-discr)/(2*a);
  35. complex<double> cSol2= ((-1*b)+discr)/(2*a);
  36.  
  37. cout << "\nThe solutions are x = " << cSol1;
  38. cout << "\nAnd x = " << cSol2 << endl;
  39.  
  40. }
  41.  
  42. else {
  43.  
  44. Sol1=sqrt(pow(b , 2)-(4*a*c))-(b*-1);
  45. Sol1= (Sol1)/(2*a);
  46. Sol2=((-1)*sqrt(pow(b, 2)-(4*a*c)))-(b*-1);
  47. Sol2=(Sol2)/(2*a);
  48.  
  49. cout <<"\n The Solutions are x = " << Sol1;
  50. cout <<" And x = " << Sol2 << endl;
  51. }
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0s 2860KB
stdin
Standard input is empty
stdout
Enter A,B, and C. From the general form of a quadratic. Ax^2+Bx+c
Enter A  
Enter B  
Enter C  
Imaginary Roots
The solutions are x = (-0.502345,-0)
And x = (-0.502345,0)