⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.cs

📁 股票预测的程序
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace NeuralNetCS
{
    sealed class Util
    {
        /// <summary>
        /// Delegate for the range function used to check input validity.
        /// </summary>
        /// <typeparam name="T">The type of the value to check.</typeparam>
        /// <param name="value">The value to check.</param>
        /// <returns>True if the value passes the range check; otherwise, false.</returns>
        public delegate bool RangeFunc<T>(T value);

        private Util()
        {
        }

        /// <summary>
        /// Range function that checks if the specified value is positive.
        /// </summary>
        /// <typeparam name="T">The type of the value to check. Must implement IComparable and be comparable to the number 0.</typeparam>
        /// <param name="value">The value to check.</param>
        /// <returns>True if the value is larger than 0; otherwise, false.</returns>
        public static bool Positive<T>(T value) where T : IComparable
        {
            return value.CompareTo(0) > 0;
        }

        /// <summary>
        /// Input a value of the specified type, checking if it matches the specified range function.
        /// </summary>
        /// <typeparam name="T">The type of the value that the input must be converted to.</typeparam>
        /// <param name="prompt">The prompt to show to the user when asking for the value.</param>
        /// <param name="rangeFunc">The range function to check the validity of the value.</param>
        /// <param name="outOfRangeMsg">The message to display when the value does not pass the range function check.
        /// This message is not used when the input can't be converted to the specified type.</param>
        /// <param name="defaultValue">The default value to use it the user inputs an empty line.</param>
        /// <returns>The value input by the user.</returns>
        /// <remarks>The input attempt is repeated until it succeeds. This function is mainly useful for
        /// inputting numerical value.</remarks>
        public static T InputValue<T>(string prompt, RangeFunc<T> rangeFunc, string outOfRangeMsg, T defaultValue)
        {
            while( true )
            {
                Console.Write(prompt);
                string input = Console.ReadLine();
                if( input.Trim().Length == 0 )
                    return defaultValue;

                try
                {
                    T value = (T)Convert.ChangeType(input, typeof(T), System.Globalization.CultureInfo.CurrentCulture);
                    if( rangeFunc(value) )
                        return value;
                    else
                        Console.WriteLine(outOfRangeMsg);
                }
                catch( FormatException )
                {
                    Console.WriteLine("Invalid input.");
                }
                catch( OverflowException )
                {
                    Console.WriteLine("The value is too large.");
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -