当前位置: 代码迷 >> C# >> c#编纂随机点名器,被老师用在教学中
  详细解决方案

c#编纂随机点名器,被老师用在教学中

热度:311   发布时间:2016-04-28 08:39:36.0
c#编写随机点名器,被老师用在教学中
 随机点名器功能又一大提升,而且之前若干bug均已修复。
修复内容:
1.当默认开启的txt文本文件被删除或的文件名被更改或由于某种未知的原因不能访问时,程序不会报错,而是将一些提示信息以学生名导入。
例如:
程序默认访问“电科一班.txt”:
如果在可执行文件的路径下有 “电科一班.txt”文件,则运行正常。


如果在可执行文件的路径下没有找到 “电科一班.txt”文件,则运行提示信息。
 
 
在这种非正常情况下,如果仍然点击正式记录,则弹出提示帮助信息窗口。
 
 
这样,用户在看到提示信息后,会自行修复该问题。

修复2:
若读取的文件中存在空行,则程序在读取文件内容的时候会自动删除空行。
 现在,我把先前文件名“电科一班121314.txt”修改为“电科一班.txt”,然后在文件内容中加两个空行,如下图所示:
 
程序在运行的时候再也不会出现空姓名。

程序运行一圈后,会给出提示信息。
 
功能提升:
1.可以选择班级,班级内容自定义。
现在,我又新建了一个班级文件“1班.txt”,文件内容如下:

 现在可以选择班级,我就把“1班.txt”选进来。
 
 一般,“选择班级...”,在程序打开后只会用一次,点击“打开”后,“选择班级...”按钮被隐藏。
 
2.增加出勤记录,答问记录功能。
若没有点击“正式记录”按钮,抽取信息不会被记录,也没有弹出出勤相关的选择按钮。功能与旧版一样。
一旦点击正式记录,由于这个记录涉及到学生的平时成绩,因此非常重要,所以弹出是否确定选择对话框。
 
点击确定后,”正式记录“按钮被隐藏。
 
再次抽取后,会弹出“缺勤”和“差评”两个选择按钮,并自动在该班级目录下新建一个“班级名+点名记录清单”文本文件。
 
文本内容自动记录第一次被点到的学生,并创建时间,原创者,班级信息相关信息。如下所示:
 
若点击差评或缺勤,记录会自动加一,而且,一旦选择之后,该两个按钮即被隐藏,防止误操作。
当然你也可以不行,直接抽下一个人,但选择按钮照样被隐藏,同样该学生的点名信息也会被记录。
 重复抽取后所有记录都会自动保存到该文件。
 
如果点击确定,或关闭程序再次打开程序选择该班级,同样执行上述步骤,则记录会在原来的基础上累加。
 经过一系列操作后得到的记录表如下:
 
当然,如果你选择其他班级,照样会执行相应的操作。
现在,举个典型的例子,我在桌面上新建一个文本文件,内容如下:
 
选择该文件,执行同样的操作,立即生成“2班点名记录清单.txt”文件。
 
 重复点名后,实时记录的点名内容:


 C#部分源程序:(本软件完全由个人编写,未经本人允许,请勿用于商业目的)using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Drawing2D;
