PROBLEMS WITH STRING PROESSING

S

stanlo

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[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
 
M

Mike Wahler

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
 
R

red floyd

Mike said:
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.
 
M

Mike Wahler

red floyd said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top