0

我认为我走在正确的轨道上并且拥有我需要的所有元素,但我不太确定如何使用类/令牌并且可能还有其他一些格式错误的东西。

 #include <string>
 #include <iostream>
 #include <vector>


 using namespace std;

 class token {
 public:
            int value;
            string unit;

            }


 int main() {

 token t;
 vector<token> v;
 string unit = ""

 cin>>x;
 while (x!=0) {
    t.value=x%10;
    if (unit==" "}
        t.unit = "ones";
    else if (unit == "ones")
        t.unit = "tens"
    else if (unit = "tens")
        t.unit = "hundreds"
    else if (unit = "hundreds")
        t.unit = "thousands"

    v.pushback(t);
    x=x/10;
 }
  v_t.push_back("zero")
  v_t.push_back("one")
  v_t.push_back("two")
  v_t.push_back("three")
  v_t.push_back("four")
  v_t.push_back("five")
  v_t.push_back("six")
  v_t.push_back("seven")
  v_t.push_back("eight")
  v_t.push_back("nine")

  cout<< "This is ";
  for(int i = v.size()-1; i>=0, i--) {
        cout<<v_t[v[i].value]<<" "<< v[i].unit << " "}


 }

我在这里得到的所有东西都是从我的笔记中提取的,但排列顺序不同。当我尝试运行它时,我收到错误消息:“新类型可能未在新类型中定义”

4

3 回答 3

2

有很多编译错误,照顾第一个,把分号放在类的末尾:

class token {
 public:
            int value;
            string unit;

            };

对于第二个,在单元声明的末尾添加一个分号:

string unit = "";

第三个,定义“x”:

int x;

第四,在此处将 '}' 更改为 ')':

if (unit==" ")

还有很多,抱歉。在所有语句的末尾添加分号以开始。

于 2012-10-13T23:40:58.413 回答
0

在这个作业中,我不会使用 a std::vector,而是使用一个固定长度的数组。

用 C 语言术语(显示思想)

struct Text_Entry
{
    unsigned int value; // Don't deal with negatives with words.
    const char * const text;
};

// Here's the table
struct Text_Entry  conversion_table[] = 
{
    {0, "zero"},
    {1, "one"},
    {2, "two"},
//...
    {10, "ten"},
    {11, "eleven"},
//...
    {20, "twenty"},
    {30, "thirty"},
    {40, "forty"},
};

编译器将在您的程序启动之前为您加载表格,从而无需push_back为每种情况使用。该value字段允许您以任何顺序排列条目。

如果允许,请选择std::map.

不要将表格用于每种组合。例如 21 将使用 20 的条目和 1 的条目。对于 135 也是如此。

HTH。

于 2012-10-14T05:17:33.157 回答
0

是这里打错了还是你忘记了所有的分号?除了你写unit = "tens"的比较unit"tens"?不应该unit == "tens"吗?并检查空字符串替换if( unit = " " )if( unit.empty() )

于 2012-10-14T00:27:52.367 回答