Code:
// Complier VC6.0
#include <time.h>
#include <string.h>
#include <stdio.h>
/*
An xary number is a number which has the base x
Eg Binary numbers, x = 2
xary numbers can have any base and any lenght
*/
#include <memory.h>
class xary_number
{
private:
bool xary_number_is_zero;
bool xary_number_is_max;
bool xary_number_is_init;
int max_digit_val; //eg for decimal number, max digit val is 9
int * digit; //All digits in the binary number, eg decimal 3457224024
int xary; //The base of the number eg binary, octal, etc for now only boolean
int lengthneeded; //The number of digits in the number, eg 256 digit hexal, 11 digit hexal
public:
xary_number(int x = 2, int l = 256)
{
xary_number_is_init = false;
init_xary_number(x, l);
};
~xary_number()
{
delete [] digit;
};
void init_xary_number(int x = 2, int l = 256)
{
//Initialize number
xary = x;
lengthneeded = l;
max_digit_val = x - 1;//1 for binary
//create the space for our digits
if(xary_number_is_init)
delete [] digit;
digit = new int[lengthneeded];
//Zero the digits
memset(digit, 0x00, lengthneeded*sizeof(int));
//Initialize some miscellaneous states
xary_number_is_zero = true;
xary_number_is_max = false;
xary_number_is_init = true;
}
bool goto_next_selection() //follows the pattern 000, 001, 002, 003, 011, 012, 013, 022, 023, 033, 111, 112, 113, 122, 123, 133, 222, 223, 233, 333 for a 4ary number
{
for(int i = 0; i < lengthneeded; i++)
{
if(digit[i] == max_digit_val)
{
digit[i] = 0;
continue;
}
else
{
digit[i]++;
//all previous digit equal this digit
for(; i > 0; i--)
{
digit[i-1] = digit[i];
}
break;
}
}
//Sets the miscellaneous state to zero if the number overflows to zero
set_if_zero();
//Returns the miscellaneous state
return is_zero();
}
//Sets xary_number_is_zero to zero if the number has all digits at zero.
void set_if_zero()
{
for(int i = 0; i < lengthneeded; i++)
{
if(digit[i] != 0)
{
xary_number_is_zero = false;
return;
}
}
xary_number_is_zero = true;
return;
}
bool is_zero() const
{
return xary_number_is_zero;
}
//Get a display string of the xary number
//If display charcters are given they\n will replace the digit characters
//The lenght of the display string should be 1 greater than the lenght of the xary_number
//The number of display characters should be the same as the base of the xary number
void get_display_string(char * display_string, const char * display_characters) const
{
for(int i = 0; i < lengthneeded; i++)
{
//Print the highest digit in the first position of the string
display_string[i] = display_characters[digit[lengthneeded-i-1]];
}
display_string[i] = 0x00;
}
};
int main()
{
//Time declarations
time_t start, finish;
double elapsed_time;
//Input declarations
FILE * stream;
stream = fopen( "input.txt", "r" );
if( stream == NULL )
{
printf("Error opening file");
fclose(stream);
return 0;
}
//Getting the first number
int N;
fscanf(stream, "%d", &N);
//Getting the test string
char S[129];
fscanf(stream, "%s", &S);
fclose(stream);
time( &start );
//code start here
const char display_chs[] = "ABCDE";
xary_number sel;
int selcount = 0;
sel.init_xary_number(strlen(display_chs), N);
char display_str[256];
while(true)
{
//selcount is the position counter
selcount++;
//sel has the state of all the counters, and get_display_string prints its state in text form, eg 0012 to AABC
sel.get_display_string(display_str, display_chs);
//display str is the text format of the counters; checking it with the base string
if(!(strcmp(display_str, S)))
break;
//go from AAA to AAB...AAE to ABB
if(sel.goto_next_selection())
break;
}
printf("selcount = %d", selcount);
//code end here
time( &finish );
elapsed_time = difftime( finish, start );
printf( "\nProgram takes %6.0f seconds.\n", elapsed_time );
return 1;
}