Hi ! Raj Ariyan
Actually! I really don't have any clue about what's wrong with it.
The only information I got is "Runtime Error" without showing "SIGSEGV"(NullPointerException) or "SIGFPE"(FloatingPointException)
BTW, my size for operators is 50 ,for numbers is 1000.
And I do some assert to make sure never divid by zero.
I guess the only possiblity for exception occurs is equation computing.
I use linklist to push the input data.
Code: Select all
void addtwo(compute *top , char O ,long long int V) {
/*
except first one , I read input in pair.
*/
compute *temp = top ;
compute *newNodeO = (compute *) malloc(sizeof(compute));
compute *newNodeV = (compute *) malloc(sizeof(compute));
newNodeO->operation = O;
newNodeO->next = newNodeV;
newNodeV->prev = newNodeO;
newNodeV->next = NULL ;
newNodeV->Value = V;
while(temp->next!=NULL)
temp = temp->next;
temp->next = newNodeO;
newNodeO->prev = temp;
}
Except the first one input, the rest of input are in the form of (operator)+(Vlaue).
Code: Select all
void calculate(compute *top ,int n) {
compute *temp = top ;
/*
first check this node is an operator or not.
if it is, I compute result in the form of ( Num1 Operator Num2 )
I use linklist to meet the priority requirements
int n is a indicator for the priorities
n == mudi 1 means mutiply and divid
n == plmi 0 means plus and minus
I use two times fuction calculate() per equation
*/
while(temp!=NULL) {
if(temp->operation=='*'||temp->operation=='+'||temp->operation=='-'||temp->operation=='/') {
if(temp->next!=NULL&&temp->prev!=NULL)/*just in case*/
if(n==mudi) {
if(temp->operation=='*') {
temp->prev->Value = temp->prev->Value * temp->next->Value;
ToNext()
}
else if(temp->operation=='/') {
temp->prev->Value = temp->prev->Value / temp->next->Value;
ToNext()
}
}/*if*/
else {
if(temp->operation=='+') {
temp->prev->Value = temp->prev->Value + temp->next->Value;
ToNext()
}
else if(temp->operation=='-') {
temp->prev->Value = temp->prev->Value - temp->next->Value;
ToNext()
}
}/*else */
}/* is operator */
temp = temp->next;
}/*while*/
}
I add some comment hope it could help
ToNext() is a macro which I use to make my code readable.
It's designed for to-next. When priority and operator is matched ,I get rid of second and third node, and point to fourth (if there is),continue the process untill all work are finished.
Detail as follow:
Code: Select all
#define ToNext() \
if(temp!=NULL&&temp->next!=NULL) \
temp->prev->next = temp->next->next; \
if(temp!=NULL&&temp->next!=NULL&&temp->next->next!=NULL) \
temp->next->next->prev = temp->prev; \
temp = temp->prev;
I am not good at pointer management ,as you see above
And I do understand it's a great pain to read other people's code ,espcially those
ugly code.
I don't think my coding sytle is good enough for other to catch easily.
But it's not helpful when I need help and other gets less information.
So it's final step for me. Or I can send you my code for you to test.
Thanks for your precious time.