【问题标题】:Decimal to hexadecimal conversion program not working?? Don't know why?十进制到十六进制的转换程序不起作用??不知道为什么?
【发布时间】:2021-10-09 15:04:56
【问题描述】:

下面是十进制到十六进制转换的c++程序!!

// decimal to hexadecimal conversion
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
string d2h(int n) {
  string s, t;
  int i = 0;
  while (n >= 1) {
    t[i] = n % 16;
    switch (t[i]) {
      case 10:
        t[i] = 'A';
        break;
      case 11:
        t[i] = 'B';
        break;
      case 12:
        t[i] = 'C';
        break;
      case 13:
        t[i] = 'D';
        break;
      case 14:
        t[i] = 'E';
        break;
      case 15:
        t[i] = 'F';
        break;
      default:
        break;
    }
    s[i] = t[i];
    n /= 16;
    i++;
  }
  return s;
}
int main() {
  int n;
  cin >> n;
  cout << d2h(n);
  return 0;
}

我没有得到任何输出!!

PS C:\Users\anmol\Desktop\c++projectwork> g++ .\dec2hexadec.cpp
PS C:\Users\anmol\Desktop\c++projectwork> ./a
479
PS C:\Users\anmol\Desktop\c++projectwork>

我应该改变什么??

【问题讨论】:

  • 您的字符串 t 为空,如果不先增加其大小,您将无法使用 t[i] = n % 16; 或任何其他 t[i] = 语句。
  • 由于t 是一个空字符串,t[i] 通过访问超出范围的索引表现出未定义的行为,对于i 的任何值

标签: c++ visual-studio


【解决方案1】:
  1. 您没有在s 或t 中为内存分配空间。使用s.push_back() 而不是s[i]。

  2. 您可能还需要反转字符串。

  3. t[i] = n % 16; 这不会分配字符'0',而是分配数字0。为什么不在你的 switch 语句中创建 16 个案例?或者使用 ASCII 怪癖 '0' + (n % 16)。

【讨论】:

  • 我无法在评论中发布代码,所以我在我的答案中发布了代码。正如比尔所说,这是一个可行的解决方案。我还做了一些修改,让程序在 n 为 0 或负数时成功运行。
【解决方案2】:
// decimal to hexadecimal conversion
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

string d2h(int n) {
    string s, t;
    string Sign;
    int i = 0;
    // n is equal to 0? <0 or >0?
    if (n == 0){
        
        return "0";
    }
    if (n < 0){
        Sign = "-";
        n *= -1;
    }
    
    while (n ) {
        t.push_back(n % 16) ;
        switch (t[i]) {
        case 0:
            t[i] = '0';
            break;
        case 1:
            t[i] = '1';
            break;
        case 2:
            t[i] = '2';
            break;
        case 3:
            t[i] = '3';
            break;
        case 4:
            t[i] = '4';
            break;
        case 5:
            t[i] = '5';
            break;
        case 6:
            t[i] = '6';
            break;
        case 7:
            t[i] = '7';
            break;
        case 8:
            t[i] = '8';
            break;
        case 9:
            t[i] = '9';
            break;
        case 10:
            t[i] = 'A';
            break;
        case 11:
            t[i] = 'B';
            break;
        case 12:
            t[i] = 'C';
            break;
        case 13:
            t[i] = 'D';
            break;
        case 14:
            t[i] = 'E';
            break;
        case 15:
            t[i] = 'F';
            break;
        default:
            break;
        }
        s.push_back(t[i]);
      
        n /= 16;
        i++;
    }
    reverse(s.begin(), s.end());
    return Sign+s;
}
int main() {
    int n;
    cin >> n;
    cout << d2h(n);
    return 0;
}

【讨论】:

  • 虽然您的代码可以工作并解决问题,但最好添加一些上下文和解释,例如必须更改的内容和原因。
猜你喜欢
  • 2018-02-23
  • 1970-01-01
  • 2014-01-09
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 2017-07-19
  • 1970-01-01
相关资源
最近更新 更多