C#实验报告上机六

题目一:编写C#程序,统计硬盘某个目录下的abc.txt文件中单词的个数。提示:要用到字符串类中的分割字符串等函数

源程序:

using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace 上机六
{
    public partial class 统计单词数 : Form
    {
        public 统计单词数()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string file = textBox1.Text;
            if (!File.Exists(@file))
                MessageBox.Show("文件" + @file + "不存在");
            else
            {
                int a;
                FileStream fs = new FileStream(@file, FileMode.Open, FileAccess.Read);
                richTextBox1.Clear();
                a = fs.ReadByte();
                while (a != -1)       //是否读到文件末尾
                {
                    richTextBox1.Text += ((char)a).ToString();
                    a = fs.ReadByte();
                }
                fs.Close();
            }

        }
        /*
         * 正则表达式中“\d”表示[0-9]的数字,“\d+”表示由[0-9]的数字组成的数字,“\w”表示[A-Z0-9],
         * “\w+”表示由数字、26 个英文字母或者下划线组成的字符串,“\d+.+\d+”表示小数
         */
        private void button2_Click(object sender, EventArgs e)
        {
            Regex reg = new Regex("\\S+\\w+");
            string InputStr = richTextBox1.Text;
            int Count = reg.Split(InputStr).Count() - 1;
            richTextBox2.Text = "单词个数:" + Count.ToString();
        }
    }
}

运行结果: 

 题目二:编写一个重复文件的检测程序:程序可以实现重复文件检测(即将硬盘某个盘符下的重复文件以ListBox控件列表的形式显示出来,例如:有1.doc、2.doc、3.doc完全一样,则这三个应该放在同一个ListBox1控件中;而a.exe、b.exe完全一样,则放在另一个ListBox2控件中)(可由设计者自行设计分组)。

源程序:

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace 上机六
{
    public partial class 重复文件检测 : Form
    {
        public 重复文件检测()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }

        string path = @"D:\demo";
        string checktxt = "";
        private void button1_Click(object sender, EventArgs e)
        {
            List<string> files = new List<string>();
            ForeachFile(path, ref files);
            foreach (var f in files)
            {
                try
                {
                    string txt = "";
                    using (StreamReader sr = new StreamReader(f.ToString()))
                    {
                        string line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            txt += line + "\n";
                            Console.WriteLine(line);
                        }
                    }
                    if (checktxt == "")
                    {
                        listBox1.Items.Add(f.ToString());
                        checktxt = txt;
                    }
                    else if (txt == checktxt)
                    {
                        listBox1.Items.Add(f.ToString());
                    }
                    else
                    {
                        listBox2.Items.Add(f.ToString());
                    }


                }
                catch
                {
                    Console.WriteLine("The file could not be read:");
                }
            }
        }
        public static void ForeachFile(string filePathByForeach, ref List<string> result)

        {
            DirectoryInfo theFolder = new DirectoryInfo(filePathByForeach);
            DirectoryInfo[] dirInfo = theFolder.GetDirectories();//获取所在目录的文件夹
            FileInfo[] file = theFolder.GetFiles();//获取所在目录的文件

            foreach (FileInfo fileItem in file) //遍历文件
            {
                result.Add(fileItem.DirectoryName + "\\" + fileItem.Name);
            }
            //遍历文件夹
            foreach (DirectoryInfo NextFolder in dirInfo)
            {
                ForeachFile(NextFolder.FullName, ref result);
            }
        }
    }
}

运行结果: 

题目三:编程实现个人通讯录信息的添加、删除、查找和更新功能。要求数据库可自行选取,采用编程方式实现,界面自行设计。

源程序:

Main.cs

namespace 手机通讯录系统
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }


        private void btn_register_Click(object sender, EventArgs e)
        {
            register register = new register();
            register.Show();
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            login login = new login();
            login.Show();
        }
    }
}

register.cs

namespace 手机通讯录系统
{
    public partial class register : Form
    {
        public register()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }
        static String connStr = "Data Source=DESKT*****V62P;Initial Catalog=S_T;Persist Security Info=True;User ID=sa;Password=*****";
        private void register_click_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            string sql2 = "INSERT INTO users (user_id, username, password) VALUES (" + new Random().Next(99) + ", '" + t_name.Text + "', '" + t_password.Text + "')";
            SqlCommand cmd = new SqlCommand(sql2, conn);
            int i = cmd.ExecuteNonQuery();
            if (i == 1)
            {
                MessageBox.Show("注册成功!");
                this.Close();
            }
            else
            {
                MessageBox.Show("注册失败!");
            }
        }
    }
}

login.cs

namespace 手机通讯录系统
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }
        static String connStr = "Data Source=DESK*****62P;Initial Catalog=S_T;Persist Security Info=True;User ID=sa;Password=*****";
        private void login_click_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            string sql2 = "SELECT count(*) count FROM users where username='" + t_name.Text + "' and password = '" + t_password.Text + "'";
            SqlCommand cmd = new SqlCommand(sql2, conn);
            int i = (int)cmd.ExecuteScalar();
            if (i > 0)
            {
                phoneInfo message = new phoneInfo();
                message.Show();
                this.Close();
            }
            else
            {
                MessageBox.Show("登录失败!");
            }
        }
    }
}

