将字符作为字母打印出来而不将其转换为数值
Printing out chars as letters without turning them into numerical values?
我的问题很简单,但我不知道如何解决。
我有一个得到随机数的整数变量。但是在特殊情况下,这个整数表示字符。例如,如果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; }
相关文章:
- HEX值到wchar_t字符(UTF-8)的转换
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 转换特殊字符(UTF-8)
- 将转换字符键入 int 以用作向量C++的索引
- 将字符随机转换为大写的函数
- 尝试将字符串/字符转换为整数会产生意外结果
- 如何在保持其值的同时将 c++ 无符号字符变量转换为 char 变量
- 转换字符时Arduino DUE 和 Arduino UNO 之间的区别
- 如何在C++中正确重载运算符转换字符*?
- 如何转换数据结构以在 c++ 中转换字符*
- 不兼容的转换字符
- 将转换字符数组[16]键入为int 128位
- 在c++中转换字符编码
- 使用递归转换字符
- 如何在Objective-C/C/C++中转换字符和字节的位置
- C++到Pep/8汇编语言的转换——字符数组
- 转换字符>十六进制