提问者:小点点

如何使用C#将递归函数的结果写入文件


我已经设法写出了一个递归函数的代码,它返回一个组合的所有序列,没有重复,也有重复。但是,我想知道是否有人可以帮助我将那些结果写入一个文件(。txt,。doc,。csv,。xls等。。)在C#中使用控制台应用程序或windows窗体应用程序。下面是我的代码:

using System;
using System.IO;

namespace Combinations
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Combinations Program");
            Console.WriteLine("-------------------------------");
            Console.Write("Start= ");
            int start = int.Parse(Console.ReadLine());
            Console.Write("End= ");
            int end = int.Parse(Console.ReadLine());
            Console.Write("Places= ");
            int places = int.Parse(Console.ReadLine());
            int[] arr = new int[places];

            Console.WriteLine("Combinations without repetition");
            CalcCombinationsWithoutRep(start, end, 0, arr);
 
        }

        static void CalcCombinationsWithoutRep(int start, int end, int index, int[] arr)
        {
            if (index == arr.Length)
            {
                Console.WriteLine(string.Join("-", arr));
                return;
                //string.Join("-", arr);
            }
            for (int i = start; i <= end; i++)
            {
                arr[index] = i;
                //Combinations without repetition
                CalcCombinationsWithoutRep(i + 1, end, index + 1, arr);
            }
        }

    }
}

共1个答案

匿名用户

下面是一个具有临时文件位置的解决方案

static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Combinations Program");
            Console.WriteLine("-------------------------------");
            Console.Write("Start= ");
            int start = int.Parse(Console.ReadLine());
            Console.Write("End= ");
            int end = int.Parse(Console.ReadLine());
            Console.Write("Places= ");
            int places = int.Parse(Console.ReadLine());
            int[] arr = new int[places];

            using (StreamWriter streamWriter = new StreamWriter(@"C:\Temp\result.txt"))
            {
                Console.WriteLine("Combinations without repetition");
                streamWriter.WriteLine("Combinations without repetition");
                CalcCombinationsWithoutRep(start, end, 0, arr, streamWriter);
            }
 
        }

        static void CalcCombinationsWithoutRep(int start, int end, int index, int[] arr, StreamWriter streamWriter)
        {
            if (index == arr.Length)
            {
                Console.WriteLine(string.Join("-", arr));
                streamWriter.WriteLine(string.Join("-", arr));
                return;
                //string.Join("-", arr);
            }
            for (int i = start; i <= end; i++)
            {
                arr[index] = i;
                //Combinations without repetition
                CalcCombinationsWithoutRep(i + 1, end, index + 1, arr, streamWriter);
            }
        }

接下来你可以做的是为你的命令行定义参数。对此有用的项目是commandlinehttps://github.com/commandlineParser/commandline