Skip to content

Instantly share code, notes, and snippets.

@Yengas
Created January 2, 2016 14:36
Show Gist options
  • Select an option

  • Save Yengas/178576e69a9c69eb2ad6 to your computer and use it in GitHub Desktop.

Select an option

Save Yengas/178576e69a9c69eb2ad6 to your computer and use it in GitHub Desktop.
Integer to Turkish Text Representation
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
const char* postnames[7] = {"Yüz", "Bin", "Milyon", "Milyar"};
const char* prenames[2][9] = {{"Bir", "İki", "Üç", "Dört", "Beş", "Altı", "Yedi", "Sekiz", "Dokuz"},
{"On", "Yirmi", "Otuz", "Kırk", "Elli", "Atmış", "Yetmiş", "Seksen", "Doksan"}};
typedef struct _NODE{
const char* text;
struct _NODE* next;
} NODE;
void add_reverse(NODE** head, const char* text){
NODE* current = (NODE*) malloc(sizeof(NODE));
current->text = text;
current->next = *head;
*head = current;
}
void remove_node(NODE** head){
NODE* ret = (*head)->next;
free(*head);
*head = ret;
}
void print(NODE* head){
while(head != 0){
printf("%s ", head->text);
head = head->next;
}
printf("\n");
}
void print_number(int number){
int step = 0;
NODE* head = 0;
if(number < 0){ printf("Eksi "); number *= -1; }
while(number > 0){
int digit = number % 10;
if(step % 3 == 0 && step > 0 && ((digit == 0 && (number / 10) % 100 > 0) || digit > 0))
add_reverse(&head, postnames[0 + step / 3]);
if(step % 3 == 2 && digit > 0)
add_reverse(&head, postnames[step % 3 - 2]);
if(digit > 0 && (step % 3 <= 1 || digit > 1))
add_reverse(&head, prenames[step % 3 == 1 ? 1 : 0][digit - 1]);
number /= 10;
step += 1;
}
if(step == 4 && head->text == prenames[0][0]) remove_node(&head);
print(head);
}
int main(){
setlocale(LC_ALL, "Turkish");
int sayi;
while(1){
scanf("%d", &sayi);
print_number(sayi);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment