Following is the implementation of infix to postfix conversion, it is working fine on my computer, but as I am submitting in on SPOJ it is giving me Runtime error SIGSEGV, I am new to competitive programming and I am unable to handle such type of errors.
#include <iostream>
#include <stack>
#include<string.h>
#include<ctype.h>
using namespace std;
int prec(char ch){
switch(ch){
case '^' : return 3;
break;
case '*':
case '/': return 2;
break;
case '+':
case '-': return 1;
break;
default: return -1;
}
}
void pti(char a[]){
stack<int> post;
int k = -1;
for(int i = 0;i<strlen(a);i++){
if(isalnum(a[i]))
a[++k] = a[i];
else if(a[i] == '(')
post.push(a[i]);
else if(a[i] == ')'){
while(!post.empty() && post.top()!= '('){
a[++k] = post.top();
post.pop();
}
post.pop();
}
else {
while(!post.empty() && prec(a[i]) <= prec(post.top())){
a[++k] = post.top();
post.pop();
}
post.push(a[i]);
}
}
while(!post.empty()){
a[++k] = post.top();
post.pop();
}
a[++k] = '\0';
cout<<a<<endl;
}
int main()
{
int t;
cin>>t;
for(int i = 0;i<t;i++){
char a[100];
cin>>a;
pti(a);
}
}
You just need to make the input array longer, e.g. a size of 1000 gets AC.
The SIGSEGV signal means a segmentation fault occured, which basically means you accessed memory that doesn't belong to you.
Related
I am trying to transfer the elements from a source stack to a destination stack. And for that i am using some variables and making sure that get transfered into the destination stack in the same order as they were in the source stack. I wrote the following code
#include <iostream>
#include <stack>
using namespace std;
template <typename S>
void transferByVar(stack<S> &source, stack<S> &dest)
{
int var = 0;
S topVal;
if (source.empty())
return;
else if (source.size() == 1)
{
dest.push(source.top());
source.pop();
}
int size = source.size();
while (count != size)
{
topVal = source.top();
source.pop();
while (source.size() != count)
{
dest.push(source.top());
source.pop();
}
source.push(topVal);
while (!dest.empty())
{
source.push(dest.top());
dest.pop();
}
++count;
}
}
int main()
{
stack <int> s1;
stack<int> s2;
s1.push(0);
s1.push(1);
s1.push(2);
s1.push(3);
s1.push(4);
s1.push(5);
s1.push(6);
s1.push(7);
s1.push(8);
s1.push(9);
transferByVar(s1, s2);
int size = s2.size();
for (int i = 0; i < size; i++)
{
cout << s2.top() << " ";
s2.pop();
}
return 0;
}
but it gives me an error of C2563: mismatch of formal parameter list. What can I do to fix this?
i am trying to convert infix to postfix but unable to get any output although program is free of error and logically correct. its just showing the value of m. I tried to print hello inside loop , but its unable to print that.I can't understand why its not going inside loop. Can't figure out the exact reason behind it.
#include <bits/stdc++.h>
#define m 21
using namespace std;
int isitoperattor(char c) //to find precdenec of operator
{
if(c=='+'||c=='-')
return 1;
if(c=='*' || c=='/')
return 2;
if(c=='('||c==')')
return 3;
}
void infixtopostfix(char str[m]){
char out[m];
cout<<m<<endl;
stack <char> s;
static int k;
for(int i=0;i<m;i++)
{
cout<<"hello";
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z')){
out[k]=str[i];
k++;}
else if (str[i]=='(')
s.push(str[i]);
else if (str[i]==')'){
//int j=i-1;
while(s.top()!='('){
out[k]=s.top();
s.pop();
k++;
//-;
}
s.pop();
}
else
{
if(isitoperattor(str[i]) && (isitoperattor(str[i])<isitoperattor(s.top()))){
while(isitoperattor(s.top())>=isitoperattor(str[i])){
out[k]=s.top();
s.pop();
k++;
}
}
}
s.push(str[i]);
}
}
while(!s.empty())
{
out[k]=s.top();
s.pop();
k++;
}
//string out;
for(int j=0;j<k;j++)
cout<<out[j];
}
int main()
{
char str[m]="a+b*(c^d-e)^(f+g*h)-i";
infixtopostfix(str);
return 0;
}
I'm doing this problem on SPOJ and I keep getting a runtime error (SIGABRT) and I've never really experienced this for. When I run my code in VS it works properly but SPOJ gives me this error. I was wondering if you could tell me why I'm getting it.
Here's my code:
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <queue>
#include <map>
using namespace std;
vector<int> marked;
vector<vector<int>> edges;
bool colorable;
void bfs(int v) {
colorable = true;
switch (marked[v]) {
case 0:
case 1:
marked[v] = 1;
for (int i = 0; i < edges[v].size(); i++) {
if (marked[edges[v][i]] != 1) {
marked[edges[v][i]] = 2;
}
else {
colorable = false;
break;
}
}
break;
case 2:
for (int i = 0; i < edges[v].size(); i++) {
if (marked[edges[v][i]] != 2) {
marked[edges[v][i]] = 1;
}
else {
colorable = false;
break;
}
}
}
}
int main() {
int N, M, a, b;
while (scanf("%d", &N) != 0) {
colorable = true;
while (N--) {
marked.push_back(0);
edges.push_back({ });
}
scanf("%d", &M);
while (M--) {
scanf("%d", &a);
scanf("%d", &b);
edges[a].push_back(b);
edges[b].push_back(a);
}
for (int i = 0; i < marked.size(); i++) {
bfs(i);
}
colorable ? printf("BICOLORABLE\n") : printf("NOT BICOLORABLE\n");
marked.clear();
edges.clear();
}
return 0;
}
I've tried Googling other similar issues and what a SIGABRT error is exactly and I'm still confused. Thanks for your help.
I found the problem and in case anyone ever has a similar issue I'll post it here. The line
while(scanf("%d", &N) != 0)
is apparently not appreciated by SPOJ. I split it into two lines
scanf("%d", &N);
while(N !=0 ) {}
and everything worked just fine. Thanks for your comments to help me understand the error.
Im a new programmer in C++ and I want to creat a code that extract IP from text files
I tried to convert txt file to Vector(string) to be easy filtering but i cant get all formes like XXX.XXX.XXX.XXX
Given that the ip could be embedded in some text, we'll parse the string and fetch it.
#include <iostream>
#include <string>
#include <iterator>
#include <vector>
#include <sstream>
using namespace std;
string ip(string str)
{
//middle portion
auto firstDot = str.find_first_of('.');
auto lastDot = str.find_last_of('.');
int dotCount = 0;
for(auto i = firstDot; i <= lastDot; i++)
{
if(!isdigit(str.at(i)) && str.at(i) != '.') //e.g 127.ss.0y.1
return string("");
if(str.at(i) == '.')
dotCount++;
}
if(dotCount != 3) //eg. 127.0.1 (not sure if this is wrong though)
return string("");
string result = str.substr(firstDot,lastDot-firstDot + 1);
//left portion
size_t x = 0; //number consegative digits from the first dot
for(auto i = firstDot-1; i >= 0; i--)
{
if(!isdigit(str.at(i)))
break;
else if(x == 3) //take first 3
break;
x++;
}
if(x == 0)
return string("");
result.insert(0,str.substr(firstDot-x,x));
//right portion
size_t y = 0;
for(auto i = lastDot + 1; i < str.length(); i++)
{
if(isdigit(str.at(i)))
{
if(y == 3)
break;
result.push_back(str.at(i));
y++;
}
else
break;
}
if(y == 0)
result.push_back('0');
return result;
}
int main()
{
string test = "1111127.0.0.11111 xx23.45.12.# xxxx.34.0.13 124.sd.2.1 sd.45.56.1";
string x,y;
vector<string> ips;
stringstream stream;
stream<<test;
while(stream>>x)
if(!(y = ip(x)).empty())
ips.push_back(y);
for(auto z : ips)
cout<<z<<"\t";
cout<<endl;
system("pause");
return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
int main() {
ifstream myReadFile;
myReadFile.open("text.txt");
char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
myReadFile >> output;
cout<<output;
}
}
myReadFile.close();
return 0;
}
Use this if the text file only includes IP's on each line.
Or depending on what c++ you're using:
std::ifstream file("Read.txt");
std::string str;
std::string file_contents;
while (std::getline(file, str))
{
file_contents += str;
file_contents.push_back('\n');
}
I'm new to C++11. I did the following simple example for you. It's based on the regex library. Hope it works for you!
#include <iostream>
#include <string>
#include <regex>
int main ()
{
std::string s ("There's no place like 127.0.0.1\n");
std::smatch m;
std::regex e ("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
while (std::regex_search (s,m,e)) {
for (auto x:m) std::cout << x << " ";
std::cout << std::endl;
s = m.suffix().str();
}
return 0;
}
I am having debug assertion error as soon as I input two elements. The program was giving me access reading or sometimes writing violation error after taking 7-8 entries but after I deleted the dynamic array, it is showing debug assertion failed after taking first two inputs and breaks down. Any idea for how to solve it? I am copying only my air class here. I have similar Fire, earth and water classes too.
The error is BLOCK_TYPE_IS_VALID (pHead->nBlockUse)
Someone else too asked this question but i can't figure out My program errors. Kindly help would be appreciated.
#ifndef AIR_H
#define AIR_H
#include <iostream>
#include <string>
#include "element.h"
using namespace std;
class Air :public Element
{
public:
string air;
string Elements [2];
int i;
string *elements;
public:
Air(string n): Element (n)
{
air = n;
i=-1;
elements = new string[2];
}
void alreadyExists (string a)
{
int lineCount = 0;
ifstream read;
read.open ("Air.txt", ios::in | ios::app);
while(!read.eof())
{
string x;
read>>x;
lineCount++;
}
lineCount--;
read.close();
read.open("Air.txt", ios::in|ios::app);
for(int i = 0; i < lineCount; i++)
{
read>>elements[i];
}
bool Found = false;
for(int i = 0; i < lineCount; i++) {
if(a == elements[i])
{
Found = true;
break;
}
}
if(!Found)
{
write2file (a);
}
}
void write2file (string air)
{
ofstream write;
write.open ("Air.txt", ios::out|ios::app);
{
write<<air<<endl;
}
}
void DisplayA ()
{
/*for(int i=0; i<2; i++)//( to read through the arrays )
{
cout<<Elements[i]<<endl;
}*/
ifstream read ("Air.txt", ios::in|ios::app);
int i=0;
while (read >> Elements[i])
{
cout<<Elements[i]<<endl;
}
}
Air operator+(Air & air)
{
Air newElement ("NULL");
if (this->air == "Air"||this->air=="air"&& air.air == "Air"||air.air=="air")
{
newElement.air = "Pressure";
cout<<"Yay!! You've made: "<<newElement.air<<endl;
alreadyExists (newElement.air);
//PushA (newElement.air);
//write2file (newElement.air);
return newElement;
}
else if ((this->air == "Air"||this->air == "air") && (air.air == "Pressure"||air.air=="pressure"))/* || ((this->air == "Pressure"||this->air=="pressure") && ( air.air == "Air"||air.air=="air")))*/
{
newElement.air = "Atmosphere";
cout<<"Wuhooo!! You've made: "<<newElement.air<<endl;
alreadyExists (newElement.air);
//PushA (newElement.air);
//write2file (newElement.air);
return newElement;
}
else return newElement;
}//pressure, atmosphere
~ Air ()
{
delete []elements;
}
};
#endif
BLOCK_TYPE_IS_VALID (pHead->nBlockUse)
Assertion means a corrupt heap at deleting/clearing.
As far as I see, you missed the virtual deconstructor of Element.
Try something like:
virtual ~Element() {}
in class Element.
And please post also the Element class.
Good luck!