gpt4 book ai didi

C语言如何在递归函数中打印到文件而不覆盖它?

转载 作者:行者123 更新时间:2023-11-30 20:42:10 28 4
gpt4 key购买 nike

我有多个递归函数。他们多次打印结果。我需要创建一个包含所有这些输出的文件。打印的函数有:void print_inorder、void print_preorder、void print pre_order;

示例输入:1(case选项预序),1 2 3 4 5 q(插入树中的数字,q结束scanf);输出:1 2 3 4 5,我也需要将其打印到文件中。

我的代码:

#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<pthread.h>
struct node1
{
int key1;
struct node1 *left1, *right1;
};

// A utility function to create a new BST node1
struct node1 *newnode1(int item)
{
struct node1 *temp1 = (struct node1 *)malloc(sizeof(struct node1));
temp1->key1 = item;
temp1->left1 = temp1->right1 = NULL;
return temp1;
}

// A utility function to do inorder traversal of BST
void inorder(struct node1 *root1)
{
if (root1 != NULL)
{
inorder(root1->left1);
printf("%d ", root1->key1);
inorder(root1->right1);
}
}

/* A utility function to insert1 a new node1 with given key1 in BST */
struct node1* insert1(struct node1* node1, int key1)
{
/* If the tree is empty, return a new node1 */
if (node1 == NULL) return newnode1(key1);

/* Otherwise, recur down the tree */
if (key1 < node1->key1)
node1->left1 = insert1(node1->left1, key1);
else
node1->right1 = insert1(node1->right1, key1);

/* return the (unchanged) node1 pointer */
return node1;
}
/* Given a non-empty binary search tree, return the node1 with minimum
key1 value found in that tree. Note that the entire tree does not
need to be searched. */
struct node1 * minValuenode1(struct node1* node1)
{
struct node1* current = node1;

/* loop down to find the left1most leaf */
while (current->left1 != NULL)
current = current->left1;

return current;
}
/* Given a binary search tree and a key1, this function deletes the key1
and returns the new root1 */
struct node1* deletenode1(struct node1* root1, int key1)
{
// base case
if (root1 == NULL) return root1;
// If the key1 to be deleted is smaller than the root1's key1,
// then it lies in left1 subtree
if (key1 < root1->key1)
root1->left1 = deletenode1(root1->left1, key1);
// If the key1 to be deleted is greater than the root1's key1,
// then it lies in right1 subtree
else if (key1 > root1->key1)
root1->right1 = deletenode1(root1->right1, key1);
// if key1 is same as root1's key1, then This is the node1
// to be deleted
else
{
// node1 with only one child or no child
if (root1->left1 == NULL)
{
struct node1 *temp1 = root1->right1;
free(root1);
return temp1;
}
else if (root1->right1 == NULL)
{
struct node1 *temp1 = root1->left1;
free(root1);
return temp1;
}
// node1 with two children: Get the inorder successor (smallest
// in the right1 subtree)
struct node1* temp1 = minValuenode1(root1->right1);
// Copy the inorder successor's content to this node1
root1->key1 = temp1->key1;
// Delete the inorder successor
root1->right1 = deletenode1(root1->right1, temp1->key1);
}
return root1;
}
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
void insert(node ** tree, int val)
{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
void print_preorder(node * tree)
{
if (tree)
{
printf("%d\n",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}

}
void print_inorder(node * tree)
{
if (tree)
{
print_inorder(tree->left);
printf("%d\n",tree->data);
print_inorder(tree->right); // i want to print this as a tree
}
}
void print_postorder(node * tree)
{
if (tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
printf("%d\n",tree->data);
}
}
void deltree(node * tree)
{
if (tree)
{
deltree(tree->left);
deltree(tree->right);
free(tree);
}
}
node* search(node ** tree, int val)
{
if(!(*tree))
{
return NULL;
}
if(val < (*tree)->data)
{
search(&((*tree)->left), val);
}
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
else if(val == (*tree)->data)
{
return *tree;
}
}
void main()
{
int a;
int searcharg;
int deletearg;
char option;
printf("1:PreOrder\n2:InOrder\n3:PostOrder\n4:search\n5:Delete Node\nYour Option: ");
scanf("%c", &option);
clock_t tic = clock();
node *root;
node *tmp;
//int i;
root = NULL;
struct node1 *root1 = NULL;
/* Inserting nodes into tree */
while (scanf("%d", &a) && a!='q'){insert(&root,a); root1 = insert1(root1, a);}
// these

/* Printing nodes of tree */
switch(option)
{
case '1':
printf("Pre Order Display\n");
print_preorder(root); // i want to print this as a tree
break;
case '2':
printf("In Order Display\n");
print_inorder(root); // i want to print this as a tree
break;
case '3':
printf("Post Order Display\n");
print_postorder(root); // i want to print this as a tree
break;

case '4':
scanf("%d", &searcharg);
tmp = search(&root, searcharg);
if (tmp)
{
printf("The searched node is present = %d\n", tmp->data);
}
else
{
printf("Data Not found in tree.\n");
}
break;
default:
printf("Error! operator is not correct\n");
break;
case '5':
// printf("scan");
// scanf("%d", &deletearg);
root1 = deletenode1(root1, 5);
inorder(root1);
}
// i want to print this as a tree
/* Deleting all nodes of tree */
deltree(root);
clock_t toc = clock();
printf("\nElapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
getch();
}

最佳答案

解决方案 1:在主函数中打开文件,并将文件指针作为参数传递给需要写入文件的每个函数。在主函数结束时关闭它。

解决方案2:使用全局变量:

FILE * g_my_file = fopen("my_file.txt", "wt");

然后代替 printf 使用:

fprintf(g_my_file, "%d\n", tree->data);

也许在程序/主程序末尾的某个地方将其关闭:

fclose(g_my_file);

关于C语言如何在递归函数中打印到文件而不覆盖它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53608977/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com