Advertisement
Guest User

grapher.cpp

a guest
Mar 25th, 2014
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.71 KB | None | 0 0
  1. //Simple graphing calculator
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <string>
  6. #include <cmath>
  7. using namespace std;
  8.  
  9. //constants are usually put in the global scope
  10. const float size = 10;
  11. string filename;
  12.  
  13. //void load();
  14. char menu();
  15. void linear(ofstream& output);
  16. void quadratic(ofstream& output);
  17. void cubic(ofstream& output);
  18. void deleteData(ofstream& output);
  19. //void exit();
  20.  
  21. //clrscr is a deprecated function used almost exclusively in Windows
  22. //and a relic from Borland C++. This method only uses the standard library
  23. void clrscr()
  24. {
  25.     for (int i = 0; i < 100; i++)
  26.         cout << endl;
  27. }
  28.  
  29. //Although void main() works on many systems, it is not and never has been
  30. //standard. The system expects a return value of 0 when the program exits successfully.
  31. //It is always expected to be int
  32. int main()
  33. {
  34.     /*char bypass;
  35.      //int o;data localization
  36.      cout << "\n\n\t\t\t\tBYPASS CODE : ";  // BYPASS FOR LOADING
  37.      bypass = cin.get();  //getch is not a standard function, cin.get() will get the
  38.      //next char in the stream
  39.      if (bypass == '+')
  40.      {
  41.      //goto main; NEVER ever ever ever ever ever ever use a goto. It makes the
  42.      //the program all but impossible to debug
  43.      }
  44.      else
  45.      load();
  46.      //goto loading;*/
  47.  
  48.     cout << "Enter the location of the output file (default graph.txt): ";
  49.     getline(cin, filename);
  50.     if (filename == "")
  51.         filename = "graph.txt";
  52.  
  53.     ofstream output;
  54.     output.open(filename.c_str(), ios::app);
  55.  
  56.     char mode = menu();
  57.     while (mode != 'x' && mode != 'X')
  58.     {
  59.         switch (mode)
  60.         {
  61.             case 'a':
  62.             case 'A':
  63.                 linear(output);
  64.                 break;
  65.             case 'b':
  66.             case 'B':
  67.                 quadratic(output);
  68.                 break;
  69.             case 'c':
  70.             case 'C':
  71.                 cubic(output);
  72.                 break;
  73.             case 'd':
  74.             case 'D':
  75.                 deleteData(output);
  76.                 break;
  77.             default:
  78.                 clrscr();
  79.                 cerr << "Invalid Choice" << endl;
  80.                 //goto main;
  81.                 break;
  82.         }
  83.         clrscr();
  84.         mode = menu();
  85.     }
  86.  
  87.     output.close();
  88.  
  89.     //The below statements are the same as the above,
  90.     //but the switch is more convenient in this case.
  91.     /*if (mode == 'a' || mode == 'A')
  92.      linear(output);
  93.      else if (mode == 'b' || mode == 'B')
  94.      quadratic(output);
  95.      else if (mode == 'c' || mode == 'C')
  96.      cubic(output);
  97.      else if (mode == 'd' || mode == 'D')
  98.      deleteData(output);
  99.      else if (mode == 'x' || mode == 'X')
  100.      exit();
  101.      // ROBUSTNESS
  102.      else
  103.      {
  104.      cerr << "Invalid Choice" << endl;
  105.      //goto main;
  106.      menu();
  107.      }*/
  108. }
  109.  
  110. char menu()
  111. {
  112.     //main: NEVER EVER use GOTO
  113.  
  114.     //int loop; it is not a good idea to reuse a variable
  115.     //in 2 loops, it can cause debugging issues is larger programs
  116.     //any time penalty will be negligible
  117.  
  118.     /*for (int loop = 0; loop < 80; loop++)
  119.      cout << "\f";*/
  120.     cout << "\n\t----------------------------------------------------------------------\n"
  121.          << "\t|                                                                    |\n"
  122.          << "\t| «««««««««««««««««««««   GRAPHIC CALCULATOR   »»»»»»»»»»»»»»»»»»»»» |\n"
  123.          << "\t|                                                                    |\n"
  124.          << "\t----------------------------------------------------------------------\n"
  125.          << "\n";
  126.     /*for (int loop = 0; loop < 80; loop++)
  127.      cout << "\f";*/
  128.     cout << "\n\n"
  129.          << " \n\n\t A--> LINEAR EQUATION \t\t D--> DELETE EXISTING DATA"
  130.          << " \n\n\t B--> QUADRATIC EQUATION"  //\t\t R--> READ-ME"
  131.          << " \n\n\t C--> CUBIC EQUATION \t\t X--> Exit"
  132.          << "\n\n> ";
  133.  
  134.     return cin.get();
  135. }
  136.  
  137. void linear(ofstream& output)
  138. {
  139.     clrscr();  // REFRESHING PAGE
  140.     cout << "----- LINEAR EQUATION -----" << endl << "y = mx + b\n\n";
  141.  
  142.     float x, y, m, b;
  143.  
  144.     cout << "\nEnter coefficient of 'x': ";
  145.     cin >> m;
  146.     cout << "\nEnter Constant ('0' if None): ";
  147.     cin >> b;
  148.  
  149.     cout << "Your Equation: y = " << m << "x + " << b << "\n\n\n";
  150.     //there is no problem with the x coefficient, it just makes a horizontal line
  151.     output << "Your Equation: y = " << m << "x + " << b << endl;
  152.  
  153.     //no reason to have an nsize variable, just use -size
  154.     for (x = -size; x < size; x++)  // LOOP TO PRINT AT COORDINATES
  155.     {
  156.         y = (x * m) + b;
  157.         output << " |" << setfill('-') << setw(y + 100)
  158.                << "+\t" << "(" << x << ", " << y << ")"
  159.                << endl;
  160.     }
  161.  
  162.     clrscr();
  163.     cout << "\n\nPlease check the output file.\n\n";
  164.     cout << "PRESS ANY KEY TO RETURN TO THE MENU";
  165.     cin.ignore();
  166.     cin.get();
  167.  
  168.     cout << endl;
  169.     //goto main;
  170. }
  171.  
  172. void quadratic(ofstream& output)
  173. {
  174.     //quadratic:
  175.     clrscr();  // REFRESHING PAGE
  176.     cout << "----- QUADRATIC EQUATION -----" << endl
  177.          << "x = m * y^2 + b\n\n";
  178.  
  179.     float x, y, a, b, c;
  180.  
  181.     cout << "Enter coefficient of x^2: ";
  182.     cin >> a;
  183.     cout << "Enter the coefficient of x: ";
  184.     cin >> b;
  185.     cout << "Enter Constant ('0' if None): ";
  186.     cin >> c;
  187.  
  188.     cout << "Your Equation: y = " << a << " * x^2 + " << b << " * x + " << c << "\n\n\n";
  189.     //there is no problem with the 0 as the coefficient,
  190.     //it just makes a horizontal line
  191.     output << "Your Equation: y = " << a << " * x^2 + " << b << " * x + " << c << "\n\n\n";
  192.  
  193.     //no reason to have an nsize variable, just use -size
  194.     for (x = -size; x < size; x++)  // LOOP TO PRINT AT COORDINATES
  195.     {
  196.         y = (pow(x, 2) * a) + b * x + c;
  197.         output << " |" << setfill('-') << setw(y + 100)
  198.                        << "+\t" << "(" << x << ", " << y << ")"
  199.                        << endl;
  200.     }
  201.  
  202.     clrscr();
  203.     cout << "\n\nPlease check the output file.\n\n";
  204.     cout << "PRESS ANY KEY TO RETURN TO THE MENU";
  205.     cin.ignore();
  206.     cin.get();
  207.     cout << endl;
  208.     //goto main;
  209. }
  210.  
  211. void cubic(ofstream& output)
  212. {
  213.     //quadratic:
  214.     clrscr();  // REFRESHING PAGE
  215.     cout << "----- QUADRATIC EQUATION -----" << endl
  216.          << "x = m * y^2 + b\n\n";
  217.  
  218.     float x, y, a, b, c, d;
  219.  
  220.     cout << "Enter coefficient of x^3: ";
  221.     cin >> a;
  222.     cout << "Enter coefficient of x^2: ";
  223.     cin >> b;
  224.     cout << "Enter the coefficient of x: ";
  225.     cin >> c;
  226.     cout << "Enter Constant ('0' if None): ";
  227.     cin >> d;
  228.  
  229.     cout << "Your Equation: y = " << a << " * x^2 + " << b << " * x + " << c << "\n\n\n";
  230.     //there is no problem with the 0 as the coefficient,
  231.     //it just makes a horizontal line
  232.     output << "Your Equation: y = " << a << " * x^2 + " << b << " * x + " << c << "\n\n\n";
  233.  
  234.     //no reason to have an nsize variable, just use -size
  235.     for (x = -size; x < size; x++)  // LOOP TO PRINT AT COORDINATES
  236.     {
  237.         y = (pow(x, 3) * a) + (pow(b, 2) * x)+ c * x + d;
  238.         output << " |" << setfill('-') << setw(y + 100)
  239.                        << "+\t" << "(" << x << ", " << y << ")"
  240.                        << endl;
  241.     }
  242.  
  243.     clrscr();
  244.     cout << "\n\nPlease check the output file.\n\n";
  245.     cout << "PRESS ANY KEY TO RETURN TO THE MENU";
  246.     cin.ignore();
  247.     cin.get();
  248.     cout << endl;
  249.     //goto main;
  250. }
  251.  
  252. void deleteData(ofstream& output)
  253. {
  254.     output.close();  // CLOSES EXISTING FILE
  255.     clrscr();
  256.     output.open(filename.c_str(), ios::trunc);
  257.     output.close();
  258.     output.open(filename.c_str(), ios::app);
  259.     clrscr();  //NEW FILE CREATED TO REPLACE OLD
  260.     cout << "\n\n\n\n\n\n\n\n\t\t\t\tDATA CLEARED";
  261.     cin.get();
  262.     //goto main;
  263.     // GETS USER INPUT AND PROCEEDS
  264. }
  265.  
  266. //This program starts and executes so quickly that a load screen is completely
  267. //unnecessary. It has been removed
  268. /*void load()
  269.  {
  270.  clrscr();
  271.  //int o, i; data localization: do not put data you only use in a loop
  272.  //outside of the loop
  273.  //gotoxy(33, 9);
  274.  cout << "_________________";  //  TO
  275.  //gotoxy(32, 10);
  276.  cout << "/";  //PRINT
  277.  //gotoxy(49, 10);
  278.  cout << "/";  //BOX
  279.  //gotoxy(35, 8);
  280.  cout << "LOADING...";
  281.  
  282.  for (int o = 0; o < 3; o++)  // LOOP TO ANIMATE LOADING
  283.  {
  284.  //There was no reason to manually increment the x coordinate in gotoxy()
  285.  //using a nested loop is also more efficient
  286.  for (int x = 0; x < 50; x++)
  287.  {
  288.  for (int i = 0; i < 800; i++)
  289.  {
  290.  //gotoxy(x, 10);
  291.  cout << "\f";
  292.  }
  293.  }
  294.  }
  295.  clrscr();
  296.  }*/
  297.  
  298. //Exit screen is unnecessary
  299. /*void exit()
  300.  {
  301.  clrscr();
  302.  cout << "\n\n\n\n\n\n\n";
  303.  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";
  304.  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";
  305.  cout << "\t\t\t\f\f        \f\f\f   \f\f\f       \f\f      \f\f   \n";
  306.  cout << "\t\t\t\f\f         \f\f\f \f\f\f        \f\f      \f\f   \n";
  307.  cout << "\t\t\t\f\f\f\f\f\f      \f\f\f\f\f         \f\f      \f\f   \n";
  308.  cout << "\t\t\t\f\f         \f\f\f \f\f\f        \f\f      \f\f   \n";
  309.  cout << "\t\t\t\f\f        \f\f\f   \f\f\f       \f\f      \f\f   \n";
  310.  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";
  311.  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";
  312.  cout << "\nžž.€°š°€žž.€°š°€žž.€°š°€žž.€°š°€žž.€°š°€žž.€°š°€žž.€°š°€žž.€°š°€žž.€°š°€žž.€°š°€";
  313.  cout << "\n";
  314.  for (int loop = 0; loop < 80; loop++)
  315.  cout << "\f";
  316.  cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
  317.  //cout << "\n\t\tPARTNERS\tBORLAND® C++\tMICROSOFT©";
  318.  cout << "\n";
  319.  for (int loop = 0; loop < 80; loop++)
  320.  cout << "\f";
  321.  cin.get();
  322.  clrscr();
  323.  }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement