C# 如何向控制台应用程序c输入非常长的输入#

C# 如何向控制台应用程序c输入非常长的输入#,c#,algorithm,console-application,stopwatch,C#,Algorithm,Console Application,Stopwatch,我已尝试使用以下方法允许自己在控制台应用程序中输入非常大的值,如10^15000。此应用程序必须计算足够大的值,以便使用秒表对象计时,并在1秒内着陆。到目前为止,我只能在使用Console.SetIn(新的StreamReader(Console.OpenStandardInput(8192))时获得允许的最大输入字符数 以下是我的自定义ReadLine()方法: 这是我的整个程序,专门用于算法时间测试: using System; using System.Collections.Generi

我已尝试使用以下方法允许自己在控制台应用程序中输入非常大的值,如
10^15000
。此应用程序必须计算足够大的值,以便使用
秒表
对象计时,并在1秒内着陆。到目前为止,我只能在使用
Console.SetIn(新的StreamReader(Console.OpenStandardInput(8192))时获得允许的最大输入字符数

以下是我的自定义ReadLine()方法:

这是我的整个程序,专门用于算法时间测试:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;
using System.Diagnostics;
using System.IO;

namespace LabGrowthOfFunctions
{
    class Program
    {        
        static void Main(string[] args)
        {
            int userNum;

            Console.Write("Please enter a number: ");
            Console.SetIn(new StreamReader(Console.OpenStandardInput(8192))); 

            string tempString = ReadLine();  
            int.TryParse(tempString, out userNum);

            Stopwatch sw = Stopwatch.StartNew();
            //nsquaredgrowthversion1(userNum);
            //ncubedfunction(userNum);
            ntothefourthpowerfunction(userNum);
            //nfunction(userNum);
            //nlognfunction(userNum);
            sw.Stop();

            Console.WriteLine("Time used: {0} secs", sw.Elapsed.TotalMilliseconds / 1000);
            Console.ReadLine();
        }

        private static void nsquaredgrowthversion1(int n)
        {
            int sum = 0;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    sum++;
        }

        private static void ncubedfunction(int n)
        {
            int sum = 0;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    for (int k = 0; k < n; k++)
                        sum++;
        }

        private static void ntothefourthpowerfunction(int n)
        {
            int sum = 0;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    for (int k = 0; k < n; k++)
                        for (int l = 0; l < n; l++)
                            sum++;
        }

        private static void nfunction(int n)
        {
            int sum = 0;

            for (int i = 0; i < n; i++)
                sum++;
        }

        private static void nlognfunction(BigInteger n)
        {
            double sum = 0;
            double result = 0.0;
            result = Math.Log((double)n,2);
            sum = (double)n * result; 
        }



        //ReadLine recreated for large input sizes.
        static string ReadLine()
        {
            StringBuilder sb = new StringBuilder();
            while(true){
                char ch = Convert.ToChar(Console.Read());
                sb.Append(ch);
                if(ch == '\n'){
                    break;
                }
            }

            return sb.ToString();
        }

    }//end Main
}//end namespace LabGrowthOfFunctions
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统数字;
使用系统诊断;
使用System.IO;
名称空间labgrowthoff函数
{
班级计划
{        
静态void Main(字符串[]参数)
{
int userNum;
控制台。写入(“请输入一个数字:”);
SetIn(新的StreamReader(Console.OpenStandardInput(8192));
string tempString=ReadLine();
int.TryParse(tempString,out userNum);
秒表sw=Stopwatch.StartNew();
//nsquaredgrowthversion1(userNum);
//ncubedfunction(userNum);
ntothefourthpowerfunction(userNum);
//n函数(userNum);
//nlognnfunction(userNum);
sw.Stop();
WriteLine(“使用的时间:{0}秒”,sw.appeased.totalmillizes/1000);
Console.ReadLine();
}
私有静态无效nsquaredgrowthversion1(int n)
{
整数和=0;
对于(int i=0;i
您可以改用ReadKey()方法。读取输入的每个字符,附加到字符串,然后将字符串转换为Int。

Int
s是32位的-您需要
biginger
。您还需要分块阅读。我尝试使用BigInteger,但它仍然限制了我可以粘贴到控制台中的大小。。使用BigInteger还冻结了我的控制台?我怎样才能读成一块块呢?相关:谢谢马特·伯兰,我没找到答案。。我只找到了与我的方法相似的答案,请具体说明。您是说要输入数字
1
后接15000
0
的文本吗?当你这样做的时候,具体发生了什么,具体以什么方式与你想要的不同?看见此外,请修剪您的代码示例,使其仅显示。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;
using System.Diagnostics;
using System.IO;

namespace LabGrowthOfFunctions
{
    class Program
    {        
        static void Main(string[] args)
        {
            int userNum;

            Console.Write("Please enter a number: ");
            Console.SetIn(new StreamReader(Console.OpenStandardInput(8192))); 

            string tempString = ReadLine();  
            int.TryParse(tempString, out userNum);

            Stopwatch sw = Stopwatch.StartNew();
            //nsquaredgrowthversion1(userNum);
            //ncubedfunction(userNum);
            ntothefourthpowerfunction(userNum);
            //nfunction(userNum);
            //nlognfunction(userNum);
            sw.Stop();

            Console.WriteLine("Time used: {0} secs", sw.Elapsed.TotalMilliseconds / 1000);
            Console.ReadLine();
        }

        private static void nsquaredgrowthversion1(int n)
        {
            int sum = 0;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    sum++;
        }

        private static void ncubedfunction(int n)
        {
            int sum = 0;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    for (int k = 0; k < n; k++)
                        sum++;
        }

        private static void ntothefourthpowerfunction(int n)
        {
            int sum = 0;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    for (int k = 0; k < n; k++)
                        for (int l = 0; l < n; l++)
                            sum++;
        }

        private static void nfunction(int n)
        {
            int sum = 0;

            for (int i = 0; i < n; i++)
                sum++;
        }

        private static void nlognfunction(BigInteger n)
        {
            double sum = 0;
            double result = 0.0;
            result = Math.Log((double)n,2);
            sum = (double)n * result; 
        }



        //ReadLine recreated for large input sizes.
        static string ReadLine()
        {
            StringBuilder sb = new StringBuilder();
            while(true){
                char ch = Convert.ToChar(Console.Read());
                sb.Append(ch);
                if(ch == '\n'){
                    break;
                }
            }

            return sb.ToString();
        }

    }//end Main
}//end namespace LabGrowthOfFunctions