今天上午进行了公司的C# C level考核,最后一道编程题是关于员工信息系统的,题目的要求大概是这样的:1、要可以保存员工信息(XXXXX),并且要用正则表达式对输入信息进行验证;2、要可以对员工信息进行查询(根据员工号和部门两种方式)。

我是通过窗体程序实现的,窗体设计如下,一共三个,分别是主窗体界面、添加员工信息窗体和查找员工信息窗体:

程序如下:

主窗体——

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Xml;
  11. using System.Xml.Linq;
  12. using System.IO;
  13.  
  14. namespace WorkmatesInfoSystem
  15. {
  16. public partial class Form1 : Form
  17. {
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. }
  22.  
  23. private void button1_Click(object sender, EventArgs e)
  24. {
  25. AddInfo addInfoForm = new AddInfo();
  26. addInfoForm.ShowDialog();
  27. }
  28.  
  29. private void button2_Click(object sender, EventArgs e)
  30. {
  31. SearchInfo searchInfoForm = new SearchInfo();
  32. searchInfoForm.ShowDialog();
  33. }
  34. }
  35. }

添加员工信息窗体:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Text.RegularExpressions;
  11. using System.Xml;
  12. using System.Xml.Linq;
  13. using System.IO;
  14.  
  15. namespace WorkmatesInfoSystem
  16. {
  17. public partial class AddInfo : Form
  18. {
  19. public AddInfo()
  20. {
  21. InitializeComponent();
  22. }
  23.  
  24. private void textBox1_Click(object sender, EventArgs e)
  25. {
  26. if(WorkerNumber.Text == "The worker's nubmer must be a number(not a string or etc).")
  27. {
  28. WorkerNumber.Text = "";
  29. }
  30. }
  31.  
  32. private void textBox2_Click(object sender, EventArgs e)
  33. {
  34. if (WorkerName.Text == "Herry(For example)")
  35. {
  36. WorkerName.Text = "";
  37. }
  38. }
  39.  
  40. private void textBox3_Click(object sender, EventArgs e)
  41. {
  42.  
  43. if (WorkerEmail.Text == "The format should like \"tylan@Avpoint.com\"")
  44. {
  45. WorkerEmail.Text = "";
  46. }
  47. }
  48.  
  49. private void textBox4_Click(object sender, EventArgs e)
  50. {
  51. if (WorkerDepartment.Text == "CC_CIQA(For example)")
  52. {
  53. WorkerDepartment.Text = "";
  54. }
  55. }
  56.  
  57. private void button1_Click(object sender, EventArgs e)
  58. {
  59. //Check the format of the worker's number.
  60. if (Regex.IsMatch(WorkerNumber.Text, "^[0-9]*$") == false)
  61. {
  62. MessageBox.Show("Worker's number is in a wrong format, please type in again.");
  63. }
  64. //Check the Email's format of the worker.
  65. else if (Regex.IsMatch(WorkerEmail.Text,@"^\w+@\w+$") == false)
  66. {
  67. MessageBox.Show("Worker's Email is in a wrong format, please type in again.");
  68. }
  69. //Check the end format of the Email Address.
  70. else if (Regex.IsMatch(EmailEndFormat.Text, "^(com|net)$") == false)
  71. {
  72. MessageBox.Show("The end format of the Email is wrong, please type in again. You can type in 'com' or 'net'.");
  73. }
  74. //Add the worker's info into the xml.
  75. else
  76. {
  77. saveToXml(SavePath.Text.ToString());
  78. MessageBox.Show("Save the worker's info successfully.");
  79. }
  80. }
  81.  
  82. //Save to XML method.
  83. private void saveToXml(string xmlPath)
  84. {
  85. string filePath = xmlPath + "WorkersInfo.xml";
  86. FileInfo file = new FileInfo(@filePath);
  87. if (file.Exists == false)
  88. {
  89. File.Create(@filePath).Close();
  90. StreamWriter sw = new StreamWriter(@filePath);
  91. string content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
  92. "<System xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
  93. "<SystemName>WorkersInfo</SystemName>" +
  94. "<Workers>" +
  95. "</Workers>" +
  96. "</System>";
  97. sw.WriteLine(content);
  98. sw.Close();
  99. }
  100. //Load XML file.
  101. XElement xe = XElement.Load(@filePath);
  102. //Create a node info.
  103. XElement Worker = new XElement("Worker",
  104. new XElement("WorkerNumber", WorkerNumber.Text),
  105. new XElement("WorkerName", WorkerName.Text),
  106. new XElement("WorkerEmail", WorkerEmail.Text + "." + EmailEndFormat.Text),
  107. new XElement("WorkerDepartment", WorkerDepartment.Text)
  108. );
  109. //Add the node under the specific node.
  110. xe.Element("Workers").Add(Worker);
  111. //Save the XML file.
  112. xe.Save(filePath);
  113. }
  114.  
  115. private void SavePath_Click(object sender, EventArgs e)
  116. {
  117. if(SavePath.Text==@"C:\Tylan\Workers(For example)")
  118. {
  119. SavePath.Text = "";
  120. }
  121. }
  122. }
  123. }

