C#程序设计及应用课后题答案_第1页
C#程序设计及应用课后题答案_第2页
C#程序设计及应用课后题答案_第3页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、4、分别写出下列语句执行的结果1) Console.WriteLine(0-0:pgood,12.34F);2) Console.WriteLine(0-0:#good,0);3) Console.WriteLine(0-0:00000good,456);【解答】12.34-1,234.00%good0-good456-00456good5、编写一个控制台应用程序,输出 1到 5的平方值,要求:1) 用 for 语句实现。2) 用 while 语句实现。3) 用 do-while 语句实现。【解答】using System;using System.Collections.Generic;us

2、ing System.Text;namespace outputSquareValueclass Programstatic void Main()/用 for 语句实现for (int i = 1; i = 5; i+)Console.WriteLine(0 的平方值为 1, i, i * i);/用 while 语句实现int j = 0;while (j+ 5)Console.WriteLine(0 的平方值为 1, j, j * j);/用 do-while 语句实现int k = 1;doConsole.WriteLine(0 的平方值为 1, k, k * k); while (k

3、+ 5);Console.ReadLine();6、编写一个控制台应用程序,要求用户输入5 个大写字母,如果用户输入的信息不满足要求,提示帮助信息并要求重新输入。【解答】using System;using System.Collections.Generic;using System.Text;namespace inputCapitalLetterclass Programstatic void Main()bool ok = false;while (ok = false)Console.Write( 请输入 5 个大写字母: );string str = Console.ReadLin

4、e();if (str.Length != 5)Console.WriteLine( 你输入的字符个数不是 5 个,请重新输入。 );elseok = true;for (int i = 0; i 5; i+)char c = stri;if (c Z)Console.WriteLine( 第 0 个字符“ 1 ”不是大写字母,请重新输入。 , i + 1, c); ok = false;break;7、编写一个控制台应用程序,要求完成下列功能。1) 接收一个整数 n。2) 如果接收的值n为正数,输出1到n间的全部整数。3) 如果接收的值为负值,用 break 或者 return 退出程序。4

5、) 转到 (1)继续接收下一个整数。【解答】using System;using System.Collections.Generic;using System.Text;namespace testOutputclass Program static void Main()while (true)Console.Write(”请输入一个整数(负值结束):);string str = Console.ReadLine();tryint i = Int32.Parse(str);if (i 0) break;for (int j = 1; j = i; j+) Console.WriteLine

