So, my final assignment for my C++ class: Take a regular equation, input it one character or value at a time, then move it through several functions.
Flowchart:
-Prepares a doubly linked list
-Sets left paran
-Reads in equation piece by piece - if piece is an operator (+, -, *, /, (, ) ) then this gets set to oper of that struct portion; otherwise, it gets set to value.
-Goes to head of equation
-Finds a right paran, sets RP to it
-Goes backward until left is found, sets LP to it
-Sends it into functions which multiply, divide, add, and subtract whenever those things are found
-Deletes parans
-If there are any more parans, runs from step 2.
Here's the code:
#include<iostream>
#include<string>
using namespace std;
struct node
{
node* prev;
char oper;
int value;
node* next;
node()
{
next = prev = NULL;
oper = 0;
value = 0;
}
};
void printlist(node* h);
void multiplier (node* l, node* r);
void adder (node* l, node* r);
void paran (node* l, node* r);
void main()
{
char str[10];
node* head = new node;
node* tail = head;
node* temp, *RP, *LP;
head->prev = 0;
int loopend = 0, check = 0, overallcheck = 2;
tail->value = 0;
//Sets first node to zero
tail->next = new node;
tail->next->prev=tail;
tail=tail->next;
tail->oper = '(';
//Sets next node to left paranthesis, for the calculating
tail->next = new node;
tail->next->prev=tail;
tail=tail->next;
cout << "Enter a number or operator";
cin >> str;
while(str[0] != 's')
{
if(str[0] == '+' || str[0] == '-' || str[0] == '*' || str[0] == '/' || str[0] == '(' || str[0] == ')')
{
tail->oper = str[0];
tail->value = 0;
}
else
{
tail->value = atoi(str);
tail->oper = 0;
}
tail->next = new node;
tail->next->prev=tail;
tail = tail->next;
cout << "Enter a number or operator";
cin >> str;
}
//Inputs expression into dll
tail->oper = ')';
//Sets a right paranthesis to help calculate
tail->next = new node;
tail->next->prev=tail;
tail = tail->next;
tail->value = 0;
//Sets last node as zero to finish list
printlist(head);
//Giant line of crazy calculations starts here
while(overallcheck != 0)
{
temp = head;
do
{
if(temp->oper == ')')
{
RP = temp;
loopend = 1;
}
else
temp = temp->next;
}while(loopend != 1);
loopend = 0;
temp = RP;
do
{
if(temp->oper == '(')
{
LP = temp;
loopend = 1;
}
else
temp = temp->prev;
}while(loopend != 1);
//Finds next set of parantheses to operate with
loopend = 0;
while(loopend != 1)
{
for(temp = LP; temp; temp = temp->next)
if(temp->oper == '*' || temp->oper == '/')
check++;
//Checks if there are any '*'s or '/'s in the dll. If there are, runs multiplier function. If not, ends the loop.
if(check > 0)
{
multiplier(LP, RP);
check = 0;
}
else
loopend = 1;
};
//Runs multiplier
loopend = 0;
while(loopend != 1)
{
for(temp = LP; temp; temp = temp->next)
if(temp->oper == '+' || temp->oper == '-')
check++;
//Checks if there are any '+'s or '-'s in the dll. If there are, runs multiplier function. If not, ends the loop.
if(check > 0)
{
adder(LP, RP);
check = 0;
}
else
loopend = 1;
};
//Runs adder
loopend = 0;
overallcheck = 0;
paran(LP, RP);
//Deletes parantheses which were operated on
for(temp = head; temp; temp = temp->next)
if(temp->oper == '(' || temp->oper == ')')
overallcheck++;
//Checks if there are any more parantheses; if not, loop ends and prints out the answer.
}
printlist(head);
system("pause");
}
void printlist(node *h)
{
for(node* p = h; p; p = p->next)
{
if (p->value != 0 && p->value != -842150451)
cout << p->value << " ";
if (p->oper != 0 && p->oper != 'Í')
cout << p->oper << " ";
}
cout << endl;
}
void multiplier (node* l, node* r)
{
int loopend = 0;
node *temp, *temp2;
temp = l;
temp2 = l;
do
{
if(temp->oper == '*')
{
temp = temp->prev;
temp2 = temp2->next;
temp->value = temp->value * temp2->value;
temp->next = temp->next->next->next;
temp2 = temp2->next;
temp2->prev = temp2->prev->prev->prev;
loopend = 1;
//If a '*' is present, gets the values before and after, multiplies them, and deletes via pointing to two later and two before
}
if(temp->oper == '/')
{
temp = temp->prev;
temp2 = temp2->next;
temp->value = temp->value / temp2->value;
temp->next = temp->next->next->next;
temp2 = temp2->next;
temp2->prev = temp2->prev->prev->prev;
loopend = 1;
//If a '/' is present, gets the values before and after, divides them, and deletes via pointing to two later and two before
}
else
{
temp = temp->next;
temp2 = temp2->next;
}
}while(loopend != 1);
}
void adder (node* l, node* r)
{
int loopend = 0;
node *temp, *temp2;
temp = l;
temp2 = l;
do
{
if(temp->oper == '+')
{
temp = temp->prev;
temp2 = temp2->next;
temp->value = temp->value + temp2->value;
temp->next = temp->next->next->next;
temp2 = temp2->next;
temp2->prev = temp2->prev->prev->prev;
loopend = 1;
//If a '+' is present, gets the values before and after, adds them, and deletes via pointing to two later and two before
}
if(temp->oper == '-')
{
temp = temp->prev;
temp2 = temp2->next;
temp->value = temp->value - temp2->value;
temp->next = temp->next->next->next;
temp2 = temp2->next;
temp2->prev = temp2->prev->prev->prev;
loopend = 1;
//If a '-' is present, gets the values before and after, subtracts them, and deletes via pointing to two later and two before
}
else
{
temp = temp->next;
temp2 = temp2->next;
}
}while(loopend != 1);
}
void paran (node* l, node* r)
{
node *temp, *temp2;
l = l->next;
l->prev = l->prev->prev;
l = l->prev;
l->next = l->next->next;
//Sets list from LP to move forward, remove the prev behind it via changing the link, then
//moving to the prev and deleting the next node through the same method
r = r->prev;
r->next = r->next->next;
r = r->next;
r->prev = r->prev->prev;
//Sets list from RP to move backward, remove the next ahead of it via changing the link, then
//moving to the next and deleting the previous node through the same method
}
The problem is, when I use parantheses in the equation, apart from those set at the beginning, the right ones don't get deleted. I can't figure out why.
I thought that changing this:
for(temp = LP; temp; temp = temp->next)
to this:
for(temp = LP; temp = RP; temp = temp->next)
would fix it, but that gets the code stuck in a neverending loop by immediately setting temp to RP and never reaching that point via temp = temp->next.
Figuring out that part would be helpful as well.
If it's too long, aggravating, or confusing, that's fine. It hardly makes any sense to me, and I wrote the dang thing.
Anyways, any help would be appreciated.

New Topic/Question
Reply




MultiQuote






|