 |
 |
Advice on how to start this program
|
 |
|
 |
|
Moderator 
Join Date: Sep 2000
Location: Irvine, CA
Status:
Offline
|
|
Hello,
I am supposed to write this program and I have spent several hours on it and have writtten several programs that each somewhat accomplishes the goal, but truthfully, I have no idea how to start it. Here are the details:
1) I am supposed to read in a postfix expression from a file.
2) I am supposed to evaluate the expression and return the value.
Here is an example of a postfix expression:
3 5 * 4 - 8 +
It is the same as (((3*5) -4) +8)
Here is what I have been doing:
while (c != EOF)
{
c = getc(fp);
stack[i] = c;
i++;
}
So I have it parsed into the character array. I have heard that it is better to read it in as all characters then convert some of them to integers.
After this part, I am completely lost as to what do next. Here is what I have been attempting to do (these are all seperate things).
1) use the sscanf function like this sscanf(stack, "%d %d %c", &t, &a, &b), but I cannot find out how to restart where I left off on the string so I can change the conditions for the function.
2) I also have been trying to do several if statements tesing whether each part of the array is either a character or a number and then moving the characters into one array and the numbers into another array so that I can process them. The problem with this method (at least the way that I am doing it) is that I cannot seperate them right. I don't know how! I have some of it right but the spacing between the numbers and characters gives me garbaled results, and the teacher suggested that we not use fscanf to read in the expression.
3) I have been using the atoi function to convert the string into numbers in parts 1 and 2, but I am lost in how to incorporate it well.
Any suggestions?
edit: I know that this is supposed to be like stacks of how Iam supposed to "push" and "pop" thing, but I need to parse the array before I can evaluate the stack.
[This message has been edited by mindwaves (edited 05-06-2001).]
|
|
{{{ mindwaves }}}
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: Sep 2000
Location: Irvine, CA
Status:
Offline
|
|
For this program, I am trying to use as many character and line oriented I/O as possible!
|
|
{{{ mindwaves }}}
|
| |
|
|
|
 |
|
 |
|
Admin Emeritus 
Join Date: Oct 2000
Location: Boston, MA
Status:
Offline
|
|
Ah! The age old problem of expression parsing. It should be relatively easy with postfix notation. Because there's no precedence or parentheses (and it looks like you're dealing with solely binary operations), there's really no need to use a stack, three variables should be fine -- One for the total, one for the next number, and one for the next operation.
I'll see if I can't whip up something trivial. For now, check epaperpress.com or the source code for "bc."
|
|
"Against stupidity, the gods themselves contend in vain" (Schiller)
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: Sep 2000
Location: Irvine, CA
Status:
Offline
|
|
Thanks parallax!
Anyways, I looked at www.epaperpress.com and found some things to like but not really what I was looking for. Also, I couldn't find the source code that you were talking about.
The problem that I am having is that my program (in its incomplete state) works ok, but it does not parse double digit integers at all. I do not know how to incorporate them into my program. Any suggestions?
|
|
{{{ mindwaves }}}
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: Sep 2000
Location: Irvine, CA
Status:
Offline
|
|
Ok, I gave up and just used fscanf; however, if I have some time, then I will scan in each number as a character and try my luck again. For those who are interested, here is my finished and working program:
Code:
#include<iostream>
#include<stdio.h>
int main(void)
{
const int max = 2; // depth of stack value
int firstnum, secondnum, tempnum; // declares the necessary variables
double temp1, stack[max], temp = 0;
char firstop, tempop;
FILE *fp;
fp = fopen("test.dat", "r");
fscanf(fp, "%d%d%s", &firstnum, &secondnum, &firstop); // scans in the first two numbers
if(firstop == '+') temp1 = double(firstnum + secondnum); // Finds the result of the
if(firstop == '-') temp1 = double(firstnum - secondnum); // first two numbers
if(firstop == '*') temp1 = double(firstnum * secondnum);
if(firstop == '/') temp1 = (double(firstnum) / double(secondnum));
stack[0] = temp1;
while( ! feof( fp ))
{
fscanf(fp, "%d%s", &tempnum, &tempop); // scans in the rest of the values
stack[1] = double(tempnum);
if(tempop == '+') {
temp = (stack[0] + stack[1]); // rest of the while loop does the necessary operations
stack[0] = temp; // depending on the operand
}
if(tempop == '-') {
temp = (stack[0] - stack[1]);
stack[0] = temp;
}
if(tempop == '*') {
temp = (stack[0] * stack[1]);
stack[0] = temp;
}
if(tempop == '/'){
temp = (stack[0] / stack[1]);
stack[0] = temp;
}
}
printf("%lf", stack[0]); // prints out the final value
fclose(fp);
return 0;
}
|
|
{{{ mindwaves }}}
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 1999
Location: Georgetown, Demerara, Guyana
Status:
Offline
|
|
Hi mindwaves,
Congrats on your solution! It's a pretty good simulation of a stack, given that the grammar for the mathematical expressions wasn't too complex. Did you eventually get the "one-char-at-a-time" approach to work, or did you just end up retaining the use of 'fscanf'?
[BTW, I'm hopefully back from an extended hiatus; unfortunately, most of this month turned out to be *extremely* hectic.]
Regards, and keep up the good work :-),
--Paul
|
|
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: Sep 2000
Location: Irvine, CA
Status:
Offline
|
|
Hi Paul,
It is really nice to see you back! I hope that everything has went well for you. Truthfully, I haven't worked on it much so I do not have a working program that reads the expression in one char at a time. I have also been busy with other homework as well, but if given then time, I will complete what I said before knowing that I can use the "atoi" function to convert each character to an integer than probably I will explicitly typecast the number into a double. But I also have another problem here if you don't mind helping me because I am totally lost once again as you can imagine. Thanks again for your reply.
[This message has been edited by mindwaves (edited 05-31-2001).]
|
|
{{{ mindwaves }}}
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 1999
Location: Georgetown, Demerara, Guyana
Status:
Offline
|
|
Hi again mindwaves,
Well, my bad... It's now two months later that I'm crawling back to this Forum. :-( I'm still swamped in some private work & other stuff, but I will definitely try to check in more regularly, at least once a week.
Over the course of last night / this morning, I've posted responses in one or two ancient threads here, but I'm sort of running on fumes right now... However, after I get some sleep this afternoon I promise I'll respond tonight to your linked thread (if you haven't already worked things out) and perhaps some of the other threads. I also have a few obscure AppleScript questions of my own that I've been meaning to post here at some point...
See you later,
--Paul
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|