Anubhav Sharma
Anubhav Sharma

Reputation: 388

C++ program to fold constants

I am writing a function in C++11 which takes expressions of the form constant1 + constant2 and folds them. constant1 and constant2 are stored in a std::string and their types are stored in an enum TypeEnum.

My attempt is as follows:

Program :

#include<iostream>
#include<assert.h>
#include<string>

using namespace std;

enum TypeEnum {
    INT, LONG_INT, LONG_LONG_INT,
    UNSIGNED_INT, UNSIGNED_LONG_INT, UNSIGNED_LONG_LONG_INT
};

long long fold(string constant1, string constant2, 
               TypeEnum typeConst1, TypeEnum typeConst2){

    if(typeConst1 == INT){
        if(typeConst2==INT)
            return stoi(constant1) + stoi(constant2);
        if(typeConst2 == LONG_INT)
            return stoi(constant1) + stol(constant2);
        if(typeConst2 == LONG_LONG_INT)
            return stoi(constant1) + stoll(constant2);
        if(typeConst2 == UNSIGNED_INT)
            return stoi(constant1) + stol(constant2);
        if(typeConst2 == UNSIGNED_LONG_INT)
            return stoi(constant1) + stoul(constant2);
        if(typeConst2 == UNSIGNED_LONG_LONG_INT)
            return stoi(constant1) + stoull(constant2);
    }else if(typeConst1 == LONG_INT){
        //...
    }else if(typeConst1 == LONG_LONG_INT){
        //...
    }else if(typeConst1 == UNSIGNED_INT){
        //...
    }else if(typeConst1 == UNSIGNED_LONG_INT){
        //...
    }else if(typeConst1 == UNSIGNED_LONG_LONG_INT){
        //...
    }

    assert(false);
}
int main(){
    cout << fold("1","9223372036854775806",INT,LONG_LONG_INT) << endl;
    cout << fold("1","2147483647",INT,INT) << endl; 
    return 0;
}

Output:

9223372036854775807

-2147483648

As you can see, the function fold has become really messy and long. I would like to know if there are better ways to do this.

Upvotes: 0

Views: 116

Answers (2)

MarianD
MarianD

Reputation: 14191

Use the switch{} construction:

switch(typeConst1){
case INT:
    switch(typeConst2){
    case INT:
        return stoi(constant1) + stoi(constant2);
    case LONG_INT:
        return stoi(constant1) + stol(constant2);
    case LONG_LONG_INT:
        return stoi(constant1) + stoll(constant2);
    case UNSIGNED_INT:
        return stoi(constant1) + stol(constant2);
    case UNSIGNED_LONG_INT:
        return stoi(constant1) + stoul(constant2);
    case UNSIGNED_LONG_LONG_INT:
        return stoi(constant1) + stoull(constant2);
case LONG_INT:
    //...

Upvotes: 1

George Kourtis
George Kourtis

Reputation: 2592

I would prefer to change and simplify the problem and write the code as follows:

#include <iostream>

short int fold(short int a, short int b){return a+b;}  
int fold(int a, int b){return a+b;}

int main(int argc, char **argv){    
  std::cout<<fold((short)32767,(short)1)<<std::endl;
  std::cout<<fold(32767,(short)1)<<std::endl;   return 0; 
}

But as you may notice if you change the short to int and the int to long the code will not work any more due to lack of continuity in the promotion rules of C++.

Upvotes: 0

Related Questions