用炭赚一笔钱
Making a sum with char
本文关键字:一笔 更新时间:2023-10-16
#include <iostream>
#include <cstring>
using namespace std;
int test(char[], int);
void decal(char[], int n);
int main()
{
char a[10], b[10], c[10];
int valid;
do {
cout << "Insert first number (maximum 5 chars hexa):" << endl;
cin >> a;
valid = test(a, strlen(a));
if(!valid)
cout << "Error." << endl;
} while (!valid);
cout << "First number: " << a << endl;
decalez(a, strlen(a));
cout << "First number after insert: " << a << endl;
do {
cout << "Insert 2nd number (maximum 5 chars hexa):" << endl;
cin >> b;
valid = test(b, strlen(b));
if(!valid)
cout << "Error." << endl;
} while (!valid);
decalez(b, strlen(b));
cout << "2nd number after insert: " << b << endl;
add(a, b); // Calculating c
cout << "Result: " << c << endl;
return 0;
}
int test(char x[], int n)
{
if(n > 5)
return 0; // Too many numbers
for(int i = 0; i < n; i++)
{
if(x[i] <48 || (x[i] > 57 &&
x[i] < 65) || (x[i] > 70 && x[i] < 97) || x[i] >
102)
return 0;
}
return 1;
}
void decal(char x[], int n)
{
int i, nz;
x[5] = ' ';
nz = 5 - strlen(x);
if(nz > 0) {
for(i = 0; i < n; i++)
x[5 - i- 1] = x[n-i-1];
}
for(i = 0; i < nz; i++)
x[i] = '0';
}
我得到了这个学校项目,制作一个十六进制计算器。老师给我们做了以下代码。命令的
我的问题是空白添加部分。怎么可能添加字符?我知道有更简单的方法来制作十六进制计算器,但我们必须使用该代码。那么我怎样才能用六进制写出像 1cec+bec=28d8 这样的总和呢?
假设这些函数存在
// Convert hexadecimal string 'number' into an integer.
int fromHex(const char* number);
// Convert 'number' to hexadecimal in 'result', assuming that the result will fit.
void toHex(int number, char* result);
你可以这样写add
:
void add(const char* hex_a, const char* hex_b, char* hex_c)
{
int a = fromHex(hex_a);
int b = fromHex(hex_b);
toHex(a + b, hex_c);
}
实现作为练习的转换函数。
下面是如何添加十六进制数的示例。这个简单的代码以 5 位溢出环绕。
#include <iostream>
#include <exception>
#include <cstring>
using std::cout;
using std::endl;
struct BadDigit : public std::exception { };
int hexToDec( const char c ) {
if ( c >= '0' && c <= '9' ) {
return c -'0';
}
else if( c>= 'A' && c<='F') {
return c - 'A' + 10;
}
else if ( c >= 'a' && c<='f' ) {
return c - 'a' + 10;
}
else {
throw BadDigit();
}
}
char decToHex( int d ) {
if ( d >= 0 && d <= 9 ) {
return d + '0';
}
else if ( d >= 10 && d <= 15 ) {
return d - 10 + 'A';
}
else {
throw BadDigit();
}
}
void add( const char *a, const char *b, char *s )
{
int rem = 0;
for (int i = 4; i >= 0; i-- ) {
int ai = hexToDec(a[i]);
int bi = hexToDec(b[i]);
int ri = ai + bi + rem;
s[i] = decToHex( ri % 16 );
rem = ri / 16;
}
}
int main()
{
const char *hex1 = "effff";
const char *hex2 = "00001";
char result[7];
try {
add(hex1, hex2, result);
}
catch (std::exception& e) {
cout << "Exception: " << e.what() << endl;
return 1;
}
cout << hex1 << " + " << hex2 << " = " << result << endl;
return 0;
}
相关文章:
- 使用strcpy将char数组的元素复制到另一个数组
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在 c++ 中拥有一组结构的正确方法是什么?
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- nlohmann-json将一个数组插入到另一个数组中
- 有没有一种方法可以在编译时获得作用域类名
- 为什么make_tie不是一件事
- 对于C++中使用智能指针的指针算术限制,有没有一种变通方法
- c++中O(n^(1/3))中一个数的除数的有效计数
- 一种在C++中读取TXT配置文件的简单方法
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 在调用接收数组的方法时,模板化数组大小是不是一种糟糕的做法
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 我是否需要在下一次转移时将所有权*转移回转移队列
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 用炭赚一笔钱