Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma hdrstop
- #pragma argsused
- #ifdef _WIN32
- #include <tchar.h>
- #else
- typedef char _TCHAR;
- #define _tmain main
- #endif
- #include <stdio.h>
- #include <iostream>
- #include <conio.h>
- #include <string>
- using namespace std;
- void inputNumInBase12(char*Num12)
- {
- cout << "Enter 12-base num( it must be lesser or equal to 41A792678515120367), using digits and symbol 'A', 'B'."<<endl;
- cin.getline(Num12,19);
- cout << "u've entered "<< Num12;
- }
- int conwert12DigitsTo10(char digit12)
- {
- if ((digit12!='A')&&(digit12!='B')) {
- return (digit12-'0');
- }
- else if (digit12!='A')
- {
- return 11;
- }
- else return 10;
- }
- long long conwerBasefrom12To10(char*base12){
- int length = strlen(base12);
- long long res=0;
- for (int i = 0; i < length; i++)
- {
- res=res*12+conwert12DigitsTo10(*base12);
- // cout << "\ndigit12"<<i<<" = "<<conwert12DigitsTo10(*base12);
- //cout << "\nres"<<i<<" = "<<res;
- base12++;
- }
- return res;
- }
- void conwertBase10To9(char * base9, long long base10)
- {
- int i= 0;
- do{
- base9[i]=char(base10%9+'0');
- base10/=9;
- i++;
- }while (base10);
- base9 = strrev(base9);
- }
- int conwert12DigitIn10Nums()
- {
- char a = getch();
- if (int(a)==13)
- {
- return -1;
- }
- cout<<a;
- if (a=='A') {
- return 10;
- }
- if (a=='B') {
- return 11;
- }
- return (a-'0');
- }
- long long enterNumIn12BaseWithoutMassAndConwertItIn10BaseNum()
- {
- long long res = 0;
- cout << "Enter 12-base num( it must be lesser or equal to 41A792678515120367), using digits and symbol 'A', 'B'."<<endl;
- for (int i = 0; (i < 20); i++) {
- int digit = conwert12DigitIn10Nums();
- if (digit==-1) {
- break;
- }
- res=res*12+digit;
- }
- return res;
- }
- void conwert10BaseTo9WithoutArray(long long base10)
- {
- int ans;
- int i = 0;
- cout << "In base9: ";
- long long ink = base10;
- do{
- ink/=9;
- i++;
- }while(ink!=0);
- for (int j =i ; j >0; j--) {
- long long incr = base10;
- for (int k = 0; k< j-1;k++ )
- {
- incr/=9;
- }
- cout <<char(incr%9+'0');
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- cout << "This program convert 12-base nums into 9-base nums"<<endl;
- char base12[19]={'\0'};
- inputNumInBase12(base12);
- long long base10=conwerBasefrom12To10(base12);
- cout<<"\nbase10 "<<base10;
- char base9[21]={'\0'};
- conwertBase10To9(base9, base10);
- cout << "\nbase9 : "<< base9;
- cout << "\n\nNow this program will solve the same problem without using arrays.";
- long long base10two = enterNumIn12BaseWithoutMassAndConwertItIn10BaseNum();
- cout << "\n\nbase10two "<< base10two;
- cout <<endl;
- conwert10BaseTo9WithoutArray(base10two);
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement