|
|
Invisible <voi### [at] devnull> wrote:
> using namespace std;
Don't do that. It might feel tempting, but don't. It's better in the long
run to avoid it.
> bool StackCheck(vector<int> stack, int min)
You are passing the vector by value here, which may not be a good idea,
especially if the vector is big. That's because the entire vector will be
copied. (Some STL implementations *might* use copy-on-write, but most of
them don't, and instead just deep-copy.)
Teach yourself to intuitively write:
bool StackCheck(const std::vector<int>& stack, int min)
Usually with big objects (such as vectors) you basically always want
to pass by const reference rather than by value. (You should pass by
value only if that's *really* what you want to do.)
Sure, this might sound like a nuisance, but C and C++ are value based
languages, and by default everything is passed by value. To pass by
reference (or by pointer) you need additional syntax.
> int x = stack.back(); stack.pop_back();
> int y = stack.back(); stack.pop_back();
> stack.push_back(x + y);
You could also save a bit of writing by doing it like this:
int x = stack.back(); stack.pop_back();
stack.back() += x;
Or if you want to really minimize things:
stack.at(stack.size()-2) += stack.back();
stack.pop_back();
Of course it starts being a bit obfuscated.
> int main()
> {
> cout << "Calc #01" << endl;
...
> cout << "Exit." << endl;
> }
While it is perfectly valid to omit the return value of main()
(IIRC it defaults to EXIT_SUCCESS), it's generally recommended to
return EXIT_SUCCESS or EXIT_FAILURE (which are defined in some header
file).
Having said that, when I write very small test programs like this one,
I don't bother writing any return command in main() either. So it's not
that bad. :P
> At present, I haven't figured out how to convert a string to an integer
> yet, so this "calculator" is pretty much useless.
It depends on what your input syntax is. Making a parser in C++ is not
one of the easiest things. Especially a stack calculator is difficult
because you can't know what the type of the next input element will be
in advance, but you have to simply read it as a string and then examine
its contents. The next element might be a number or an operator, and you
can't know which until you read it as a string and examine it.
If the input had a very specific format, then it would be easier.
For example if your input consisted of lines like:
command 1 2
that is, if the keyword "command" appears in the input, it will always
be followed by two integers, then you could write code like this:
std::string keyword;
std::cin >> keyword;
if(keyword == "command")
{
int x, y;
std::cin >> x >> y;
executeCommand(x, y);
}
But as I said, if the next parameter can be a number or an operator or
a keyword or something else, it becomes more complicated.
> Also, there's an interesting glitch where the program won't continue if
> you enter a blank line - you must enter *something* or the program won't
> continue. Presumably this is just the defined behaviour of the >>
> operator...
If you call >> with a string, then it will read the input until it finds
a whitespace-delimited string. >> is not line-based in any way, it's
whitespace-based.
> PS. Nano is a horrid, horrid text editor!
Why don't you use something better?
--
- Warp
Post a reply to this message
|
|