Šajā piemērā jūs iemācīsities manuāli pārveidot bināros skaitļus oktālos un otrādi, izveidojot lietotāja definētu funkciju.
Lai saprastu šo piemēru, jums vajadzētu būt zināšanām par šādām C programmēšanas tēmām:
- C funkcijas
- C Lietotāja definētas funkcijas
Programma binārā konvertēšanai uz oktālo
Šajā programmā mēs vispirms pārveidosim bināro skaitli par decimāldaļu. Pēc tam decimālskaitlis tiek pārvērsts oktālā.
#include #include int convert(long long bin); int main() ( long long bin; printf("Enter a binary number: "); scanf("%lld", &bin); printf("%lld in binary = %d in octal", bin, convert(bin)); return 0; ) int convert(long long bin) ( int oct = 0, dec = 0, i = 0; // converting binary to decimal while (bin != 0) ( dec += (bin % 10) * pow(2, i); ++i; bin /= 10; ) i = 1; // converting to decimal to octal while (dec != 0) ( oct += (dec % 8) * i; dec /= 8; i *= 10; ) return oct; )
Rezultāts
Ievadiet bināro skaitli: 101001 101001 binārā = 51 oktālā
Programma, lai konvertētu oktālo uz bināro
Šajā programmā astotais skaitlis vispirms tiek pārveidots par decimāldaļu. Pēc tam decimālskaitlis tiek pārveidots par bināro skaitli.
#include #include long long convert(int oct); int main() ( int oct; printf("Enter an octal number: "); scanf("%d", &oct); printf("%d in octal = %lld in binary", oct, convert(oct)); return 0; ) long long convert(int oct) ( int dec = 0, i = 0; long long bin = 0; // converting octal to decimal while (oct != 0) ( dec += (oct % 10) * pow(8, i); ++i; oct /= 10; ) i = 1; // converting decimal to binary while (dec != 0) ( bin += (dec % 2) * i; dec /= 2; i *= 10; ) return bin; )
Rezultāts
Ievadiet oktāla skaitli: 67 67 astotniekā = 110111 binārā