I came across this little code which uses the ternary operator, vectors and a recursive function. I think I can see how the ternary operator ("?:") is used, but I dont understand several things:
1. What controls when the doit function is exited?
2. In the doit function which member of the p vector is called
with "/100,p)" ?
Can anyone help explain how this program is working?
Code:
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Multiplier {
public:
//
// a>0: this is amount spent on goods
// a<0: this the amount spent on services
int doit(int a, vector <int> &p){ //doit is recursive function ?
return
a>0? a + doit(a*p[0]/100,p) + doit(-a*p[1]/100,p):
a<0? -a + doit(-a*p[2]/100,p) + doit(a*p[3]/100,p):0;
}
int spending(int amount, vector <int> percent){
return doit(amount, percent)-amount;
}
};
//
int main(void){
int amount;
vector <int> percent;
/*
//0)
23
{20,50,10,40}
//
//
amount=23;
percent.push_back(20);
percent.push_back(50);
percent.push_back(10);
percent.push_back(40);
/*
// 1)
100000
{35,60,70,25}
*/
//
amount=100000;
percent.push_back(35);
percent.push_back(60);
percent.push_back(70);
percent.push_back(25);
/*
// 2)
5
{19,19,19,19}
//
//
amount=5;
percent.push_back(19);
percent.push_back(19);
percent.push_back(19);
percent.push_back(19);
*/
//
Multiplier p;
cout<<"Total Add. Expenditure Gen.="<< p.spending(amount,percent);
cin >> ""; //pause console
percent.~vector<int>(); //Vector Destructor-frees memory
return (0);
}