30/09/2018, 16:30

Cải tiến chương trình đổi các hệ nhị phân, bát phân, thập phân, thập lục phân

Đây là code của em. Mong các anh chị cải tiến giúp em. Em còn gà lắm ạ!

#include <iostream>

int NhapSo(char*m);
int KiemTraSoNhiPhan();
int KiemTraSoBatPhan();
int DaoNguoc(unsigned int b, int a);
int DoiNhiPhanSangThapPhan(unsigned int d);
int DoiThapPhanSangBatPhan(unsigned int e);
int DoiBatPhanSangThapPhan(unsigned int f);
int DoiThapPhanSangNhiPhan(unsigned int g);
void DoiThapPhanSangThapLucPhan(unsigned int c);
void NhapNhiPhanSangBatPhan();
void NhapBatPhanSangNhiPhan();
void NhapNhiPhanSangThapPhan();
void NhapThapPhanSangNhiPhan();
void NhapNhiPhanSangThapLucPhan();
void NhapThapLucPhanSangNhiPhan();
void NhapBatPhanSangThapPhan();
void NhapThapPhanSangBatPhan();
void NhapBatPhanSangThapLucPhan();
void NhapThapLucPhanSangBatPhan();
void NhapThapPhanSangThapLucPhan();
void NhapThapLucPhanSangThapPhan();

int i = 0;

int main()
{
	int iChon;
	std::cout << "1. Nhi Phan --> Bat Phan." << std::endl;
	std::cout << "2. Bat Phan --> Nhi Phan." << std::endl;
	std::cout << "3. Nhi Phan --> Thap Phan." << std::endl;
	std::cout << "4. Thap Phan --> Nhi Phan." << std::endl;
	std::cout << "5. Nhi Phan --> Thap Luc Phan." << std::endl;
	std::cout << "6. Thap Luc Phan --> Nhi Phan." << std::endl;
	std::cout << "7. Bat Phan --> Thap Phan." << std::endl;
	std::cout << "8. Thap Phan --> Bat Phan." << std::endl;
	std::cout << "9. Bat Phan --> Thap Luc Phan." << std::endl;
	std::cout << "10. Thap Luc Phan --> Bat Phan." << std::endl;
	std::cout << "11. Thap Phan --> Thap Luc Phan." << std::endl;
	std::cout << "12. Thap Luc Phan --> Thap Phan." << std::endl;
	std::cout << "Chon phuong thuc doi: ";
	std::cin >> iChon;
	switch (iChon)
	{
	case 1:
		NhapNhiPhanSangBatPhan();
		break;
	case 2:
		NhapBatPhanSangNhiPhan();
		break;
	case 3:
		NhapNhiPhanSangThapPhan();
		break;
	case 4:
		NhapThapPhanSangNhiPhan();
		break;
	case 5:
		NhapNhiPhanSangThapLucPhan();
		break;
	case 6:
		NhapThapLucPhanSangNhiPhan();
		break;
	case 7:
		NhapBatPhanSangThapPhan();
		break;
	case 8:
		NhapThapPhanSangBatPhan();
		break;
	case 9:
		NhapBatPhanSangThapLucPhan();
		break;
	case 10:
		NhapThapLucPhanSangBatPhan();
		break;
	case 11:
		NhapThapPhanSangThapLucPhan();
		break;
	case 12:
		NhapThapLucPhanSangThapPhan();
		break;
	}
	system("pause");
	return 0;
}

int NhapSo(char*m)
{
	int iSo;
	std::cout << m;
	std::cin >> iSo;
	return iSo;
}

int KiemTraSoNhiPhan()
{
	int iNhiPhan = NhapSo("Nhap so nhi phan: ");
	int iTam = iNhiPhan;
	do
	{
	if (iNhiPhan % 10 > 1)
		{
			std::cout << "Nhap so chua cac so tu 0 den 1." << std::endl;
			iNhiPhan = NhapSo("Nhap so nhi phan: ");
			iTam = iNhiPhan;
		}
		iNhiPhan = iNhiPhan / 10;
	} while (iNhiPhan > 0);
	iNhiPhan = iTam;
	return iNhiPhan;
}

