# Please could someone assist me with double digits

This is a discussion on Please could someone assist me with double digits within the C Programming forums, part of the General Programming Boards category; this was what I did but it didn't work. Can you help me and study it to see what I'm ...

1. this was what I did but it didn't work. Can you help me and study it to see what I'm missing?
Code:
#include<stdio.h>
#include<stdlib.h>
#define MAXCOLS 80
main(){
char instring[MAXCOLS], postring[MAXCOLS];
int position = 0;
float eval();
float result;
printf("&#37;s", "Enter postfix expression: ");
while ((postring[position++] = getchar()) != '\n');
postring[--position] = '\0';
printf("%s%s", "postfix expression is", postring);
getchar();
result=eval(postring);
printf("%s%f\n", "value is", result);
getchar();
}
struct stack{
int top;
float items[MAXCOLS];
};
float eval(expr)
char expr[];
{
int c, position;
float op1, op2, value;
float oper(), pop();
struct stack opndstk;
opndstk.top = -1;
for (position =0; (c=expr[position]) != '\0'; position++)
if(c== '  '){
position++;
}
if(isdigit(c)){
double val, power,res; int sign;
sign = (expr[position] == '-') ? -1 : 1;
if (expr[position] == '+' || expr[position] == '-')
position++;
for (val = 0.0; isdigit(expr[position]); position++)

val = 10.0 * val + (expr[position] - '0');
if (expr[position] == '.')
{
position++;
}
else
{
}
for (power = 1.0; isdigit(expr[position]); position++)
{
val = 10.0 * val + (expr[position] - '0');
power *= 10;
}
res= sign * val / power;
push(&opndstk, res);
}
else if ( c  == '*' || c  == '+' || c  == '/' || c  == '%' || c  == '-' )
{
op1 = pop ( );
op2 = Pop ( );

switch(c)
{
case '+' : value= op2+op1; break;
case '-' : value= op2-op1; break;
case '*' : value= op2*op1; break;
case '/' : value= op2/op1; break;
}
push (&opndstk, value);
}
result = pop ( );
return result;
}

2. Yes, so you need to debug it. It's no point in me debugging it, because you will just take the fix I've written and paste it into your code. Learning to debug your own (or someone elses!) code is definitely something you need if you plan to ever do any real programming.

There are a few main ways to debug code:
1. Read the code and step by step follow what happens. This can be time-consuming. This works well if you have a small piece of code where you KNOW it's broken in that piece.
2. Use a debugger and step through the code line-by-line and follow what happens. Again, best if you know which part of the code is broken.
3. Add debug-code to print (and perhaps verify assumptions) important data in a suspect piece of code. As long as the added printout isn't TOO lengthy, it's usually a very quick way to check out where things go wrong.
4. Ask someone else to do it for you. If you have a manager position or a big checkbook, this method works, but on a "hobby" basis, it's perhaps not such a good idea. The main drawback here is that you don't learn anything from your mistakes (if you have debugged a problem for 6 hours (or a few weeks), you WILL NOT make that mistake again anytime soon! - if someone else spent hours to figure out what is wrong, you will probably just apply the fix and go on making similar mistakes again).

--
Mats

3. when I enter the postfix 34 25 + it works, result 59.0000, but when I enter 12 2 * 12 + it gives 24.0000. It seems to stop after executing the first operator. Again if I have 2.5 2 * it doesn't work at all. which means the '.' is causing some problem but in the code I have provision for point. Just help me and figure out where the problem is.

4. One subtle error is the "if (c == ' ')" above - it has two spaces in it, which probably means that it's ALWAYS going to be false.

You may want to add a printf to see what digits you are processing in the "after point" code - are they valid digits, is it what you think it should be, etc, etc?

--
Mats

5. I've sorted the '.' stuff. The only problem I've got now is the loop, it stops after executing the first operator. Any suggestions?

6. Hmm, I'm actually surprised that you get as far as the first operation, as I don't see anything that would "repeat" aside from the loops to make a number.

--
Mats

7. I don't understand what you said, but I'm saying that if enter a "2 3 * 4 +" it gives me the result as 6 i.e after executing the fisrt operator, I guess I'm missing out something in the loop, but I can't figure out what it is. Or there a way I could reconstruct the loop?

8. Can someone help me with the loop. when I enter the postfix 2 3 * 4 +, it gives me result as 6, i.e after executing the first operator and stops. It doesn't get to 4 and +.

