void Fun_10_Any(int number, int cet) { Stack S = (Stack)malloc(sizeof(struct StackNode));; int e; InitStack(S); if (number == 0) { printf("转换后的%d进制数为:0", cet); return; } while (number) { Push(S, number % cet); number = number / cet; } printf("转换后的%d进制数为:", cet); while (!IsEmpty(S)) { e = Top(S); Pop(S); switch (e) { case 10: case 11: case 12: case 13: case 14: case 15: e += 55; break; default:e += 48; } printf("%c", e); } free(S); }
时间: 2023-07-15 10:11:18 浏览: 97
这段代码实现了将一个十进制数转换为任意进制的功能。函数名为 Fun_10_Any,接受两个参数:一个是待转换的十进制数 number,另一个是目标进制 cet。代码中首先创建了一个栈 S,并初始化为空栈。然后判断待转换的数是否为 0,如果是,则直接输出转换后的结果为 0,结束函数。如果不是,则通过 while 循环将 number 不断除以 cet 并将余数压入栈 S 中,直到 number 变为 0。接下来,通过 while 循环取出栈 S 中的元素并打印,需要注意的是,如果取出的元素是 10~15(即十六进制数中的 A~F),需要将其转换为对应的字符,这里使用了 switch 语句实现。最后释放栈 S 的内存。
相关问题
int Fun_Any_10(int cet) { Stack S = (Stack)malloc(sizeof(struct StackNode));; InitStack(S); char c; int num, sum = 0; printf("请输入要转化的数:\n"); c = getchar(); while (c != '\n') { if (isalnum(c)) Push(S, c - '0'); else Push(S, tolower(c) - 'a' + 10); c = getchar(); } int i = 0; while (!IsEmpty(S)) { num = Top(S); Pop(S); sum += (num * (int)pow(cet, i)); ++i; } free(S); return sum; }
这是一个将任意进制的数转成十进制的函数。输入参数是一个整数cet,表示需要转化的数的进制。函数内部首先创建了一个栈S,并且通过InitStack函数初始化。接着,程序通过getchar()函数逐个读取输入的字符,如果是数字或字母,则将其转化成数值并压入栈S中;如果不是,则直接忽略。最后,程序通过弹出栈顶元素并乘以对应进制的幂次方,计算出十进制数值。最后释放栈内存,并返回所得到的十进制数值。
优化以下代码#include <stdio.h> #include <stdlib.h> typedef struct TreeNode { char data; struct TreeNode* left; struct TreeNode* right; } TreeNode; TreeNode* createTree() { char ch; TreeNode* root; scanf("%c", &ch); if (ch == '#') { return NULL; } root = (TreeNode*)malloc(sizeof(TreeNode)); root->data = ch; root->left = createTree(); root->right = createTree(); return root; } void digui(TreeNode* root) { if (root == NULL) { return; } digui(root->left); printf("%c ", root->data); digui(root->right); } typedef struct StackNode { TreeNode* tree; struct StackNode* next; } StackNode; typedef struct Stack { StackNode* top; int size; } Stack; Stack* createStack() { Stack* stack = (Stack*)malloc(sizeof(Stack)); stack->top = NULL; stack->size = 0; return stack; } void push(Stack* stack, TreeNode* tree) { StackNode* node; node = (StackNode*)malloc(sizeof(StackNode)); node->tree = tree; node->next = stack->top; stack->top = node; stack->size++; } TreeNode* pop(Stack* stack) { TreeNode* tree; StackNode* temp; if (stack->size == 0) { return NULL; } tree = stack->top->tree; temp = stack->top; stack->top = stack->top->next; stack->size--; free(temp); return tree; } void feidigui(TreeNode* root) { Stack* stack; TreeNode* p; stack = createStack(); p = root; while (p != NULL || stack->size != 0) { while (p != NULL) { push(stack, p); p = p->left; } if (stack->size != 0) { p = pop(stack); printf("%c ", p->data); p = p->right; } } } int getHeight(TreeNode* root) { int leftHeight,rightHeight,max; if (root == NULL) { return 0; } leftHeight = getHeight(root->left); rightHeight = getHeight(root->right); max=leftHeight>rightHeight?leftHeight:rightHeight; return max+1; }
首先,这段代码的缩进不太规范,可以调整一下,使得代码更加清晰易读。
其次,在函数中出现了多次重复的代码,如创建栈、入栈、出栈等,可以将这些代码封装成一个通用的函数,以便在程序中反复使用。
最后,可以考虑使用更加简洁的写法,如使用三目运算符代替if-else语句,使用递归函数代替while循环等,以提高代码的可读性和可维护性。同时,可以加上注释,方便他人理解代码逻辑。
下面是一个优化后的代码示例:
```
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
char data;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 创建二叉树
TreeNode* createTree() {
char ch;
scanf("%c", &ch);
if (ch == '#') {
return NULL;
}
TreeNode *root = (TreeNode*)malloc(sizeof(TreeNode));
root->data = ch;
root->left = createTree();
root->right = createTree();
return root;
}
// 递归遍历二叉树
void digui(TreeNode *root) {
if (root == NULL) {
return;
}
digui(root->left);
printf("%c ", root->data);
digui(root->right);
}
typedef struct StackNode {
TreeNode *tree;
struct StackNode *next;
} StackNode;
typedef struct Stack {
StackNode *top;
int size;
} Stack;
// 创建栈
Stack* createStack() {
Stack *stack = (Stack*)malloc(sizeof(Stack));
stack->top = NULL;
stack->size = 0;
return stack;
}
// 入栈
void push(Stack *stack, TreeNode *tree) {
StackNode *node = (StackNode*)malloc(sizeof(StackNode));
node->tree = tree;
node->next = stack->top;
stack->top = node;
stack->size++;
}
// 出栈
TreeNode* pop(Stack *stack) {
if (stack->size == 0) {
return NULL;
}
TreeNode *tree = stack->top->tree;
StackNode *temp = stack->top;
stack->top = stack->top->next;
stack->size--;
free(temp);
return tree;
}
// 非递归遍历二叉树
void feidigui(TreeNode *root) {
Stack *stack = createStack();
TreeNode *p = root;
while (p != NULL || stack->size != 0) {
if (p != NULL) {
push(stack, p);
p = p->left;
} else {
p = pop(stack);
printf("%c ", p->data);
p = p->right;
}
}
}
// 获取二叉树的高度
int getHeight(TreeNode *root) {
if (root == NULL) {
return 0;
}
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
int main() {
TreeNode *root = createTree();
digui(root);
printf("\n");
feidigui(root);
printf("\n");
printf("%d\n", getHeight(root));
return 0;
}
```
阅读全文