int KiemTraSoBatPhan()
{
	int iBatPhan = NhapSo("Nhap so bat phan: ");
	int iTam = iBatPhan;
	do
	{
		if (iBatPhan % 10 > 8)
		{
			std::cout << "Nhap so chua cac so tu 0 den 8." << std::endl;
			iBatPhan = NhapSo("Nhap so bat phan: ");
			iTam = iBatPhan;
		}
		iBatPhan = iBatPhan / 10;
	} while (iBatPhan > 0);
	iBatPhan = iTam;
	return iBatPhan;
}

int DaoNguoc(unsigned int b, int a)
{
	int iDaoSo = 0;
	for (int i = 0; i < a; i++)
	{
		iDaoSo = iDaoSo * 10 + b % 10;
		b = b / 10;
	}
	return iDaoSo;
}

int DoiNhiPhanSangThapPhan(unsigned int d)
{
	int j = 1;
	int iThapPhan = 0;
	for (i = 0; d > 0; i++)
	{
		iThapPhan = iThapPhan + (d % 10)*j;
		d = d / 10;
		j = j * 2;
	}
	return iThapPhan;
}

int DoiThapPhanSangBatPhan(unsigned int e)
{
	int iBatPhan = 0;
	int iDem = 0;
	for (i = 0; e > 0; i++)
	{
		iBatPhan = iBatPhan * 10 + (e % 8);
		e = e / 8;
		iDem++;
	}
	iBatPhan = DaoNguoc(iBatPhan, iDem);
	return iBatPhan;
}

int DoiBatPhanSangThapPhan(unsigned int f)
{
	int j = 1;
	int iThapPhan = 0;
	for (i = 0; f > 0; i++)
	{
		iThapPhan = iThapPhan + (f % 10)*j;
		f = f / 10;
		j = j * 8;
	}
	return iThapPhan;
}

int DoiThapPhanSangNhiPhan(unsigned int g)
{
	int iNhiPhan = 0;
	int iDem = 0;
	for (i = 0; g > 0; i++)
	{
		iNhiPhan = iNhiPhan * 10 + (g % 2);
		g = g / 2;
		iDem++;
	}
	iNhiPhan = DaoNguoc(iNhiPhan, iDem);
	return iNhiPhan;
}

void DoiThapPhanSangThapLucPhan(unsigned int c)
{
	int iTam[20];
	for (i = 0; c > 0; i++)
	{
		iTam[i] = c % 16;
		c = c / 16;
	}
	std::cout << "He thap luc phan: ";
	for (int j = i - 1; j >= 0; j--)
	{
		if (iTam[j] < 10)
			std::cout << iTam[j];
		else
		{
			switch (iTam[j])
			{
			case 10: 
				std::cout << "A"; 
				break;
			case 11: 
				std::cout << "B"; 
				break;
			case 12: 
				std::cout << "C";
				break;
			case 13: 
				std::cout << "D";
				break;
			case 14: 
				std::cout << "E";
				break;
			case 15: 
				std::cout << "F";
				break;
			}
		}
	}
	std::cout << std::endl;
}

int DoiThapLucPhanSangThapPhan(char h[255])
{
	int iThapPhan = 0;
	i = 0;
	while (h[i] != 0)
	{
		i++;
	}
	for (int j = 0; j < i; j++)
	{
		if (h[j] == '0')
		{
			iThapPhan = iThapPhan * 16;
		}
		else if (h[j] == '1')
		{
			iThapPhan = iThapPhan * 16 + 1;
		}
		else if (h[j] == '2')
		{
			iThapPhan = iThapPhan * 16 + 2;
		}
		else if (h[j] =='3')
		{
			iThapPhan = iThapPhan * 16 + 3;
		}
		else if (h[j] == '4')
		{
			iThapPhan = iThapPhan * 16 + 4;
		}
		else if (h[j] == '5')
		{
			iThapPhan = iThapPhan * 16 + 5;
		}
		else if (h[j] =='6')
		{
			iThapPhan = iThapPhan * 16 + 6;
		}
		else if (h[j] == '7')
		{
			iThapPhan = iThapPhan * 16 + 7;
		}
		else if (h[j] == '8')
		{
			iThapPhan = iThapPhan * 16 + 8;
		}
		else if (h[j] == '9')
		{
			iThapPhan = iThapPhan * 16 + 9;
		}
		else if (h[j] == 'A')
		{
			iThapPhan = iThapPhan * 16 + 10;
		}
		else if (h[j] == 'B')
		{
			iThapPhan = iThapPhan * 16 + 11;
		}
		else if (h[j] == 'C')
		{
			iThapPhan = iThapPhan * 16 + 12;
		}
		else if (h[j] == 'D')
		{
			iThapPhan = iThapPhan * 16 + 13;
		}
		else if (h[j] == 'E')
		{
			iThapPhan = iThapPhan * 16 + 14;
		}
		else if (h[j] == 'F')
		{
			iThapPhan = iThapPhan * 16 + 15;
		}
	}
	return iThapPhan;
}

