| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 739 人关注过本帖
标题:求一段代码,谢谢
只看楼主 加入收藏
wqs904071694
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-6-17
结帖率:0
收藏
已结贴  问题点数:20 回复次数:8 
求一段代码,谢谢
用c#编写一个学生通讯录《控制台应用程序》1.不能用数据库存放数据,用文件存放。
2.要有增加,删除,修改,查找的功能。
3.数据字段要有,学生(主键),姓名,电话号码,住址。
就这些了,谢谢。。我不知道怎么做!
求高手帮助我,谢谢
搜索更多相关主题的帖子: 应用程序 通讯录 数据库 控制台 
2013-06-20 21:23
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:5 
ConsoleStuAddressBook.rar (32.98 KB)

可能有些bug,自己再改改就好
2013-06-20 23:09
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
2楼代码如下
class Program
    {
        static void Main(string[] args)
        {
            LoadInfo();
            LoadDataSet();
            Input();
        }

        private static void LoadInfo()
        {
            Console.WriteLine("**********通迅录**********");
            Console.WriteLine("1 新增");
            Console.WriteLine("2 修改");
            Console.WriteLine("3 删除");
            Console.WriteLine("4 显示");
            Console.WriteLine("5 查找");
            Console.WriteLine("6 保存");
            Console.WriteLine("7 退出");
            Console.WriteLine("**************************");
        }

        private static void LoadDataSet()
        {
            Oper.ds = new DataSet();
            Oper.initDataSet();
            ClsSerial serial = new ClsSerial();
            Oper oper = (Oper)serial.DeSerializeNow(Application.StartupPath + "\\stu.dll");
            if (oper != null && oper.list.Count > 0)
            {
                IEnumerator ie = oper.list.GetEnumerator();
                while (ie.MoveNext())
                {
                    Student stu = (Student)ie.Current;
                    DataTable dt = Oper.ds.Tables["student"];
                    dt.Rows.Add(new string[] { stu.StuNum, stu.StuName, stu.StuPhoneNum, stu.StrAddress });
                }
            }
        }

        private static void Input()
        {
            bool b = false;
            while (true)
            {
                string str = Console.ReadLine();
                if (str == "1")
                    Add();
                else if (str == "2")
                    Edit();
                else if (str == "3")
                    Del();
                else if (str == "4")
                    Show();
                else if (str == "5")
                    Find();
                else if (str == "6")
                    Save();
                else if (str == "7")
                    Exit(out b);
                if (b)
                {
                    return;
                }
            }
        }

        private static void Add()
        {
            Console.WriteLine("***********新增***********");
            Student stu = new Student();
            Console.Write("学号:");
            stu.StuNum = Console.ReadLine();
            Console.Write("姓名:");
            stu.StuName = Console.ReadLine();
            Console.Write("电话:");
            stu.StuPhoneNum = Console.ReadLine();
            Console.Write("地址:");
            stu.StrAddress = Console.ReadLine();

            DataTable dt = Oper.ds.Tables["student"];
            try
            {
                dt.Rows.Add(new string[] { stu.StuNum, stu.StuName, stu.StuPhoneNum, stu.StrAddress });
            }
            catch (ConstraintException ex)
            {
                Console.WriteLine("学号重复!");
                Add();
            }
            Console.WriteLine("**************************");
        }

        private static void Edit()
        {
            DataTable dt = Oper.ds.Tables["student"];
            if (dt.Rows.Count > 0)
            {
                Console.WriteLine("***********修改***********");
                Console.Write("需要修改的学生(学号):");
                string Num = Console.ReadLine();
                DataRow[] dr = dt.Select("StuNum = " + Num);
                if (dr.Length > 0)
                {
                    Console.Write("姓名:");
                    dr[0]["StuName"] = Console.ReadLine();
                    Console.Write("电话:");
                    dr[0]["StuPhoneNum"] = Console.ReadLine();
                    Console.Write("地址:");
                    dr[0]["StrAddress"] = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("通迅录中未找到该学生!");
                }
            }
            else
            {
                Console.WriteLine("通迅录中没有学生!");
            }
            Console.WriteLine("**************************");
        }

        private static void Del()
        {
            DataTable dt = Oper.ds.Tables["student"];
            if (dt.Rows.Count > 0)
            {
                Console.WriteLine("***********修改***********");
                Console.Write("需要删除的学生(学号):");
                string Num = Console.ReadLine();
                DataRow[] dr = dt.Select("StuNum = " + Num);
                if (dr.Length > 0)
                {
                    dt.Rows.Remove(dr[0]);
                }
                else
                {
                    Console.WriteLine("通迅录中未找到该学生!");
                }
            }
            else
            {
                Console.WriteLine("通迅录中没有学生!");
            }
            Console.WriteLine("**************************");
        }

        private static void Show()
        {
            Console.WriteLine("***********显示***********");
            Console.WriteLine("学号\t\t姓名\t\t电话\t\t地址");
            DataTable dt = Oper.ds.Tables["student"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Console.WriteLine(dt.Rows[i]["StuNum"] + "\t\t" + dt.Rows[i]["StuName"] + "\t\t" + dt.Rows[i]["StuPhoneNum"] + "\t\t" + dt.Rows[i]["StrAddress"]);
                }
            }
            Console.WriteLine("**************************");
        }

        private static void Find()
        {
            DataTable dt = Oper.ds.Tables["student"];
            if (dt.Rows.Count > 0)
            {
                Console.WriteLine("***********查找***********");
                Console.Write("需要查找的学生(学号):");
                string Num = Console.ReadLine();
                DataRow[] dr = dt.Select("StuNum = " + Num);
                if (dr.Length > 0)
                {
                    Console.WriteLine("学号\t\t姓名\t\t电话\t\t地址");
                    Console.WriteLine(dr[0]["StuNum"] + "\t\t" + dr[0]["StuName"] + "\t\t" + dr[0]["StuPhoneNum"] + "\t\t" + dr[0]["StrAddress"]);
                }
                else
                {
                    Console.WriteLine("通迅录中未找到该学生!");
                }
            }
            else
            {
                Console.WriteLine("通迅录中没有学生!");
            }
            Console.WriteLine("**************************");
        }

        private static void Save()
        {
            string path = Application.StartupPath + "\\stu.dll";
            Oper oper = new Oper();
            oper.AddList();
            ClsSerial serial = new ClsSerial(path, oper);
            serial.SerializeNow();
            Console.WriteLine("*********保存成功*********");
        }

        private static void Exit(out bool b)
        {
            Console.Write("所有未保存的数据将丢失,确认退出(Y/N)");
            string str = Console.ReadLine();
            b = false;
            if (str == "Y" || str == "y")
            {
                b = true;
            }
        }
    }

    [Serializable]
    public class Student
    {
        public string StuNum { get; set; }
        public string StuName { get; set; }
        public string StuPhoneNum { get; set; }
        public string StrAddress { get; set; }
    }

    [Serializable]
    public class Oper
    {
        public static DataSet ds = null;
        public ArrayList list = null;

        public static void initDataSet()
        {
            DataTable dt = new DataTable();
            dt.TableName = "student";
            dt.Columns.Add("StuNum");
            dt.Columns.Add("StuName");
            dt.Columns.Add("StuPhoneNum");
            dt.Columns.Add("StrAddress");
            dt.PrimaryKey = new DataColumn[] { dt.Columns["StuNum"] };
            if (ds != null)
            {
                ds.Tables.Add(dt);
            }
        }

        public void AddList()
        {
            if (ds != null)
            {
                DataTable dt = ds.Tables["student"];
                list = new ArrayList();
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        Student stu = new Student();
                        stu.StuNum = dt.Rows[i]["StuNum"].ToString();
                        stu.StuName = dt.Rows[i]["StuName"].ToString();
                        stu.StuPhoneNum = dt.Rows[i]["StuPhoneNum"].ToString();
                        stu.StrAddress = dt.Rows[i]["StrAddress"].ToString();
                        list.Add(stu);
                    }
                }
            }
        }
    }

