puzzlecracker said:
puzzlecracker ha scritto:
char buffer[50];
double d=-205;
that contains a number (negative or positive), and I know what number
it is, Now I need to enclose the number in the buffer with columns if
it negative.
Here is the twist, the format varies, it can be "%s", can also be
something else. So, after I constructed that buffer with number, want
to enclose it with columns.
I fear I do not understand the problem. Why don't you simply use
(if d < 0)
sprintf(buffer, "%s", d)
else
sprintf(buffer, "|%s|", d)
?
sprintf(buffer,((d<0)?"|%s|":"%s"),d);
well, I am given a final string with which contains a number. It's
constructed based on various criteria, and it would be painful to
modify every format (around 100 in total) that a number is
constructed with.
So I just want to take the final product and enclose it with columns
Then why do you lie to us?
You do not have a number, you have a string!
And it looks like you want to concatenate this string with some other
when it starts with a '-' character.
#include <ciso646>
std::string f(std::string number){
return(((number.size()>0) and (number[0]=='-')) ? "|"+number+"|" : number);
}
Or perhaps the string may be left-padded:
std::string f(std::string number){
return((number.position('-')!=std::string::npos) ? "|"+number+"|" : number);
}
Or perhaps your string may be anything, and you will still have to
modify all the places where the number is formated:
class SignedString {
private:
std::string formatedNumber;
bool negative;
public:
SignedString(std::string aFormatedNumber,bool aNegativeFlag):formatedNumber(aFormatedNumber),negative(aNegativeFlag){}
inline std::string getFormatedNumber(){return(formatedNumber);}
inline bool getNegative(){return(negative);};
};
in places where you format your numbers:
sprintf(buffer,"...",d);
return SignedString(buffer,d<0);
then:
std::string f(SignedString number){
return(number.getNegative() ? "|"+number.getFormatedNumber()+"|" : number.getFormatedNumber());
}
But in any case, do not lie, when you have a string don't say you have a number!