frndz plz告诉我将数字转换为单词的逻辑. [英] frndz plz tell me the logic of converting a number to word.
本文介绍了frndz plz告诉我将数字转换为单词的逻辑.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
frndz我有代码,但最多4位数字.我需要超过4位数字.
例子.
56789-五万六千八百八十九.
该代码也可以是6位数字..plz/.......
请帮助我.代码不应过多..
frndz i have code but it is upto 4 digits .i need it more than 4 digits.
example.
56789-fifty six thousand seven hundred and eighty nine.
the code could be fr 6 digits also.. plz/.......
please help me..the code should no exceed a lot..
推荐答案
^ ]
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId = 8383& lngWId = 3 [ Google [
http://www.daniweb.com/software-development/cpp/threads/20788/1457798#post1457798[^]
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=8383&lngWId=3[^]
Google[^] for more.
这里是从此处提取的一种解决方案:
http://www.daniweb.com/software-development/cpp/threads/20788 [ ^ ]
Here is one solution which was extracted from here :
http://www.daniweb.com/software-development/cpp/threads/20788[^]
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *make_words(char *s, int ncomma);
char *insert_comma(long n, int *ncomma);
char *int2words(int n);
int a;
int main(void)
{
printf("Enter number: ");
scanf("%d",&a);
printf("%d = %s\n",a,int2words(a));
getch();
return 0;
}
char *make_words(char *s, int ncomma)
{
int i, len, rest = 0;
char *p = NULL;
static char zzz[256];
static char *ones[] = {"one ","two ","three ","four ",
"five ","six ","seven ","eight ","nine "};
static char *tens[] = {"ten ","eleven ","twelve ","thirteen ",
"fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","nineteen "};
static char *twenties[] = {"","twenty ","thirty ","forty ",
"fifty ","sixty ","seventy ","eighty ","ninety "};
static char *hundreds[] = {
"hundred ","thousand ","million "};
memset(zzz, '\0', 256); // fill with nulls
len = strlen(s);
for(i = 0; i < len; i++)
{
if ((p = strchr((s[i] == ',') ? &s[++i] : &s[i], ',')) == NULL)
{
p = &s[strlen(s)];
}
if (s[i] == '0')
{
continue; // skip one iteration
}
if ((rest = (p - &s[i])) != 0)
{
if (rest == 3)
{
strcat(zzz, ones[s[i] - '0' - 1]);
strcat(zzz, hundreds[0]);
if (len == 7 && s[2] == '0') strcat(zzz, hundreds[1]);
if (len == 11 && s[2] == '0') strcat(zzz, hundreds[2]);
}
else if (rest == 2)
{
if (s[i] == '1')
{
strcat(zzz, tens[s[++i] - '0']);
rest--;
}
else
{
strcat(zzz, twenties[s[i] - '0' - 1]);
}
}
else
strcat(zzz, ones[s[i] - '0' - 1]);
}
if (rest == 1 && ncomma != 0)
{
strcat(zzz, hundreds[ncomma--]);
}
}
return zzz;
}
char *insert_comma(long n, int *ncomma)
{
static char zzz[30];
int i = 0;
char *p = &zzz[sizeof(zzz)-1];
*p = '\0';
*ncomma = 0;
do
{
if (i % 3 == 0 && i != 0)
{
*--p = ',';
++*ncomma;
}
*--p = (char)('0' + n % 10);
n /= 10;
i++;
} while(n != 0);
return p;
}
char *int2words(int n)
{
int nc;
char *ps, *zzz, *minus;
char *buffer;
buffer = (char *) malloc(256);
// save any - sign
if (n < 0)
{
minus = "minus";
n = abs(n);
}
else
{
minus = "";
}
ps = insert_comma(n, &nc);
zzz = make_words(ps, nc);
sprintf(buffer,"%s %s", minus, zzz);
return buffer;
getch();
}
这篇关于frndz plz告诉我将数字转换为单词的逻辑.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文