将字符作为字母打印出来而不将其转换为数值

Printing out chars as letters without turning them into numerical values?

本文关键字:转换 字符 打印      更新时间:2023-10-16

我的问题很简单,但我不知道如何解决。

我有一个得到随机数的整数变量。但是在特殊情况下,这个整数表示字符。例如,如果rand()函数给出100,则变量变为A。我使用switch-case来管理这些异常。然后我执行程序,rand()100一个变量。但是,程序没有打印出A char,而是给出了它的ASCII值65。我考虑过打开另一个print函数,并为这些异常实现类型转换。但是它们有很多,而且,我在程序中得到了很多随机数,所以几乎不可能让它发生。

// Program gets random values for a lot of variables (`rand_val1`, `rand_val2`, `rand_val3`...)
// For some integers, it converts them to pre-defined chars
char A = 'A', char B = 'B' ...
...
switch (rand_val1)
   case 100:
   rand_val1 = A;
   break;
   case 200:
   rand_val2 = B;
   break;
   ...
switch (rand_val2)
   ...
// It prints out each one of them.
cout << rand_val1 << " " << rand_val2 << ... << endl;
/* As output, it doesn't give chars and instead it gives their ASCII values
>>> 65 66 300 400 500 70 71 ...

在这种情况下我能做什么?什么好主意吗?

定义一个"例外列表"怎么样?下面是一个例子

#include <bits/stdc++.h>
using namespace std;
int main ()
{
    const unordered_map<int, char> exceptionList = {
        { 100, 'A'},
        { 200, 'B'},
        { 300, 'C'},
        { 400, 'D'},
        { 500, 'E'}
    };
    int rand_val1 = 100;
    if (exceptionList.find(rand_val1) == exceptionList.end())
    {
        cout << rand_val1 << endl;
    }
    else
    {
        cout << exceptionList.at(rand_val1) << endl;
    }
    return 0;
}

或带lambda:

#include <bits/stdc++.h>
using namespace std;
int main ()
{
    const std::unordered_map<int, char> exceptionList = {
        { 100, 'A'},
        { 200, 'B'},
        { 300, 'C'},
        { 400, 'D'},
        { 500, 'E'}
    };
    int rand_val1 = 100;
    int rand_val2 = 101;
    const auto charOrInt = [&exceptionList] (const int val) -> string {
        if (exceptionList.find(val) == exceptionList.end())
        {
            return to_string(val);
        }
        else
        {
            return string{exceptionList.at(val)};
        }
    };
    cout << charOrInt(rand_val1) << " " << charOrInt(rand_val2) << endl;
    return 0;
}

因为你说有很多这样的转换,所以我认为没有任何function/std::cout类型转换适合你。您可以在这里使用包装器类。只需将所有的rand_valXXX声明为Integer类。

如果每个rand_val都有不同的转换逻辑,那么我认为你没有办法,但每次打印时都要强制转换。

#include <iostream>
class Integer
{
public:
    Integer( int a ) : _a(a){}
    operator int() const { return _a; }
private:
    int _a;
};
std::ostream& operator<< ( std::ostream& out, const Integer& val )
{
    switch( val )
    {
    case 100:
        out << 'A';
        break;
    case 200:
        out << 'B';
        break;
    default:
        out << (int)val;
    }
    return out;
}
int main()
{
    Integer a = 100;
    std::cout << a;
    a = 200;
    std::cout << a;
    a = a + 100;
    std::cout << a;
    a = a / 200;
    std::cout << a;
}

试试这个。这是一个非常简单的If Else条件,但是根据您的需求,它不需要是动态的

#include<iostream>
using namespace std;
char Convert(int input)
{ char ToBeReturn = ' ';
if (input == 100)
{
    ToBeReturn = 'A';
}
if (input == 200)
{
    ToBeReturn = 'B';
}
if (input == 300)
{
    ToBeReturn = 'C';
}
if (input == 400)
{
    ToBeReturn = 'D';
}
return ToBeReturn;
}

void main()

{ int rand_val1, rand_val2, rand_val3;
cout << "Enter Value 1: ";
cin >> rand_val1;
cout << "Enter Value 2: ";
cin >> rand_val2;
cout << "Enter Value 3: ";
cin >> rand_val3;
cout << Convert(rand_val1) << " " << Convert(rand_val2) << " " << Convert(rand_val3) << endl; }