public class ClsSerial
    {
        private string path = "";
        private object obj = null;
        private Type type = null;

        public ClsSerial()
        {
        }

        public ClsSerial(string path, object obj)
        {
            this.path = path;
            this.obj = obj;
        }

        public void SerializeNow(string path, object obj)
        {
            try
            {
                this.path = path;
                this.obj = obj;
                SerializeNow();
            }
            catch (Exception ex)
            {
               
            }
        }

        public void SerializeNow()
        {
            try
            {   
                FileStream fileStream = new FileStream(path, FileMode.Create);
                BinaryFormatter b = new BinaryFormatter();
                b.Serialize(fileStream, obj);
                fileStream.Close();
            }
            catch (Exception ex)
            {
               
            }
        }

        public object DeSerializeNow(string path)
        {
            try
            {
                this.path = path;
                return DeSerializeNow();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public object DeSerializeNow()
        {
            try
            {
                object obj = null;
                FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                BinaryFormatter b = new BinaryFormatter();
                obj = b.Deserialize(fileStream);
                fileStream.Close();
                return obj;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

    }
2013-06-20 23:11
QJlin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:186
专家分:560
注 册:2013-5-18
收藏
得分:5 

慢慢前进走,不求一步登天,只求慢慢前进
2013-06-20 23:37
wqs904071694
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-6-17
收藏
得分:0 
回复 2楼 yhlvht
谢谢你!非常感谢!.....
2013-06-21 09:14
黄辉
Rank: 4
等 级:业余侠客
帖 子:106
专家分:255
注 册:2013-4-28
收藏
得分:5 
学习中。。。

你不勇敢,谁替你坚强。。。
2013-06-21 09:27
wqs904071694
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-6-17
收藏
得分:0 
回复 2楼 yhlvht
求教,这个程序运用了什么样的设计原谅。可以你觉得这个问题很好笑,但是我真的不清楚
2013-06-23 18:03
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
因为没有几行代码,也说不上什么设计原理
总共就4个类
Program类,增删查改方法封装在这个类中
Student类,通讯录的学号,姓名,电话,地址封装在这个类中
Oper类,封装了一个DataSet,程序运行时,通讯录的数据存放在这里面
ClsSerial类,用于将数据写入文件以及从文件中读取数据
2013-06-23 19:29
杰与贤3
Rank: 2
等 级:论坛游民
帖 子:11
专家分:18
注 册:2013-5-17
收藏
得分:5 
2013-06-23 21:46
快速回复:求一段代码,谢谢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027563 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved