鱼C论坛

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

[已解决]翻译语言

[复制链接]
发表于 2023-10-6 21:12:00 | 显示全部楼层 |阅读模式

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

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

x
from pythonds.basic import Stack
ub = Stack()
def jinzhizhuanhua(num, fundamention):
    dic = "0123456789ABCDEF"
    if num < fundamention:
        ub.push(dic[num])
    else:
        ub.push(dic[num % fundamention])
        jinzhizhuanhua(int(num//fundamention),fundamention)
jinzhizhuanhua(22543253, 16)
for i in range(ub.size()):
    print(ub.pop(),end='')

把这段代码翻译成c语言可以吗,效果相同就行
最佳答案
2023-10-6 21:12:35
可以将这段代码翻译成C语言,实现相同的功能。以下是翻译后的C代码:

  1. #include <stdio.h>

  2. #define MAX_SIZE 100

  3. struct Stack {
  4.     int top;
  5.     char items[MAX_SIZE];
  6. };

  7. void push(struct Stack* stack, char item) {
  8.     if (stack->top == MAX_SIZE - 1) {
  9.         printf("Stack Overflow\n");
  10.         return;
  11.     }
  12.     stack->items[++stack->top] = item;
  13. }

  14. char pop(struct Stack* stack) {
  15.     if (stack->top == -1) {
  16.         printf("Stack Underflow\n");
  17.         return '\0';
  18.     }
  19.     return stack->items[stack->top--];
  20. }

  21. void jinzhizhuanhua(int num, int fundamention) {
  22.     struct Stack ub;
  23.     ub.top = -1;
  24.     char dic[] = "0123456789ABCDEF";

  25.     if (num < fundamention) {
  26.         push(&ub, dic[num]);
  27.     }
  28.     else {
  29.         push(&ub, dic[num % fundamention]);
  30.         jinzhizhuanhua(num/fundamention, fundamention);
  31.     }

  32.     while (ub.top != -1) {
  33.         printf("%c", pop(&ub));
  34.     }
  35. }

  36. int main() {
  37.     jinzhizhuanhua(22543253, 16);
  38.     return 0;
  39. }
复制代码


这段C代码定义了一个结构体 Stack 来表示栈,实现了 push 和 pop 函数来操作栈。 jinzhizhuanhua 函数实现了进制转换的逻辑,将结果打印出来。在 main 函数中调用 jinzhizhuanhua 来进行进制转换。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-10-6 21:12:35 | 显示全部楼层    本楼为最佳答案   
可以将这段代码翻译成C语言,实现相同的功能。以下是翻译后的C代码:

  1. #include <stdio.h>

  2. #define MAX_SIZE 100

  3. struct Stack {
  4.     int top;
  5.     char items[MAX_SIZE];
  6. };

  7. void push(struct Stack* stack, char item) {
  8.     if (stack->top == MAX_SIZE - 1) {
  9.         printf("Stack Overflow\n");
  10.         return;
  11.     }
  12.     stack->items[++stack->top] = item;
  13. }

  14. char pop(struct Stack* stack) {
  15.     if (stack->top == -1) {
  16.         printf("Stack Underflow\n");
  17.         return '\0';
  18.     }
  19.     return stack->items[stack->top--];
  20. }

  21. void jinzhizhuanhua(int num, int fundamention) {
  22.     struct Stack ub;
  23.     ub.top = -1;
  24.     char dic[] = "0123456789ABCDEF";

  25.     if (num < fundamention) {
  26.         push(&ub, dic[num]);
  27.     }
  28.     else {
  29.         push(&ub, dic[num % fundamention]);
  30.         jinzhizhuanhua(num/fundamention, fundamention);
  31.     }

  32.     while (ub.top != -1) {
  33.         printf("%c", pop(&ub));
  34.     }
  35. }

  36. int main() {
  37.     jinzhizhuanhua(22543253, 16);
  38.     return 0;
  39. }
复制代码


这段C代码定义了一个结构体 Stack 来表示栈,实现了 push 和 pop 函数来操作栈。 jinzhizhuanhua 函数实现了进制转换的逻辑,将结果打印出来。在 main 函数中调用 jinzhizhuanhua 来进行进制转换。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-6 21:12:46 | 显示全部楼层
当然可以。以下是将给定的 Python 代码翻译成 C 语言的版本,实现了相同的功能:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. typedef struct Stack {
  5.     char *data;
  6.     int top;
  7.     int capacity;
  8. } Stack;

  9. Stack *create_stack(int capacity) {
  10.     Stack *stack = (Stack *)malloc(sizeof(Stack));
  11.     stack->data = (char *)malloc(capacity * sizeof(char));
  12.     stack->top = -1;
  13.     stack->capacity = capacity;
  14.     return stack;
  15. }

  16. void push(Stack *stack, char value) {
  17.     if (stack->top == stack->capacity - 1) {
  18.         stack->capacity *= 2;
  19.         stack->data = (char *)realloc(stack->data, stack->capacity * sizeof(char));
  20.     }
  21.     stack->data[++stack->top] = value;
  22. }

  23. char pop(Stack *stack) {
  24.     return stack->data[stack->top--];
  25. }

  26. int is_empty(Stack *stack) {
  27.     return stack->top == -1;
  28. }

  29. void jinzhizhuanhua(int num, int fundamention, Stack *stack) {
  30.     const char *dic = "0123456789ABCDEF";
  31.     if (num < fundamention) {
  32.         push(stack, dic[num]);
  33.     } else {
  34.         push(stack, dic[num % fundamention]);
  35.         jinzhizhuanhua(num / fundamention, fundamention, stack);
  36.     }
  37. }

  38. int main() {
  39.     Stack *stack = create_stack(10);
  40.     jinzhizhuanhua(22543253, 16, stack);
  41.     while (!is_empty(stack)) {
  42.         printf("%c", pop(stack));
  43.     }
  44.     printf("\
  45. ");
  46.     free(stack->data);
  47.     free(stack);
  48.     return 0;
  49. }
复制代码


这段 C 代码实现了一个简单的栈结构,并使用递归函数 jinzhizhuanhua 将给定的十进制数转换为指定进制。在这个例子中,我们将 22543253 转换为十六进制。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-6 21:13:34 | 显示全部楼层
Mike_python小 发表于 2023-10-6 21:12
None

球一个最佳答案谢谢啦!这对我非常重要!

《None》
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 17:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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