I have the warnings from the gcc compiler showing the const char issue.
How to get rid of the warnings?
Thanks,
Michael
char * str_convert(int op) {
/*returns the string corresponding to an operation opcode. Used for screen output.*/
if(op == PLUS) {
return "plus";
}
else if (op == MULT) {
return "mult";
}
else if (op == SUBS) {
return "subs";
}
else if (op == MOD) {
return "mod";
}
else if (op == ABS) {
return "abs";
}
else if (op == MAX) {
return "max";
}
else if (op == MIN) {
return "min";
}
else {
return NULL;
}
}
I think the fix is adding const to the return type (to prevent modification of the contents).
I'd also change the if cascade to switch / case, but that's unrelated to the problem.
const char * str_convert(int op) {
/*returns the string corresponding to an operation opcode. Used for screen output.*/
switch (op) {
case ABS: return "abs";
case MAX: return "max";
case MIN: return "min";
case MOD: return "mod";
case MULT: return "mult";
case PLUS: return "plus";
case SUBS: return "subs";
default: return NULL;
}
}
You may also wish to consider using templated 'op' value as the compiler will replace the jump table it will use for the the switch statement implementation, evaluated at runtime, with a compile time evaluated version that calls on of N functions, depending on the template value.
template <int op>
const char * str_convert(void)
{
/*returns the string corresponding to an operation opcode. Used for screen output.*/
switch (op)
{
case ABS: return "abs";
case MAX: return "max";
case MIN: return "min";
case MOD: return "mod";
case MULT: return "mult";
case PLUS: return "plus";
case SUBS: return "subs";
default: return NULL;
}
}
Related
When I tried making if condition to while loop to remove more than one operator from stack in bracket (f+g/h) here output should be (fgh/+) but i am not able to run the code with while loop my output is coming (fgh/) due to if condition , how do I put while loop without SIGSEGV, im getting run-time error SIGSEGV ?
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
//Function to convert an infix expression to a postfix expression.
string infixToPostfix(string s)
{
// Your code here
stack<char> op;
string res;
int i=0;
while(i<s.length()){
if(s[i]>='a' && s[i]<='z' || s[i]>='A' && s[i]<='Z' ){
res.push_back(s[i]);
cout<<res<<" ";
}
else if(s[i]=='(')
op.push(s[i]);
else if(s[i]==')'){
if(op.top()!='('){ //here SIGSEGV I want this in while loop not if statement
res.push_back(s[i]);
op.pop();
cout<<res<<" ";
}
op.pop();
}
else {
if(op.empty())
op.push(s[i]);
else if(precedence(s[i])>precedence(op.top()))
op.push(s[i]);
else if(precedence(s[i])<precedence(op.top())){
while(precedence(s[i])<precedence(op.top())){
res.push_back(op.top());
op.pop();
}
op.push(s[i]);
}
else{
res.push_back(op.top());
op.pop();
op.push(s[i]);
}
}
i++;
}
return res;
}
int precedence(char a) //precedence function
{
if (a == '^')
return 3;
else if (a == '*' || a == '/')
return 2;
else if (a == '+' || a == '-')
return 1;
else if (a == '(' || a == ')')
return 0;
}
};
int main(){
int t;
t=1;
cin.ignore(INT_MAX, '\n');
while(t--){
string exp;
cin>>exp;
Solution ob;
cout<<ob.infixToPostfix(exp)<<endl;
}
return 0;
}
I'm cross-compiling using ARM/GNU C Compiler:6.3.1 with optimizations -O0. A simple function like this:
static uint8_t xyzzy(void) {
if (<predicate one>) {
return 1;
} else if (<predicate two>) {
return 2;
} else {
return 4;
}
}
results in an error message:
Error control reaches end of non-void function [-Werror=return-type]
Is it really the case that the compiler doesn't know that one of those return statements must always be executed? Or is it me that is confused? And what is a recommended fix?
(P.S.: No, this is not a duplicate of Control reach end of non-void function, since I do have an else as the last clause.)
Simply add a return statement at the end of your function. GCC considers that your function doesn't have a return statement.
static uint8_t xyzzy(void)
{
if (<predicate one>) {
return 1;
} else if (<predicate two>) {
return 2;
} else {
return 4;
}
return 0; // This will never be reached
}
You can get rid of unnecessary {}s and the elses:
static uint8_t xyzzy(void)
{
if (<predicate one>)
return 1;
if (<predicate two>)
return 2;
return 4; // Executed if none of the above conditions are met.
}
I already have a code that works fine with C++ 14 but now I need to set my compiler settings to C++ 11.
auto LookupPredicate(const std::string& aUnitId)
{
return [aUnitId](Coach* pCoach) {return aUnitId == pCoach->getUnitId();};
}
template<typename C, typename UnaryPredicate>
auto findIn_impl(C& container, UnaryPredicate p) {
return std::find_if(std::begin(container), std::end(container), p);
}
findCoach(const std::string& aUnitId)
{
auto it = findIn_impl(_Coaches, LookupPredicate(aUnitId));
if( it != std::end(_Coaches))
cout<<"Selected Unit must exist. Incorrect value of aUnitId = " << aUnitId);
return **it;
}
So, I made the following changes
auto LookupPredicate(const std::string& aUnitId) -> bool
{
return [aUnitId](Coach* pCoach) {return aUnitId == pCoach->getUnitId();};
}
Added a trailing return type bool..
But I get errors like
error: cannot convert '{anonymous}::LookupPredicate(const string&)::' to 'bool' in return
Any idea what must be the trailing type for this
Does CAPL support something like typedef? My goal is to create a boolean:
typedef char bool;
I am able to do this:
enum bool {
false = 0,
true = 1
};
but it isn't what I was going for because I have to do:
enum bool isValid()
instead of:
bool isValid()
Unfortunately there is no typedef in CAPL.
enum is the closest you can get regarding boolean values.
The following code shows the usage of such enum:
variables
{
enum Bool {
true = 1,
false = 0
};
}
on Start {
enum Bool state;
// setting the value
state = true;
// accessing the integer value
write("state (integer value): %d", state); // prints "1"
// accessing the value identifier
write("state (value identifier ): %s", state.name()); // prints "true"
// usage in if-statement
if (state == true) {
write("if-statement: true");
} else {
write("if-statement: false");
}
// usage in switch-statement
switch (state) {
case true:
write("switch-statement: true");
break;
case false:
write("switch-statement: false");
break;
default:
write("switch-statement: undefined");
break;
}
}
How would I convert the chars in this code to Strings? The InfixToPostfix has to be able to take in Strings rather than chars so it can accept double digit numbers and spaces. Any suggestions? Thanks in advance.
import java.util.Stack;
public class InfixToPostfix
{
private Stack operators = new Stack();
public InfixToPostfix()
{
}
public String toPostfix(String infix)
{
char[] characters = new char[100];
int i;
int length = infix.length();
infix.getChars(0, length, characters, 0);
char operator;
String output = "";
for (i = 0; i < length; i++)
{
if (isOperator(characters[i]))
if (operators.empty())
operators.push(characters[i]);
else
{
if (operatorLessPrecedence(characters[i]))
{
do
{
output = output + operators.pop();
}
while (!operators.empty() && operatorLessPrecedence(characters[i]));
operators.push(characters[i]);
}
else
operators.push(characters[i]);
}
else
output = output + characters[i];
}
while (!operators.empty())
{
operator = (char)operators.pop();
output = output + operator;
}
return output;
}
/**
* operator
*/
public boolean isOperator(char c)
{
if ( c == '*' ||
c == '/' ||
c == '+' ||
c == '^' ||
c == '-')
return true;
else
return false;
}
public boolean operatorLessPrecedence(char o)
{
int operatorPrecedence = precedence(o);
int tosPrecedence = precedence((char)operators.peek());
return (operatorPrecedence <= tosPrecedence);
}
/**
* precedence
*/
public int precedence(char o)
{
switch (o)
{
case '+': return 1;
case '-': return 1;
case '*': return 2;
case '/': return 2;
case '^': return 3;
}
return 5;
}
}
I would split the code into two with a lexical analysis phase which splits the input into set of tokens. These are then passed to the shunting yard algorithm, which uses an array of tokens. In the lexing phase you can use regular expressions to detect the different type of input. Say /\d+/ detects sequence of one or more digits.