Witam!
Mam za zadanie napisać program w języku C, który po podaniu słownego zapisu liczby ma ją wyświetlić jako int.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include <string.h>
int convert(char *word)
{
printf("Word: %s\n", word);
if(strcmp(word, "zero"))return 0;
if(strcmp(word, "one"))return 1;
if(strcmp(word, "two"))return 2;
if(strcmp(word, "three"))return 3;
if(strcmp(word, "four"))return 4;
if(strcmp(word, "five"))return 5;
if(strcmp(word, "six") == 0)return 6;
if(strcmp(word, "seven"))return 7;
if(strcmp(word, "eight"))return 8;
if(strcmp(word, "nine"))return 9;
if(strcmp(word, "ten"))return 10;
if(strcmp(word, "eleven"))return 11;
if(strcmp(word, "twelve"))return 12;
if(strcmp(word, "thirteen"))return 13;
if(strcmp(word, "fourteen"))return 14;
if(strcmp(word, "fifteen"))return 15;
if(strcmp(word, "sixteen"))return 16;
if(strcmp(word, "seventeen"))return 17;
if(strcmp(word, "eighteen"))return 18;
if(strcmp(word, "nineteen"))return 19;
if(strcmp(word, "twenty"))return 20;
if(strcmp(word, "thirty"))return 30;
if(strcmp(word, "forty"))return 40;
if(strcmp(word, "fifty"))return 50;
if(strcmp(word, "sixty"))return 60;
if(strcmp(word, "seventy"))return 70;
if(strcmp(word, "eighty"))return 80;
if(strcmp(word, "ninety"))return 90;
}
int main()
{
char line[256];
fgets(line, sizeof line, stdin);
int negative = 0;
int num = 0, total = 0;
char *p = strtok(line, " ");
while (p != NULL)
{
printf("P: %s\n", p);
if (strcmp(p,"negative"))
{
negative = 1;
}
else if(strcmp(p,"million"))
{
printf("1000000\n");
total += num * 1000000;
num = 0;
}
else if(strcmp(p,"thousand"))
{
total += num * 1000;
num = 0;
}
else if(strcmp(p,"hundred"))
{
printf("100");
num = num * 100;
}
else
{
num += convert(p);
}
p = strtok(NULL, " ");
}
total += num;
if (negative == 1)
total = (-1)*total;
printf("Total = %d", total);
return 0;
}
Kod niestety nie daje dobrego wyniku, w ogóle nie wchodzi do else gdzie jest num += convert(p)
Poza pętlą while gdy napiszę np printf("%d", convert("six")) wchodzi do funkcji, ale nie zwraca żądanej 6.