phoneInfo.cs

namespace 手机通讯录系统
{
    public partial class phoneInfo : Form
    {
        static String connStr = "Data Source=DES******V62P;Initial Catalog=S_T;Persist Security Info=True;User ID=sa;Password=*****";
        SqlCommand cmd;
        int RowIndex = -1;
        public phoneInfo()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            Load();
        }
        public new void Load()
        {
            this.ManagePhone_Load(null, null);
        }

        private void ManagePhone_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            String sql = "select * from phone";
            SqlCommand cmd = new SqlCommand(connStr);
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            phoneInfo manage = this;
            addPhone addStudent = new addPhone(conn, manage);
            addStudent.Show();
            ManagePhone_Load(sender, e);
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Selected == true)
                {
                    RowIndex = i;
                }
            }
            if (RowIndex == -1)
            {
                MessageBox.Show("请选择学生再修改!");
            }
            else
            {
                SqlConnection conn = new SqlConnection(connStr);
                conn.Open();
                phoneInfo manage = this;
                updatePhone phone = new updatePhone(dataGridView1.Rows[RowIndex].Cells, conn, manage);
                phone.Show();
                ManagePhone_Load(sender, e);
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Selected == true)
                {
                    RowIndex = i;
                }
            }
            if (RowIndex == -1)
            {
                MessageBox.Show("请选择学生再删除!");
            }
            else
            {
                DialogResult result = MessageBox.Show("确定删除" + dataGridView1.Rows[RowIndex].Cells[1].Value + "的信息?", "删除", MessageBoxButtons.OKCancel);
                int pid = 0;
                if (result == DialogResult.OK)
                {
                    SqlConnection conn = new SqlConnection(connStr);
                    conn.Open();
                    pid = Convert.ToInt32(dataGridView1.Rows[RowIndex].Cells[0].Value);
                    Console.WriteLine(pid);
                    string sql2 = "delete from phone where pid =" + pid;
                    cmd = new SqlCommand(sql2, conn);
                    cmd.ExecuteNonQuery();
                    RowIndex = -1;
                }
                ManagePhone_Load(sender, e);
            }
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
          
        }

        private void phoneInfo_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“s_TDataSet.phone”中。您可以根据需要移动或移除它。
            this.phoneTableAdapter.Fill(this.s_TDataSet.phone);

        }
    }
}

addPhone.cs

namespace 手机通讯录系统
{
    public partial class addPhone : Form
    {
        private SqlConnection conn;
        private phoneInfo manage;

        public addPhone(SqlConnection conn, phoneInfo manage)
        {
            this.conn = conn;
            this.manage = manage;
            InitializeComponent();

        }

        public addPhone()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            string sql2 = "insert into phone (pid, pname, pnumber, pemail) VALUES (" + new Random().Next(999) + ", '" + text_name.Text + "', '" + text_phone.Text + "', '" + text_email.Text + "')";
            SqlCommand cmd = new SqlCommand(sql2, conn);
            MessageBox.Show(sql2);
            cmd.ExecuteNonQuery();
            conn.Close();
            Console.Write(sql2);
            MessageBox.Show("添加成功!");
            manage.Load();
            this.Close();
        }
    }
}

updatePhone.cs

namespace 手机通讯录系统
{
    public partial class updatePhone : Form
    {
        private DataGridViewCellCollection row = null;
        private SqlConnection conn = null;
        private phoneInfo manage = null;
        private int status = 0;
        public updatePhone(DataGridViewCellCollection row, SqlConnection conn, phoneInfo manage)
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            this.conn = conn;
            this.row = row;
            this.text_id.Text = Convert.ToString(row[0].Value);
            this.text_name.Text = Convert.ToString(row[1].Value);
            this.text_phone.Text = Convert.ToString(row[2].Value);
            this.text_email.Text = Convert.ToString(row[3].Value);
            int pid = Convert.ToInt32(row[0].Value);
            this.manage = manage;
        }
       
        public int getStatus()
        {
            return status;
        }


        private void btn_update_Click(object sender, EventArgs e)
        {
            int pid = Convert.ToInt32(this.text_id.Text);
            Console.WriteLine(pid);
            string sql2 = "update phone set pname = '" + text_name.Text + "',pnumber = '" + text_phone.Text + "',pemail = '" + text_email.Text + "' where pid =" + pid;
            SqlCommand cmd = new SqlCommand(sql2, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            Console.Write(sql2);
            MessageBox.Show("修改成功!");
            this.status = -1;
            manage.Load();
            this.Close();
        }
    }
}

运行结果: 

手机通讯系统

注册

登录

 手机通讯录系统信息

添加新联系人

删除联系人

修改联系人

猜你喜欢

转载自blog.csdn.net/qq_45037155/article/details/124744812