查找员工信息窗体:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Xml;
  11. using System.Xml.Linq;
  12. using System.IO;
  13.  
  14. namespace WorkmatesInfoSystem
  15. {
  16. public partial class SearchInfo : Form
  17. {
  18. public SearchInfo()
  19. {
  20. InitializeComponent();
  21. }
  22.  
  23. private void SearchDepartmentButton_Click(object sender, EventArgs e)
  24. {
  25. XDocument workersInfo = XDocument.Load(@SearchPath.Text.ToString());
  26. var results = from worker in workersInfo.Element("System").Element("Workers").Elements() where worker.Element("WorkerDepartment").Value == SearchWorkerDepartment.Text.ToString() select worker;
  27. foreach (var result in results)
  28. {
  29. MessageBox.Show(result.Element("WorkerNumber").Value + "\n" + result.Element("WorkerName").Value + "\n" + result.Element("WorkerEmail").Value + "\n" + result.Element("WorkerDepartment").Value);
  30. }
  31. }
  32.  
  33. private void SearchNumberButton_Click(object sender, EventArgs e)
  34. {
  35. XDocument workersInfo = XDocument.Load(@SearchPath.Text.ToString());
  36. var results = from worker in workersInfo.Element("System").Element("Workers").Elements() where int.Parse(worker.Element("WorkerNumber").Value) == int.Parse(SearchWokerNumber.Text.ToString()) select worker;
  37. foreach (var result in results)
  38. {
  39. MessageBox.Show(result.Element("WorkerNumber").Value + "\n" + result.Element("WorkerName").Value + "\n" + result.Element("WorkerEmail").Value + "\n" + result.Element("WorkerDepartment").Value);
  40. }
  41. }
  42. }
  43. }

由于考试时间仓促,只是在功能上满足了这道题的要求,匆匆收尾,还请大家只关注以下三点在实际应用中的实践:

1、使用LinQ查询员工信息;

2、添加员工信息到XML的过程;

3、通过正则表达式验证用户输入信息。

