hi to everyone, this is still a follow up of my project ,mathematical
expression.this project is meant to evaluate mathemtical expressions
with oparators,+,-,*,/.more than two operands can be done, eg it should
be able to do,1+9-45*7/12,or 4* 5+6*21.from reading and help from you i
have been able to write the program.but my problem is to process the
string of the input.i.e given for example an input string, 12+6*65/7,
you know i have to move from 12 and evalaute the expression through,so
the problem is to be able to jump from one operator to another at any
point in time when need be.i tried to use point, as i will show
below, but i have tried to uderstand so much that i am kind of
confused.i need your help, here is just the first part of the program,
you may or may not want to look at it, but i think a look at it will
make you understand my problem better., after this i will then include
the part i need help on.
#include <string.h>
#include <iostream.h>
namespace
{
const int maxLength = 82; // including end character and the zero
character
const char finishLoopChar = '$';
// Input and error atlets
const char EnterExp[] = "Please give an expession and press the ENTER
key";
const char result[]= " The Answer is: ";
const char DivByZero[]= "WARNING!!! :No Division by Zero;Check answer.";
const char FloatPtNo[] = "WARNING!!! :No decimal points
allowed,discard ed.";
const char wrongSyntax[]= "WARNING!!!:Wro ng syntax, Watch out for
answer!";
//operations and Levels of operation
const opLevels = 2;
const opsPerLevel = 2;
const char opTable[opLevels][opsPerLevel] = { {'*','/'}, {'+','-'} };
//Function declarations
bool searchOp(char* line, char &curOp, int &curOpPos);
long evaluate(long op1, long op2, char operation);
bool verArr(const char array[opLevels][opsPerLevel], char testChar);
int goToOp1(char* line, int startPos, int &curExprBegi n);
int goToOp2(char* line, int startPos, int &curExprEnd) ;
void reduceArr(char* line, int curExprBegin, int curExprEnd, long
number);
void discardBadChar( char* line, bool &exit);
void errorOutput(con st char* erroralert);
//Function definitions
{
bool verArr(const char array[opLevels][opsPerLevel], char testChar)
//Check if char "test" is in array
{
int i=0;
int j=0;
while (i < operatorLevels) {
if (array[i][j]==testChar) return true;
j++;
if (j == opsPerLevel) {
j=0;
i++;
}
}
return false;
}
//Function definitions
long evaluate(long op1, long op2, char op)
{
long result = 0;
switch (op)
{
case '*':
{
result = op1 * op2;
break;
}
case '/':
{
if (op2==0)
{
errorOutput(Div ByZero);
result = 0;
}
else
{
result = op1 / op2;
}
break;
}
case '+':
{
result = op1 + op2;
break;
}
case '-':
{
result = op1 - op2;
break;
}
default :
{
result = 0;
break;
}
}
return result;
}
<end code>
ok, welcome back, here is the part that troubles me.please just kindly
take the pain to go through it don t complain if you see silly
mistakes, i am a beginner.
//Function definitions: string processing
int goToOp1(char* line, int startPos, int &curExprBegi n)
{
int pos=startPos-2;
int op1Size=0;
int answer;
answer = 0;
//Construct operand until another operator or begin of line reached
}
if ((line[pos]=='-') && ((verArray(opTa ble, line[pos-1])) || (pos==0)))
{ //Detect negative sign
answer*=-1;
pos--;
}
curExprBegin = pos + 1;
return answer;
}
int goToOp2(char* line, int startPos, int &curExprEnd) //Detect right
operand
{
int pos=startPos;
int answer=0;
int factor=1;
if (line[startPos]=='-') { //Detect negative operand
factor=-1;
pos++;
}
//Construct operand until another operator or end of line reached
while ((line[pos]!='\0') && (verArray(opTab le, line[pos])==false)) {
answer = answer * 10 + line[pos] - '0';
pos++;
}
curExprEnd = pos - 1;
return answer*factor;
}
//Overwrite operand1, operator and operand2 by calculation result,
shorten line
void shortArray(char * line, int curExprBegin, int curExprEnd, long
number)
{
char newLine[maxLength];
int oldLinePos=0, newLinePos=0;
while (line[oldLinePos]!='\0') {
if (((oldLinePos < curExprBegin) || (oldLinePos > curExprEnd))) {
newLine[newLinePos]=line[oldLinePos]; //Simple copy from line to
newLine
newLinePos++;
}
else { //Inserting intermediate result number
if (number <0) {
newLine[newLinePos]='-';
newLinePos++;
number*=-1;
}
}
oldLinePos = curExprEnd;
}
oldLinePos++;
}
newLine[newLinePos]='\0'; //Append null character
strcpy(line,new Line);
}
my problem is my aproach seem to be to error, prone, place i think you
understood want to do, u could make modifications or propose your own
fragment to solve the problem 3 1461
"stanlo" <mu******@yahoo .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. . my problem is my aproach seem to be to error, prone, place i think you understood want to do, u could make modifications or propose your own fragment to solve the problem
I suggest you use the 'std::string' type instead of 'C-style'
strings. 'std::string' objects are much easier and safer to
use, and handle all the memory management for you. It also
offers many searching and manipulation member functions. The
functions declared by <algorithm> add even more functions which
could be useful.
-Mike
Mike Wahler wrote: "stanlo" <mu******@yahoo .com> wrote in message news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
my problem is my aproach seem to be to error, prone, place i think you understood want to do, u could make modifications or propose your own fragment to solve the problem
I suggest you use the 'std::string' type instead of 'C-style' strings. 'std::string' objects are much easier and safer to use, and handle all the memory management for you. It also offers many searching and manipulation member functions. The functions declared by <algorithm> add even more functions which could be useful.
Also, the OP should not use <iostream.h>. It's non-standard. He should
use <iostream> instead.
"red floyd" <no*****@here.d ude> wrote in message
news:yf******** *******@newssvr 14.news.prodigy .com... Mike Wahler wrote: "stanlo" <mu******@yahoo .com> wrote in message news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
my problem is my aproach seem to be to error, prone, place i think you understood want to do, u could make modifications or propose your own fragment to solve the problem
I suggest you use the 'std::string' type instead of 'C-style' strings. 'std::string' objects are much easier and safer to use, and handle all the memory management for you. It also offers many searching and manipulation member functions. The functions declared by <algorithm> add even more functions which could be useful.
Also, the OP should not use <iostream.h>. It's non-standard. He should use <iostream> instead.
Yes. His posts of the last week or so indicate to me that
he's enrolled in a very poor quality C++ course, of which
there seems to be no dearth.
-Mike This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Eva |
last post by:
Hi,
I wanted to know how i can enter values into a specific
column of a listview. I have tried the following code but
this seems to enter all my values into the first column!!!
Can anyone please help me out on this??
hers my code so far.....
|
by: Jim Hubbard |
last post by:
Are you up to speed on the difficulties in using the 1.1 .Net framework?
Not if you are unaware of the 1,596 issues listed at KBAlertz
(http://www.kbalertz.com/technology_3.aspx).
If you are going to use .Net......I highly recommend signing up for the free
KBAlertz newsletter at http://www.kbalertz.com/default.aspx.
Looking at all of the errors and quirks sometimes makes me wonder if this
thing is really ready for prime time.
|
by: Wayne Wengert |
last post by:
I am still stuck trying to create a Class to use for exporting and importing
array data to/from XML. The format of the XML that I want to import/export
is shown below as is the Class and the code I am using to create a sample
XML file. I am trying to dimension the ArrayOfJudgeEntity to have two sets
of the JudgeTableEntity values. When I run the code I get an error that the
XML is not correct.
I jsut can't get my head around the array...
|
by: Chua Wen Ching |
last post by:
Hi,
I read from this tutorial at codeproject
Question A:
http://www.codeproject.com/csharp/GsXPathTutorial.asp
regarding xpath.. but i try to apply in my situation, and can't get it work...
|
by: Peter R. Vermilye |
last post by:
I am involved on a web application that is using a third party set of
APIs for remote database access (middleware). I've been brought in
because of my background in programming, thus I'm new to this web
development process so forgive my ignorance.
The third party has a shopping cart which must be saved in the
session.
When navigating from page to page we experience some odd behavior on
the hosted server that we do not see on our...
| |
by: Joel D. Kraft |
last post by:
I've been very happy with the performance and new features of my site
since we converted to ASP.NET 2.0 beta 2.
I have noticed a couple of interesting problems, though, which I am
trying to figure out. I can't confirm that they didn't exist under
1.1, but I didn't notice them at that time.
My error handling is configured as follows:
- In IIS, 404 errors are mapped to /site/error/404.aspx.
- "Verify that file exists" is ON for .aspx...
|
by: Mike |
last post by:
Hi,
I am new to C and having problems with the following program.
Basically I am trying to read some files, loading data structures into
memory for latter searching.
I am trying to use structres and arrays of pointers to them.
I have gotten the program to compile with gcc on WinXP.
If the file i read doesnt have alot of records, it runs thru. But once
i add more, it dies. In this program i have 4 files setup to read. The
|
by: neoret |
last post by:
Hello.
I have developed an application (an office addin).The application works
fine with no error messages on my devoping machine (that machine is not
connected to any domains).
I have problems getting this application to run on a computer where the
user is on a domain (as in a big company where you log on to the
company domain).
|
by: neoret |
last post by:
Hello.
I have developed an application (an office com addin).The application works
fine with no error messages on my devoping machine (that machine is not
connected to any domains).
I have problems getting this application to run on a computer where the user
is on a domain (as in a big company where you log on to the company domain).
The funny thing is that the application works fine in outlook. But not
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |