Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Simple graphing calculator
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- #include <cmath>
- using namespace std;
- //constants are usually put in the global scope
- const float size = 10;
- string filename;
- //void load();
- char menu();
- void linear(ofstream& output);
- void quadratic(ofstream& output);
- void cubic(ofstream& output);
- void deleteData(ofstream& output);
- //void exit();
- //clrscr is a deprecated function used almost exclusively in Windows
- //and a relic from Borland C++. This method only uses the standard library
- void clrscr()
- {
- for (int i = 0; i < 100; i++)
- cout << endl;
- }
- //Although void main() works on many systems, it is not and never has been
- //standard. The system expects a return value of 0 when the program exits successfully.
- //It is always expected to be int
- int main()
- {
- /*char bypass;
- //int o;data localization
- cout << "\n\n\t\t\t\tBYPASS CODE : "; // BYPASS FOR LOADING
- bypass = cin.get(); //getch is not a standard function, cin.get() will get the
- //next char in the stream
- if (bypass == '+')
- {
- //goto main; NEVER ever ever ever ever ever ever use a goto. It makes the
- //the program all but impossible to debug
- }
- else
- load();
- //goto loading;*/
- cout << "Enter the location of the output file (default graph.txt): ";
- getline(cin, filename);
- if (filename == "")
- filename = "graph.txt";
- ofstream output;
- output.open(filename.c_str(), ios::app);
- char mode = menu();
- while (mode != 'x' && mode != 'X')
- {
- switch (mode)
- {
- case 'a':
- case 'A':
- linear(output);
- break;
- case 'b':
- case 'B':
- quadratic(output);
- break;
- case 'c':
- case 'C':
- cubic(output);
- break;
- case 'd':
- case 'D':
- deleteData(output);
- break;
- default:
- clrscr();
- cerr << "Invalid Choice" << endl;
- //goto main;
- break;
- }
- clrscr();
- mode = menu();
- }
- output.close();
- //The below statements are the same as the above,
- //but the switch is more convenient in this case.
- /*if (mode == 'a' || mode == 'A')
- linear(output);
- else if (mode == 'b' || mode == 'B')
- quadratic(output);
- else if (mode == 'c' || mode == 'C')
- cubic(output);
- else if (mode == 'd' || mode == 'D')
- deleteData(output);
- else if (mode == 'x' || mode == 'X')
- exit();
- // ROBUSTNESS
- else
- {
- cerr << "Invalid Choice" << endl;
- //goto main;
- menu();
- }*/
- }
- char menu()
- {
- //main: NEVER EVER use GOTO
- //int loop; it is not a good idea to reuse a variable
- //in 2 loops, it can cause debugging issues is larger programs
- //any time penalty will be negligible
- /*for (int loop = 0; loop < 80; loop++)
- cout << "\f";*/
- cout << "\n\t----------------------------------------------------------------------\n"
- << "\t| |\n"
- << "\t| ««««««««««««««««««««« GRAPHIC CALCULATOR »»»»»»»»»»»»»»»»»»»»» |\n"
- << "\t| |\n"
- << "\t----------------------------------------------------------------------\n"
- << "\n";
- /*for (int loop = 0; loop < 80; loop++)
- cout << "\f";*/
- cout << "\n\n"
- << " \n\n\t A--> LINEAR EQUATION \t\t D--> DELETE EXISTING DATA"
- << " \n\n\t B--> QUADRATIC EQUATION" //\t\t R--> READ-ME"
- << " \n\n\t C--> CUBIC EQUATION \t\t X--> Exit"
- << "\n\n> ";
- return cin.get();
- }
- void linear(ofstream& output)
- {
- clrscr(); // REFRESHING PAGE
- cout << "----- LINEAR EQUATION -----" << endl << "y = mx + b\n\n";
- float x, y, m, b;
- cout << "\nEnter coefficient of 'x': ";
- cin >> m;
- cout << "\nEnter Constant ('0' if None): ";
- cin >> b;
- cout << "Your Equation: y = " << m << "x + " << b << "\n\n\n";
- //there is no problem with the x coefficient, it just makes a horizontal line
- output << "Your Equation: y = " << m << "x + " << b << endl;
- //no reason to have an nsize variable, just use -size
- for (x = -size; x < size; x++) // LOOP TO PRINT AT COORDINATES
- {
- y = (x * m) + b;
- output << " |" << setfill('-') << setw(y + 100)
- << "+\t" << "(" << x << ", " << y << ")"
- << endl;
- }
- clrscr();
- cout << "\n\nPlease check the output file.\n\n";
- cout << "PRESS ANY KEY TO RETURN TO THE MENU";
- cin.ignore();
- cin.get();
- cout << endl;
- //goto main;
- }
- void quadratic(ofstream& output)
- {
- //quadratic:
- clrscr(); // REFRESHING PAGE
- cout << "----- QUADRATIC EQUATION -----" << endl
- << "x = m * y^2 + b\n\n";
- float x, y, a, b, c;
- cout << "Enter coefficient of x^2: ";
- cin >> a;
- cout << "Enter the coefficient of x: ";
- cin >> b;
- cout << "Enter Constant ('0' if None): ";
- cin >> c;
- cout << "Your Equation: y = " << a << " * x^2 + " << b << " * x + " << c << "\n\n\n";
- //there is no problem with the 0 as the coefficient,
- //it just makes a horizontal line
- output << "Your Equation: y = " << a << " * x^2 + " << b << " * x + " << c << "\n\n\n";
- //no reason to have an nsize variable, just use -size
- for (x = -size; x < size; x++) // LOOP TO PRINT AT COORDINATES
- {
- y = (pow(x, 2) * a) + b * x + c;
- output << " |" << setfill('-') << setw(y + 100)
- << "+\t" << "(" << x << ", " << y << ")"
- << endl;
- }
- clrscr();
- cout << "\n\nPlease check the output file.\n\n";
- cout << "PRESS ANY KEY TO RETURN TO THE MENU";
- cin.ignore();
- cin.get();
- cout << endl;
- //goto main;
- }
- void cubic(ofstream& output)
- {
- //quadratic:
- clrscr(); // REFRESHING PAGE
- cout << "----- QUADRATIC EQUATION -----" << endl
- << "x = m * y^2 + b\n\n";
- float x, y, a, b, c, d;
- cout << "Enter coefficient of x^3: ";
- cin >> a;
- cout << "Enter coefficient of x^2: ";
- cin >> b;
- cout << "Enter the coefficient of x: ";
- cin >> c;
- cout << "Enter Constant ('0' if None): ";
- cin >> d;
- cout << "Your Equation: y = " << a << " * x^2 + " << b << " * x + " << c << "\n\n\n";
- //there is no problem with the 0 as the coefficient,
- //it just makes a horizontal line
- output << "Your Equation: y = " << a << " * x^2 + " << b << " * x + " << c << "\n\n\n";
- //no reason to have an nsize variable, just use -size
- for (x = -size; x < size; x++) // LOOP TO PRINT AT COORDINATES
- {
- y = (pow(x, 3) * a) + (pow(b, 2) * x)+ c * x + d;
- output << " |" << setfill('-') << setw(y + 100)
- << "+\t" << "(" << x << ", " << y << ")"
- << endl;
- }
- clrscr();
- cout << "\n\nPlease check the output file.\n\n";
- cout << "PRESS ANY KEY TO RETURN TO THE MENU";
- cin.ignore();
- cin.get();
- cout << endl;
- //goto main;
- }
- void deleteData(ofstream& output)
- {
- output.close(); // CLOSES EXISTING FILE
- clrscr();
- output.open(filename.c_str(), ios::trunc);
- output.close();
- output.open(filename.c_str(), ios::app);
- clrscr(); //NEW FILE CREATED TO REPLACE OLD
- cout << "\n\n\n\n\n\n\n\n\t\t\t\tDATA CLEARED";
- cin.get();
- //goto main;
- // GETS USER INPUT AND PROCEEDS
- }
- //This program starts and executes so quickly that a load screen is completely
- //unnecessary. It has been removed
- /*void load()
- {
- clrscr();
- //int o, i; data localization: do not put data you only use in a loop
- //outside of the loop
- //gotoxy(33, 9);
- cout << "_________________"; // TO
- //gotoxy(32, 10);
- cout << "/"; //PRINT
- //gotoxy(49, 10);
- cout << "/"; //BOX
- //gotoxy(35, 8);
- cout << "LOADING...";
- for (int o = 0; o < 3; o++) // LOOP TO ANIMATE LOADING
- {
- //There was no reason to manually increment the x coordinate in gotoxy()
- //using a nested loop is also more efficient
- for (int x = 0; x < 50; x++)
- {
- for (int i = 0; i < 800; i++)
- {
- //gotoxy(x, 10);
- cout << "\f";
- }
- }
- }
- clrscr();
- }*/
- //Exit screen is unnecessary
- /*void exit()
- {
- clrscr();
- cout << "\n\n\n\n\n\n\n";
- cout << "\t\t\t\f\f\f\f\f\f \f\f\f \f\f\f \f\f\f\f\f\f \f\f\f\f\f\f \n";
- cout << "\t\t\t\f\f\f\f\f\f \f\f\f \f\f\f \f\f\f\f\f\f \f\f\f\f\f\f \n";
- cout << "\t\t\t\f\f \f\f\f \f\f\f \f\f \f\f \n";
- cout << "\t\t\t\f\f \f\f\f \f\f\f \f\f \f\f \n";
- cout << "\t\t\t\f\f\f\f\f\f \f\f\f\f\f \f\f \f\f \n";
- cout << "\t\t\t\f\f \f\f\f \f\f\f \f\f \f\f \n";
- cout << "\t\t\t\f\f \f\f\f \f\f\f \f\f \f\f \n";
- cout << "\t\t\t\f\f\f\f\f\f \f\f\f \f\f\f \f\f\f\f\f\f \f\f \n";
- cout << "\t\t\t\f\f\f\f\f\f \f\f\f \f\f\f \f\f\f\f\f\f \f\f \n";
- cout << "\nžž.€°š°€žž.€°š°€žž.€°š°€žž.€°š°€žž.€°š°€žž.€°š°€žž.€°š°€žž.€°š°€žž.€°š°€žž.€°š°€";
- cout << "\n";
- for (int loop = 0; loop < 80; loop++)
- cout << "\f";
- cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
- //cout << "\n\t\tPARTNERS\tBORLAND® C++\tMICROSOFT©";
- cout << "\n";
- for (int loop = 0; loop < 80; loop++)
- cout << "\f";
- cin.get();
- clrscr();
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement