Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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);
- //void functionMonoty(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.functionMonoty(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);
- }
- /*
- void Polynomial::functionMonoty(Polynomial poly) {
- }
- */
- int main () {
- Polynomial poly;
- Utils::mainMenu(poly);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement