html <%编程积累——html <%实现html ./制与html //制之间的互相转换
html <%编程积累——html <%实现html ./制与html //制之间的互相转换
html ./制与html //制之间的转换
html ./制转换html //制
html ./制对2整除,得到的余数的倒序即为转换而成的html //制
html <%实现html ./制转换html //制
主函数main.cpp为:
#html --<iostream>
#html --<fstream>
#html --<string>
#html --<vector>
#html --<cmath>
using namespace std;
int main()
{
int N;
cout << "Please enter the number: ";
cin >> N;
cout << endl;
//html ./制转html //制的声明和调用
void convert_10_to_2(int num);
convert_10_to_2(N);
}
html ./制转换为html //制函数为:
#html --<iostream>
#html --<fstream>
#html --<string>
#html --<vector>
#html --<cmath>
using namespace std;
void convert_10_to_2(int num)
{
vector<int> ivec;
int Num = num; //存储原数据
//对原数据循环求除以2的余数,并存储在vector对象中
while (num != 0)
{
ivec.push_back(num % 2);
num = num / 2;
}
//对求得的余数倒序排序并存储在vector中,求得html //制
vector<int>::size_type sz = ivec.size();
vector<int> ivec2;
for (vector<int>::size_type index = 0; index != sz; ++index)
ivec2.push_back(ivec[sz - 1 - index]);
//输出html ./制转换为html //制的数
cout <<Num << "html ./制转html //制后为:";
for (auto item : ivec2)
cout << item;
cout << endl<<endl;
}
html //制转换html ./制
将html //制从右往左从零次幂开始乘以html //制数并求和
html <%实现html //制转换html ./制
html <%主函数为:
#html --<iostream>
#html --<fstream>
#html --<string>
#html --<vector>
#html --<cmath>
using namespace std;
int main()
{
double convert_2_to_10();
convert_2_to_10();
return 0;
}
html //制转html ./制函数为:
#html --<iostream>
#html --<fstream>
#html --<string>
#html --<vector>
using namespace std;
double convert_2_to_10()
{
//输入html //制数并存储在vector变量ivec2中
cout << "Please enter the ErJInZhi numbers: " << endl;
int n;
vector<int> ivec2;
while (cin >> n)
{
ivec2.push_back(n);
}
//html //制转html ./制计算
double sum = 0, times;
vector<int>::size_type sz = ivec2.size();
for (vector<int>::size_type index = 0; index != sz; ++index)
{
times = pow(2, sz - 1 - index);
sum += ivec2[index] * times;
}
//输出转化后的结果
cout << "2进制——";
for (vector<int>::size_type index = 0; index != sz; ++index)
cout << ivec2[index];
cout << "转10进制为:";
cout << sum << endl;
return 0;