首页 > 新闻中心 > 技术百科

c++ 计算器 语法实现 返回列表

网络2023-08-27 00:00:00编辑发布,已经有个小可爱看过这篇文章啦

#include using namespace std;//语法逻辑//exp: factor//| exp \’+\’ factor//| exp \’-\’ factor//;////factor: term//| factor \’*\’ term//| factor \’/\’ term//;////term: NUMBER//| \'(\’ exp \’)\’//| \’-\’ term//| \’+\’ term//;//%%int str_index = 0;string cacl_str;void skip_space() { while (cacl_str[str_index] == \’ \’) { ++str_index; }}void index_add() { //跳过无用的空格 skip_space(); ++str_index; skip_space();}char get_char() { skip_space(); return cacl_str[str_index];}struct Node { bool tag; double num; Node(bool tagg, double numm) { tag = tagg; num = numm; } Node operator+(const Node &b) { Node node(this->tag & b.tag, this->num + b.num); return node; } Node operator-(const Node &b) { Node node(this->tag & b.tag, this->num – b.num); return node; } Node operator*(const Node &b) { Node node(this->tag & b.tag, this->num * b.num); return node; } Node operator/(const Node &b) { Node node(this->tag & b.tag, this->num / b.num); return node; }};//实现加减法Node exp();//实现乘除法Node factor();//获取括号中的值Node term();//获取一个数字Node number();Node exp() { Node node = factor(); if (get_char() == \’+\’) { index_add(); Node node2 = exp(); return node + node2; } else if (get_char() == \’-\’) { index_add(); Node node2 = exp(); return node – node2; } return node;}Node factor() { Node node = term(); if (get_char() == \’*\’) { index_add(); Node node2 = factor(); return node * node2; } else if (get_char() == \’/\’) { index_add(); Node node2 = factor(); return node / node2; } return node;}int read_num_char() { return cacl_str[str_index++] – \’0\’;}Node number() { skip_space(); if (cacl_str[str_index] >= \’0\’ && cacl_str[str_index] = \’0\’ && cacl_str[str_index]

  • 跳过
  • double
  • void
  • class
  • Node
  • struct
  • char
  • bool
  • namespace
  • include

热门新闻

来电咨询