| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

已结贴   问题点数：8  回复次数：7

#include<iostream.h>
class translate
{
public:
translate()
{
string1[9][9]={"one","two","three","four","five","six","seven","eight","nine","ten"};
string2[9][9]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
string3[9][10]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninteen"};
}

int num;
void display();
friend ostream& operator<<(ostream&output,translate &c);
private:
char string1[9][9];
char string2[9][9];
char string3[9][10];
};
。cpp文件
#include<iostream.h>
#include"translate.h"
ostream& operator<<(ostream&output,translate &a)
{
int c=a.num;
if(c<11)
output<<string1[c-1]<<endl;
if(c>10&&c<20)
output<<string3[c-11];
if(c>20&&c%10!=0)
output<<string2[c/10-2]<<' '<<string1[c%10-1]<<endl;
if(c>=20&&c%10==0)
output<<string2[c%10-1];
return output;
}
void translate::display()
{
cout<<"请输入一个0到99的数："<<endl;
cin>>num;

}
main函数
#include<iostream.h>
#include"translate.h"
void main()
{
translate c;
c.display();
cout<<c;

}

得分:0

得分:4

---------------------------------------------
string1[9][9]={"one","two","three","four","five","six","seven","eight","nine","ten"};
string1 是一个char[9][9]
string1[9] 是一个char[9] 并且越界了
string1[9][9] 是一个char 并且越界了
char 不能用 = {.............} 进行赋值操作
得分:0

char应该怎么赋值呢？
得分:0

得分:0

得分:4

string1[9][9]={……，很明显数组溢出了，而且既非初始化，又非赋值，看不懂
void display(); 这什么玩意儿
ostream& operator<<(ostream&output,translate &c); 为什么必须要求c是可修改的
if(c<11)…… 逻辑没看懂，难道不是 if( c<20 ) 吗
void translate::display() { cout<<"请输入一个0到99的数："<<endl; 怎么display又变成了输入？
void main() 就不说了

#include <string>
#include <cassert>

std::string foo( unsigned num )
{
static const char* less20[]  = { "zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninteen" };
static const char* tenfold[] = { "twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety" };
assert( num < 100 );
if( num < 20 )
return less20[num];
if( num%10 == 0 )
return tenfold[num/10-2];
return tenfold[num/10-2] + std::string(" ") + less20[num%10];
}

#include <iostream>
using namespace std;

int main( void )
{
cout<<"请输入一个0到99的数："<<endl;
unsigned num;
cin >> num;
cout << foo(num) << endl;
}

得分:0

• 8
• 1/1页
• 1