鱼C论坛

 找回密码
 立即注册
查看: 1808|回复: 3

c++

[复制链接]
发表于 2023-11-13 19:18:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
用函数实现将一个以字符串形式表示的十六进制数转换为一个十进制整数。例如,输入”A2”转换为162。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-13 19:18:48 | 显示全部楼层
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>

  4. int hexToDecimal(std::string hex) {
  5.     int decimal = 0;
  6.     int power = 0;
  7.     for (int i = hex.length() - 1; i >= 0; i--) {
  8.         int digitValue;
  9.         if (hex[ i] >= '0' && hex[ i] <= '9') {
  10.             digitValue = hex[ i] - '0';
  11.         } else if (hex[ i] >= 'A' && hex[ i] <= 'F') {
  12.             digitValue = hex[ i] - 'A' + 10;
  13.         } else if (hex[ i] >= 'a' && hex[ i] <= 'f') {
  14.             digitValue = hex[ i] - 'a' + 10;
  15.         } else {
  16.             throw std::invalid_argument("Invalid hexadecimal digit");
  17.         }
  18.         decimal += digitValue * pow(16, power);
  19.         power++;
  20.     }
  21.     return decimal;
  22. }

  23. int main() {
  24.     std::string hex = "A2";
  25.     int decimal = hexToDecimal(hex);
  26.     std::cout << "Hexadecimal " << hex << " is equivalent to decimal " << decimal << std::endl;
  27.     return 0;
  28. }
复制代码

这段代码定义了一个名为hexToDecimal的函数,它接受一个十六进制字符串作为输入,并返回一个对应的十进制整数。在函数内部,通过从字符串的末尾开始遍历每个十六进制位,并根据其对应的十进制值以16的幂的形式累加到结果中。最后,在main函数中调用hexToDecimal函数并输出结果。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-13 19:19:46 | 显示全部楼层
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. using namespace std;
  5. int hexToDec(const string& hex) {
  6.     int dec = 0;
  7.     int power = 0;
  8.     for (int i = hex.size() - 1; i >= 0; i--) {
  9.         char digit = hex[i];
  10.         int value;
  11.         if (isdigit(digit)) {
  12.             value = digit - '0';
  13.         } else {
  14.             value = toupper(digit) - 'A' + 10;
  15.         }
  16.         dec += value * pow(16, power);
  17.         power++;
  18.     }
  19.     return dec;
  20. }
  21. int main() {
  22.     string hexString = "A2";
  23.     int decimal = hexToDec(hexString);
  24.     cout << "The decimal equivalent of " << hexString << " is " << decimal << endl;
  25.     return 0;
  26. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-13 19:32:13 | 显示全部楼层
  1. sh-5.2$ cat main.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/wait.h>

  7. void exec_bc(const char *str) {
  8.     int pipe_fd[2];
  9.     if(pipe(pipe_fd) == -1) { // 创建管道
  10.         perror("pipe");
  11.         exit(EXIT_FAILURE);
  12.     }

  13.     pid_t pid = fork();
  14.     if(pid == -1) {
  15.         perror("fork");
  16.         exit(EXIT_FAILURE);
  17.     } else if(pid == 0) {                          // 子进程
  18.         if(dup2(pipe_fd[0], STDIN_FILENO) == -1) { // 将管道输入重定向到标准输入
  19.             perror("dup2");
  20.             exit(EXIT_FAILURE);
  21.         }
  22.         close(pipe_fd[0]);
  23.         close(pipe_fd[1]);

  24.         execlp("bc", "bc", NULL); // 调用bc程序
  25.         // 如果execlp函数执行失败,输出错误信息并直接退出程序。
  26.         perror("execlp failed");
  27.         exit(EXIT_FAILURE);
  28.     } else {                                 // 父进程
  29.         write(pipe_fd[1], str, strlen(str)); // 将str写入管道
  30.         close(pipe_fd[0]);
  31.         close(pipe_fd[1]);
  32.         // 不需要获取子进程的退出状态,直接使用NULL即可。
  33.         waitpid(pid, NULL, 0);
  34.     }
  35. }

  36. int main() {
  37.     char buff[1024];
  38.     fgets(buff, 1024, stdin);
  39.     exec_bc(buff);
  40.     return 0;
  41. }
  42. sh-5.2$ g++ -g3 -Wall -o main main.c
  43. sh-5.2$ ./main
  44. ibase=16; A2
  45. 162
  46. sh-5.2$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 00:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表