6、(j);catchConsole.WriteLine( 你输入的不是数字或超出整数的表示范围,请重新输入 );8、编写一个控制台应用程序,求1000之内的所有“完数” 。所谓“完数”是指一个数恰好等于它的所有因子之和。例如, 6 是完数,因为 6=1+2+3。【解答】using System;using System.Text;namespace completeNumberclass Programstatic void Main(string args)for (int i = 2; i = 1000; i+)int s = 1; string str = 1;for (int j = 2

7、; j = (int)Math.Sqrt(i); j+)if (j * (i / j) = i)if (j != i / j) s += j + i / j;str += string.Format(+0+1, j, i / j);elses += j;str += stri ng.Format(+0, j);if (s = i) Con sole.WriteL in e(0=1, i, str);Con sole.ReadL in e();3、编写一个控制台应用程序,计算234nXXX(n 1) x1 xL ( 1)(2!3!4!n!要求精度为10-8。【解答】using System;cl

8、ass Test3public static void Mai n()int n = 50;double x = 3;double s = 0;double a = 1;for (int i = 1; i = n; i+)a *= i;s += Math.Pow(-1, i + 1) * Math.Pow(x, i) / a;Co nsole.WriteLi ne( n=0,s=1:0.00000000, n, s);4、 编写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能。(1) 输出字符串的长度。(2) 输出字符串中第一个出现字母 a的位置。(3) 在字符串的第3个字符后面插

9、入子串“ hello”,输出新字符串。(4) 将字符串“hello”替换为“ me”,输出新字符串。(5) 以字符“ m”为分隔符,将字符串分离,并输出分离后的字符串。【解答】【解答】class Test4public static void Main()string str = ;while (str.Length -1)Console.WriteLine(第一个出现字母 a的位置是:0, i);elseConsole.WriteLine( 字符串中不包含字母 a。);/(3)string str1 = str.Insert(3, hello);/在第 3 个(初始序号为)字符前插入 hel

10、loConsole.WriteLine( 插入 hello 后的结果为: 0, str1);/(4)string str2 = str1.Replace(hello, me);Console.WriteLine( 将 hello 替换为 me 后的结果为: 0, str2);/(5)string arr = str2.Split(m);Console.WriteLine(以m为分隔符分离后的字符串有:”);for (int j = 0; j arr.Length; j+)Console.WriteLine(arrj);1、编写一个控制台应用程序,完成下列功能。(1)创建一个类,用无参数的构造函

11、数输出该类的类名。(2) 增加一个重载的构造函数,带有一个string 类型的参数,在此构造函数中将传递的字符串打印出来。(3)在 Main 方法中创建属于这个类的一个对象,不传递参数。(4)在 Main 方法中创建属于这个类的另一个对象,传递一个字符串“ This is a string.”。(5)在 Main 方法中声明类型为这个类的一个具有 5 个对象的数组,但不要实际创建分配到数组里的对象。(6)写出运行程序应该输出的结果。【解答】class Test1public Test1()Console.WriteLine(this);public Test1(string str)Conso

12、le.WriteLine(str);public static void Main()Test1 t1 = new Test1();Test1 t2 = new Test1(This is a string.);Test1 t3 = new Test15;输出结果:Test1This is a string.2、编写一个控制台应用程序,定义一个类MyClass,类中包含有public、private以及protected数据成员及方法。然后定义一个从MyClass类继承的类 MyMain ,将 Main 方法放在 MyMain 中,在 Main 方法中创建 MyClass 类的一个对象,并分别

13、访问类中的数据成员及方法。要求注明在试图访问 所有类成员时哪些语句会产生编译错误。【解答】using System;class MyClasspublic int i;private int j;protected int k;public void method1()Console.WriteLine(public method.);private void method2()Console.WriteLine(private method.);protected void method3()Console.WriteLine(protected method.);class MyMain

14、: MyClasspublic static void Main()MyClass t = new MyClass();Console.WriteLine(i=0, t.i);Console.WriteLine(j=0, t.j);/会出现编译错误,私有成员不允许在其它类中访问Console.WriteLine(k=0, t.k);/会出现编译错误,应该创建 MyMain 的对象,然/ 后通过 MyMain 的对象访问t.method1();t.method2();/ 会出现编译错误,私有的方法不允许在其它类中调用t.method3();/ 会出现编译错误,应该创建 MyMain 的对象,然后

15、通过 MyMain 的/对象调用该方法protected 数据。3、创建一个类包含有 protected 数据。在相同的文件里创建第二个类,用一个方法操纵第一个类里的 【解答】using System;class Class1protected int i = 5;protected void MyMethod()Console.WriteLine(protected method.);class Class2 : Class1private void NewMethod()Console.WriteLine(this.i);this.i += 10;Console.WriteLine(thi

16、s.i);public static void Main()Class2 t = new Class2();t.NewMethod();3、编写一个控制台应用程序,完成下列功能,并回答提出的问题。(1) 创建一个类A,在构造函数中输出“ A”,再创建一个类B,在构造函数中输出“ B”。(2) 从A继承一个名为C的新类,并在C内创建一个成员B。不要为C创建构造函数。(3) 在 Main 方法中创建类 C 的一个对象,写出运行程序后输出的结果。(4) 如果在C中也创建一个构造函数输出“ C”,整个程序运行的结果又是什么?【解答】using System;public class Apublic A

17、()Console.WriteLine(A);public class Bpublic B()Console.WriteLine(B);public class C : AB newb = new B();class MainClasspublic static void Main()C newc = new C();Console.ReadLine();输出结果:BA如果在C中也创建一个构造函数输出“ C ”,即添加:public C()Console.WriteLine(C);则整个程序运行的结果为:BAC4、编写一个控制台应用程序,完成下列功能,并写出运行程序后输出的结果。10后的结果(

18、1) 创建一个类A,在A中编写一个可以被重写的带int类型参数的方法MyMethod,并在该方法中输出传递的整型值加(2) 再创建一个类B,使其继承自类A,然后重写A中的MyMethod方法,将A中接收的整型值加50,并输出结果。(3) 在 Main 方法中分别创建类 A 和类 B 的对象,并分别调用 MyMethod 方法。【解答】using System;public class Apublic virtual void MyMethod(int num)num += 10;Console.WriteLine(num);public class B : Apublic override v

19、oid MyMethod(int num)num += 50;Console.WriteLine(num);class MainClasspublic static void Main()A newa = new A();newa.MyMethod(2);B newb = new B();newb.MyMethod(2); Console.ReadLine();输出结果:12525、假设Node类的每一个节点包括有两个字段:m_data (引用节点的数据)和 m_next (引用链接列表中的下一项)。这两个字段都是由构造函数方法设置的。该类有两个功能,第一个功能是通过名为 Data和Next的

20、只读属性访问m_data和m_next字段。第二个功能是对 System.Object的ToString 虚拟方法进行重写。试分别用类和泛型两种方法编写程序实现上述功能。【解答】using System;class NodeObject m_data;Node m_next;public Node(Object data, Node next)m_data = data;m_next = next;/ 访问结点数据public Object Dataget return m_data; / 访问下一个结点public Node Nextget return m_next; / 获取结点数据描述

21、public override String ToString()return m_data.ToString();/ 链表结点类的泛型定义class NodeT m_data;Node m_next;public Node(T data, Node next)m_data = data;m_next = next;/ 访问结点数据public T Dataget return m_data; set m_data = value; / 访问下一个结点public Node Nextget return m_next; set m_next = value; / 获取结点数据描述public

22、override String ToString()return m_data.ToString();/ 使用结点类型或泛型结点类型class LinkedListstatic void Main(string args)/ 创建整数链表/Node head = new Node(5, null);/head = new Node(10, head);/head = new Node(15, head);/遍历链表求整数和lnt32 sum = 0;/for (Node current = head; current != null;/ current = current.Next)/ sum

23、 += (Int32)current.Data;/输岀结果Console.WriteLine(Sum of nodes = 0, sum);/用泛型创建整数链表Node head = new Node(5, null);head = new Node(10, head);head = new Node(15, head);/遍历求和Int32 sum = 0;for (Node current = head; current != null;current = current.Next)sum += current.Data;/输出Co nsole.WriteL in e(Sum of nod

24、es = 0, sum.ToStri ng();4、设计一个 Windows应用程序,窗体上有一个 TextBox控件、一个 Button控件。要求:每当用户单击按钮时,文本框都会增加一行文字来反映 单击的次数,例如,“第3次单击按钮”。【解答】1)窗体界面如图6-1所示;控件Name属性功能其它属性TextBox 控件textBox1显示信息ScrollBars=Vertical; Multiline=TrueButton控件Butt on1触发添加信息事件Button2触发结束添加事件2)窗体中主要控件属性设置如表6-1;表6-1窗体中的主要控件属性3)主要事件代码int i = 1;bo

