鱼C论坛

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

C++,不能使用sstream,要用using namespace std

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

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

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

x
编写一个将十进制整数转换为十六进制字符串的函数。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

  3. using namespace std;

  4. string decimalToHex(int decimal) {
  5.     string hex = "";
  6.     while (decimal > 0) {
  7.         int remainder = decimal % 16;
  8.         if (remainder < 10) {
  9.             hex = char('0' + remainder) + hex;
  10.         } else {
  11.             hex = char('A' + remainder - 10) + hex;
  12.         }
  13.         decimal /= 16;
  14.     }
  15.     return hex;
  16. }

  17. int main() {
  18.     int decimal;
  19.     cout << "请输入一个十进制整数:";
  20.     cin >> decimal;
  21.     string hex = decimalToHex(decimal);
  22.     cout << "转换为十六进制为:" << hex << endl;
  23.     return 0;
  24. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-13 19:27:30 | 显示全部楼层
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. string toHex(int num) {
  5.     string hex = "0123456789ABCDEF";
  6.     string result = "";
  7.     if (num == 0) {
  8.         result = "0";
  9.     } else {
  10.         while (num > 0) {
  11.             result = hex[num % 16] + result;
  12.             num /= 16;
  13.         }
  14.     }
  15.     return result;
  16. }
  17. int main() {
  18.     int decimalNumber;
  19.     cout << "请输入一个十进制整数:";
  20.     cin >> decimalNumber;
  21.     string hexadecimalString = toHex(decimalNumber);
  22.     cout << "对应的十六进制字符串为:" << hexadecimalString << endl;
  23.     return 0;
  24. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-13 19:34:43 | 显示全部楼层
  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$ ./main
  43. obase=16; 1234
  44. 4D2
  45. sh-5.2$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 03:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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