如何用C语言将输入的数字转化成英语
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用C语言将输入的数字转化成英语相关的知识,希望对你有一定的参考价值。
数字为1-10000之间,由于我刚开始学C,我用的一种比较传统的方法比较复杂要写很多语句,所以想请教一下高手有没有什么简单的方法,谢谢!
是将数字转化成英文啊 如;11:ELEVEN,135:ONE HUNDRED FIVE
和你的相比,不知是否复杂。
此程序的计算范围:0<=num<1000。如果还想要计算更大的数,可以在最后面加判断语句,方法类似。
#include<stdio.h>
void main()
char *Eng1[20]="zero","one","two","three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen",
"sixteen","seventeen","eighteen","nineteen";
char *Eng2[8]="twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety";
int num;
printf("请输入数字: ");
scanf("%d",&num);
printf("对应的英文为: ");
if(num>=0&&num<=19)
printf("%s\n",Eng1[num]);
else if(num<100)
int s,y;
s=num/10;
y=num%10;
printf("%s %s\n",Eng2[s-2],Eng1[y]);
else if(num<1000)
int b,s,y;
b=num/100;
y=num%100;
if(y>9)
s=(num%100)/10;
y=(num%100)%10;
if(y==0)
printf("%s hundred and %s\n",Eng1[b],Eng2[s-2]);
else
printf("%s hundred and %s %s\n",Eng1[b],Eng2[s-2],Eng1[y]);
else
printf("%s hundred and %s\n",Eng1[b],Eng1[y]);
参考技术A 只有按照ASCII码进行对应转换,把输入的数字转换为相应的英文字母,没有其他方法了 参考技术B #include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
char a;
while('\n'!=a)
a=getchar();
switch (a)
case '0':printf("zero ");break;
case '1':printf("one ");break;
case '2':printf("two ");break;
case '3':printf("three ");break;
case '4':printf("four ");break;
case '5':printf("five ");break;
case '6':printf("six ");break;
case '7':printf("seven ");break;
case '8':printf("eight ");break;
case '9':printf("nine ");break;
return 0;
参考技术C #include<stdio.h>
char *Eng1[20] = "zero", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen" ;
char *Eng2[8] = "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" ;
int num;
void fun(int num)
if (num >= 0 && num <= 19)
printf("%s\n", Eng1[num]);
else if (num<100)
int s, y;
s = num / 10;
y = num % 10;
printf("%s %s\n", Eng2[s - 2], Eng1[y]);
else if (num<1000)
int b, s, y;
b = num / 100;
y = num % 100;
if (y>9)
s = (num % 100) / 10;
y = (num % 100) % 10;
if (y == 0)
printf("%s hundred %s ", Eng1[b], Eng2[s - 2]);
else
printf("%s hundred %s %s", Eng1[b], Eng2[s - 2], Eng1[y]);
else
printf("%s hundred %s", Eng1[b], Eng1[y]);
void main()
scanf("%d", &num);
if (num > 0)
if (num > 1000)
if (num > 1000000)
if (num > 1000000000)
int numw = num / 1000000000;
num = num % 1000000000;
fun(numw);
printf(" billion ");
int numx = num / 1000000;
num = num % 1000000;
fun(numx);
printf(" million ");
int numy = num / 1000;
num = num % 1000;
fun(numy);
printf(" thousand ");
int numz = num;
fun(num);
根据我的题目中间不用and,要改可以自调,数据范围一直到100billon,在此谢谢mhy8946(不会是清洁工吧。。。同行么)的算法,要不然我也搞不出来hh 参考技术D 没有
如何用C++将数字“1”转换成字符“1”?
参考技术A 1、字符串数字之间的转换\\x0d\\x0a\\x0d\\x0a(1)string --> char *\\x0d\\x0astring str("OK");\\x0d\\x0achar * p = str.c_str();\\x0d\\x0a\\x0d\\x0a(2)char * -->string\\x0d\\x0achar *p = "OK";\\x0d\\x0astring str(p);\\x0d\\x0a\\x0d\\x0a(3)char * -->CString \\x0d\\x0achar *p ="OK";\\x0d\\x0aCString m_Str(p);\\x0d\\x0a//或者\\x0d\\x0aCString m_Str;\\x0d\\x0am_Str.Format("%s",p);\\x0d\\x0a\\x0d\\x0a(4)CString --> char *\\x0d\\x0aCString str("OK");\\x0d\\x0achar * p = str.GetBuffer(0);\\x0d\\x0a...\\x0d\\x0astr.ReleaseBuffer();\\x0d\\x0a\\x0d\\x0a(5)string --> CString \\x0d\\x0aCString.Format("%s", string.c_str()); \\x0d\\x0a\\x0d\\x0a(6)CString --> string\\x0d\\x0astring s(CString.GetBuffer(0)); \\x0d\\x0aGetBuffer()后必然要ReleaseBuffer(),不然就没有开释缓冲区所占的空间,CString对象不克不及动态增长了。\\x0d\\x0a\\x0d\\x0a(7)double/float->CString\\x0d\\x0adouble data;\\x0d\\x0aCString.Format("%.2f",data); //保存2位小数\\x0d\\x0a\\x0d\\x0a(8)CString->double\\x0d\\x0aCString s="123.12";\\x0d\\x0adouble d=atof(s); \\x0d\\x0a\\x0d\\x0a(9)string->double\\x0d\\x0adouble d=atof(s.c_str());\\x0d\\x0a\\x0d\\x0a2、数字转字符串:应用sprintf()函数\\x0d\\x0a\\x0d\\x0achar str[10];\\x0d\\x0aint a=1234321;\\x0d\\x0asprintf(str,"%d",a);\\x0d\\x0a--------------------\\x0d\\x0achar str[10];\\x0d\\x0adouble a=123.321;\\x0d\\x0asprintf(str,"%.3lf",a);\\x0d\\x0a--------------------\\x0d\\x0achar str[10];\\x0d\\x0aint a=175;\\x0d\\x0asprintf(str,"%x",a);//10进制转换成16进制,若是输出大写的字母是sprintf(str,"%X",a)\\x0d\\x0a--------------------\\x0d\\x0achar *itoa(int value, char* string, int radix); \\x0d\\x0a同样也可以将数字转字符串,不过itoa()这个函数是平台相干的(不是标准里的),故在这里不推荐应用这个函数。\\x0d\\x0a\\x0d\\x0a3、字符串转数字:应用sscanf()函数\\x0d\\x0a\\x0d\\x0achar str[]="1234321";\\x0d\\x0aint a;\\x0d\\x0asscanf(str,"%d",&a);\\x0d\\x0a.............\\x0d\\x0achar str[]="123.321";\\x0d\\x0adouble a;\\x0d\\x0asscanf(str,"%lf",&a);\\x0d\\x0a.............\\x0d\\x0achar str[]="AF";\\x0d\\x0aint a;\\x0d\\x0asscanf(str,"%x",&a); //16进制转换成10进制\\x0d\\x0a\\x0d\\x0a别的也可以应用atoi(),atol(),atof().\\x0d\\x0a\\x0d\\x0a4、应用stringstream类\\x0d\\x0a\\x0d\\x0a用ostringstream对象写一个字符串,类似于sprintf() \\x0d\\x0aostringstream s1;\\x0d\\x0aint i = 22;\\x0d\\x0as1 << "Hello " << i << endl;\\x0d\\x0astring s2 = s1.str();\\x0d\\x0acout << s2;\\x0d\\x0a\\x0d\\x0a用istringstream对象读一个字符串,类似于sscanf() \\x0d\\x0aistringstream stream1;\\x0d\\x0astring string1 = "25";\\x0d\\x0astream1.str(string1);\\x0d\\x0aint i;\\x0d\\x0astream1 >> i;\\x0d\\x0acout << i << endl; // displays 25以上是关于如何用C语言将输入的数字转化成英语的主要内容,如果未能解决你的问题,请参考以下文章