1. ## arithmetic calculation ...again

hi there...

to sean 135...thanx for the free parser. but then i'm not allowed to use any <string.h> functions. any ideas?

and thanx rahaydenuk for the input on RPN. actually it sort of confused me a bit but it's all good.

well if anybody out there would like to help...i'm trying to do an arithmetic function that reads a line from stdin and process it accordingly.

eg.

input : 4+5/3
result:3

note -> order of precedence is not used

any help appreciated! thanx !

2. ## Re: arithmetic calculation ...again

Originally posted by wombat
to sean 135...thanx for the free parser. but then i'm not allowed to use any <string.h> functions. any ideas?
You mean the strlen function he used?
Code:
`pos = strlen(str)-1;`
Just rewrite that to:
Code:
```while(str[pos]) ++pos;
--pos;```

3. Hi, Im going to assume only the +-/* operations. What I'd do is first read the whole thing in as a string with fgets(..). Im doing this way because I've assumed the equation can vary in length. Next I would read in a character at a time, if it is a number print it to a string. When you get to an operator then I would put the number on a stack, or a list or something and continue reading in the next number. I'll let you figure out the rest, like the priority of */ over +-. There probably are other numerous ways to about this. But this is a possibility. Hope this helps

Cheers kwigibo

4. What you can do is parse the original string into a new string with tags. For example:
3+5*2
would become
(Num 3)(Op1 +)(Num 5)(Op2 *)(Num 2)
Then you can determine the order of operation. The * would go first so it would be rewritten like:
(Num 3)(Op1 +)(Num 10)
Then:
(Num 13)
You have your answer. You could also use paranthesis by giving them a tag like (Par#) where number is how embedded it is in other parenthesis.

You can do a search for parsing where I believe Prelude gave a good example of this type of parsing.

- Sean

5. ## Re: Re: arithmetic calculation ...again

Originally posted by Monster
You mean the strlen function he used?
Code:
`pos = strlen(str)-1;`
Just rewrite that to:
Code:
```while(str[pos]) ++pos;
--pos;```
strlen is not string.h is it?

6. ## Re: Re: Re: arithmetic calculation ...again

Originally posted by moi
strlen is not string.h is it?
Of course it is. Why do you think they call it strlen?

Quzah.

7. ## Re: Re: Re: Re: arithmetic calculation ...again

Originally posted by quzah
Of course it is. Why do you think they call it strlen?

Quzah.
oopsie

8. Hi Guys....

Somebody can help me make a simple arithmetic calculation program using STACK.

i) pop
ii) push
iii) ispeek
iii) isempty
iv) Isfull

Plzz... ??

9. >>Somebody can help me make a simple arithmetic calculation program using STACK.

Look around for some sample code. Try writing something and post your questions here. In other words, yes we'll help, but you have to start it.

10. Push.
stack[top++] = new_item;

Pop.
pop_item = stack[--top];

Peek.
peek_item = stack[top - 1];

Isempty.
if( top == 0 )

Isfull.
if( top == N_ITEMS )

Stacks are very straightforward.

11. ## whats wrond with this code?

could somebody try and fix this code for me?

void arithmetic()
{

char [20];
int number = 0, result = 0, i;
char operator = '1';

printf("Enter input:");
fgets(input, 20, stdin);

for(i=0; input[i] != '\n'; i++)
{
while(isdigit(input[i]))
{
number *= 10;
number += input[i];
i++;
}

if(operator == '1')
{
result += number;
}
else
{
switch(operator)
{
case '+': result += number; break;
case '-': result -= number; break;
case '*': result *= number; break;
case '/': result /= number; break;
default: printf("Error in switch operator.\n");
return 0;
}
}
number = 0;

if(input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/')
{
operator = input[i];
printf("Result: %d", result);

}

}
return 0;
}

12. >wombat could somebody try and fix this code for me?
What do you think is wrong with it?

>char [20];
Is not a valid statement.
>char input[20];
...would be better.

>number += input[i];
This will not work properly, you'll need to convert the ASCII input to a number. Something like
>number += input[i] - '0';

Have another go, and post again when you get stuck. And don't forget the code tags.