void NhapNhiPhanSangBatPhan()
{
	int iNhiPhan = KiemTraSoNhiPhan();
	std::cout << "He bat phan: " << DoiThapPhanSangBatPhan(DoiNhiPhanSangThapPhan(iNhiPhan)) << std::endl;
}

void NhapBatPhanSangNhiPhan()
{
	int iBatPhan = KiemTraSoBatPhan();
	std::cout << "He nhi phan: " << DoiThapPhanSangNhiPhan(DoiBatPhanSangThapPhan(iBatPhan)) << std::endl;
}

void NhapNhiPhanSangThapPhan()
{
	int iNhiPhan = KiemTraSoNhiPhan();
	std::cout << "He thap phan: " << DoiNhiPhanSangThapPhan(iNhiPhan) << std::endl;
}

void NhapThapPhanSangNhiPhan()
{
	int iThapPhan = NhapSo("Nhap so thap phan: ");
	std::cout << "He nhi phan: " << DoiThapPhanSangNhiPhan(iThapPhan) << std::endl;
}

void NhapNhiPhanSangThapLucPhan()
{
	int iNhiPhan = KiemTraSoNhiPhan();
	DoiThapPhanSangThapLucPhan(DoiNhiPhanSangThapPhan(iNhiPhan));
}

void NhapThapLucPhanSangNhiPhan()
{
	fflush(stdin);
	char iThapLucPhan[255];
	i = 0;
	std::cout << "Nhap so thap luc phan: ";
	std::cin.getline(iThapLucPhan, 255);
	while (iThapLucPhan[i] != 0)
	{
		i++;
	}
	for (int j = 0; j < i; j++)
	{
		if (iThapLucPhan[j] != '0' && iThapLucPhan[j] != '1' && iThapLucPhan[j] != '2' && iThapLucPhan[j] != '3' && iThapLucPhan[j] != '4' && iThapLucPhan[j] != '5' && iThapLucPhan[j] != '6' && iThapLucPhan[j] != '7' && iThapLucPhan[j] != '8' && iThapLucPhan[j] != '9' && iThapLucPhan[j] != 'A' && iThapLucPhan[j] != 'B' && iThapLucPhan[j] != 'C' && iThapLucPhan[j] != 'D' && iThapLucPhan[j] != 'E' && iThapLucPhan[j] != 'F')
		{
			std::cout << "Nhap cac so tu 1 den 9 va tu A den F (viet hoa)." << std::endl;
			std::cout << "Nhap so thap luc phan: ";
			std::cin.getline(iThapLucPhan, 255);
			j = 0;
		}
	}
	std::cout << "He nhi phan: " << DoiThapPhanSangNhiPhan(DoiThapLucPhanSangThapPhan(iThapLucPhan)) << std::endl;
}

void NhapBatPhanSangThapPhan()
{
	int iBatPhan = KiemTraSoBatPhan();
	std::cout << "He thap phan: " << DoiBatPhanSangThapPhan(iBatPhan) << std::endl;
}

void NhapThapPhanSangBatPhan()
{
	int iThapPhan = NhapSo("Nhap so thap phan: ");
	std::cout << "He bat phan: " << DoiThapPhanSangBatPhan(iThapPhan) << std::endl;
}

