using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace testtest { class PersonHabit { private int userid; public int Userid { get { return userid; } set { userid = value; } } private int age; public int Age { get { return age; } set { age = value; } } private int sex; public int Sex //0为男,1为女,2为不男不女 { get { return sex; } set { sex = value; } } private DateTime LHSetTime; public DateTime LHSetTime1 { get { return LHSetTime; } set { LHSetTime = value; } } private int dietHabit; public int DietHabit { get { return dietHabit; } set { dietHabit = value; } } private int drinkHabit; public int DrinkHabit { get { return drinkHabit; } set { drinkHabit = value; } } private int sportHabit; public int SportHabit { get { return sportHabit; } set { sportHabit = value; } } private int smokeHabit; public int SmokeHabit { get { return smokeHabit; } set { smokeHabit = value; } } private int sleepHabit; public int SleepHabit { get { return sleepHabit; } set { sleepHabit = value; } } public PersonHabit() { Random rd = new Random(); //LHSetTime = new DateTime(System.DateTime.Now.Millisecond); Random random = new Random((int)(DateTime.Now.Ticks)); int hour = random.Next(2, 5); int minute = random.Next(0, 60); int second = random.Next(0, 60); string tempStr = string.Format("{0} {1}:{2}:{3}", DateTime.Now.ToString("yyyy-MM-dd"), hour, minute, second); LHSetTime = Convert.ToDateTime(tempStr); //Console.WriteLine(rTime.ToString()); dietHabit = rd.Next(0, 4); drinkHabit = rd.Next(0, 4); sportHabit = rd.Next(0, 4); smokeHabit = rd.Next(0, 4); sleepHabit = rd.Next(0, 4); } public PersonHabit(int nAge, int nSex) { age = nAge; sex = nSex; //LHSetTime = new DateTime(System.DateTime.Now.Millisecond); Random random = new Random((int)(DateTime.Now.Ticks)); int hour = random.Next(2, 5); int minute = random.Next(0, 60); int second = random.Next(0, 60); string tempStr = string.Format("{0} {1}:{2}:{3}", DateTime.Now.ToString("yyyy-MM-dd"), hour, minute, second); LHSetTime = Convert.ToDateTime(tempStr); //Console.WriteLine(rTime.ToString()); //造时间数据 // int flag = 4; if (1 <= nAge && nAge <= 18) { // flag = 2; int[] data={0,0,0,0,0}; data = makeDataOneEighteen(); dietHabit = data[0]; drinkHabit = data[1]; sportHabit = data[2]; smokeHabit = data[3]; sleepHabit = data[4]; } if (19 <= nAge && nAge <= 35 && nSex == 0) { // flag = 2; int[] data = { 0, 0, 0, 0, 0 }; data = makeDataNinteenThirtyFiveMale(); dietHabit = data[0]; drinkHabit = data[1]; sportHabit = data[2]; smokeHabit = data[3]; sleepHabit = data[4]; } if (19 <= nAge && nAge <= 35 && nSex == 1) { // flag = 2; int[] data = { 0, 0, 0, 0, 0 }; data = makeDataNinteenThirtyFiveFemale(); dietHabit = data[0]; drinkHabit = data[1]; sportHabit = data[2]; smokeHabit = data[3]; sleepHabit = data[4]; } if (36 <= nAge && nAge <= 50 && nSex == 0) { // flag = 2; int[] data = { 0, 0, 0, 0, 0 }; data = makeDataThirtySixFiftyMale(); dietHabit = data[0]; drinkHabit = data[1]; sportHabit = data[2]; smokeHabit = data[3]; sleepHabit = data[4]; } if (36 <= nAge && nAge <= 50 && nSex == 1) { // flag = 2; int[] data = { 0, 0, 0, 0, 0 }; data = makeDataThirtySixFiftyFemale(); dietHabit = data[0]; drinkHabit = data[1]; sportHabit = data[2]; smokeHabit = data[3]; sleepHabit = data[4]; } if (51 <= nAge && nAge <= 75 && nSex == 0) { // flag = 2; int[] data = { 0, 0, 0, 0, 0 }; data = makeDataFiftyOneSeventyFiveMale(); dietHabit = data[0]; drinkHabit = data[1]; sportHabit = data[2]; smokeHabit = data[3]; sleepHabit = data[4]; } if (51 <= nAge && nAge <= 75 && nSex == 1) { // flag = 2; int[] data = { 0, 0, 0, 0, 0 }; data = makeDataFiftyOneSeventyFiveFemale(); dietHabit = data[0]; drinkHabit = data[1]; sportHabit = data[2]; smokeHabit = data[3]; sleepHabit = data[4]; } } public static int[] makeDataOneEighteen() //1-18年龄段划分 { Random rd = new Random(); int index1 = 0, index2 = 0, index3 = 0, index4 = 0, index5 = 0; int flag1 = rd.Next(0, 20); //算法依据,将1-18岁年龄段按照14:4:1:1的比例划分diet习惯; if (flag1 >= 0 && flag1 <= 13) index1 = 0; else if (flag1 >= 14 && flag1 <= 17) index1 = 1; else if (flag1 == 18) index1 = 2; else index1 = 3; int flag2 = rd.Next(0, 20); //算法依据,将1-18岁年龄段按照8:8:2:2的比例划分drink习惯; if (flag2 >= 0 && flag2 <= 7) index2 = 0; else if (flag2 >= 8 && flag2 <= 15) index2 = 1; else if (flag2 == 16 || flag2 == 17) index2 = 2; else index2 = 3; int flag3 = rd.Next(0, 20); //算法依据,将1-18岁年龄段按照5:8:5:2的比例划分sport习惯; if (flag3 >= 0 && flag3 <= 4) index3 = 0; else if (flag3 >= 5 && flag3 <= 12) index3 = 1; else if (flag3 >= 13 && flag3 <= 17) index3 = 2; else index3 = 3; int flag4 = rd.Next(0, 20); //算法依据,将1-18岁年龄段按照8:5:3:4的比例划分smoke习惯; if (flag4 >= 0 && flag4 <= 7) index4 = 0; else if (flag4 >= 8 && flag4 <= 12) index4 = 1; else if (flag4 == 13 || flag4 == 15) index4 = 2; else index4 = 3; int flag5 = rd.Next(0, 20); //算法依据,将1-18岁年龄段按照8:6:4:2的比例划分sleep习惯; if (flag5 >= 0 && flag5 <= 7) index5 = 0; else if (flag5 >= 8 && flag5 <= 13) index5 = 1; else if (flag5 >= 14 && flag5 <= 17) index5 = 2; else index5 = 3; int[] data = {0,0,0,0,0}; data[0] = index1; data[1] = index2; data[2] = index3; data[3] = index4; data[4] = index5; return data; } public static int[] makeDataNinteenThirtyFiveMale() //19-35年龄段男性划分 { Random rd = new Random(); int index1 = 0, index2 = 0, index3 = 0, index4 = 0, index5 = 0; int flag1 = rd.Next(0, 20); //算法依据,将19-35岁年龄段按照2:6:8:4的比例划分diet习惯; if (flag1 >= 0 && flag1 <= 1) index1 = 0; else if (flag1 >= 2 && flag1 <= 7) index1 = 1; else if (flag1 >= 8 && flag1 <= 17) index1 = 2; else index1 = 3; int flag2 = rd.Next(0, 20); //算法依据,将19-35岁年龄段按照10:6:2:2的比例划分drink习惯; if (flag2 >= 0 && flag2 <= 9) index2 = 0; else if (flag2 >= 10 && flag2 <= 15) index2 = 1; else if (flag2 >= 16 && flag2 <= 17) index2 = 2; else index2 = 3; int flag3 = rd.Next(0, 20); //算法依据,将19-35岁年龄段按照4:6:6:6的比例划分sport习惯; if (flag3 >= 0 && flag3 <= 3) index3 = 0; else if (flag3 >= 4 && flag3 <= 9) index3 = 1; else if (flag3 >= 10 && flag3 <= 15) index3 = 2; else index3 = 3; int flag4 = rd.Next(0, 20); //算法依据,将19-35岁年龄段按照4:2:4:10的比例划分smoke习惯; if (flag4 >= 0 && flag4 <= 3) index4 = 0; else if (flag4 >= 4 && flag4 <= 5) index4 = 1; else if (flag4 >= 6 && flag4 <= 9) index4 = 2; else index4 = 3; int flag5 = rd.Next(0, 20); //算法依据,将19-35岁年龄段按照6:4:4:6的比例划分sleep习惯; if (flag5 >= 0 && flag5 <= 5) index5 = 0; else if (flag5 >= 6 && flag5 <= 9) index5 = 1; else if (flag5 >= 10 && flag5 <= 13) index5 = 2; else index5 = 3; int[] data = { 0, 0, 0, 0, 0 }; data[0] = index1; data[1] = index2; data[2] = index3; data[3] = index4; data[4] = index5; return data; } public static int[] makeDataNinteenThirtyFiveFemale() //19-35年龄段女性划分 { Random rd = new Random(); int index1 = 0, index2 = 0, index3 = 0, index4 = 0, index5 = 0; int flag1 = rd.Next(0, 20); //算法依据,将19-35岁年龄段按照3:8:7:2的比例划分diet习惯; if (flag1 >= 0 && flag1 <= 2) index1 = 0; else if (flag1 >= 3 && flag1 <= 10) index1 = 1; else if (flag1 >= 11 && flag1 <= 17) index1 = 2; else index1 = 3; int flag2 = rd.Next(0, 20); //算法依据,将19-35岁年龄段按照12:6:1:1的比例划分drink习惯; if (flag2 >= 0 && flag2 <= 11) index2 = 0; else if (flag2 >= 12 && flag2 <= 17) index2 = 1; else if (flag2 >= 18 && flag2 <= 18) index2 = 2; else index2 = 3; int flag3 = rd.Next(0, 20); //算法依据,将19-35岁年龄段按照4:5:7:4的比例划分sport习惯; if (flag3 >= 0 && flag3 <= 3) index3 = 0; else if (flag3 >= 4 && flag3 <= 8) index3 = 1; else if (flag3 >= 9 && flag3 <= 15) index3 = 2; else index3 = 3; int flag4 = rd.Next(0, 20); //算法依据,将19-35岁年龄段按照8:6:4:2的比例划分smoke习惯; if (flag4 >= 0 && flag4 <= 7) index4 = 0; else if (flag4 >= 8 && flag4 <= 13) index4 = 1; else if (flag4 >= 14 && flag4 <= 17) index4 = 2; else index4 = 3; int flag5 = rd.Next(0, 20); //算法依据,将19-35岁年龄段按照10:6:2:2的比例划分sleep习惯; if (flag5 >= 0 && flag5 <= 9) index5 = 0; else if (flag5 >= 10 && flag5 <= 15) index5 = 1; else if (flag5 >= 16 && flag5 <= 17) index5 = 2; else index5 = 3; int[] data = { 0, 0, 0, 0, 0 }; data[0] = index1; data[1] = index2; data[2] = index3; data[3] = index4; data[4] = index5; return data; } public static int[] makeDataThirtySixFiftyMale() //36-50年龄段男性划分 { Random rd = new Random(); int index1 = 0, index2 = 0, index3 = 0, index4 = 0, index5 = 0; int flag1 = rd.Next(0, 20); //算法依据,将36-50岁年龄段按照4:8:6:2的比例划分diet习惯; if (flag1 >= 0 && flag1 <= 3) index1 = 0; else if (flag1 >= 4 && flag1 <= 11) index1 = 1; else if (flag1 >= 12 && flag1 <= 17) index1 = 2; else index1 = 3; int flag2 = rd.Next(0, 20); //算法依据,将36-50岁年龄段按照6:6:4:4的比例划分drink习惯; if (flag2 >= 0 && flag2 <= 0) index2 = 0; else if (flag2 >= 1 && flag2 <= 3) index2 = 1; else if (flag2 >= 4 && flag2 <= 11) index2 = 2; else index2 = 3; int flag3 = rd.Next(0, 20); //算法依据,将36-50岁年龄段按照2:6:4:8的比例划分sport习惯; if (flag3 >= 0 && flag3 <= 1) index3 = 0; else if (flag3 >= 2 && flag3 <= 7) index3 = 1; else if (flag3 >= 8 && flag3 <= 11) index3 = 2; else index3 = 3; int flag4 = rd.Next(0, 20); //算法依据,将36-50岁年龄段按照2:2:10:6的比例划分smoke习惯; if (flag4 >= 0 && flag4 <= 2) index4 = 0; else if (flag4 >= 3 && flag4 <= 4) index4 = 1; else if (flag4 >= 5 && flag4 <= 14) index4 = 2; else index4 = 3; int flag5 = rd.Next(0, 20); //算法依据,将36-50岁年龄段按照3:5:4:8的比例划分sleep习惯; if (flag5 >= 0 && flag5 <= 2) index5 = 0; else if (flag5 >= 3 && flag5 <= 7) index5 = 1; else if (flag5 >= 8 && flag5 <= 11) index5 = 2; else index5 = 3; int[] data = { 0, 0, 0, 0, 0 }; data[0] = index1; data[1] = index2; data[2] = index3; data[3] = index4; data[4] = index5; return data; } public static int[] makeDataThirtySixFiftyFemale() //36-50年龄段女性划分 { Random rd = new Random(); int index1 = 0, index2 = 0, index3 = 0, index4 = 0, index5 = 0; int flag1 = rd.Next(0, 20); //算法依据,将36-50岁年龄段按照6:8:4:2的比例划分diet习惯; if (flag1 >= 0 && flag1 <= 5) index1 = 0; else if (flag1 >= 6 && flag1 <= 13) index1 = 1; else if (flag1 >= 14 && flag1 <= 17) index1 = 2; else index1 = 3; int flag2 = rd.Next(0, 20); //算法依据,将36-50岁年龄段按照7:6:6:1的比例划分drink习惯; if (flag2 >= 0 && flag2 <= 6) index2 = 0; else if (flag2 >= 7 && flag2 <= 12) index2 = 1; else if (flag2 >= 13 && flag2 <= 18) index2 = 2; else index2 = 3; int flag3 = rd.Next(0, 20); //算法依据,将36-50岁年龄段按照3:3:4:10的比例划分sport习惯; if (flag3 >= 0 && flag3 <= 2) index3 = 0; else if (flag3 >= 3 && flag3 <= 5) index3 = 1; else if (flag3 >= 6 && flag3 <= 9) index3 = 2; else index3 = 3; int flag4 = rd.Next(0, 20); //算法依据,将36-50岁年龄段按照7:6:5:2的比例划分smoke习惯; if (flag4 >= 0 && flag4 <= 6) index4 = 0; else if (flag4 >= 7 && flag4 <= 12) index4 = 1; else if (flag4 >= 13 && flag4 <= 17) index4 = 2; else index4 = 3; int flag5 = rd.Next(0, 20); //算法依据,将36-50岁年龄段按照12:4:3:1的比例划分sleep习惯; if (flag5 >= 0 && flag5 <= 11) index5 = 0; else if (flag5 >= 12 && flag5 <= 15) index5 = 1; else if (flag5 >= 16 && flag5 <= 18) index5 = 2; else index5 = 3; int[] data = { 0, 0, 0, 0, 0 }; data[0] = index1; data[1] = index2; data[2] = index3; data[3] = index4; data[4] = index5; return data; } public static int[] makeDataFiftyOneSeventyFiveMale() //51-75年龄段男性划分 { Random rd = new Random(); int index1 = 0, index2 = 0, index3 = 0, index4 = 0, index5 = 0; int flag1 = rd.Next(0, 20); //算法依据,将51-75岁年龄段按照2:6:6:6的比例划分diet习惯; if (flag1 >= 0 && flag1 <= 1) index1 = 0; else if (flag1 >= 2 && flag1 <= 7) index1 = 1; else if (flag1 >= 8 && flag1 <= 13) index1 = 2; else index1 = 3; int flag2 = rd.Next(0, 20); //算法依据,将51-75岁年龄段按照1:3:8:8的比例划分drink习惯; if (flag2 >= 0 && flag2 <= 0) index2 = 0; else if (flag2 >= 1 && flag2 <= 3) index2 = 1; else if (flag2 >= 4 && flag2 <= 11) index2 = 2; else index2 = 3; int flag3 = rd.Next(0, 20); //算法依据,将51-75岁年龄段按照1:3:4:12的比例划分sport习惯; if (flag3 >= 0 && flag3 <= 0) index3 = 0; else if (flag3 >= 1 && flag3 <= 3) index3 = 1; else if (flag3 >= 4 && flag3 <= 7) index3 = 2; else index3 = 3; int flag4 = rd.Next(0, 20); //算法依据,将51-75岁年龄段按照1:4:8:7的比例划分smoke习惯; if (flag4 >= 0 && flag4 <= 0) index4 = 0; else if (flag4 >= 1 && flag4 <= 4) index4 = 1; else if (flag4 >= 5 && flag4 <= 12) index4 = 2; else index4 = 3; int flag5 = rd.Next(0, 20); //算法依据,将51-75岁年龄段按照2:2:6:10的比例划分sleep习惯; if (flag5 >= 0 && flag5 <= 1) index5 = 0; else if (flag5 >= 2 && flag5 <= 3) index5 = 1; else if (flag5 >= 4 && flag5 <= 9) index5 = 2; else index5 = 3; int[] data = { 0, 0, 0, 0, 0 }; data[0] = index1; data[1] = index2; data[2] = index3; data[3] = index4; data[4] = index5; return data; } public static int[] makeDataFiftyOneSeventyFiveFemale() //51-75年龄段女性划分 { Random rd = new Random(); int index1 = 0, index2 = 0, index3 = 0, index4 = 0, index5 = 0; int flag1 = rd.Next(0, 20); //算法依据,将51-75岁年龄段按照6:4:8:2的比例划分diet习惯; if (flag1 >= 0 && flag1 <= 5) index1 = 0; else if (flag1 >= 6 && flag1 <= 9) index1 = 1; else if (flag1 >= 10 && flag1 <= 17) index1 = 2; else index1 = 3; int flag2 = rd.Next(0, 20); //算法依据,将51-75岁年龄段按照4:4:10:2的比例划分drink习惯; if (flag2 >= 0 && flag2 <= 3) index2 = 0; else if (flag2 >= 4 && flag2 <= 7) index2 = 1; else if (flag2 >= 8 && flag2 <= 17) index2 = 2; else index2 = 3; int flag3 = rd.Next(0, 20); //算法依据,将51-75岁年龄段按照1:3:8:8的比例划分sport习惯; if (flag3 >= 0 && flag3 <= 0) index3 = 0; else if (flag3 >= 1 && flag3 <= 3) index3 = 1; else if (flag3 >= 4 && flag3 <= 11) index3 = 2; else index3 = 3; int flag4 = rd.Next(0, 20); //算法依据,将51-75岁年龄段按照2:8:6:4的比例划分smoke习惯; if (flag4 >= 0 && flag4 <= 1) index4 = 0; else if (flag4 >= 2 && flag4 <= 9) index4 = 1; else if (flag4 >= 10 && flag4 <= 15) index4 = 2; else index4 = 3; int flag5 = rd.Next(0, 20); //算法依据,将51-75岁年龄段按照14:4:1:1的比例划分sleep习惯; if (flag5 >= 0 && flag5 <= 13) index5 = 0; else if (flag5 >= 14 && flag5 <= 17) index5 = 1; else if (flag5 >= 18 && flag5 <= 18) index5 = 2; else index5 = 3; int[] data = { 0, 0, 0, 0, 0 }; data[0] = index1; data[1] = index2; data[2] = index3; data[3] = index4; data[4] = index5; return data; } public void print() { Console.WriteLine("当前时间段:" + LHSetTime); Console.WriteLine("饮食习惯 :" + change(dietHabit)); Console.WriteLine("饮酒习惯 :" + change(drinkHabit)); Console.WriteLine("运动习惯 :" + change(sportHabit)); Console.WriteLine("抽烟习惯 :" + change(smokeHabit)); Console.WriteLine("睡眠习惯 :" + change(sleepHabit)); } public String change(int value) { String flag = "异类"; switch (value) { case 0: flag = "优";break; case 1: flag = "良";break; case 2: flag = "中";break; case 3: flag = "差";break; } return flag; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data.Sql; namespace testtest { class Program { public static void sqlExcute(String sql) { string connectionString = "Server = 10.255.97.137;" + "Database = Diseasemonitor;" + "User ID = sa;" + "Password = 123456;"; SqlConnection conn = new SqlConnection(connectionString); try { conn.Open(); SqlCommand comm = new SqlCommand(sql, conn); comm.ExecuteReader(); } catch (Exception e) { //出现异常显示 Console.WriteLine("模拟数据插入发生错误..."); } finally { //关闭数据库连接,避免占用 conn.Close(); } } public static int querySqlExcute(String sql) { int flag; string connectionString = "Server = 10.255.97.137;" + "Database = Diseasemonitor;" + "User ID = sa;" + "Password = 123456;"; SqlConnection conn = new SqlConnection(connectionString); try { conn.Open(); SqlCommand comm = new SqlCommand(sql, conn); SqlDataReader dataReader = comm.ExecuteReader(); // 判断数据是否读到尾. // while (dataReader.Read()) flag = int.Parse(dataReader[0].ToString()); return flag; } catch (Exception e) { //出现异常显示 Console.WriteLine("数据查询发生错误..."); } finally { //关闭数据库连接,避免占用 conn.Close(); } return 999999; } public static String change(int value) { String flag = "异类"; switch (value) { case 0: flag = "优"; break; case 1: flag = "良"; break; case 2: flag = "中"; break; case 3: flag = "差"; break; } return flag; } //高血压病态模拟生成数据 public static String changeSex(int value) { String flag = "男"; //flag = (value==0):"男"?"女"; if (value == 0) flag = "男"; else flag = "女"; return flag; } // public static void static void Main(string[] args) { Console.WriteLine("请输入你需要生成多少人的数据以及年龄最大值(75以内):"); int flag,end,maxAge; flag = int.Parse(Console.ReadLine()); maxAge = int.Parse(Console.ReadLine()); Random rd = new Random(); Random rdd = new Random(); //数据生成 for (int i = 0; i < flag; i++) { int age = rd.Next(1, maxAge+1); int sex = rdd.Next(0, 2); PersonHabit ph = new PersonHabit(age,sex);//这里随机构造出年龄和性别 Console.WriteLine("年龄:"+age+" 性别:"+((sex==0)?"男":"女")); //ph.Userid = i + 1; string sql = "insert into LiftHabit(LHSetTime, age, sex, dietHabit, drinkHabit,sportHabit,smokeHabit,sleepHabit) values ('" + ph.LHSetTime1 + "', '" + ph.Age + "', '" + changeSex(ph.Sex) + "', '" + change(ph.DietHabit) + "', '" + change(ph.DrinkHabit) + "', '" + change(ph.SportHabit) + "', '" + change(ph.SmokeHabit) + "', '" + change(ph.SleepHabit) + "')"; sqlExcute(sql); Console.WriteLine("正在生成数据中... 当前为第"+(i+1)+"个人"); //System.Threading.Thread.Sleep(2000); if (i == flag) break; //ph.print(); } Console.WriteLine("数据生成完毕...\n***************按0键直接退出,按3键删除生成数据退出...***************"); end = int.Parse(Console.ReadLine()); if (end == 3) { //拼装sql String sql = "delete from LiftHabit"; sqlExcute(sql); Console.WriteLine("数据删除完毕..."); } for (int i = 1; i <= 75; i++) Console.WriteLine(i); return; } } }
C#的大作业,一个大项目,分成很多小块,自己做的小块内容,代码分享给大家
代码写的有点乱,相关的文件(源码以及数据库脚本)我已经上传了,大家可以下载测试下,本程序仅供做测试用途