【问题标题】:Having issues converting numbers into roman numerals将数字转换为罗马数字时遇到问题
【发布时间】:2017-11-27 00:59:34
【问题描述】:

对于类,我必须创建一个代码,以便它可以向用户询问 1-99 之间的整数,并能够以罗马数字打印该整数。问题是在创建我的代码后,它只会打印完全到 39 的数字。一旦达到 40,它就不会输出罗马数字,然后从 41-99 它将不会打印第十位的值(例如 45 将显示为 V) .这是我目前的代码。

    #include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int num;
    int tenum;
    int remnum;
    string romnum;
    string fromnum;

    cout << "Enter a number between 1 and 99. \n";
    cin >> num;

    tenum = num / 10;
    remnum = num % 10;

    if (tenum == 9)
    {
        fromnum == "XC";
    }
    else if (tenum == 8)
    {
        fromnum == "LXXX";
    }
    else if (tenum == 7)
    {
        fromnum == "LXX";
    }
    else if (tenum == 6)
    {
        fromnum == "LX";
    }
    else if (tenum == 5)
    {
        fromnum == "L";
    }
    else if (tenum == 4)
    {
        fromnum == "XL";
    }
    else if (tenum == 3)
    {
        fromnum = "XXX";
    }
    else if (tenum == 2)
    {
        fromnum == "XX";
    }
    else if (tenum == 1)
    {
        fromnum == "X";
    }

    if (remnum == 9)
    {
        romnum = "IX";
    }
    else if (remnum == 8)
    {
        romnum = "VIII";
    }
    else if (remnum == 7)
    {
        romnum = "VII";
    }
    else if (remnum == 6)
    {
        romnum = "VI";
    }
    else if (remnum == 5)
    {
        romnum = "V";
    }
    else if (remnum == 4)
    {
        romnum = "IV";
    }
    else if (remnum == 3)
    {
        romnum = "III";
    }
    else if (remnum == 2)
    {
        romnum = "II";
    }
    else if (remnum == 1)
    {
        romnum = "I";
    }



    cout << tenum << endl;
    cout << remnum << endl;
    cout << fromnum;
    cout << romnum << endl;

    return 0;


}

【问题讨论】:

  • 欢迎来到 Stack Overflow!听起来您可能需要学习如何使用调试器来逐步执行代码。使用好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏差在哪里。如果您要进行任何编程,这是必不可少的工具。延伸阅读:How to debug small programs
  • 你的课程是否涵盖了循环和switch
  • vtc 因为这是一个小错误。
  • 帮助自己。把它分解成一个函数。实际编程是练习的重点。

标签: c++ converter roman-numerals


【解决方案1】:

你把===搞混了

换行

fromnum == "XL"

fromnum = "XL"

Demo

(为简洁起见,我使用了switch 声明)

【讨论】:

  • 告诉 OP 打开 /W4 会更好(我认为他在 Windows 上,或者 -wall 如果不是),编译器会指出问题所在。
  • 非常感谢!我不知道为什么我之前没有注意到这一点:)我的代码现在运行完美!
【解决方案2】:

同样的答案:Converting integer to Roman Numeral

#include <iostream>
#include <string>

std::string to_roman(int value)
{
  struct romandata_t { int value; char const* numeral; };
  static romandata_t const romandata[] =
     { 1000, "M",
        900, "CM",
        500, "D",
        400, "CD",
        100, "C",
         90, "XC",
         50, "L",
         40, "XL",
         10, "X",
          9, "IX",
          5, "V",
          4, "IV",
          1, "I",
          0, NULL }; // end marker

  std::string result;
  for (romandata_t const* current = romandata; current->value > 0; ++current)
  {
    while (value >= current->value)
    {
      result += current->numeral;
      value  -= current->value;
    }
  }
  return result;
}

int main()
{
  for (int i = 1; i <= 4000; ++i)
  {
    std::cout << to_roman(i) << std::endl;
  }
}

此代码最多可转换为 1000。只需拿走你需要的东西,你就可以开始了! 来自http://rosettacode.org/wiki/Roman_numerals/Encode#C.2B.2B

【讨论】:

    猜你喜欢
    • 2012-10-27
    • 2019-01-09
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    相关资源
    最近更新 更多