void NhapBatPhanSangThapLucPhan()
{
	int iBatPhan = KiemTraSoBatPhan();
	DoiThapPhanSangThapLucPhan(DoiBatPhanSangThapPhan(iBatPhan));
}

void NhapThapLucPhanSangBatPhan()
{
	fflush(stdin);
	char iThapLucPhan[255];
	i = 0;
	std::cout << "Nhap so thap luc phan: ";
	std::cin.getline(iThapLucPhan, 255);
	while (iThapLucPhan[i] != 0)
	{
		i++;
	}
	for (int j = 0; j < i; j++)
	{
		if (iThapLucPhan[j] != '0' && iThapLucPhan[j] != '1' && iThapLucPhan[j] != '2' && iThapLucPhan[j] != '3' && iThapLucPhan[j] != '4' && iThapLucPhan[j] != '5' && iThapLucPhan[j] != '6' && iThapLucPhan[j] != '7' && iThapLucPhan[j] != '8' && iThapLucPhan[j] != '9' && iThapLucPhan[j] != 'A' && iThapLucPhan[j] != 'B' && iThapLucPhan[j] != 'C' && iThapLucPhan[j] != 'D' && iThapLucPhan[j] != 'E' && iThapLucPhan[j] != 'F')
		{
			std::cout << "Nhap cac so tu 1 den 9 va tu A den F (viet hoa)." << std::endl;
			std::cout << "Nhap so thap luc phan: ";
			std::cin.getline(iThapLucPhan, 255);
			j = 0;
		}
	}
	std::cout << "He bat phan: " << DoiThapPhanSangBatPhan(DoiThapLucPhanSangThapPhan(iThapLucPhan)) << std::endl;
}

void NhapThapPhanSangThapLucPhan()
{
	int iThapPhan = NhapSo("Nhap so thap phan: ");
	DoiThapPhanSangThapLucPhan(iThapPhan);
}

void NhapThapLucPhanSangThapPhan()
{
	fflush(stdin);
	char iThapLucPhan[255];
	i = 0;
	std::cout << "Nhap so thap luc phan: ";
	std::cin.getline(iThapLucPhan, 255);
	while (iThapLucPhan[i] != 0)
	{
		i++;
	}
	for (int j = 0; j < i; j++)
	{
		if (iThapLucPhan[j] != '0' && iThapLucPhan[j] != '1' && iThapLucPhan[j] != '2' && iThapLucPhan[j] != '3' && iThapLucPhan[j] != '4' && iThapLucPhan[j] != '5' && iThapLucPhan[j] != '6' && iThapLucPhan[j] != '7' && iThapLucPhan[j] != '8' && iThapLucPhan[j] != '9' && iThapLucPhan[j] != 'A' && iThapLucPhan[j] != 'B' && iThapLucPhan[j] != 'C' && iThapLucPhan[j] != 'D' && iThapLucPhan[j] != 'E' && iThapLucPhan[j] != 'F')
		{
			std::cout << "Nhap cac so tu 1 den 9 va tu A den F (viet hoa)." << std::endl;
			std::cout << "Nhap so thap luc phan: ";
			std::cin.getline(iThapLucPhan, 255);
			j = 0;
		}
	}
	std::cout << "He thap phan: " << DoiThapLucPhanSangThapPhan(iThapLucPhan) << std::endl;
}
Sáng Béo viết 18:39 ngày 30/09/2018

sao bạn ko viết chung hàm đổi kiểu:

void function(int số, int hệĐầu, int hệSau){
<các lệnh đổi>;
}
*grab popcorn* viết 18:45 ngày 30/09/2018

:v Thập sang hệ n thì cũng chỉ có 1 function th ^^
tương tự cho hệ n sang thập

Hung Nguyen Minh viết 18:33 ngày 30/09/2018

Chỉ là mình tách công việc nó ra giống như chia để trị ý

Sáng Béo viết 18:45 ngày 30/09/2018

bạn có thấy chương trình rất dài dòng?

Hung Nguyen Minh viết 18:39 ngày 30/09/2018

Làm xong mới biết

Bài liên quan
0