C#中一道关于员工信息系统的题(主要考察LinQ和正则表达式验证)的更多相关文章

  1. C#中一道关于多线程的编程题

    题目的意思是这样的:让两个线程A和B将自己的ID轮番写入一个文件中,每个线程重复十次写入后执行一个回调函数,说“I'm OK”,就这样.我是一名QA,不是开发,出于兴趣报考了公司的C#课程考试,多线程 ...

  2. 一道很经典的 BFS 题

    一道很经典的 BFS 题 想认真的写篇题解. 题目来自:https://www.luogu.org/problemnew/show/P1126 题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运 ...

  3. python--简易员工信息系统编写

    补充内容:eval 将字符串变成变量名locals   看输入的是否是字典中的一个keyfunc.__name____怎么看变量名的数据类型斐波那契数列 li=[1,1] while li[-1]&l ...

  4. ACM_一道耗时间的水题

    一道耗时间的水题 Time Limit: 2000/1000ms (Java/Others) Problem Description: Do you know how to read the phon ...

  5. 一道有趣的for循环题

    一道有趣的for循环题 今天在复习js基础知识时发现了一个for循环的题,第一眼看到直接懵逼了,没想到for循环竟然还可以这样玩?涨姿势了. 题目是这样的 for(i=0, j=0; i<10, ...

  6. SQL-50 将employees表中的所有员工的last_name和first_name通过(')连接起来

    题目描述 将employees表中的所有员工的last_name和first_name通过(')连接起来.CREATE TABLE `employees` (`emp_no` int(11) NOT ...

  7. 【图灵杯 F】一道简单的递推题(矩阵快速幂,乘法模板)

    Description 存在如下递推式: F(n+1)=A1*F(n)+A2*F(n-1)+-+An*F(1) F(n+2)=A1*F(n+1)+A2*F(n)+-+An*F(2) - 求第K项的值对 ...

  8. 检查字符串长度 检查字符串是否为空 用正则表达式验证出版物的ISBN号 用正则表达式验证邮证编码 验证字符串中是否含有汉字

    <?php /** * 常用的正则表达式来验证信息.如:网址 邮箱 手机号等 */ class check { /** * 正则表达式验证email格式 * * @param string $s ...

  9. Python抓取页面中超链接(URL)的三中方法比较(HTMLParser、pyquery、正则表达式) <转>

    Python抓取页面中超链接(URL)的3中方法比较(HTMLParser.pyquery.正则表达式) HTMLParser版: #!/usr/bin/python # -*- coding: UT ...

随机推荐

  1. 〖Windows〗zigbee实验之cygwin编译tinyos.jar编译出错的解决方法

    1. 使用的cygwin安装包下载地址:cygwin-files.zip 2. 使用的一些rpm安装包的下载地址:cygwin_cc2430_rpms.zip 3. cygwin的默认安装目录是:C: ...

  2. jquery实现下拉联动

    很多项目用到这个功能,虽然写了不下5次以上了,一直没做过记录,记录一下,下次直接拷贝了,免得还得要重复写浪费时间. 先上HTML代码: 品牌: <select class="sa&qu ...

  3. Windows下安装OpenSSL及其使用

    方法一: Windows binaries can be found here: http://www.slproweb.com/products/Win32OpenSSL.html You can ...

  4. 触发器学习笔记(:new,:old用法)

    触发器学习笔记(:new,:old用法) 触发器是数据库发生某个操作时自动运行的一类的程序         用于保持数据的完整性或记录数据库操作信息方面         触发器不能够被直接调用,只能够 ...

  5. WINDOWS操作系统中可以允许最大的线程数

      默认情况下,一个线程的栈要预留1M的内存空间 而一个进程中可用的内存空间只有2G,所以理论上一个进程中最多可以开2048个线程 但是内存当然不可能完全拿来作线程的栈,所以实际数目要比这个值要小.  ...

  6. coding云进行git push报:permission denied

    1.原因可能是 登录其他的git 项目,本地缓存了其他用户的 用户名和密码 认证信息,导致一直权限不通过 解决: git remote add origin http://yourname:passw ...

  7. PHP扩展的基本结构

    1.下载php源码 git clone https://github.com/php/php-src.git  2,创建扩展 cd php-src/ext/ ./ext_skel --extname= ...

  8. shell脚本死循环判断nginx日志reqest_time时间大于3秒是否增加,若增加发送相关日志信息到开发人员

    #!/bin/bash while [ 1 ] do pre_request_time_count=`cat /var/log/nginx/access.log |awk '{print $NF}'| ...

  9. js replaceChild

    //父亲元素.replaceChild(新,旧) 1 <ul id="city"> <li id="bj">北京</li> ...

  10. HDUOJ-----Difference Between Primes

    Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...