So I've been trying to determine if a statement is syntactically correct or not. For example ([]{}[]) is syntactically correct while [](} is not. Below is my code and I've been getting '31463 segmentation fault C++' this error when I run it. Also this 'warning: control may reach end of non-void function [-Wreturn-type]'.
#include <iostream>
#include "/Users/gansaikhanshur/Documents/atom/C++/Libraries/stdc++.h"
using namespace std;
ifstream f("/Users/gansaikhanshur/Documents/atom/C++/Project_Airi/data.in");
ofstream g("/Users/gansaikhanshur/Documents/atom/C++/Project_Airi/data.out");
stack<char> my_stack;
string imp;
char last_char;
char ReverseChar(char x){
if(x == '{'){
x = '}';
return x;
} else if(x == '[') {
x = ']';
return x;
} else if(x == '('){
x = ')';
return x;
} else if(x == '}') {
x = '{';
return x;
} else if(x == ')'){
x = '(';
return x;
} else if(x == ']') {
x = '[';
return x;
}
}
int main(){
cin >> imp;
for(int i = 0; i < imp.size(); i++){
my_stack.push(imp[i]);
}
last_char = my_stack.top();
my_stack.pop();
while(!my_stack.empty()){
char lchar = my_stack.top();
my_stack.pop();
if(ReverseChar(lchar) == my_stack.top()){
my_stack.pop();
} else {
cout << "Syntactically False";
}
if(my_stack.size() == 1){
if(my_stack.top() == ReverseChar(last_char)){
cout << "Syntactically Correct";
} else {
cout << "Syntactically False";
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire