/*
gcc -o test test .c
如果没有提示错误的话
./test就可以运行
测试数据:
a+(b*(c-d^e)+f)/m
a+(b*(c-d^e)+f/m
a+(b*(c-d^e+f/m
a+b*c-d^e)+f)/m
a+(b*(#c-d^e)+f)/m
*/
#include <stdio.h>
#include <iostream>
#include <deque>
using namespace std;
#define Max 100
char str[Max], exp[Max], stack1[Max], ch;
int i = 0, j = 0, t = 0, top = 0, errorblog = 0, n = 0, powervalue = 1, zmblog = 0, sfblog = 0;
deque <int> pipei;
deque <int> zifu;
deque <int> congfu;
void start(){
i = 0, j = 0, t = 0, top = 0, n = 0, errorblog = 0, zmblog = 0, sfblog = 0, powervalue = 1;
char ch2;
printf("************************ Expression to NBL form ************************\n\n");
printf("Please input the expression: ");
while((ch2 = getchar()) != '\n'){
str[n] = ch2;
n++;
}
str[n] = '\0';
ch = str[i];
i++;
}
void end(){
while(top != 0){
exp[t] = stack1[top];
t++;
top--;
}
printf("\nNBL form:------------------- ");
for(j = 0; j < t; j++) printf("%c", exp[j]);
printf("\n\n###############################################################################\n\n");
}
void error(){
while(1){
for(int z = 0; z < 1; z++){
printf(" ");
for(int x = 0; x < strlen(str); x++){
if(!zifu.empty() && zifu.front() == x){
printf("$");
zifu.pop_front();
if(powervalue % 2 != 0)powervalue *= 2;
}
else if(!pipei.empty() && pipei.front() == x) {
printf("^");
pipei.pop_front();
if(powervalue % 3 != 0)powervalue *= 3;
}
else if(!congfu.empty() && congfu.front() == x){
printf("!");
congfu.pop_front();
if(powervalue % 5 != 0)powervalue *= 5;
}
else printf(" ");
}
}
printf("\n\n");
if(powervalue % 2 == 0){
printf("Error: $所指字符为不可识别字符\n");
while(!zifu.empty()){
zifu.pop_front();
}
}
if(powervalue % 3 == 0){
printf("Error: ^所指括号为孤独的括号\n");
while(!pipei.empty()){
pipei.pop_front();
}
}
if(powervalue % 5 == 0){
printf("Error: !所指符号不可放在这里\n");
while(!congfu.empty()){
congfu.pop_front();
}
}
errorblog = 1;
break;
}
//exit(0);
}
int main(){
while(1){
start();
while(ch != '\0'){
if((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')){
if(ch >= 'a' && ch <= 'z' && zmblog == 1) {zmblog = 0; congfu.push_back(i - 1);}
else if(ch >= 'a' && ch <= 'z') zmblog = 1;
sfblog = 0;
exp[t] = ch;
t++;
}
else if(ch == '('){
zmblog = 0;
top++;
stack1[top] = ch;
pipei.push_back(i - 1);
if(str[i - 2] >= 'a' && str[i - 2] <= 'z'){
printf("Error: '('处应添加运算符\n");
errorblog = 1;
}
}
else if(ch == ')'){
sfblog = 0;
while(stack1[top] != '(' && top != 0 && pipei.empty() != 1){
exp[t] = stack1[top];
top--;
t++;
}
if(pipei.empty()) pipei.push_front(i - 1);
else if(str[pipei.back()] == '('){
pipei.pop_back();
top--;
}
else if(pipei.empty() != 1) pipei.push_back(i - 1);
else if(top != 0) top--;
}
else if(ch=='+' || ch=='-'){
if(sfblog == 1){
congfu.push_back(i-1);
}
else if(sfblog == 0) sfblog = 1;
zmblog = 0;
while(top != 0 && stack1[top] != '('){
exp[t] = stack1[top];
top--;
t++;
}
top++;
stack1[top] = ch;
}
else if(ch == '^'){
if(sfblog == 1){congfu.push_back(i-1);}
else if(sfblog == 0) sfblog = 1;
zmblog = 0;
while(stack1[top] == '^'){
exp[t] = stack1[top];
top--;
t++;
}
top++;
stack1[top] = ch;
}
else if(ch == '*' || ch == '/'){
if(sfblog == 1){congfu.push_back(i-1);}
else if(sfblog == 0) sfblog = 1;
zmblog = 0;
while(stack1[top] == '*' || stack1[top] == '/'){
exp[t] = stack1[top];
top--;
t++;
}
top++;
stack1[top] = ch;
}
else if(ch == ' '){}
else zifu.push_back(i - 1);
ch = str[i];
i++;
}
if(sfblog != 0) printf("Error: 运算符后应付操作数\n");
else if(!pipei.empty() || !congfu.empty() || !zifu.empty()) error();
else if(errorblog != 1)end();
}
return 0;
}
|
|