题目(11)答案改

我突然发现【题目(11)】答案有一点问题,输出控制的那一段。所以我改了一下,还加了点小特效。(非常非常小的特效)

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include "windows.h"
#include "stdio.h"

using namespace std;

string int2str(int aNum)
{
	stringstream res;
	res << aNum;
	string s;
	res >> s;
	return s;
}

int char2int(char s)
{
	stringstream res;
	res << s;
	int n;
	res >> n;
	return n;
}

int cifang16(int aNum, int time)//1 0
{
	int sum = 1;
	for(int i = 0; i < time; i++)
	{
		sum = sum * 16;
	}
	return sum * aNum;
}

int hex2dec(char hex, int time)
{
	int num;
	if(hex == 'A' || hex == 'a')
		num = 10;
	else if(hex == 'B' || hex == 'b')
		num = 11;
	else if(hex == 'C' || hex == 'c')
		num = 12;
	else if(hex == 'D' || hex == 'd')
		num = 13;
	else if(hex == 'E' || hex == 'e')
		num = 14;
	else if(hex == 'F' || hex == 'f')
		num = 15;
	else
		num = char2int(hex);
	num = cifang16(num, time);
	return num;
}
int binLen;
string ShortDivide(int dec)
{
	 int everytime, yu;
	 string num = "";
	 while(dec != 0)
	 {
	 	num += int2str(dec % 2);
	 	dec = dec / 2;
	 }
	 binLen = num.size();
	 return num;
}

string Dec2Bin(int dec)
{
	string lbin = ShortDivide(dec);
	return lbin;
}

int main()
{
	string hexNum;//十六进制数 △ 
	printf("[Hex change to Bin]\n----------------\n");
	printf("输入一个十六进制数:");
	cin >> hexNum;//10
	int hlen = hexNum.size();
	int dec = 0;//十进制数 △ 
	for(int i = 0; i < hlen; i++)
	{
		dec += hex2dec(hexNum[hlen - i - 1], i);
	}
	
	for(int i = 0; i < 3; i++){
		cout << "……";
		Sleep(1000);
	}
	cout << endl;
	system("cls");
	cout << "十进制数是 " << dec << endl;
	//ShortDivide()函数 
	string bin = Dec2Bin(dec);//二进制数△ 
	int k = binLen % 4;
	cout << "二进制数是";
	if(k == 0){
	}
	else
		cout << setw(4 - k + 1) << setfill('0');
	for(int i = binLen - 1; i >= 0; i--)
	{
		cout << bin[i];
	}
	cout << endl;
	return 0;
}

其中Sleep()是睡眠,也就是停顿一下,system(“cls”)是清屏,需要windows.h函数库


再点个赞,谢谢!

题目(11)答案改题目(11)答案改 杰克、第五,盛宴伯爵 发布了29 篇原创文章 · 获赞 4 · 访问量 512 私信 关注
上一篇:Oracle数据库AWR报告导出


下一篇:shell命令的替换