-3

i want to write a program that reads post-fix expressions from a file , each post-fix expression is on a line , the program then evaluates the post-fix expression then writes the result next to each line .

it goes something like this :

file information :

1 2 +

9 3 * 

after post-fix evaluation : file looks like this :

1 2 +  3

9 3 *  27

where the additional number is the result of the post-fix evaluation . I'm done with the post-fix evaluation but cannot seem to know how to write them to the end of each line .

anybody has an idea how i can achieve this ?

T.Matar
  • 1
  • 3
  • I have no idea how you "cant achieve this". But I have an idea how you can do this: open your C++ book and look for `std::ifstream`, or type `std::ifstream` into Google and learn. Maybe this will help you: [`std::ifstream`](https://en.cppreference.com/w/cpp/io/basic_ifstream#Example). – L. F. Jan 31 '19 at 23:24
  • I think the problem the OP might be facing is writing the result in the middle of a file. If so, that's not possible. Maybe this answer helps you. https://stackoverflow.com/questions/30642827/how-to-write-to-middle-of-a-file-in-c – Sahil Dhoked Jan 31 '19 at 23:28
  • Hard to say what you're doing wrong if we don't know what you are doing, so I strongly recommend adding a [mcve] that covers your output writing. – user4581301 Jan 31 '19 at 23:32
  • just edited the question , i would appreciate that you see what I,m trying to achieve – T.Matar Jan 31 '19 at 23:43

2 Answers2

1

Use a std:ifstream to open the input file, and a std::ofstream to open a separate output file. Then use std::getline() in a loop to read lines from the input file, processing each line and writing it to the std::ofstream along with its result. When the loop is done, you can close the streams and replace the input file with the output file, if needed.

#include <fstream>
#include <string>

std::ifstream in("input.txt");
std::ifstream out("output.txt");
std::string line;

while (std::getline(in, line))
{
    if (!line.empty())
    {
        out << line;

        // process line as needed...

        out << "  " << result;
    }

    out << "\n";
}

in.close();
out.close();
// replace input.txt with output.txt, if needed...
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

I'm assuming there will be no brackets in your code and all numbers will be positive integers, separated by operators or spaces.

Use a stack data structure. (In case you don't know what that is, it's a data structure with two operations called pop and push, you should read up about it if you have never heard of it before).

Whenever you read a digit, keep reading until you find a non-digit. All the digits you read now make up an integer. Push that integer onto the stack.

Repeat until you read an operator (+, -, *, /)

Once you read an operator, apply that operator to the last two values on the stack and push the result on the stack.

This is how math is done internally in compilers.

Once you have completed a line, check if there is only one value on the stack. If there isn't, it means there is a non-matching number of integers and operators in the instructions. If there is only one number, write it to the file.

EDIT: It's probably best if you create a new file for your output (and if necessary, rename that to the name of the input file at the end)

Then read each line from the input file and paste it and the result into the output file.

L. Kue
  • 473
  • 5
  • 19
  • thank you for your explanation , but my problem really lies in the way i want to display the information to the file , not the post-fix evaluation itself – T.Matar Jan 31 '19 at 23:42
  • @T.Matar I've updated my answer. – L. Kue Jan 31 '19 at 23:47
  • The only thing in this answer that even remotely addresses the question asked is the very last 2 sentences. Read a line from the input file, process it as needed, write it and its result to another output file, and repeat until no more lines are read. The rest is irrelevant and should be deleted. – Remy Lebeau Jan 31 '19 at 23:57