使用程序的输出作为同一程序中的输入 [英] using output of a program as input in same program

查看:89
本文介绍了使用程序的输出作为同一程序中的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,以生成给定数字序列的所有可能的排列,然后从该排列中生成所有可能的二叉树,因此,我认为有一个程序可以生成排列并将结果存储到文件中,然后编写更多代码以逐行读取(包含所有置换)并从中生成二进制树,因此,现在我编写了半个程序,该置换生成置换并将结果存储在文件中.

I am writing a program to generate all possible permutations of a given series of numbers and then generate all possible binary trees from that permutations so, what I thought is having a program which generates permutations and stores the result to a file and then write further code to read line by line (which has all permutations ) and generate binary trees out of them, so right now I have written half program which generates permutation and it stores the result in file.

#include <stdio.h>

//function to print the array
void printarray(int arr[], int size)
{
        FILE *fp;
    int i,j;
        fp=fopen("result.txt","w");
    for(i=0; i<size; i++)
    {
          //  printf("%d\t",arr[i]);
          fprintf(fp,"%d\t",arr[i]);
    }
    printf("\n");
    fclose(fp);
}

//function to swap the variables
void swap(int *a, int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

//permutation function
void permutation(int *arr, int start, int end)
{
    if(start==end)
    {
        printarray(arr, end+1);
        return;
    }
    int i;
    for(i=start;i<=end;i++)
    {
        //swapping numbers
        swap((arr+i), (arr+start));
        //fixing one first digit
        //and calling permutation on
        //the rest of the digits
        permutation(arr, start+1, end);
        swap((arr+i), (arr+start));
    }
}

int main()
{
   //taking input to the array
    int size;
    printf("Enter the size of array\n");
    scanf("%d",&size);
    int i;
    int arr[size];
    for(i=0;i<size;i++)
        scanf("%d",&arr[i]);
    //calling permutation function
    permutation(arr, 0, size-1);
    return 0;
}

但是此程序中的问题是该程序仅存储一个排列,而没有将其他排列存储在result.txt文件中,我该如何继续存储结果.程序也不会结束空白光标闪烁,这会给错误的无限while循环印象. 我必须按Ctrl + c结束程序才能摆脱这种情况?

but the problem here in this program is that this program only stores one permutation and does not stores other permutations in result.txt file, how do I go on storing result this way. Also program does not ends a blank cursor blinking which gives a false impression of infinite while loop. I had to press Ctrl+c to end the program how to get rid of this?

推荐答案

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define N 10

void print(int *num, int n)
{
        FILE *fp;
        fp=fopen("result.txt","a");
    int i;
    for ( i = 0 ; i < n ; i++)
//        printf("%d ", num[i]);
          fprintf(fp,"%d ",num[i]);
    fprintf(fp,"\n");
   fclose(fp);
}
int main()
{
    int num[N];
    int *ptr;
    int temp;
    int i, n, j;
    printf("\nHow many number you want to enter: ");
        scanf("%d", &n);
    printf("\nEnter a list of numbers to see all combinations:\n");
    for (i = 0 ; i < n; i++)
        scanf("%d", &num[i]);
    for (j = 1; j <= n; j++) {
        for (i = 0; i < n-1; i++) {
            temp = num[i];
            num[i] = num[i+1];
            num[i+1] = temp;
            print(num, n);
        }
    }
    return 0;
}

这篇关于使用程序的输出作为同一程序中的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