9. What I was trying to say is that I can't see anything that _WILL_ make your eval() function work to even do two operands and an operation. But maybe the code you've posted isn't acutally the code you're running.

--
Mats

10. The only thing missing in the code above is the pop() and push() which is on page 1 of this tread. You can run it and see, you know what I want to do, and how to implement it. Just run it and see, if you've got suggestions then you can give concerning the loop.

11. Here is the code put together again, please if anyone has got suggestions about the loop, please make. It works for 2 3 + but not more than that. say 2 3 * 4 +
Code:
#include<stdio.h>
#include<stdlib.h>
#define MAXCOLS 80
main(){
char instring[MAXCOLS], postring[MAXCOLS];
int position = 0;
float eval();
float result;
printf("&#37;s", "Enter postfix expression with space as delimiter: ");
while ((postring[position++] = getchar()) != '\n');
postring[--position] = '\0';
printf("%s%s", "postfix expression is", postring);
getchar();
result=eval(postring);
printf("%s%f\n", "value is", result);
getchar();
}
struct stack{
int top;
float items[MAXCOLS];
};
float eval(expr)
char expr[];
{
int c, position;
float op1, op2, value;
float oper(), pop();
struct stack opndstk;
opndstk.top = -1;
for (position =0; (c=expr[position]) != '\0'; position++)
if(isdigit(c)){
double val, power,res; int sign;
sign = (expr[position] == '-') ? -1 : 1;
if (expr[position] == '+' || expr[position] == '-')
position++;
for (val = 0.0; isdigit(expr[position]); position++)

val = 10.0 * val + (expr[position] - '0');
if (expr[position] == '.')
{
position++;
}
else
{
}
for (power = 1.0; isdigit(expr[position]); position++)
{
val = 10.0 * val + (expr[position] - '0');
power *= 10;
}
res= sign * val / power;

push(&opndstk, res);

}       //  push (&opndstk, (float)(c-'0'));
else {
op2 = pop(&opndstk);
op1 = pop(&opndstk);
value = oper(c, op1, op2);
push (&opndstk, value);
}
return (pop(&opndstk));
}

float oper(symb, operand1, operand2)
int symb;
float operand1, operand2;
{
switch(symb){
case '+': return (operand1 + operand2);
case '-': return (operand1 - operand2);
case '*': return (operand1 * operand2);
case '/': return (operand1 / operand2);
default: printf("%s", "illegal operation");
exit(1);
}
}

push(ps, x)
struct stack *ps;
float x;
{
if(ps->top == MAXCOLS-1)
{
printf("%s", "stack overflow");
exit(1);
}
else
ps->items[++(ps->top)] =x;
return;
}

float pop(ps)
struct stack *ps;
{
int top;
if (top==-1)
{
printf("%s", "stack underflow");
exit(1);
}
else
return(ps->items[ps->top--]);
}

12. Can you try to answer as many of these questions as possible:

What compiler are you using?
Does it support enabling warnings?
If so, please do enable warnings.
Does it support ANSI standard?
If so enable it, and fix up your code to use ANSI.

Your string-to-float code uses double, whilst the rest of the code is using float - is there a reason for that?

The pop operation has an uninitialized variable to check for top == -1, which means that if you get a stack underflow, it's not going to be detected (and if you are really unlucky, the uninitialized variable turns into -1 by chance, and you get a stack-underflow when there isn't one).

What happens if you see a "space" in the expr in eval?

Code:
Enter postfix expression with space as delimiter: 23 45 + 18 * 19 /
postfix expression is: 23 45 + 18 * 19 /
value is 64.421051
--
Mats

13. Mat I'm using dev-c++, I've just enabled checked the compiler settings as folows:
*Support all ANSI standard C programs---"YES"
*Inhibit all warning messages---"NO"
*Display one error per line ---"YES"
*Attempt to support some aspects of traditonal C pre----"YES"

I used if(c==' ') position++; in the program though I missed it out on the page here.

Could these be the problem? Is the loop ok as it is? Please put me through, I've really toiled over this.

14. Now fix all the warnings that the compiler gives!

It is VERY GOOD PRACTICE to use compiler warnings to tell you when you do something wrong in the code. I made your code compile on MS VC 7 without warnings, so you should be able to too. And NO I'm not going to just give you the code.

You probably want to look over where you do your check for space, and how you do that. I think that will fix your problem. But I want (as thanks for the help) see your code when you're finished, and I want it to NOT give any warnings when I compile it.

--
Mats

15. I've still tried it but it's not working, only for two operands and one operator

Page 2 of 3 First 123 Last