Hi everyone,
i guess i have a quite common problem with "an access violation (segmentation fault) raised in your program" but none of the cases i found out there in the internet were helpful. The program should take a phrase from a file, written in infix notation, and convert it into postfix. I guess the problem may lay in my not-so-perfect using of forward lists, because the algorithm worked just okay on static arrays.
The debugger shows this line (47) as crucial:
1 2 3
|
else if(tab[i].priorytet>head->prev->key.priorytet || head->prev==NULL){
|
This should define the case when the stack (which is the list) is empty or if on its top there's an element with "priorytet" greater than "prioryet" of the sign which i have extracted from the file and put into array with its "priorytet".
(The file of course does not contain priorytets, i assign them in the array, as showed in the comment, where there are also their ascii codes)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include<fstream>
#include<cstdlib>
/* ( 40 0
) 41 1
+ 43 1
- 45 1
* 42 2
/ 47 2
% 37 2
*/
using namespace std;
ifstream plik;
ofstream zapis;
struct znak{
char element;
int priorytet;
};
struct lista{
znak key;
lista *prev, *next;
};
void onp(znak tab[], unsigned long int n){
lista *head=new lista;
head->prev=NULL;
lista *p;
for(int i=0;i<n;i++){if(isdigit((int)tab[i].element)) zapis<<tab[i].element;
else if(tab[i].priorytet==0){ head->key=tab[i]; // (
p= new lista;
p->prev=head;
head->next=p;
head=p;
head->next=NULL;
}
else if((int)tab[i].element==41){ while((int)head->prev->key.element!=40){ // )
zapis<<head->prev->key.element;
head=head->prev;
head->next=NULL;
}
head=head->prev;
head->next=NULL;
}
else if((tab[i].priorytet)>(head->prev->key.priorytet) || head->prev==NULL){ head->key=tab[i];
p= new lista;
p->prev=head;
head->next=p;
head=p;
head->next=NULL;
}
else{ while(head->prev->key.priorytet>=tab[i].priorytet){
zapis<<head->prev->key.element;
head=head->prev;
head->next=NULL;
}
head->key=tab[i];
p=new lista;
p->prev=head;
head->next=p;
head=p;
head->next=NULL;
}
}
while(head->prev!=NULL){zapis<< head->prev->key.element;
head=head->prev;
head->next=NULL;
}
}
int main(){
plik.open("we.txt");
string c;
getline(plik, c);
znak tab[c.length()];
plik.clear();
plik.seekg(0);
for (int i=0;i<c.length();i++) plik>>tab[i].element;
for (int j=0;j<c.length();j++){
if((int)tab[j].element==40) tab[j].priorytet=0;
else if((int)tab[j].element==41 || (int)tab[j].element==43 || (int)tab[j].element==45) tab[j].priorytet=1;
else if((int)tab[j].element==42 || (int)tab[j].element==47 || (int)tab[j].element==37) tab[j].priorytet=2;
}
zapis.open("wy.txt");
onp(tab, c.length());
return 0;
}
|
I will be grateful for any tips
PS. Sorry for non-English variables, i hope it's not a big problem
EDIT:
Ok, I found what was wrong! I had to add exception that (head->prev!=NULL), otherwise it would check some undefined place in memory, at least that's what I understood. Now it looks like this:
1 2 3
|
else if((head->prev!=NULL && (tab[i].priorytet)>(head->prev->key.priorytet)) || head->prev==NULL)
|
I don't delete, maybe it will be helpful for somebody else. If anything, you could just tell me if I am right with the origin of the error or if you have any other advice with the program.