读一个数字,给我个字! [英] Read a number, show me a word!

查看:82
本文介绍了读一个数字,给我个字!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!!!
好吧,我想用C编写一个程序来做到这一点:

我将键入1,它将在屏幕上显示为"1".
如果我只有少量变量,可以这样做:

Hello!!!
Well, I want to make a programme in C which will do this:

I will type 1 and it will appear "one" on the screen.
If I had a small number of variables I could do it like this:

switch (x)

{
  case 0:printf("Zero.\n");
         break;
  case 1:printf("One.\n"); etc...
         break;
}

但是在我的程序中,我想用数字0到99来做到这一点! 我不认为我应该在转换后写一百个案例!(在清单中,我希望如此...)

先感谢您!! :-)

But in my programme i wanna do this with the numbers 0 to 99!!
I don''t think that I should write a hundred cases after the switch!(at list I hope so...)

Thank you in advance!! :-)

推荐答案

这很简单.
如果只需要[0-99],则只需要下面的GetHundredsWords,但是此代码将打印您可以放入整数的任何数字.

对于每1000个订单,编号都是相同的,例如:123是one hundred twenty three,而123,000是one hundred twenty three thousand,因此我们只需要可以增加到1000(GetHundredsWords())的代码,然后对每1000个数量级,加上正确的量级(GetWord())

This is quite simple.
If all you want is [0-99] then all you need is the GetHundredsWords below, but this code will print any number you can put in an integer.

For every order of 1000, the numbering is the same, for example: 123 is one hundred twenty three and 123,000 is one hundred twenty three thousand, so we only really need code which can go up to 1000 (GetHundredsWords()), then just do that for every order of 1000, adding in the correct magnitude (GetWord())

#include <stdio.h>

static size_t GetHundredsWords(char *buffer, int n, bool &bAppended) {
	//Shorthand to make sure spaces are added when needed
	#define APPEND(fmt, ...) \
		if (bAppended) { \
			*ptr++ = ' '; \
		} else { \
			bAppended = true; \
		} \
		ptr += sprintf(ptr, fmt, __VA_ARGS__);

	const char *szOnesWords[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
	const char *szTeenWords[] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
	const char *szTensWords[] = { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
	char *ptr = buffer;
	if (n >= 100) {
		APPEND("%s hundred", szOnesWords[n / 100 - 1]);
		n %= 100;
	}
	if (n >= 20) {
		APPEND(szTensWords[n / 10 - 2]);
		n %= 10;
	}
	if (n >= 10) {
		APPEND(szTeenWords[n - 10]);
	} else if (n > 0) {
		APPEND(szOnesWords[n - 1]);
	}
	return ptr - buffer;
}

char *GetWord(char *buffer, int n) {
	const char *szMagnitudes[] = { " billion", " million", " thousand", "" };
	char *ptr = buffer;
	if (n == 0) {
		return "zero"; //Special case
	}
	if (n < 0) { //Is this a negative number
		ptr += sprintf(ptr, "negative ");
		n *= -1;
	}
	bool bAppended = false;
	int nMagnitude = 1000 * 1000 * 1000; //1 billion
	for (int i = 0; nMagnitude > 0; ++i) {
		if (n >= nMagnitude) {
			ptr += GetHundredsWords(ptr, n / nMagnitude, bAppended);
			ptr += sprintf(ptr, szMagnitudes[i]);
			n %= nMagnitude;
		}
		nMagnitude /= 1000;
	}
	//GetHundredsWords(ptr, n, bAppended, bAnd);
	return buffer;
}

#define TEST(n) printf("%d is \'%s\'\n", n, GetWord(buffer, n)) //Save having to type this for every test

int main(int argc, char *argv[]) {
	char buffer[64];
	TEST(0);
	TEST(1);
	TEST(5);
	TEST(9);
	TEST(10);
	TEST(11);
	TEST(12);
	TEST(19);
	TEST(20);
	TEST(31);
	TEST(42);
	TEST(100);
	TEST(101);
	TEST(109);
	TEST(110);
	TEST(115);
	TEST(120);
	TEST(151);
	TEST(546);
	TEST(1000);
	TEST(1001);
	TEST(1024);
	TEST(1564);
	TEST(1000000);
	TEST(1000000000);
	TEST(1561981328);
	TEST(-2147483647);
	return 0;
}


您可能希望将其细分为多个范围,例如20到29之间的任何内容都将以二十"开头,因此如下所示:
You''ll probably want to break it down into ranges, for example anything between 20 and 29 will start with "twenty", so something like:
if (number >= 20 && number <= 29)
{
    printf("Twenty");
}



当然,您需要30、40等类似的东西

然后,您需要在此之后根据最后一位数字进行打印,可以通过几种不同的方式进行操作,例如模运算符,或者如果要分解成这样的范围,则可以使用减法(例如,如果它在20英寸范围内,则减去二十,如果最后一位数字,21变为1、22变为2等,您将剩下的全部.)因此,在打印第一个单词之后(如果有一个),然后可以打印剩余的单词(使用最后一部分的开关即可.)

不要忘记特殊情况,例如,如果仅是0,则希望它打印零",但如果是20,则不希望二十零"!



Of course, you''d need something similar for 30, 40, and so on

Then you''ll want to print something after that depending on the last digit, there are a few different ways to do that, like the modulus operator, or if you''re breaking down into ranges like this you can use subtraction (e.g. if its in the range of 20''s, subtract twenty and all you''ll have left if the last digit, 21 goes to 1, 22 goes to 2, etc.) So after printing the first word (if there is one) you can then print the remaining word (using a switch for this last part could work).

Don''t forget special cases, for example if it''s just 0 you''d want it to print "Zero" but if it''s 20 you wouldn''t want "Twenty Zero"!


从不使用仅包含少数几种情况的开关.在您的样本中,即使是两个案例也将构成完全滥用.

您只需要一个函数就可以接受一定范围内的整数并返回一个字符串.如果值超出允许范围,则返回例如null.请执行以下操作:创建一个字符串数组0到99.该函数应首先检查输入值是否在范围内,并返回空值(表示某些输入无效).如果该值在有效范围内,则按其索引返回数组元素.

就这么简单.

—SA
Never ever use a switch which contains just a few cases. And in your sample, even two cases would be a total abuse.

You need just one function which accepts an integer in certain range and returns a string. If a value is outside the allowed range, return, for example, null. Do the following: create an array of strings 0 to 99. The function should first check if input value is in the range and return null of something to indicate invalid input. If the value is in the valid range, return the array element by its index.

Simple as that.

—SA


这篇关于读一个数字,给我个字!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