Originally Posted by
claudiu
This kind of development strategy of stabbing in the dark is a very poor approach to this kind of problem. I suggest you spend some time thinking about HOW you are going to solve the problem (i.e. the algorithm) rather than how you are going to code it in C.
I did put some thought into it. I'm just not great at algorithms. I figured I would start with one problem at a time. I figured I'd seperate out the operands and the numbers into seperate arrays as you see here. Its the only way I could think of to do it. I'm still struggling with this.
Latest source with corrections:
Code:
// Calculator Program. Let's Dance!
#include <stdio.h>
#include <stdbool.h>
int strToInt (const char string[])
{
int i=0, intValue, result=0;
bool negate = false;
if(string[0]=='-')
{
i=1;
negate=true;
}
for(;string[i]>='0' && string[i]<= '9'; i++)
{
if(string[i]=='.')
{
i++;
}
intValue = string[i] - '0';
result = result * 10 +intValue;
}
if(negate)
{
result= -result;
}
return result;
}
int main(void)
{
bool multDivide;
int numbers[100],numbersArrayCount=0,numCount=0,operationsCount=0,tempCount=0,i,j,n;
int multDivideResult,multDivideResultPosition=0,answersArray[20],result,countStop=0;
char workString[25]="24*5*3+12*32-19";
char temp[25]={'\0'};
char operations[6];
// Pluck out all operations characters.
for(j=0;workString[j]!='\0';j++)
{
if(workString[j]=='*' || workString[j]=='\\' || workString[j]=='+' || workString[j]=='-')
{
operations[operationsCount]=workString[j];
operationsCount++;
}
}
printf("\noperationsCount value:%i",operationsCount);
operations[operationsCount]='\0';
printf("\nOperators found:%s",operations);
getchar();
printf("Parsing out character integers...\n");
printf("%s",workString);
do
{
// Check and see if current char is an int char, if so add to temp string.
if(workString[numCount]>='0'&& workString[numCount]<='9')
{
temp[tempCount]=workString[numCount];
tempCount++;
numCount++;
}
else if (workString[numCount]<'0' || workString[numCount]>'9')
{
// end of a int char string found. Send to function for conversion.
temp[tempCount]='\0';
tempCount=0;
numbers[numbersArrayCount]=strToInt(temp);
numbersArrayCount++;
do // Get past non integer characters.
{
numCount++;
}while(workString[numCount]<'0' || workString[numCount]>'9');
printf("\nNumbersArrayCount=%i\n", numbersArrayCount);
}
}while(workString[numCount]!='\0');
temp[tempCount]='\0';
for(i=0;i<=numbersArrayCount;i++)
printf("%i\n",numbers[i]);
printf("Beginning calculations...\n");
// Do all multiply/Divide problems first and put the result in the proper place of the answer array.
multDivide=false;
i=0;
do
{
if(operations[i]=='*' || operations[i]=='/')
{
if(multDivide==true)// Check for consecutive multiplication or division flag
{
n = result;
}
else
{
n = numbers[i];
}
switch(operations[i])
{
case '*':
result= n * numbers[i+1];
break;
case '/':
result= n / numbers[i+1];
break;
default:
break;
}
printf("\nValue of result is %i.\n",result);
// Check to see if consecutive mult/divide problems coming up.
if((operations[i]=='*' || operations[i]=='/') && (operations[i+1]=='*' || operations[i+1]=='/'))
{
multDivide=true;
countStop++;
}
else
{
multDivide=false;
}
if(multDivide !=true) // insert CountStop flag in here somewhere.
{
answersArray[multDivideResultPosition]=result;
printf("\nmultDivide positions:%i\n",multDivideResultPosition);
}
} // close of if(operations * or / ) bracket
// Stop counter of multDivide result position in final array from incrementing willy nilly
if((multDivide !=true)||(countStop<=1))
{
// answersArray[multDivideResultPosition]=result;
multDivideResultPosition++;
}
i++;
}while(operations[i]!='\0');
getchar();
getchar();
}
In the workstring it is yanking and converting every digit except the last 19 and I am having trouble seeing why that is.
Beyond that I think I almost got it. This project is pushing the limit of what I can hold in my head at once.
The last person who replied talking about tokenizing them, I have no idea what he is trying to say. Went right over my head.