I'm currently reading a C++ book from the very beginning to learn it. I already have coding knowledge but I really want to deeply learn C++ by trying to code as much as possible from my own instead of always use libraries. (While i'm learning. I'll use libraries later.)
I'm currently reading about the casts. While i'm reading, I try to make exercises to apply what I just read. But well this time I lost control and it goes far away of what I was practicing (casts) but it was fun to code. [EDIT] I will read Different casts. - thanks @Chnossos
I learned a lot while making this code such as stringsstream, reverse etc..
This is a simple int to base 2 and I wish to know if there is a way to optimize the code, or if it's could be correct like this.
Could be great to learn more with an optimized code to see what experts are doing.
Thanks.
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
int toBase_8(int number){ //basé sur un exemple de chiffre 14 avec base 8 -> 016
int base(8);
int entier1;
double entier2;
double decimal;
double resTemp;
int res;
resTemp = (double)number / base; //donne 1.75
entier1 = (int)resTemp; //donne 1 car transforme le double en int alors le int garde seulement l'entier
decimal = resTemp - entier1; //donne 0.75 restant
entier2 = decimal * (double)base; // donne 6
res = ((entier1*10)+(int)entier2); //donne 16
return res;
}
int toBase_2(float number){
if (number > 0 && number <= 255){ //s'sassure que le nombre est entre 0 et 255
int arr[8];
int pos(0);
while ((int)number != 0 || pos == 7){ // à chaque tour de boucle, convertie le float en entier et le compare. ajouté le ou pour etre certains de mettre des 0 jusqu'à la fin meme si la premiere condition (!=) est atteinte
number = number / 2;
if (number == (int)number){
arr[pos] = 0; //si entier
}else{
arr[pos] = 1; //si decimal
}
number = (int)number; // remet le float en int pour la prochaine division. En base 2 chaque nombre decimal doit etre redivisé en entier. Ex.: (9/2) = 4.5. Donne 1 car decimal. Apres on recommence à partir de (4/2) = 2, et non pas 4.5/2. Donne 0 car entier etc..
pos++;
}
reverse(begin(arr), end((arr))); //reverse le array avec algorithm.h car la réponse binaire ce lit sens inverse.
//mettre le array(int) en string puis en un seul int en utilisant stringstream <sstream>
stringstream ss;
for (int d(0); d<(sizeof(arr)/ sizeof(arr[0])); d++){
ss << arr[d]; //ajoute les int dans le stream
}
//METHODE 1: utiliser en string
//cout << ss.str() << endl; //sort le stream en string
//METHODE 2: utiliser en int
int myInt;
ss >> myInt; //envoie le string dans le int
//cout << myInt << endl;
return myInt;
}else{
return 0;
}
}
int main()
{
cout << setw(8) << setfill('0') << toBase_2(234) << endl;
// ex.: 14 en base 8 => 016
//cout << setw(3) << setfill('0') << toBase_8(14) << endl;
return 0;
}
[EDIT - Final] What I understand from all the answers is in fact that my vision of optimization is not the same as yours. @Chnossos understood the most by gaving me good tips such as cast type differences and dropping all the Double and use Modulo (%) instead. For the rest, I think it's all about your vision of optimization such as use more libraries as possible.
Thanks for the answers. If anybody have other code optimization in the same minding as @Chnossos, your welcome.