注册 登录
编程论坛 C++教室

c++问题,谢谢

danieljune 发布于 2013-02-22 03:07, 452 次点击
不好意思,学了几节课又不知道怎么入手了。。


题目是
In reverse Polish notation the operators follow their operands;
To add 3 and 4, one would write "3 4 +" rather than "3 + 4".
If there are multiple operations, the operator is given immediately after its second operand
The expression written "3 − 4 + 5” would be written "3 4 − 5 +" in RPN: first subtract 4 from 3, then add 5 to that
An advantage of RPN is that it obviates the need for parentheses that are required by infix notation.

* Produce a Reverse Polish Notation calculator that operates on input files.
* First, ask the user for the location of an input file. Open this file.
* Read in the input file, and interpret the input data as RPN. Each new line should be interpreted as its own math problem.
* Display the resulting answers to the screen.
* If a math problem has an error in its syntax, display SYNTAX ERROR for that line and continue with the next problem.
* Do not support unary operators.
* Add support for the following operators, written exactly as shown: + - * / % pow
* Math problems are guaranteed to not have more than 5 total operands. As a result, you do not need to use arrays (which we have not yet covered).

Sample Input:
3 4 5.0 * −
7
4. * 8 30 +
banana
9 10 + 30 -

Sample Output:
-17
7
SYNTAX ERROR
SYNTAX ERROR
-11



一开始需要输入文件的位置,然后根据这个地址来读取这个文件。。。用户输入file location(比如c:\Download\abc.txt)后不知道怎么写code来调用了。。。我只会ifstream myfile ("C:\\Users\\Daniel\\Documents\\Visual Studio 2012\\Projects\\Lab2\\Lab2\\input.txt");这种已知地址的调用。。。长长的一串地址不知道怎么用ifstream调用。。。

接下来调用文件中每个数字符号,然后分析计算,我应该能够写出来,就是一开始怎么根据用户输入的地址调用文件不会,有大神帮我一下吗?

谢谢
2 回复
#2
SwanK2013-02-24 16:31
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

//声明变量
//声明 输入文件
int main()
.......
inData.open("c:\\abc.txt"); // 可以直接把文件拷贝到你程序所在目录,不然你必须写明盘符和地址。例如 闪存盘,你必须写明e:\\abc.txt.
cin>>......
#3
SwanK2013-02-24 16:33
#include
#include
#include
using namespace std;

//声明变量
//声明 输入文件
int main()
{
.......
inData.open("c:\\abc.txt"); // 可以直接把文件拷贝到你程序所在目录,不然你必须写明盘符和地址。例如 闪存盘,你必须写明e:\\abc.txt.
cin>>......
...
retunr0;

}
1