-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbase.cpp
More file actions
54 lines (54 loc) · 1.06 KB
/
base.cpp
File metadata and controls
54 lines (54 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//MingkuanPang
//This function can print 1-23 on base 3
#include<iostream>
#include<string>
#include<limits.h>
using namespace std;
void print_base(int,int);
string convert_to_base(int,int);
int main()
{
print_base(3, 23);
return 0;
}
void print_base(int base, int term)
{
int number=1;
while (number <= term)
{
cout << "number " << number << " on base " << base << " is:" << convert_to_base(number, base)<<endl;
number++;
}
}
string convert_to_base(int number,int base)//Below ten.
{
int temp_num;
string num_after_convert,temp_string;
char letter = 'A';
if (number > INT_MAX)
{
cout << "The number is too big" << endl;
}
else
{
while (number >=1)
{
temp_num = number % base;
if (temp_num >= 10)
{
letter = 'A';
temp_num -= 10;
letter += temp_num;
temp_string = letter;
num_after_convert.append(temp_string);
}
else
num_after_convert.append(to_string(temp_num));
if (number == 1)
number = 0;
number /= base;
}
reverse(num_after_convert.begin(), num_after_convert.end());
}
return num_after_convert;
}