namespace 随机抽样
{
    public partial class Form1 : Form
    {
        static int step = 0;
        static bool Startload = false;
        static string[] pathstudent = {System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"//","电科一班.txt"};
        static string[] name = getName();
        static int[] ppp = GetRandomArray(name.Length);
        public Form1()
        {
            InitializeComponent();
            Timer t = new Timer();
            t.Interval =2000;
            t.Tick += new EventHandler(changeBackground);
            t.Start();
            Timer t2 = new Timer();
            t2.Interval = 100;
            t2.Tick += new EventHandler(changestudent);
            t2.Start();
        }
        static int iiii = 0;
        void changestudent(object sender, EventArgs e)
        {
            if (name == null)
            {
                return;
            }
            if (this.startrand.Text=="停  止  抽  取")
            {
                iiii++;
                this.disinf.Text =name[ppp[(step+iiii)%name.Length]];
                int[]  q = GetRandomArray(3);
                int[] colorval = { 0, 100, new Random().Next(100) };
                this.disinf.ForeColor = Color.FromArgb(255, colorval[q[0]], colorval[q[1]], colorval[q[2]]);
            }
            
        }
        void changeBackground(object sender, EventArgs e)
        {
            //int i = DateTime.Now.Second/15;
            switch (GetRandomArray(18)[0])
            {
                case 0: BackgroundImage =imageList1.Images[0]; break;
                case 1: BackgroundImage = imageList1.Images[1]; break;
                case 2: BackgroundImage = imageList1.Images[2]; break;
                case 3: BackgroundImage = imageList1.Images[3]; break;
                case 4: BackgroundImage = imageList1.Images[4]; break;
                case 5: BackgroundImage = imageList1.Images[5]; break;
                case 6: BackgroundImage = imageList1.Images[6]; break;
                case 7: BackgroundImage = imageList1.Images[7]; break;
                case 8: BackgroundImage = imageList1.Images[8]; break;
                case 9: BackgroundImage = imageList1.Images[9]; break;
                case 10: BackgroundImage = imageList1.Images[10]; break;
                case 11: BackgroundImage = imageList1.Images[11]; break;
                case 12: BackgroundImage = imageList1.Images[12]; break;
                case 13: BackgroundImage = imageList1.Images[13]; break;
                case 14: BackgroundImage = imageList1.Images[14]; break;
                case 15: BackgroundImage = imageList1.Images[15]; break;
                case 16: BackgroundImage = imageList1.Images[16]; break;
                case 17: BackgroundImage = imageList1.Images[17]; break;
                default: break;
            }
        }
        private void startrand_Click(object sender, EventArgs e)//开始暂停按钮
        {
            this.button1.Visible = false;
            this.button2.Visible = false;
            //获取对比度大的随机深彩色
            int[] q = GetRandomArray(3);
            int[] colorval = { 170, 255, new Random().Next(80)+170 };
            startrand.FlatAppearance.MouseOverBackColor = Color.FromArgb(255, colorval[q[0]], colorval[q[1]], colorval[q[2]]);//随机设置鼠标悬停在按钮上的颜色
            startrand.FlatAppearance.MouseDownBackColor = Color.FromArgb(255, colorval[q[0]] - 50, colorval[q[1]] - 50, colorval[q[2]] - 50);//随机设置鼠标按下按钮后的颜色
            try
            {
                if (this.startrand.Text=="开  始  抽  取")//判断字符串检测鼠标是否按下
                {
                    this.startrand.Text = "停  止  抽  取";//如果按下,将字符串的颜色修改
                }
                else
                {
                    String[] abcd={""};
                    StreamWriter sw;
                    step=step+1;
                    this.startrand.Text = "开  始  抽  取";
                    this.disinf.Text = name[ppp[step - 1]];
                    if (Startload==true)
                    {
                        try
                        {
                            abcd = File.ReadAllLines(pathstudent[0] + pathstudent[1].Replace(".txt", "点名记录清单.txt"));//编码格式
                        }
                        catch
                        {
                            sw = File.AppendText(pathstudent[0] + pathstudent[1].Replace(".txt", "点名记录清单.txt"));
                            sw.Write(pathstudent[1].Replace(".txt", "") + "点名记录清单" + "创建日期:" + DateTime.Today.ToString("yyyy" + "年" + "MM" + "月" + "dd" + "日") + "    随机点名器原创者:陈鹏" + "\r\n");
                            sw.Write("学生姓名(学号)".PadRight(30 - 8, ' ') + "被点次数".PadRight(20 - 4, ' ') + "缺勤次数".PadRight(20 - 4, ' ') + "老师不满意次数\r");
                            sw.Close();
                        }
                        abcd = Array.FindAll(abcd, line => !string.IsNullOrEmpty(line)); // 去除是null或是""的string
                        string n;
                        n = findn(abcd, this.disinf.Text);
                        sw = File.AppendText(pathstudent[0] + pathstudent[1].Replace(".txt", "点名记录清单.txt"));
                        if (n == "1")
                        {
                            sw.Write("\n" + this.disinf.Text.PadRight(30, ' ') + n.PadRight(20, ' ') + "0".PadRight(20, ' ') + "0".PadRight(5, ' ') + "\r");//string1.PadLeft(100," ")这句话的意思是如果string1的长度不够100位,则在前面加空格,知道补够100位。
                        }
                        else
                        {
                            sw.Write("\n" + this.disinf.Text.PadRight(30, ' ') + n + "\r");
                        }
 
                        sw.Close();
                        //sw.Flush();
                        this.button1.Visible = true;
                        this.button2.Visible = true;
                    }
                }
            }
            catch
            {
                this.disinf.Text = "木有了咦(;`)";
                MessageBox.Show("人已经抽完了,重新开始吧?");
                ppp = GetRandomArray(name.Length);//重新排序
                step = 0;
                iiii = 0;
                return;
            }
        }
        string findn(string[] f,string w)
        {
            int n = 1;
            string a;
                for (int i = 0; i < f.Length; i++)//查找文件夹中是否存在该学生的信息
                {
                    try
                    {
                        a=f[i].Substring(0,w.Length);//从第m个位置,截取n个长度
                        if (a == w)//如果找到了学生的信息,刷新学生的得分
                        {
                            a = f[i].Substring(25, 10).Replace(" ", "");
                            n=Convert.ToInt32(a);
                            StreamWriter sw = new StreamWriter(pathstudent[0] + pathstudent[1].Replace(".txt", "点名记录清单.txt"), false);
                            for (int j = 0; j < f.Length; j++)
                            {
                                if (i != j)
                                {
                                    sw.WriteLine(f[j]);
                                }
                            }
                            //sw.Flush();
                            sw.Close();
                            //sw.Dispose();
                            return (n + 1).ToString() .PadRight(20,' ')+ f[i].Substring(50,25);
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }
            return n.ToString();
        }
       
可执行文件下载链接以及下载密码
链接:http://pan.baidu.com/s/1gdH6QL9 密码:f1p2  
------解决思路----------------------
这是what......
------解决思路----------------------

共享?
------解决思路----------------------
我们老师 都直接用微信,,
------解决思路----------------------
引用
C#部分源程序:(本软件完全由个人编写,未经本人允许,请勿用于商业目的)


我商用了会怎样?
------解决思路----------------------
这是要共享吗
------解决思路----------------------
快看,被外星人绑架的楼主终于被放回来了
  相关解决方案