栈  0.1
数据结构_第3章
ch3_4.cc
Go to the documentation of this file.
1 
12 #include "Rpn.hh"
13 #include <ctime>
14 
15 int main(int argc, char const *argv[])
16 {
17  // time
18  clock_t t = clock();
19  time_t rawtime;
20  struct tm *timeinfo;
21 
22  time(&rawtime); // Get the current calendar time
23  timeinfo = localtime(&rawtime); // Convert time_t to tm as local time
24  printf("Current local time and date: %s\n", asctime(timeinfo)); // Convert tm structure to string
25 
26  try
27  {
28  // 用户在命令行参数arg1输入中缀式
29  if (argc > 1)
30  {
31  std::cout << "\nInfix: " << argv[1] << '\n';
32  std::cout << "Postfix: " << RPN::Rpn_t(argv[1]).postfix() << std::endl;
33 
34  t = clock() - t;
35  printf("\nIt took me %ld clicks (%f seconds).\n", t, ((float)t) / CLOCKS_PER_SEC);
36  return 0;
37  }
38 
39  std::string infixStr1{"(5+6^2*(7+3)/3)/4+5"};
40  RPN::Rpn_t rpn{infixStr1, RPN::notationType::Infix};
41 
42  std::cout << "\nInfix: " << infixStr1 << '\n';
43  std::cout << "Postfix: " << rpn.postfix() << '\n';
44 
45  // 用户交互输入中缀式
46  char expr[RPN::buf_size];
47  do
48  {
49  std::cout << "Input infix expression: ";
50  std::cin.getline(expr, RPN::buf_size, '\n');
51  if (!std::cin.fail())
52  break;
53 
54  std::cin.clear();
55  std::cin.sync();
56  } while (true);
57 
58  std::cout << "\nInfix: " << expr << '\n';
59  std::cout << "Postfix: " << rpn.assign(expr).postfix() << std::endl;
60  }
61  catch (const std::exception &e)
62  {
63  std::cerr << e.what() << '\n';
64  }
65 
66  t = clock() - t;
67  printf("\nIt took me %ld clicks (%f seconds).\n", t, ((float)t) / CLOCKS_PER_SEC);
68 
69  return 0;
70 }
RPN::buf_size
const int buf_size
项的最大长度 = buf_size - 1
Definition: Rpn.hh:32
RPN::Rpn_t::postfix
std::string postfix() const
获取后缀形式的string
Definition: Rpn.hh:421
RPN::Rpn_t
Definition: Rpn.hh:126
main
int main(int argc, char const *argv[])
Definition: ch3_4.cc:15
Rpn.hh
后缀表达式类
RPN::notationType::Infix
@ Infix
中缀表达式