main.cpp

来自「数据结构与程序设计教材源码 数据结构与程序设计教材源码」· C++ 代码 · 共 150 行

CPP
150
字号
#include "../../C/UTILITY.H"

#include "../../6/LINKLIST/LIST.H"
#include "../../6/LINKLIST/LIST.CPP"

class List_int: public List<int> {
   public:
   List_int() {}
};

#include <string.h>
#include <stdlib.h>
#include "../../6/STRINGS/STRING.H"
#include "../../6/STRINGS/STRING.CPP"

#include "STACK.H"
#include "STACK.CPP"

#include "AUXIL.CPP"
#include "TOKEN_R.H"
#include "LEXICON.H"
#include "TOKEN.H"
#include "TOKEN_R.CPP"
#include "LEXICON.CPP"
#include "TOKEN.CPP"

typedef double Value;
#include "VALUES.CPP"
#include "EXPR.H"
#include "EXPR.CPP"

#include "POINT.H"
#include "POINT.CPP"
typedef Point Key;
#include "../../8/LINKLIST/SORTABLE.H"
#include "PLOT.H"
#include "../../8/LINKLIST/MERGE.CPP"
#include "PLOT.CPP"
 
char get_command()
{
   char c, d;
   cout << "Select command and press <Enter>:";
   while (true) {
      do {
         cin.get(c);
      } while (c == '\n');
      do {
         cin.get(d);
      } while (d != '\n');
      c = tolower(c);
      if (c == 'r' || c == 'w' || c == 'g' || c == 'l' ||
          c == 'p' || c == 'n' || c == 'h' || c == 'q') {
         return c;
      }
      cout << "Please enter a valid command or type H for help:" << endl;
   }
}

void help()
{
   cout << "Valid commands are:" << endl;
   cout << "\n\t[R]ead new function\t[W]rite function\t[G]raph function\n"
        << "\tset plot [L]imits\twrite [P]arameters\tset [N]ew parameters\n"
        << "\t[H]elp\t[Q]uit.\n";
}
 
void do_command(char c, Expression &infix, Expression &postfix, Plot &graph)
/* 
 
Pre:  None
Post: Performs the user command represented by char c on the
      Expression infix, the Expression postfix, and the Plot graph.
Uses: Classes Token, Expression and Plot.
 
*/

{
   switch (c) {
   case 'r':      //  Read an infix expression from the user.
      infix.clear();
      infix.read();
      if (infix.valid_infix() == success) postfix = infix.infix_to_postfix();
      else cout << "Warning: Bad expression ignored. " << endl;
      break;

   case 'w':      //  Write the current expression.
      infix.write();
      postfix.write();
      break;

   case 'g':      //  Graph the current postfix expression.
      if (postfix.size() <= 0)
         cout << "Enter a valid expression before graphing!" << endl;
      else {
         graph.clear();
         graph.find_points(postfix);
         graph.draw();
      }
      break;

   case 'l':    //  Set the graph limits.
      if (graph.set_limits() != success)
         cout << "Warning: Invalid limits" << endl;
      break;

   case 'p':    //  Print the graph parameters.
      Token::print_parameters();
      break;

   case 'n':    //  Set new graph parameters.
      Token::set_parameters();
      break;

   case 'h':    //  Give help to user.
      help();
      break;
   }
}
 
void introduction()
{
   List<Point> t; List<Point> s = t; //  To help certain compilers.
   cout << "Running the menu driven graphing program."
        << endl << endl;
   help();
   cout << endl;
   cout << "Functions should be specified using a variable called: x"
        << endl;
}
 
int main()
/* 
Pre:  None
Post: Acts as a menu-driven graphing program.
Uses: Classes Expression and Plot,
      and functions introduction, get_command,
      and do_command.
*/
{
   introduction();
   Expression infix;      //  Infix expression from user
   Expression postfix;    //  Postfix translation
   Plot graph;

   char ch;
   while ((ch = get_command()) != 'q')
      do_command(ch, infix, postfix, graph);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?