cauko,
mam premennu typu float napr 21.45, potreboval by som nejako zistit alebo vypisat hodnotu na pozicii jednotiek teda 1 (napr pri cisle 35.61, by to bola 5). Ako to spravim? (bez delenia)... a este pls by som potreboval zistit ci je cislo parne alebo neparne (tiez bez delenia), existuje na to nejaka funkcia?
dakujem
Fórum › C / C++
Pozicia jednotiek

To by mě zajímalo, proč to nechceš dělat dělením, resp. pomocí operátoru modulo. Samozřejmě musíš float explicitně převést na int.
int jednotky = (int)cislo % 10;
bool parne = (int)cislo % 2;
Řekl bych, že se to ani nijak jinak zjistit nedá a pokud na to existuje nějaká funkce, tak dělá to co jsem tady teď napsal.Ta parnost se da snad zjistit prece jenom bez deleni (operatorem binarniho soucinu).
float x=122.67;
int cislo=(int)x;
bool parne = !(cislo & 1);
Ta jednotka asi nepujde bez deleni. Resp. me napada cachrovani s aritmetickymi posuvy a odcitanim, ale zkouset to nebudu :)To czbero : no muze odcitat zaklad dokud nebude zbytek vetsi nez ten zaklad.. zaroven si pocitat kolikrat odecetl... formalne je to odcitani.. co na tom ze to vlastne vydeli :smile5:
To czbero : já bych řek, že bonesskoj je jedno jestli to ta funkce dělí, nebo co s tim dělá, ale řekl bych, že mu jde o to, aby to nemusel dělit on.......
nicméně jsem o tom přemýšlel, a vymyslel jsem toto....
#include <iostream>
#include <cmath>
using namespace std;
int jednotka(int);
int main()
{
float a;
cout << "Zadejte cislo: "; cin >> a;
int vysledek=(int) a;
int vys =0;
while (vysledek > 9) {
vysledek=jednotka(vysledek);
if (vys!=vysledek) vys=vysledek;
else break;
}
if (vysledek>9 and vysledek < 16) vysledek-=10;
cout << "Cislo obsahuje " << vysledek << " jednotek.";
cin.get();
cin.get();
return 0;
}
int jednotka(int cislo) {
int velikost=sizeof(cislo)*8-1, vysledek, a;
int pole[4]={2, 4, 6, 8};
vysledek=cislo & 1;
for (int i=1; i<=velikost; i++)
if (cislo & (unsigned int)pow(2.0, (double)i)) {
if ((i & 3)==3) a=3;
else if ((i & 2)==2) a=1;
else if ((i & 1)==1) a=0;
else a=2;
vysledek+=pole[a];
};
return vysledek;
}
nevím o tom, že by tam byla použita nějaká funkce, co by dělila.......zatim to se všema číslama co jsem zkoušel fungovalo, ale možná se vyskytne pár čísel, který se budou muset ošetřit v if (jako 10-15).....
#include <iostream>
int main() //Další možnost
{
float cislo = 0;
cout << "Zadej cislo:"
cin >> cislo;
cislo = (int)cislo; //Odstranění desetinných míst.
cislo *= 0.1f; //Posunutí čísel o jedno místo doprava,
// tím posuneme jednotky na 1. desetinné místo
cislo -= (int)cislo; //Odečte čísla před desetinnou čárkou
cislo *= 10; //Posuneme jednotky zpět
cout << cislo; //A máme výsledek.
return 0; //Jak jednoduché
}
Včera mi to moc nemyslelo... kocovina...

Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 92 hostů
Podobná vlákna
Cislo odzadu a vypis poctu jednotiek v cisle — založil RedSoul
C# pozícia v richtextboxe — založil supervisor
Pozícia obrázka — založil Majak
Pozicia obrazku v jqeury — založil Tomas
Blue J problém z nahodnimy poziciamy — založil aha
Moderátoři diskuze