我需要帮助,在此程序中,我想将数字转换为单词 [英] i need a help , in this program i want to convert a number to word

查看:80
本文介绍了我需要帮助,在此程序中,我想将数字转换为单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// help num2word.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
using namespace std;

     char one[10][10] = {"One", "Two","Three","Four","Five","Six","Seven","Eight","Nine"};
     char ten[15][15] = {"Ten","Eleven","Tweleve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
     char tens[15][15] = {"Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};

int main()
{
	int num , n , r , c = 0 ;
	cout << "please enter the number :" << endl ;
	cin >> num ;

	n = num ;
	while( n )
	{
		r = n % 10 ;
		n = n / 10 ;
		c++ ;
	}

	switch(c)
	{
	case 1:
	  if( num >0 && num < 10 )
	   {
           cout << one[num-1];
	     }else{
		   cout<< "ZERO ";
	    }
		  break;
	   
	case 2:
		/*if( num > 10 && num < 20 )
			cout<<"yay"<<ten[num-10];
		if( num > 20 && num < 100 )
			cout << tens[num/10-2] ;
			*/
		
		  ( num > 10 && num < 20 ) ? (cout << ten[num-10]) :( cout << tens[num/10-1] && ((num%10)?one(num%10):1));
		break;
		
	
	}

	return 0;
}

</iostream>




我的程序从0到19的数字是正确的,但是它不能写55-> 55.
只写五十,我不知道该怎么办?
和ia在((cout< <十[num-10]):( cout< 但我不知道为什么?




my program for numbers from 0 till 19 is true but it cant write 55 ->fifty five
and just write fifty , i dont know what should i do ?
and ia have an error in ((cout << ten[num-10]) :( cout << tens[num/10-1] && ((num%10)?one(num%10):1));)
but i dont know why ?

推荐答案

我要在这里指点您: ^ ],但您已经付出了努力...:laugh:

您需要重新考虑您的操作方式,并摆脱特殊情况.
如果仅使用正两位数整数(因为您没有任何一百个字符串),那么您需要查看以下三种情况:
1)num< = 9
2)10< = num< = 19
3)20< = num

我不会给你代码(嘿!这是你的功课!),但是一般的原则是:

情况1:打印数字编号(零",一个",两个"
情况2:打印青少年编号(十,十一,十二)这些必须是特殊情况,因为它们是英语中的特殊情况
否则:打印十进制数字(二十",三十"等),如果数字非零,则打印-",后跟数字数字.

因此,我将从将数字分解为数字开始,然后从那里开始工作!
I was going to point you here: Converting numbers to the word equivalent. [^] but since you have made an effort... :laugh:

You need to re-think the way you do this a bit, and get rid of the special cases.
If you are only working with positive two digit integers (as you are since you don''t have any hundred strings), then there are three cases you need to look at:
1) num <= 9
2) 10 <= num <= 19
3) 20 <= num

I''m not going to give you the code (hey! It''s your homework!), but the general principle is:

case 1: print digit number ("zero", "one", "two"
case 2: print teen number (ten, eleven, twelve) These have to be a special case because they are a special case in English
otherwise: print tens number ("twenty", "thirty", etc) then if digit is non-zero print "-" followed by the digit number.

So, I would start by breaking the number into its digits, and work from there!


这篇关于我需要帮助,在此程序中,我想将数字转换为单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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