Sorry about the length, but spoiler tags are apparently a no-go on this forum.
I've included a program I wrote (that works perfectly) that solves quadratics. It's functional, and has a sufficient UI, but what I'm looking for here is more along the lines of ways I can improve it.
For instance, I'd like to create a function that allows the user to save all the output they generate. I already know how to do that with push_back, a vector<double> and a ofstream to a .txt file, but I wanted it to maintain the contents of said file across sessions, and I'm not clear on how to do that. When I've tried using fstream with a .txt file on other programs I've written, it overwrites everything, which isn't all that helpful.
I'm also wondering if there's a shorter and more exact method for running square roots than the one I'm using?
And I'm sure this is obvious and I'm just a moron, but how on earth do you get the program to work on a computer that doesn't have a compiler?
Thanks very much!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
void alg(double a, double b, double c);
double sqrt(double m);
void menu();
int main()
{
int x;
do
{
cout << "Type 1 to use program, else type 0 to quit: ";
cin >> x;
if (x == 1)
{
menu ();
}
else;
}while (x != 0);
}
void menu()
{
cout << "\nQuadratic Equation program. Enter values for a, b, c to receive both roots" << endl;
double a, b, c;
cout << "\nDefine a: ";
cin >> a;
cout << "\nDefine b: ";
cin >> b;
cout << "\nDefine c: ";
cin >> c;
cout << endl;
alg(a, b, c);
}
void alg (double a, double b, double c)
{
double x1, x2, x12, xs1;
x12= ((b*b)-(4*a*c));
if (x12 <0)
{
cout << "(b^2 - 4ac) = " << x12 << endl;
xs1= sqrt(-x12);
cout << "The square root of (b^2 - 4ac) is: " << xs1 << "i." << endl;
cout << "Therefore the roots of this equation are imaginary!" << endl;
double f = 2*a;
x1= (-b/(f));
x2= (-b/(f));
cout << "\nx = -b/2a + discriminant/2a is: " << x1 << " + (" << xs1 << "i / " << f << ")" << endl;
cout << "\nx = -b/2a - discriminant/2a is: " << x2 << " + (" << xs1 << "i / " << f << ")" << endl;
}
else if (x12 >= 0)
{
cout << "(b^2 - 4ac) = " << x12 << endl;
xs1= sqrt(x12);
cout << "The square root of (b^2 - 4ac) is: " << xs1 << endl;
cout << "Therefore the roots of this equation are real!" << endl;
x1= ((-b + xs1)/(2*a));
x2= ((-b - xs1)/(2*a));
cout << "\nx= -b +... is: " << x1 << endl;
cout << "\nx= -b -... is: " << x2 << endl;
}
}
double sqroot(double m)
{
double i=0;
double x1,x2;
double ((i*i) <= m);
i+=0.1;
x1=i;
for(int j=0;j<10;j++)
{
x2=m;
x2/=x1;
x2+=x1;
x2/=2;
x1=x2;
}
return (x2);
}
|
As a side note -- which you may of course ignore -- I only started doing this coding stuff about three days ago (I've been reading the cplusplus.com pdf tutorial, Eckhart's free Thinking in C++ 2nd ed, and the cprogramming.com tutorial), mostly because I'm a bored and (currently) unemployed college grad. If you're up to it and you know a better (free and legal) source, feel free to provide a link here, or through a PM if it's rude to link to unrelated things in one of these threads.