MyTask2
先把核心代码贴上
public void solve() { //Console.WriteLine("请输入你需要生成多少人的数据以及年龄最大值(75以内):"); //int flag,end,maxAge; int test=0; //flag = int.Parse(Console.ReadLine()); //maxAge = int.Parse(Console.ReadLine()); Random rd = new Random(GetRandomSeed()); Random rdd = new Random(GetRandomSeed()); clearDataPie();//每次分析之前都要对之前一次的数据清空 //数据生成 for (int i = 0; i < numPerson; i++) { int age = rd.Next(1, agePerson+1); int sex = rdd.Next(0, 2); if(flagSex == 1) { sex = Nsex; } PersonHabit ph = new PersonHabit(age,sex);//这里随机构造出年龄和性别 //可自己选取性别,默认为空的话则随机 calculateDataPie(ph);//每个人的数据 solveSum(ph);//生活习惯数据统计 //messagePrint+="年龄:"+age+" 性别:"+((sex==0)?"男":"女")+"\r\n"; // Console.WriteLine("年龄:"+age+" 性别:"+((sex==0)?"男":"女")); //ph.Userid = i + 1; string sql = "insert into LifeHabit()"; // if(==false){ // test = 1; // } if(this.radioButton1.Checked == true&&this.radioButton2.Checked == false) { sqlExcute(sql); this.button4.Enabled = true; } //Console.WriteLine("正在生成数据中... 当前为第"+(i+1)+"个人"); //System.Threading.Thread.Sleep(2000); if (i == numPerson) break; //ph.print(); } countDataPie();//统计完所有数据之后进行饼状图绘制 if(test==1) messagePrint += "-------------数据库未布置,操作失败!-------------\r\n\r\n"; messagePrint += "本次模拟数据统计:"+"\r\n\r\n"; messagePrint += "男生: " + numMen +" 女生: "+ numWomen +"\r\n\r\n"; messagePrint += "年龄数据分布: 下限-"+ageMin+"岁 上限- "+ageMax+"岁\r\n\r\n"; messagePrint += "各年龄段分布: 1-18岁 "+age1+"人 19-35岁 "+age2+"人 36-50岁 "+age3+"人 51-75岁 "+age4+"人\r\n\r\n"; messagePrint += "\r\n\r\n生活习惯数据:\r\n"; messagePrint += "DietHabit : "+"优-"+menDiet1 +"("+returnPercent(menDiet1)+") 良-"+menDiet2 +"("+returnPercent(menDiet2)+") 中-"+menDiet3 +"("+returnPercent(menDiet3)+") 差-"+menDiet4 +"("+returnPercent(menDiet4)+")\r\n\r\n"; messagePrint += "DrinkHabit: "+"优-"+menDrink1+"("+returnPercent(menDrink1)+") 良-"+menDrink2+"("+returnPercent(menDrink2)+") 中-"+menDrink3+"("+returnPercent(menDrink3)+") 差-"+menDrink4+"("+returnPercent(menDrink4)+")\r\n\r\n"; messagePrint += "SportHabit: "+"优-"+menSport1+"("+returnPercent(menSport1)+") 良-"+menSport2+"("+returnPercent(menSport2)+") 中-"+menSport3+"("+returnPercent(menSport3)+") 差-"+menSport4+"("+returnPercent(menSport4)+")\r\n\r\n"; messagePrint += "SmokeHabit: "+"优-"+menSmoke1+"("+returnPercent(menSmoke1)+") 良-"+menSmoke2+"("+returnPercent(menSmoke2)+") 中-"+menSmoke3+"("+returnPercent(menSmoke3)+") 差-"+menSmoke4+"("+returnPercent(menSmoke4)+")\r\n\r\n"; messagePrint += "SleepHabit: "+"优-"+menSleep1+"("+returnPercent(menSleep1)+") 良-"+menSleep2+"("+returnPercent(menSleep2)+") 中-"+menSleep3+"("+returnPercent(menSleep3)+") 差-"+menSleep4+"("+returnPercent(menSleep4)+")\r\n\r\n"; /* 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; }
这是最原始的框架,其实最开始做的时候只是一个dos界面,然后能插入数据和取出数据的程序
后面增加了WinForm界面,然后把一些基本的数据能够显示出来
这里给一些比较常用的方法吧
public static int GetRandomSeed()//随机数种子 { byte[] bytes = new byte[4]; System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider( ); rng.GetBytes(bytes); return BitConverter.ToInt32( bytes , 0 ); }
public static string returnPercent(int a)//返回数值的百分比 { double percent=Convert.ToDouble(a)/Convert.ToDouble(numPerson); string result=string.Format("{0:0.00%}",percent);//得到5.88% return result; }
public bool outputExcel()//数据导出Excel模版 { using (DbConnection connection = new SqlConnection("server=localhost; user id=sa; password=; database=")) { connection.Open(); DbCommand command = connection.CreateCommand(); command.CommandType = System.Data.CommandType.Text; command.CommandText = "";//sql表字段 var reader = command.ExecuteReader(); Excel.Application application = new Excel.Application(); Excel.Workbook workbook = application.Workbooks.Add(); Excel.Worksheet worksheet = workbook.ActiveSheet as Excel.Worksheet; var index = 1; while (reader.Read()) { worksheet.Cells[index, 1] = reader.GetInt32(0).ToString(); worksheet.Cells[index, 2] = reader.GetString(1); worksheet.Cells[index, 3] = reader.GetString(2); if ("NULL".Equals(reader.GetString(3))==true) worksheet.Cells[index, 4] = ""; worksheet.Cells[index, 4] = reader.GetString(3); if ("NULL".Equals(reader.GetString(4)) == true) worksheet.Cells[index, 5] = ""; worksheet.Cells[index, 5] = reader.GetString(4); if (reader.GetString(5) == null) worksheet.Cells[index, 6] = ""; worksheet.Cells[index, 6] = reader.GetString(5); if (reader.GetString(6) == null) worksheet.Cells[index, 7] = ""; worksheet.Cells[index, 7] = reader.GetString(6); if (reader.GetString(7) == null) worksheet.Cells[index, 8] = ""; worksheet.Cells[index, 8] = reader.GetString(7); index++; } reader.Close(); workbook.SaveAs(@"F:\test.xls"); workbook.Close(); application.Quit(); connection.Close(); } return true; }
void Timer1Tick(object sender, System.EventArgs e)//监控饼状图并且在form1内传入数据 //因为之前设置的是在design里面做,是可以弄出来的,然后鉴于数据不知道怎么传输过来,我就在design里面设置timer,然后可以做到 //点击按钮之后启动 { if(flagAgeShowPie == 1) { chart1.Series["Series1"].Points.DataBindXY(xValues, yValues1); //chart1.Series["Series1"].Label = "#PERCENT{P}"; //System.Diagnostics.Debug.WriteLine("123321"+yValues2[0]+" "+yValues3[1]+" "+yValues4[2]); chart2.Series["Series2"].Points.DataBindXY(xValues, yValues2); //chart2.Series["Series2"].Label = "#PERCENT{P}"; //chart2.Series["Series2"].LegendText = "偏低"; chart3.Series["Series3"].Points.DataBindXY(xValues, yValues3); //chart3.Series["Series3"].Label = "#PERCENT{P}"; //chart3.Series["Series3"].LegendText = "中"; chart4.Series["Series4"].Points.DataBindXY(xValues, yValues4); //chart4.Series["Series4"].Label = "#PERCENT{P}"; } if(flagValueChartAreaComboBox<=10&&flagLifeHabitButton==1)//在折线图左边下拉框有值的时候并且上面的按钮也按下才绘图 { changeValueChartAreaByIndex();//通过下拉框的值来改变数组值间接改变图形 chart5.Series["Series5"].Points.DataBindXY(xValues5, yValues5);//绑定数据 } //chart4.Series["Series4"].LegendText = "偏高"; //System.Diagnostics.Debug.WriteLine("123123"); flagAgeShowPie = 0; //flagLifeHabitButton = 0; }
好了,这一章主要是列出一些常用的方法分享给大家,下一章我们开始讲我遇到的困难~~