void digui(int a) { if (m[a] == 'a') { m[a] = 'z'; digui(a - 1); } else m[a]=m[a]-1; }哪里有问题
时间: 2024-05-25 12:15:31 浏览: 51
这段代码的问题在于没有考虑到当m[a]为‘a’并且a为0的情况。此时会出现数组下标越界的问题。应该加上一个判断语句,如下所示:
void digui(int a) {
if (m[a] == 'a') {
m[a] = 'z';
if(a>0) //加上判断语句
digui(a - 1);
} else {
m[a]=m[a]-1;
}
}
相关问题
优化以下代码#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;
}
```
类名:digui 注释:使用Scanner类对象来输入,输入字符串用sc.nextline 输入整数用sc.nextInt,输入实数用sc.nextDouble
import java.util.Scanner;
public class digui {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = sc.nextLine();
System.out.println("请输入一个整数:");
int num1 = sc.nextInt();
System.out.println("请输入一个实数:");
double num2 = sc.nextDouble();
System.out.println("您输入的字符串是:" + str);
System.out.println("您输入的整数是:" + num1);
System.out.println("您输入的实数是:" + num2);
}
}
阅读全文