程序流程图
代码
// BianYiYuanLi2.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string>
#include <iostream>
void E(int& TZ, std::string& ST);
void ADVANCE(std::string& ST) {
ST.erase(0, 1);
}
char SYM(std::string& ST) {
std::string::iterator it = ST.begin();
return *it;
}
void F(int& TZ, std::string& ST) {//(SYM >= 'a'&&SYM <= 'z') ||
if ((SYM(ST) >= 'A'&&SYM(ST) <= 'Z'))
{
ADVANCE(ST);
return;
}
else {
if (SYM(ST) != '(') {
//std::cout << "ERROR" << std::endl;
TZ = 1;
return;
}
else {
ADVANCE(ST);
E(TZ, ST);
if (SYM(ST) != ')')
{
std::cout << "ERROR" << std::endl;
TZ = 1;
return;
}
else {
ADVANCE(ST);
return;
}
}
}
}
void T(int& TZ, std::string& ST) {
F(TZ, ST);
while (SYM(ST) == '*' || SYM(ST) == '/') {
ADVANCE(ST);
F(TZ, ST);
}
}
void E(int& TZ, std::string& ST) {
do {
T(TZ, ST);
if (SYM(ST) == '+' || SYM(ST) == '-') {
ADVANCE(ST);
}
else {
return;
}
} while (true);
}
int main()
{
while (true) {
std::cout << "Input Expression" << std::endl;
//接受输入的表达式
std::string ST;
std::cin >> ST;
int TZ = 0;
E(TZ, ST);
if (SYM(ST) != '#' || TZ == 1)
{
std::cout << "ERROR AGAIN!";
TZ = 0;
}
else {
std::cout << "RIGHT AGAIN!";
}
system("pause");
}
return 0;
}
输入示例:
工程项目下载:https://download.csdn.net/download/a1013642808/10485041