Я попытался использовать следующие методы, чтобы позволить себе вводить в мое консольное приложение очень большие значения, такие как 10^15000
. Это приложение должно вычислять значения, достаточно большие, чтобы быть синхронизированными с использованием объекта Stopwatch
и приземляться менее чем за 1 секунду. До сих пор я могу получить максимальное количество входных символов, разрешенных только при использовании Console.SetIn(new StreamReader(Console.OpenStandardInput(8192)));
Как вводить чрезвычайно длинные входы в консольное приложение C#
Вот мой заказ ReadLine() метод:
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();
}
Вот вся моя программа, которая используется специально для тестирования времени алгоритма :
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
'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''. вам также нужно прочитать куски. –
Я пробовал использовать BigInteger, но он все же ограничивал размер, который я мог бы вставить в консоль. Используя BigInteger, я также застыл в консоли? Как я могу читать куски? – Chisx
Связанный: http://stackoverflow.com/questions/28245018/increase-buffer-for-console-readline/28245149#28245149 –