G
G Patel
Can people please comment on the layout/style of my problem? The major
issue I had was the layout. I ended up having to put a relatively
large switch statement, inside an if statement, which is inside a while
loop. If someone can tell me how I can rearrange these elements a
little to make it cleaner, I would appreciate that. I've tested it on
the command line, and it works *well*, but the source code layout is
bugging me.
I'd also like to add that I really appreciate all the help I've had for
my previous posts.
Thx all
/* expr K&R2 Ex 5-10 */
#include <stdio.h>
#include <stdlib.h>
void push(double);
double pop(void);
int main(int argc, char *argv[])
{
double num;
double op2; /* for non-commutative binary operations */
char *p; /* for strtod */
if(argc == 1)
{
printf("Error, no arguments provided after program name\n");
exit(EXIT_FAILURE);
}
while((++argv)[0] != NULL)
{
num = strtod(argv[0], &p); /* will add errno checking later */
if(p != argv[0]) /* if valid number */
push(num);
else if(argv[0][1] != '\0') /* if more than one character */
{
printf("Error, %s not valid operator or operand\n",
argv[0]);
exit(EXIT_FAILURE);
}
else
switch(argv[0][0])
{
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '/':
op2 = pop();
if(op2 != 0.0)
push(pop() / op2);
else
printf("Error, division by zero\n");
exit(EXIT_FAILURE);
break;
case '%':
op2 = pop();
if(op2 != 0.0)
push((int)pop() % (int)op2);
else
printf("Error, division by zero\n");
exit(EXIT_FAILURE);
break;
default:
printf("Error, %c not valid operator or operand\n",
argv[0][0]);
exit(EXIT_FAILURE);
break;
}
}
printf("Top of Stack = %.8g\n", pop());
exit(EXIT_SUCCESS);
}
issue I had was the layout. I ended up having to put a relatively
large switch statement, inside an if statement, which is inside a while
loop. If someone can tell me how I can rearrange these elements a
little to make it cleaner, I would appreciate that. I've tested it on
the command line, and it works *well*, but the source code layout is
bugging me.
I'd also like to add that I really appreciate all the help I've had for
my previous posts.
Thx all
/* expr K&R2 Ex 5-10 */
#include <stdio.h>
#include <stdlib.h>
void push(double);
double pop(void);
int main(int argc, char *argv[])
{
double num;
double op2; /* for non-commutative binary operations */
char *p; /* for strtod */
if(argc == 1)
{
printf("Error, no arguments provided after program name\n");
exit(EXIT_FAILURE);
}
while((++argv)[0] != NULL)
{
num = strtod(argv[0], &p); /* will add errno checking later */
if(p != argv[0]) /* if valid number */
push(num);
else if(argv[0][1] != '\0') /* if more than one character */
{
printf("Error, %s not valid operator or operand\n",
argv[0]);
exit(EXIT_FAILURE);
}
else
switch(argv[0][0])
{
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '/':
op2 = pop();
if(op2 != 0.0)
push(pop() / op2);
else
printf("Error, division by zero\n");
exit(EXIT_FAILURE);
break;
case '%':
op2 = pop();
if(op2 != 0.0)
push((int)pop() % (int)op2);
else
printf("Error, division by zero\n");
exit(EXIT_FAILURE);
break;
default:
printf("Error, %c not valid operator or operand\n",
argv[0][0]);
exit(EXIT_FAILURE);
break;
}
}
printf("Top of Stack = %.8g\n", pop());
exit(EXIT_SUCCESS);
}