25、ol Add = true;private void butt on 1_Click(object sen der, Even tArgs e) if(Add) textBox1.Text +=第+ i + 次单击按钮 rn; i+;private void butt on 2_Click(object sen der, Even tArgs e)图6-1窗体界面Add = false;5、编写一段程序,向名为listBox1的ListBox控件中,自动添加10个随机数,每个数占一项。解答】主要代码如下。public partial class Form1 : Form int m = 1;p

26、rivate void button1_Click(object sender, EventArgs e)for (int i = m ; i m+10; i+) listBox1.Items.Add(i); m = m + 10; 2、编写程序用 Directory 类提供的方法确定指定的目录是否存在,如果不存在,则创建该目录。然后在其中创建一个文件,并将一个字符串写到文 件中。【解答】程序清单如下:using System;using System.IO;class Testpublic static void Main()string path = c:MyDir;tryif (!Dir

27、ectory.Exists(path)Directory.CreateDirectory(path);StreamWriter sw=File.CreateText(path+myfile.txt);sw.WriteLine(This is a String!);sw.close();catch (Exception e)Console.WriteLine( 操作失败 : 0, e.ToString();finally 3、编写程序,使用 File 类实现删除指定目录下的指定文件。【解答】程序清单如下:using System;using System.IO;class FileTestpub

28、lic static void Main()Console.WriteLine( 确认删除当前目录下的所有文件 ?);Console.WriteLine(”点击Y键继续,其它键取消操作”);int a = Console.Read();if(a = Y | a = y)Console.WriteLine( 正在删除文件 .);elseConsole.WriteLine( 用户取消操作 );return;Directory Info dir = new Directory Info (.);foreach (FileInfo f in dir.GetFiles()f.Delete();2、使用

29、Panel 控件分别以矩形、椭圆和圆形的方式动态显示图片,图片的大小由 Panel 控件的大小决定。【解答】(1) 新建一个 Windows应用程序,命名为“ ShowImageExe”,调整窗体到适当大小。更改“Form1.cs”为“ FromShowlmageExe.cs”。(2) 切换到代码方式,添加名称空间引用:using System.Drawing.Drawing2D;添加四个 Button 控件分别命名为“ buttonOpenFile”、“buttonRectangle”、“buttonEllipse”、“buttonRound”,以及一个 openFileDiolog 禾廿

30、Panel控件在Form类下声明两个私有变量filename和flag,分别用来记录打开的文件名和判断哪个按钮的 click时间被触发。private string filename = ;private int flag = 0;(5) 添加【打开文件】按钮的 click 事件private void buttonOpenFile_Click(object sender, EventArgs e)openFileDialog1.ShowDialog();filename = openFileDialog1.FileName;panel1.Refresh();(6) 在 panel1 控件的

31、paint 事件下添加如下代码:private void panel1_Paint(object sender, PaintEventArgs e)if (filename.Trim() = )return;Bitmap mybitmap = new Bitmap(filename);Graphics g = e.Graphics;TextureBrush mybrush = new TextureBrush(mybitmap,WrapMode.Clamp);/保证图片完全由绘制对象的边框决定switch (flag)case 1:g.FillRectangle(mybrush, panel1

32、.ClientRectangle);break;case 2:g.FillEllipse(mybrush, panel1.ClientRectangle);break;case 3: g.FillEllipse(mybrush,(panel1.Width-panel1.Height)/2,0, panel1.Height,panel1.Height); break;(7) 在其他几个按钮的 click 事件中分别添加如下代码:private void buttonRectangle_Click(object sender, EventArgs e)flag = 1;panel1.Refresh

33、();private void buttonEllipse_Click(object sender, EventArgs e)flag = 2;pan el1.Refresh();private void butt onRoun d_Click(object sen der, Even tArgs e) flag = 3;pan el1.Refresh();(8) 结果如图所示。第2题以矩形、椭圆和圆形的方式显示图片运行 图3、编写一个 Windows窗体应用程序,利用 PictureBox控件和Panel控件实现滚动浏览大图片的功能。【解答】由于Picturebox控件在显示图片时不能直接使

34、用滚动条,所以必须借助Panel控件实现以滚动条的方式浏览大图片。具体操作步骤如下:(1) 新建一个 Windows应用程序,命名为“ scrollBar”,调整窗体到适当大小。更改“ Form1.cs”为“ FormScrollBar.cs”。(2) 切换到代码方式,添加名称空间引用:using System.Drawing.Drawing2D;在窗体上分别添加一个 button控件命名为“buttonOpenFile”,一个openFileDiolog控件,Picturebox和Panel控件各一个,将Panel控件的AutoScroll 属性设为true。 在“ buttonOpenFi

35、le”控件的click事件中添加如下代码:private void buttonOpenFile_Click(object sender, EventArgs e)openFileDialog1.ShowDialog();if (openFileDialog1.FileName.Trim()=)return;tryBitmap mybitmap = new Bitmap(openFileDialog1.FileName);pictureBox1 .Im age = mybitmap;catch (Exception Err)MessageBox.Show(“打开文件错误!,信息提示, Mess

36、ageBoxButtons.OK, MessageBoxIcon.Information);(5)结果如图所示4、编写一个 Windows窗体应用程序,实现对图片进行旋转、缩放和平移的功能。【解答】(1)在窗体上添加六个label控件供中labell用来显示图片)、一个button控件(用于打开图片文件)和五个numericUpDown控件(分别用来选择图片缩放的比例、图片旋转的角度、图片位移的大小)(2)在构造函数上方添加代码:private string strfilename=;(3) 在button控件的click事件里添加如下代码:private void button1_Click

37、(object sender, EventArgs e)openFileDialog1.ShowDialog();strfilename=openFileDialog1.FileName; label1.Refresh();在每一个numericUpDown控件的ValueChanged事件中添加如下代码:label1.Refresh();(5)在labell控件的paint事件中添加如下代码:private void label1_Paint(object sender, PaintEventArgs e) if (this.strfilename.Trim()=)return ;tryBi

38、tmap mybitmap = new Bitmap(strfilename);Graphics g = e.Graphics;TextureBrush mybrush = new TextureBrush(mybitmap);float x = (float)(numericUpDownS1.Value / 100);float y = (float)(numericUpDownS2.Value / 100);mybrush.ScaleTransform(x, y);g.FillRectangle(mybrush, 0, 0, ClientRectangle.Width, ClientRec

39、tangle.Height);float r = (float)(numericUpDownR1.Value);mybrush.RotateTransform(r);g.FillRectangle(mybrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height);float tx = (float)(numericUpDownT1.Value);float ty = (float)(numericUpDownT2.Value);mybrush.TranslateTransform(tx, ty); catch(Exception Err

40、)g.FillRectangle(mybrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height);MessageBox.Show(“打开文件错误!,信息提示,MessageBoxButtons.OK, MessageBoxIcon.Information);1、使用保持连接方式编写程序,计算各年级平均成绩,并显示结果。【解答】using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.D

41、rawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace 习题 9_1public partial class Form1 : Formpublic Form1()InitializeComponent();/ 添加 Button 按钮在 ListBox 中显示结果private void button1_Click(object sender, EventArgs e) listBox1.Items.Add( 年级 平均成绩 );string connectionStri

42、ng = Properties.Settings.Default.MyDatabaseConnectionString; /根据连接字符串创建 SqlConnection 实例SqlConnection conn = new SqlConnection(connectionString);创建SqlCommand实例,并设置SQL语句和使用的连接实例SqlCommand cmd = new SqlCommand();cmd.CommandText = select substring(学号,1,2) as 年级,avg(成绩)as 平均成绩 from MyTable2 group by sub

43、string(学号,1,2); cmd.Connection = conn;tryconn.Open();SqlDataReader r = cmd.ExecuteReader();while (r.Read() = true)listBox1.Items.Add(string.Format(0 级1, r0, r1);r.Close();catch (Exception err)MessageBox.Show(err.Message, 计算成绩失败 );finallyconn.Close();2、使用保持连接方式编写程序,查询MyTable2 中不及格学生的学号、姓名、性别和成绩。并将结果在

44、 ListBox 中显示出来。【解答】using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace 习题 9_2public partial class Form1 : Formpublic Form1()InitializeComponent();private vo

45、id button1_Click(object sender, EventArgs e)listBox1.Items.Add( 学号 姓名 性别 成绩 );string connectionString = Properties.Settings.Default.MyDatabaseConnectionString; /根据连接字符串创建 SqlConnection 实例SqlConnection conn = new SqlConnection(connectionString);创建SqlCommand实例,并设置SQL语句和使用的连接实例SqlCommand cmd = new SqlC

46、ommand();cmd.CommandText =Select 学号,姓名,性别, 成绩 From MyTable2 Where (成绩60);cmd.Connection = conn;tryconn.Open();SqlDataReader r = cmd.ExecuteReader();while (r.Read() = true)listBox1.Items.Add( string.Format(0123, r0, r1, r2, r3);r.Close();catch (Exception err)MessageBox.Show(err.Message, 查询成绩失败 );fina

47、llyconn.Close();3、编写程序,以“ 编码 名称”的样式在 comboBox1 中显示 MyTable1 的内容。 【解答】 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace 习题 9_3public partial class Form1 :

48、 Formpublic Form1()InitializeComponent();private void Form1_Load(object sender, EventArgs e)string connectionString = Properties.Settings.Default.MyDatabaseConnectionString; /根据连接字符串创建 SqlConnection 实例SqlConnection conn = new SqlConnection(connectionString);创建SqlCommand实例,并设置SQL语句和使用的连接实例SqlCommand

49、cmd = new SqlCommand();cmd.CommandText = Select * From MyTable1;cmd.Connection = conn;tryconn.Open();SqlDataReader r = cmd.ExecuteReader(); while (r.Read() = true) comboBox1.Items.Add(string.Format(0 1, r0, r1); comboBox1.SelectedIndex = 0;r.Close();catch (Exception err)MessageBox.Show(err.Message,

50、显示数据失败 );finallyconn.Close();18 的所有纪4、已知数据库 MyDb.mdf 中定义了一张 person 表,表中有一个“年龄”字段,编写存储过程并调用存储过程显示该数据表中年龄大于 录。【解答】using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace 习题 9_4public partial class Form1 : Formpublic Form1()InitializeComponent();private void button1_Click(object sender, EventArgs e)string connectionstring = Properties.Settings.Default.MyDatabaseConnectionString ;SqlConnection conn = new SqlConnection(connectionstrin

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论