C++ Tlapohualli

This C# program allows you to convert integer numbers (base 10) to their Nahuatl translation (which is base 20).

Feel free to use it and test it. It is quite fun.

 

#include "stdafx.h"

 

#define MAX_DIVIDER  160000
#define MAX_NUMBER   (MAX_DIVIDER * 20) - 1

 

void printNumberName(int number, bool wasZero)
{
    switch(number)
    {
        case 160000:
            printf("xochiti ");
            break;
        case 8000:
            printf("xiquipilli ");
            break;
        case 400:
            printf("cenzontli ");
            break;
        case 20:
            printf("cempohualli ");
            break;
        case 15:
            printf("caxtollin ");
            break;
        case 10:
            printf("mahtlahtli ");
            break;
        case 9:
            printf("chicnahui ");
            break;
        case 8:
            printf("chicuei ");
            break;
        case 7:
            printf("chicome ");
            break;
        case 6:
            printf("chicuace ");
            break;
        case 5:
            printf("macuilli ");
            break;
        case 4:
            printf("nahui ");
            break;
        case 3:
            printf("yei ");
            break;
        case 2:
            printf("ome ");
            break;
        case 1:
            printf("ce ");
            break;
        case 0:
            if (wasZero)
            {
                printf("amitla ");
            }
            break;
        default:
            break;
    }
}

 

void separate(int number, int divider, bool wasZero)
{
    int multiples;
    int residue;

 

    // Base case for all number between 20 - 3,199,999
    if (number >= 20 && number <= MAX_NUMBER)
    {
        multiples = number / divider;
        residue = number % divider;
        
        // avoid prepending the multiplier "ce <number>"
        if (multiples > 1)
        {
            separate(multiples, divider / 20, wasZero);
        }
        // Print the actual number if it is a multiple of the divider
        if (multiples > 0)
        {
            printNumberName(divider, wasZero);
            // Only print huan if the current multiplier could be printed and there is a residue
            if (residue > 0)
            {
                printf("huan ");
            }
        }
        // Process the rest of the number
        if (residue > 0)
        {
            separate(residue, divider / 20, wasZero);
        }

 

    }
    // Special case for 15 - 19
    else if (number >= 15)
    {
        residue = number % 15;
        printNumberName(15, wasZero);
        if (residue > 0)
        {
            printNumberName(residue, wasZero);
        }
    }
    // Special case for 10 - 14
    else if (number >= 10)
    {
        residue = number % 10;
        printNumberName(10, wasZero);
        if (residue > 0)
        {
            printNumberName(residue, wasZero);
        }
    }
    // 0 - 9 can be directly printed
    else if (number < 10)
    {
        printNumberName(number, wasZero);
    }
    
}

 

int main()
{
    int number = -1;

 

    printf("-- TLAPOHUALLI --\n");
    printf("-----------------\n");
    printf("Write a positive integer [0, n, 3,200,000) to get its Nahuatl name. Type a negative number to exit.\n");

 

    do
    {
        printf("\nNumber: ");
        scanf("%d", &number);
        if (number >= 0 && number <= MAX_NUMBER)
        {
            separate(number, MAX_DIVIDER, number == 0);
        }
    }
    while (number >= 0);

 

    return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: