Total members 11894 |It is currently Sun Nov 24, 2024 1:08 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Hi,
I am having trouble with a c++ program I am trying to write. The program should be able to solve equations like "3 / ( 4 * 5 ) =" however when i run it a get a runtime error (Unhandled exception). The program is not entirely finished but I can't continue untill i figure this out. It must use a doubly linked list. I am a beginning programmer so please take the time to explain everything, and stick with the simplist code.

Thanks for your time.

Code:
//program needs to be able to solve equations like 3 / ( 4 * 5 ) =
//note: when imputing equation, separate each character with a space and end it with a “=”
//when I run the program, I get a runtime error – not sure why

#include <iostream>
#include <string>

using namespace std;

struct node
{
   node * prev;
   int value;
   char op;
   char type;
   node * next;
};


void printlist (node *h);
void mult (node * right, node * left);


void main ( )
{
   char str[15];
   node * dll, *head, *tail, *rp, *lp;

   cout << "Enter equation: " << endl;
   cin >> str;

   dll = new node;
   head = dll;
   tail = dll;
   head ->prev = 0;
   head -> op = '(';
   head -> type = 'o';
   head -> value = -1;
   


   while (str[0] != '=') //check for end of expression, doesn't include = sign.  Loop will get all imput and put it into a doubly linked list.
   {
      if (isdigit(str[0]))
      {
         tail->next = new node;
         tail->next->prev = tail;
         tail = tail->next;
         tail->value= atoi(str);
         tail->type='n';
         tail->op='$';
      }
      else
      {
         tail->next = new node;
         tail->next->prev = tail;
         tail = tail->next;
         tail ->op = str[0];
         tail->type='o';
         tail->value=-1;
      }
      cin >> str;
   }
   
   tail->next = new node;
   tail->next->prev = tail;
   tail = tail->next;
   tail->next=0;
   tail->op=')';
   tail->type='o';
   tail->value=-1;
printlist (dll);

   //while (dll->op == '(')
   //{   
      rp = head;
      while (rp->op != ')')
         rp = rp -> next;
      lp = rp;
      while (lp->op != '(')
         lp = lp -> prev;
      cout << "lp: " << lp << " rp: " << rp;
      mult (rp, lp);
      printlist (dll);
   //}


system("pause");
}



//Following function used for testing purposes

void printlist (node *h)
{
   while ( h != 0)
   {
      
      cout << " prev: " << h->prev << " addr: " << h <<" value: " << h->value  << " op: " << h->op << " type: " << h->type<< " address of next node: " << h->next << endl;
      h=h->next;
   }
   cout << endl;
}

void mult (node * right, node * left)
{

   node * temp;
   temp = left;
   
   cout <<endl<< "Left: " << left << " Right: " << right << " Temp: " << temp << " Temp->op: " << temp->op;
   system("pause");

   while (temp != right)
   {
   while ((temp->op != '*') || (temp->op != '/') )
      temp = temp ->next;
      
   if (temp -> op == '*')
      {
         temp->value = temp->prev->value * temp->next->value; //computes
         temp->prev->prev->next= temp->next->next; //deletes extra numbers
      }
   if (temp -> op == '/')
      {
         temp->value = temp->prev->value * temp->next->value;
         temp=temp->prev->prev->next;//not correct?
         temp=temp->next->next->prev;//not correct?
      }
   }
   
      return;
}





Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

what exactly you want to create a calculator withc solves arithmetic combinations of a kind ..or something exact such as 3/(5*4) ?? you only want to be able to pick up the numbers or what i was looking the lines of your code the 2 last lines temp = ...etc definatly rong

cout << " prev: " << h->prev << " addr: " << h <<" value: " << h->value << " op: " << h->op << " type: " << h->type<< " address of next node: " << h->next << endl;

hear what is going on .? too many pointers..:P)))


Author:
Moderator
User avatar Posts: 47
Have thanks: 1 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Program to solve equations using double linked list
 Linked List C++ Code Implementation     -  
 I need you to help me to solve this program by c++ language     -  
 Remove border of the linked image     -  
 Draw Circles using Mid-Point Algorithm Equations- On Mouse     -  
 Draw Circles using using Polar form equations-On mouse Click     -  
 Draw curves using Hermite Curve equations with mouse clicks     -  
 how to solve that isssue i m nt able to connect cam and code     -  
 solve the complex numbers and do operations on it     -  
 use one dimensional array to solve sales commissions     -  
 the text file i saved all in one line. How can i solve it?     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com