求C#高手做一道题,高分悬赏哦!!

2024-11-25 12:52:18
推荐回答(2个)
回答1:

using System.Text.RegularExpressions;
using System.IO;
using System.Xml.Linq;
using System.Xml;
static  string txtPath = @"D:\data.txt";
        static void Main(string[] args)
        {
            Console.WriteLine(@"请选择功能:
1:输入数据
2:输出数据");
            bool flag = true;
            while (flag)
            {
                char input = Console.ReadKey().KeyChar;
                if (input == '1') 
                {
                    flag = false;
                    InputData();
                }
                else if (input == '2') 
                {
                    flag = false;
                    OutputData();
                    Console.ReadLine();
                }
            }
        }
        static void CheckXml() //检查文件,如果没有则创建XML文档存储数据
        {
            if (!File.Exists(txtPath)) 
            {
                XmlDocument doc = new XmlDocument();
                doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                XmlElement root = doc.CreateElement("root");
                root.AppendChild(doc.CreateElement("chinese"));
                root.AppendChild(doc.CreateElement("english"));
                root.AppendChild(doc.CreateElement("number"));
                root.AppendChild(doc.CreateElement("all"));
                doc.AppendChild(root);
                doc.Save(txtPath);
            } 
        }
        static void InputData()     //输入数据
        {
            CheckXml();
            XDocument xdoc = XDocument.Load(txtPath);
            while (true) 
            {
                Console.WriteLine("请输入数据:");
                string line = Console.ReadLine();
                if (Regex.IsMatch(line, @"^[a-zA-Z]+$")) //英文
                {
                    xdoc.Element("root").Element("english").Value += "\r\n" + line;
                }
                else if (Regex.IsMatch(line, @"^\d+$")) //数字
                {
                    xdoc.Element("root").Element("number").Value += "\r\n" + line;
                }
                else if (Regex.IsMatch(line, @"^[一-龥]+$")) //中文
                {
                    xdoc.Element("root").Element("chinese").Value += "\r\n" + line;
                }
                else //混合
                {
                    xdoc.Element("root").Element("all").Value += "\r\n" + line;
                }
                xdoc.Save(txtPath, SaveOptions.None);
            }
        }
        static void OutputData() //输出数据
        {
            CheckXml();
            XDocument xdoc = XDocument.Load(txtPath);
            Console.WriteLine("英文:");
            Console.WriteLine(xdoc.Element("root").Element("english").Value);
            Console.WriteLine("数字:");
            Console.WriteLine(xdoc.Element("root").Element("number").Value);
            Console.WriteLine("中文:");
            Console.WriteLine(xdoc.Element("root").Element("chinese").Value);
            Console.WriteLine("混合:");
            Console.WriteLine(xdoc.Element("root").Element("all").Value);
        }

回答2:

这哪个老师出的题目,想累死机器啊,每个字符都要反复判断...