473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

PROBLEMS WITH STRING PROESSING

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,discarded.";

const char wrongSyntax[]= "WARNING!!!:Wrong 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 &curExprBegin);

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(const 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(DivByZero);
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 &curExprBegin)
{
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(opTable, 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(opTable, 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,newLine);

}

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

Jul 22 '05 #1
3 1439

"stanlo" <mu******@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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
Jul 22 '05 #2
Mike Wahler wrote:
"stanlo" <mu******@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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.
Jul 22 '05 #3

"red floyd" <no*****@here.dude> wrote in message
news:yf***************@newssvr14.news.prodigy.com. ..
Mike Wahler wrote:
"stanlo" <mu******@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
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...
14
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...
4
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...
5
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...
0
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...
2
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...
2
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...
0
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...
0
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.