黑马程序员----自写了一个输入数值的类库

 

由于C#的数据转换需要判断异常或是否转换成功,觉得有点麻烦,就自己字一个类库.写得不好的请各位多多指点,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConvertMath
{
    public static class ConvertMath
    {
        static bool flag;       //此变量判断,用于输入小数点       
        static string ChuLi()            //输入字符处理
        {
            string strMath = "";        //声明一个字符串,用来存放输入的值
            int x = Console.CursorLeft;         //获取光标的x坐标的位置
            int y = Console.CursorTop;          //获取光标的y坐标的位置
            ConsoleKey inputKey = Console.ReadKey(true).Key;
            while (inputKey != ConsoleKey.Enter && inputKey != ConsoleKey.Spacebar)
            {
                if ((int)inputKey >= 48 && (int)inputKey <= 57)   //判断输入的值是不是0到9,如果是继续向下执行.
                {
                    strMath += ((char)inputKey.GetHashCode()).ToString();   //strMath加刚键入的值
                    Console.Write(((char)inputKey.GetHashCode()).ToString());   //向屏幕显示刚键入的值
                }
                else if (inputKey == ConsoleKey.Backspace && strMath.Length != 0) //当用户按下退格键时,字符减1
                {
                        strMath = strMath.Remove(strMath.Length - 1);
                        Console.SetCursorPosition(x,y);     //设置光标最初的位置.
                        Console.Write(strMath +" ");    //在光标最初的位置上显示字符串和一个空格.
                        Console.SetCursorPosition(x+strMath.Length,y);      //清除光标多余的空格
                }
                else if ((int)inputKey == 189 && strMath.Length == 0)   //负号处理
                {
                    strMath = "-";      //符号只能在索引0处,当输入-字,向屏幕输出-;
                    Console.Write("-");
                }
                else if ((int)inputKey == 190 && flag == true && strMath.Length != 0)
                {
                    strMath += ".";     //double型数字处理...
                    flag = false;
                    Console.Write(".");
                }
                inputKey = Console.ReadKey(true).Key;       //再次接受用户输入
            }
            Console.WriteLine();        //换行
            return strMath;         
        } 

       public static int ToInt32()
        {
            string myStrMath;
            int intResult;
            flag = false;       //此处赋值为假,是不能让用户输入点.
            myStrMath = ChuLi();        //用来接受用户输入
            try
            {
                intResult = Convert.ToInt32(myStrMath);
                return intResult;
            }
            catch
            {
              //  Console.WriteLine("转换失败,数字太大或太小,不能超过整型最大范围或最小范围!");
                return 0;
            }
        }           //返回整型数值

 public static double ToDouble()
        {
            string myStrMath;
            double doubleResult;
            flag = true;        //用户能输入小数点
            myStrMath = ChuLi();     //用来接受用户输入
            try
            {
                doubleResult = Convert.ToDouble(myStrMath);     //转换为double型
                return doubleResult;
            }

    catch
            {
               // Console.WriteLine("转换失败,数字太大或太小,不能超过double型最大范围或最小范围!");
                return 0;
            }

  }

 

public static long ToInt64()
        {
            string myStrMath;
            long longResult;
            flag = false;
            myStrMath = ChuLi();
            try
            {
                longResult = Convert.ToInt64(myStrMath);
                return longResult;
            }

     catch
            {
               // Console.WriteLine("转换失败,数字太大或太小,不能超过长整型最大范围或最小范围!");
                return 0;
            }

        }           //返回长整型数值
    }
}

 

    

 

posted @ 2012-10-07 19:34  再美也是伤  阅读(114)  评论(0编辑  收藏  举报