Jak widać na tą chwilę napisałem sobie na szybko funkcję liczącą potęgi liczb całkowitych o wykładniku będącym liczbą naturalną - ponieważ tylko takie wystąpią w tym programie (dzieli on wprowadzone liczby na połowy). Gdy chciałem skorzystać ze standardowej funkcji pow() pojawiły się problemy z 1. liczeniem modulo z typu double 2. wynikiem potęgowania po rzutowaniu na int.
#include <iostream>
#include <cmath>
using namespace std;
unsigned digitsNumber (unsigned i)
{
return i > 0 ? (int) log10 ((double) i) + 1 : 1;
}
int power(int base, int exponent) //potegowanie - z powodu problemu z liczeniem modulo z liczby zwroconej przez standardowo funkcje pow()
{
if (exponent>1)
{
exponent--;
base*=power(base, exponent);
}
else if (exponent==0)
return 1;
else if (exponent<0)
{
return 0; // funckja nie liczy poteg ujemnych
}
return base;
}
int main()
{
while(true)
{
int K;
cin>>K;
int dN = digitsNumber(K);
cout<<dN<<endl;
if (dN%2 == 0)
{
int leftHalf = K / power(10, (dN/2));
int rightHalf = K % power(10, (dN/2));
cout<<"LH:"<<leftHalf<<endl<<"RH:"<<rightHalf<<endl;
}
else
{
int leftHalf = K / power(10, ((dN+1)/2));
int rightHalf = K % power(10, ((dN-1)/2));
cout<<"LH:"<<leftHalf<<endl<<"RH:"<<rightHalf<<endl;
}
}
return 0;
}