Need Help Using "Sort" Within My Program
I have a simple program that lists input in order of precedence, checking only for operators and ranking them like so, "*/+-":
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int prec(char op)
{
if (op == '*' || op == '/') return 0;
return 1;
}
bool compareprec(char a, char b)
{
return prec(a) < prec(b);
}
int main()
{
char input[] = "+-/*";
cin >> input;
sort(input, input + 4, &compareprec);
cout << input;
}
I'm trying to implement it within a more complex program that uses stacks to check alpha numerical input and do an infix to postfix conversion, ranking something that looks like this: "9*9+9" into "9 9 9 * +". The more complex program is as follows:
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int prec(char op)
{
if (op == '*' || op == '/' || op == '+' || op == '-') return 0;
return 1;
}
bool compareprec(char a, char b)
{
return prec(a) < prec(b);
}
int main()
{
stack<char> s;
char input;
while (cin.get(input) && input != '\n')
{
if (isalnum(input))
cout << input << " ";
else if (input == '(')
s.push(input);
else if (input == ')')
{
while (!s.empty() && s.top() != '(')
{
cout << s.top() << " ";
s.pop();
}
if(!s.empty())
s.pop();
else
cout << "ERROR: No Matching ( \n";
}
else if (s.empty() && input == '*'|| input == '/'|| i开发者_Go百科nput == '+'|| input == '-')
{
sort(input, input + 4, &compareprec); // Error Begins Here?
s.push(input);
}
else if (input == '*'||input == '/'||input == '+'|| input =='-')
while (!s.empty())
{
sort(input, input + 4, &compareprec); // More Errors Here?
cout << s.top() << "\n ";
s.pop();
s.push(input);
}
}
while (!s.empty())
{
cout << s.top() << " ";
s.pop();
}
}
But I keep getting an error that says:
error: no matching function for call to 'sort(char&, int, bool (*)(char, char))'
error: no matching function for call to 'sort(char&, int, bool (*)(char, char))'
And I'm not sure why. I know it's probably something painfully obvious/silly but I can't figure it out. Any help would be appreciated. Thanks in advance!
sort expects something that can be iterated over.
Your working example has
char input[]
(an array of char)
Your non working example removes the array syntax and makes it a plain char
char input
When you attempt to do:
sort(input, input + 4, &compareprec)
In the working case, because you are providing an array, you're telling it to iterate from the beginning of input to 4 past the location. In the non-working case you're telling it to go from say 'a' to 'd' (which is 'a' + 4).
Why do you have to sort anything in the second example? You have maximally two operators you have to compare, one on the top of the stack and one in the input. Just use your compareprec
-function and act accordingly based on the result.
And by the way, to make your code a little prettier, create a function:
bool is_operator(char s);
Almost forgot to tell you, the version of int prec(char a)
in the second example is wrong, use the first one.
精彩评论