Paste: super_base_changer

Author: Rex Ford again
Mode: c++
Date: Fri, 24 Feb 2012 01:02:57
Plain Text |
/*
Written by Rex Ford for fun
*/

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
	int base=-1;
	int from_base=-1;
	string num_str;
	int num=0;
	string symbols = "0123456789abcdefghijklmnopqrstuvwxyz";
	
	cout<<"What number?\n";
	cin >> num_str;

	while(from_base>36 || from_base<2)
	{
		cout<<"What base was that in?\n";
		cin >> from_base;
		if(base>36 || base<2)
			cout<<"Your chosen base was not from 2-36...\n";
	}
	
	//Horner's method
	for(int i=0;i<num_str.size();i++)
	{
		num+=symbols.find(num_str.at(i));
		if(i!=num_str.size()-1)
			num*=from_base;
	}
	cout<<"Your number was "<<num<<" in base 10\n";

	while(base>36 || base<2)
	{
		cout<<"What base?\n";
		cin >> base;
		if(base>36 || base<2)
			cout<<"Your chosen base was not from 2-36...\n";
	}
	
	//calculates the numbers of digits that num will have when represented in the new base
	int num_digits=(int)1+(log((double)num)/log((double)base));

	for(int exp=num_digits-1;exp>=0;exp--)
	{
		int digit_value = num/pow(base,exp);
		cout<<symbols.at(digit_value);
		num=num-digit_value*pow(base,exp);
	}
	cout<<endl;
}

Annotation: small fix

Author: Rex Ford again
Mode: c++
Date: Fri, 24 Feb 2012 01:08:15
Plain Text |
/*
Written by Rex Ford for fun
*/

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
	int base=-1;
	int from_base=-1;
	string num_str;
	int num=0;
	string symbols = "0123456789abcdefghijklmnopqrstuvwxyz";
	
	cout<<"What number?\n";
	cin >> num_str;

	while(from_base>36 || from_base<2)
	{
		cout<<"What base was that in?\n";
		cin >> from_base;
		if(from_base>36 || from_base<2)
			cout<<"Your chosen base was not from 2-36...\n";
	}
	
	//Horner's method
	for(int i=0;i<num_str.size();i++)
	{
		num+=symbols.find(num_str.at(i));
		if(i!=num_str.size()-1)
			num*=from_base;
	}
	cout<<"Your number was "<<num<<" in base 10\n";

	while(base>36 || base<2)
	{
		cout<<"What base?\n";
		cin >> base;
		if(base>36 || base<2)
			cout<<"Your chosen base was not from 2-36...\n";
	}
	
	//calculates the numbers of digits that num will have when represented in the new base
	int num_digits=(int)1+(log((double)num)/log((double)base));

	for(int exp=num_digits-1;exp>=0;exp--)
	{
		int digit_value = num/pow(base,exp);
		cout<<symbols.at(digit_value);
		num=num-digit_value*pow(base,exp);
	}
	cout<<endl;
}

New Annotation

Summary:
Author:
Mode:
Body: