The program basically takes a polynomial and does some simple calculations with it.
With a couple of hours of work I managed to get my first console application to work (aside from the obligatory "Hello, World!", of course). However, I'm in total doubt that the style and habits I've used are even close to being correct.
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <math.h>
#include <cmath>
using namespace std;
class Polynomial {
float a, b, c, value;
public:
Polynomial ();
void functionDefine(Polynomial poly);
void functionValue(Polynomial poly);
void functionZero(Polynomial poly);
};
namespace Utils {
void mainMenu(Polynomial poly);
void skipToMenu();
string save(string str);
int exitProgram();
void mainMenu(Polynomial poly) {
int choice = 0;
cout << " ---------- Main Menu ----------" << endl;
cout << "1: ---- Define New Polynomial ----" << endl;
cout << "2: ------ Solve for x-value ------" << endl;
cout << "3: --------- Find roots ----------" << endl;
cout << "4: ------------ Exit -------------" << endl << endl;
cout << "Enter the value of the submenu you want to enter: ";
cin >> choice;
cout << endl;
switch(choice) {
case 1:
poly.functionDefine(poly);
break;
case 2:
poly.functionValue(poly);
break;
case 3:
poly.functionZero(poly);
break;
case 4:
exitProgram();
break;
}
}
void skipToMenu() {
cout << "Press enter to return to the main menu."<< endl << endl;
system("pause > nul");
}
string save(string str) {
string saved = "Yes or no"; // To do.
return saved;
}
int exitProgram() {
return 0;
}
}
Polynomial::Polynomial() {
a = 0;
b = 0;
c = 0;
}
void Polynomial::functionDefine (Polynomial poly) {
char currentChar = 'a';
cout << "---- Define New Polynomial ----" << endl;
for (int i = 0; i < 3; i++) {
cout << "Enter a value for " << currentChar << ": ";
switch(i)
{
case 0:
cin >> poly.a; break;
case 1:
cin >> poly.b; break;
case 2:
cin >> poly.c; break;
}
if (!cin) {
cout << "Please enter a number instead." << endl;
system("pause");
}
currentChar = static_cast<char>(currentChar + 1);
}
cout << endl;
cout << "The function is: f(x) = " << poly.a << "x^2 + " << poly.b << "x + " << poly.c << "." << endl << endl;
Utils::skipToMenu();
Utils::mainMenu(poly);
}
void Polynomial::functionValue(Polynomial poly) {
int input;
cout <<"------ Solve for x-value ------" << endl << endl;
cout << "Enter the x-value that you would like to evaluate the equation for: ";
cin >> input;
value = poly.a * pow(input, 2) + poly.b * input + poly.c;
cout << "The value of the function for x = " << input << " is " << value << endl << endl;
Utils::mainMenu(poly);
}
void Polynomial::functionZero(Polynomial poly) {
float d = pow(poly.b, 2) - 4 * poly.a * poly.c;
if (d > 0) {
float result = ((-poly.b) + sqrt(abs(d))) / (2 * poly.a);
float secondResult = (-poly.b - sqrt(abs(d))) / (2 * poly.a);
cout << "The polynomials roots are at x = " << result << " and x = " << secondResult << "." << endl << endl;
}
else if (d == 0) {
float result = ((-b) + sqrt(abs(d))) / (2 * a);
cout << "The polynomials root is at x = " << result << "." << endl << endl;
}
else {
cout << "The polynomial has no roots." << endl << endl;
}
Utils::mainMenu(poly);
}
int main () {
Polynomial poly;
Utils::mainMenu(poly);
return 0;
}
I was told it would be a good idea to create different namespaces for my "calculations" and my "utilities". Creating a class with the utility functions wouldn't make sense as there would never be created an instance of this...
I'm open to all the critique you have. If anybody wants the Pastebin link it's here.