C#操作题答案.docx
- 文档编号:26726629
- 上传时间:2023-06-22
- 格式:DOCX
- 页数:32
- 大小:21.71KB
C#操作题答案.docx
《C#操作题答案.docx》由会员分享,可在线阅读,更多相关《C#操作题答案.docx(32页珍藏版)》请在冰豆网上搜索。
C#操作题答案
5.编写一个控制台应用程序,输入任意的10个整数,输出这10个数中的最大值和最小值。
staticvoidMain(string[]args)
{
Console.WriteLine("请输入任意的10个整数:
");
int[]input=newint[10];
for(inti=0;i<10;i++)
input[i]=Convert.ToInt32(Console.ReadLine());
intmin=input[0];
intmax=input[9];
foreach(inttempininput)
{
if(max max=temp; if(min>temp) min=temp; } Console.WriteLine("最小值: ");Console.WriteLine(min); Console.WriteLine("最大值: ");Console.WriteLine(max); Console.ReadLine(); } 编写一个控制台应用程序,要求输入X的值,计算下面分段函数的Y的值。 classProgram { staticdoubleCalculate(doublex) { doubley; if(x>100) y=10*Math.Pow(x,3)+5*x*x+3*x+2; elseif(x>=-100&&x<=100) y=6*x+1; else y=Math.Pow(x,4)+8*Math.Pow(x,3)+6*Math.Pow(x,2)-100; returny; } staticvoidMain(string[]args) { doublex; Console.Write("请输入X的值: "); x=Convert.ToDouble(Console.ReadLine()); doubley=Calculate(x); Console.WriteLine("结果Y={0}",y); Console.ReadKey(); } } 编写一个控制台应用程序,要求输入任意一串长度的字符,输出其字符串的长度和反序 classProgram { staticvoidMain(string[]args) { stringstr; intlen1=0,len2=0,len3=0; str=Console.ReadLine(); Console.WriteLine("该字符吕的长度为: {0}",str.Length); Console.Write("该字符串的反序为: "); for(inti=str.Length-1;i>=0;i--) Console.Write(str[i]); Console.Write('\n'); foreach(charcinstr) { if(char.IsDigit(c)) len1++; elseif(char.IsLower(c)) len2++; elseif(char.IsUpper(c)) len3++; } Console.WriteLine("该字符串的数字有: "+len1+",小写字母有: "+len2+",大写字母有: "+len3); Console.ReadKey(); } } 编写一个Windows应用程序,界面如下图所示,其中个的所得税、社保、房租和水电分别占工资的3%、4%、2%和1%。 publicpartialclassForm1: Form { publicForm1() { InitializeComponent(); } privatevoidbutton1_Click(objectsender,EventArgse) { floatpercent=0; floatsum; for(inti=1;i<=4;i++) { switch(i) { case1: if(checkBox1.Checked==true) percent+=0.03f; break; case2: if(checkBox2.Checked==true) percent+=0.04f; break; case3: if(checkBox3.Checked==true) percent+=0.02f; break; case4: if(checkBox4.Checked==true) percent+=0.01f; break; } } sum=(float.Parse(textBox1.Text))*percent; textBox2.Text=sum.ToString(); } } 编写一个Windows应用程序,界面如下图所示,要求运行时左边列表框能出现100、200到1000之间的10个选项,并实现相应命令按钮的功能。 namespace_3._5 { publicpartialclassForm1: Form { publicForm1() { InitializeComponent(); int[]arry=newint[10]; for(inti=0;i<10;i++) { arry[0]=100; arry[1]=200; arry[2]=300; arry[3]=400; arry[4]=500; arry[5]=600; arry[6]=700; arry[7]=800; arry[8]=900; arry[9]=1000; } for(inti=0;i<10;i++) listBox1.Items.Add(arry[i]); } privatevoidbutton1_Click(objectsender,EventArgse) { objectItem=(object)textBox1.Text; listBox1.Items.Add(Item); } privatevoidlistBox1_SelectedIndexChanged(objectsender,EventArgse) { } privatevoidbutton2_Click(objectsender,EventArgse) { objectSettleOnItem=listBox1.SelectedItem; if(listBox2.Items.Contains(SettleOnItem)) { MessageBox.Show(SettleOnItem.ToString()+"已存在! ","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning); } else { listBox1.Items.Remove(SettleOnItem); listBox2.Items.Add(SettleOnItem); } } privatevoidbutton3_Click(objectsender,EventArgse) { objectSettleOnItem=listBox2.SelectedItem; if(listBox1.Items.Contains(SettleOnItem)) { MessageBox.Show(SettleOnItem.ToString()+"已存在! ","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning); } else { listBox2.Items.Remove(SettleOnItem); listBox1.Items.Add(SettleOnItem); } } privatevoidbutton4_Click(objectsender,EventArgse) { this.Dispose(); } } } 要求在窗体界面中实现使用不保持连接的方式编写程序,并在各个文本框中分别显示表的当前记录的学生的学号,姓名,性别,成绩信息,并在相应的命令按钮中输入代码能实现显示第一条、上一条、下一条和最后一条记录的信息的功能。 namespace_4._4 { publicpartialclassForm1: Form { DataSetdata; OleDbDataAdapteradapter; inti=0; publicForm1() { InitializeComponent(); stringsqlstr="select*fromscore"; stringstrPath=AppDomain.CurrentDomain.BaseDirectory+"./score.mdb"; stringstr="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+strPath+";PersistSecurityInfo=False"; OleDbConnectionconn=newOleDbConnection(str); adapter=newOleDbDataAdapter(sqlstr,conn); OleDbCommandBuilderbuilder=newOleDbCommandBuilder(adapter); data=newDataSet(); adapter.Fill(data,"score"); this.textBox1.Text=data.Tables["score"].Rows[i]["学号"].ToString(); this.textBox2.Text=data.Tables["score"].Rows[i]["姓名"].ToString(); this.textBox3.Text=data.Tables["score"].Rows[i]["性别"].ToString(); this.textBox4.Text=data.Tables["score"].Rows[i]["成绩"].ToString(); label6.Text=(i+1).ToString()+"/"+data.Tables["score"].Rows.Count; button1.Enabled=false; button2.Enabled=false; } privatevoidbutton2_Click(objectsender,EventArgse) { button3.Enabled=true; button4.Enabled=true; i--; this.textBox1.Text=data.Tables["score"].Rows[i]["学号"].ToString(); this.textBox2.Text=data.Tables["score"].Rows[i]["姓名"].ToString(); this.textBox3.Text=data.Tables["score"].Rows[i]["性别"].ToString(); this.textBox4.Text=data.Tables["score"].Rows[i]["成绩"].ToString(); label6.Text=(i+1).ToString()+"/"+data.Tables["score"].Rows.Count; if(i==0) { button2.Enabled=false; button1.Enabled=false; } } privatevoidbutton3_Click(objectsender,EventArgse) { button2.Enabled=true; button1.Enabled=true; i++; this.textBox1.Text=data.Tables["score"].Rows[i]["学号"].ToString(); this.textBox2.Text=data.Tables["score"].Rows[i]["姓名"].ToString(); this.textBox3.Text=data.Tables["score"].Rows[i]["性别"].ToString(); this.textBox4.Text=data.Tables["score"].Rows[i]["成绩"].ToString(); label6.Text=(i+1).ToString()+"/"+data.Tables["score"].Rows.Count; if(i==data.Tables["score"].Rows.Count-1) { button3.Enabled=false; button4.Enabled=false; } } privatevoidbutton4_Click(objectsender,EventArgse) { button1.Enabled=true; button2.Enabled=true; i=data.Tables["score"].Rows.Count-1; this.textBox1.Text=data.Tables["score"].Rows[i]["学号"].ToString(); this.textBox2.Text=data.Tables["score"].Rows[i]["姓名"].ToString(); this.textBox3.Text=data.Tables["score"].Rows[i]["性别"].ToString(); this.textBox4.Text=data.Tables["score"].Rows[i]["成绩"].ToString(); label6.Text=(i+1).ToString()+"/"+data.Tables["score"].Rows.Count; button3.Enabled=false; button4.Enabled=false; } } } 使用保持连接的方式编写程序,查询符合应用程序界面所选的条件的SCORE数据库中的score表的学生的学号,姓名,性别,成绩,并将结果在列表框中显示出来,要求在列表框中首先显示表的所有字段信息,排序字段选用学号。 namespace_4._3 { publicpartialclassForm1: Form { publicForm1() { InitializeComponent(); button1.Enabled=false; boBox1.SelectedIndex=0; boBox2.SelectedIndex=0; listBox1.Items.Add(""+"学号"+"\t\t"+"姓名"+"\t"+"性别"+"\t"+"成绩"); listBox1.Items.Add(""); stringsqlstr="select*fromscorewhere成绩<600orderby成绩desc"; stringstrPath=AppDomain.CurrentDomain.BaseDirectory+"./score.mdb"; stringstr="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+strPath+";PersistSecurityInfo=False"; OleDbConnectionconn=newOleDbConnection(str); conn.Open(); OleDbCommandcommand=newOleDbCommand(sqlstr,conn); OleDbDataReaderdr1=command.ExecuteReader(); while(dr1.Read()) { listBox1.Items.Add(dr1[0].ToString()+"\t"+dr1[1].ToString()+"\t"+dr1[2].ToString()+"\t"+dr1[3].ToString()); } conn.Close(); } privatevoidbutton1_Click(objectsender,EventArgse) { for(inti=listBox1.Items.Count-1;i>=0;i--) { listBox1.Items.RemoveAt(i); } stringsqlstr; listBox1.Items.Add(""+"学号"+"\t\t"+"姓名"+"\t"+"性别"+"\t"+"成绩"); listBox1.Items.Add(""); if(radioButton1.Checked) sqlstr="select*fromscorewhere成绩"+comboBox1.Text+""+textBox1.Text+"orderby"+comboBox2.Text+"asc"; else sqlstr="select*fromscorewhere成绩"+comboBox1.Text+""+textBox1.Text+"orderby"+comboBox2.Text+"desc"; stringstrPath=AppDomain.CurrentDomain.BaseDirectory+"./score.mdb"; stringstr="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+strPath+";PersistSecurityInfo=False"; OleDbConnectionconn=newOleDbConnection(str); conn.Open(); OleDbCommandcommand=newOleDbCommand(sqlstr,conn); OleDbDataReaderdr1=command.ExecuteReader(); while(dr1.Read()) { listBox1.Items.Add(dr1[0].ToString()+"\t"+dr1[1].ToString()+"\t"+dr1[2].ToString()+"\t"+dr1[3].ToString()); } conn.Close(); } privatevoidtextBox1_TextChanged_1(objectsender,EventArgse) { if(textBox1.Text=="") button1.Enabled=false; else button1.Enabled=true; } } } 8.设计一个DNS解析的程序界面,输入一个网址,在列表框中显示出网站所在的所有IP地址,主机名和别名。 usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Net; namespace_3 { publicpartialclassForm1: Form { publicForm1() { InitializeComponent(); } privatevoidtextBox1_TextChanged(objectsender,EventArgse) { } privatevoidbutton1_Click(objectsender,EventArgse) { listBox1.Items.Clear(); stringname=textBox1.Text; IPHostEntryme=Dns.Resolve(name); listBox1.Items.Add(me.HostName); foreach(IPAddressipinme.AddressList) { listBox1.Items.Add(ip.ToString()); } for(inti=0;i { listBox1.Items.Add(me.Aliases[i]); } } } } 9.编写一个Windows应用程序,判断二个IP地址是否在同一子网内(二个IP的子网掩码是相同的)。 namespace_6._2 { publicpartialclassForm1: Form { publicForm1() { InitializeComponent(); } privatevoidbutton1_Click(objectsender,EventArgse) { bytea1,a2,a3,a4
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 操作 答案