C#程序设计及应用课后题答案.docx

上传人:牧羊曲112 文档编号:4883243 上传时间:2023-05-21 格式:DOCX 页数:22 大小:246.11KB
返回 下载 相关 举报
C#程序设计及应用课后题答案.docx_第1页
第1页 / 共22页
C#程序设计及应用课后题答案.docx_第2页
第2页 / 共22页
C#程序设计及应用课后题答案.docx_第3页
第3页 / 共22页
C#程序设计及应用课后题答案.docx_第4页
第4页 / 共22页
C#程序设计及应用课后题答案.docx_第5页
第5页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《C#程序设计及应用课后题答案.docx》由会员分享,可在线阅读,更多相关《C#程序设计及应用课后题答案.docx(22页珍藏版)》请在三一办公上搜索。

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;using Sys

2、tem.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+ 5);Console.ReadLine(

3、);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.ReadLine();if (str.Length != 5)C

4、onsole.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) 转到(1)继续接收下一个整数。【解答】using System;using S

5、ystem.Collections.Generic;using System.Text;namespace testOutputclass Programstatic 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(j);catchConsole.WriteLine(你输入的不是数字或超出整数的表示

6、范围,请重新输入”);8、编写一个控制台应用程序,求1000之内的所有“完数”。所谓“完数”是指一个数恰好等于它的所有因子之和。例如,6是完数,因为6=1+2+3。【解答】using System;using System.Collections.Generic;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; j = (int)Mat

7、h.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 += string.Format(+0, j);if (s = i) Console.WriteLine(0=1, i, str);Console.ReadLine(); 3、编写一个控制台应用程序,计算X 2x31 2! 3!x4、+ .+ (一 1)( n+D4!n要求精度为10 8。【解答】using System;class Test3 public st

8、atic void Main()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;Console.WriteLine(n=0,s=1:0.00000000”, n, s);4、编写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能。(1) 输出字符串的长度。(2) 输出字符串中第一个出现字母a的位置。(3) 在字符串的第3个字符后面插入子串 hello”,输出新字符串。(4) 将

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

10、ne(插入 hello 后的结果为:0”, strl);/(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)创建一个类,用无参数的构造函数输出该类的类名。(2)增加一个重载的构

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

12、ublic static void Main()Testi tl = new Test1();Testi 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 : MyClasspublic static void

14、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的对象,然后通过MyMain的对象调用该方法3、创建一个类包含有protected数据。在相同

15、的文件里创建第二个类,用一个方法操纵第一个类里的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(this.i);public static void Main()Class2 t = new Cla

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

17、e.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、编写一个控制台应用程序,完成下列功能,并写出运行程序后输出的结果。(1) 创建一个类A,在A中编写一个可以被重写的带int类型参数的方法MyMethod,并在该方法中输出传递的整型值加10后

18、的结果。(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 void MyMethod(int num)num += 50;Console.WriteLine(num);class M

19、ainClasspublic 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的只读属性访问m_data和m_next字段。第二个功能是对System.Object的ToString 虚拟方法进行重写。试分

20、别用类和泛型两种方法编写程序实现上述功能。【解答】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 Next get return m_next; /获取结点数据描述public override String ToString()return m_data.ToString();/链表结点类的

21、泛型定义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 override String ToString()return m_data.ToString();/使用结点类型或泛型结点类型clas

22、s LinkedListstatic void Main(string args)/创建整数链表/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 += (Int32)current.Data;/输出结果/Console.WriteLine(Sum of nodes = 0, sum)

23、;/用泛型创建整数链表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;/输出Console.WriteLine(Sum of nodes = 0, sum.ToString(); 4、设计一个Windows应用程序,窗体上有一个TextBox控件、一个Button控件。要求:

24、每当用户单击按钮时,文本框都会增加一行文字来反映单击的次数,例如,“第3次单击按钮”。【解答】藐成Bi睇凹矿 惭池撒 抽I&.tf尊it辰i图6-1窗体界面1)窗体界面如图6-1所示;2)窗体中主要控件属性设置如表6-1;表6-1窗体中的主要控件属性控件Name属性功能其它属性TextBox 控件textBox1显示信息ScrollBars=Vertical; Multiline=TrueButton控件Button1触发添加信息事件Button2触发结束添加事件3)主要事件代码。int i = 1;bool Add = true;private void button1_Click(obje

25、ct sender, EventArgs e)if(Add) textBox1.Text +=,第+ i + 次单击按钮 rn;i+;private void button2_Click(object sender, EventArgs e)Add = false;5、编写一段程序,向名为listBox 1的ListBox控件中,自动添加10个随机数,每个数占一项。【解答】主要代码如下。public partial class Forml : Formint m = 1;private void button1_Click(object sender, EventArgs e) for (in

26、t 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 (!Directory.Exists(path)Directory.CreateDirectory(path);StreamWriter

27、 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 FileTestpublic static void Main()Console.WriteLine(确认删除当前目录下的所有文件?); Console.W

28、riteLine(点击Y键继续,其它键取消操作); int a = Console.Read();if(a = Y | a = y)Console.WriteLineC 正在删除文件.);elseConsole.WriteLine(用 户取消操作);return;Directory Info dir = new Directoryinfo (.);foreach (Fileinfo f in dir.GetFiles()f.Delete();2、使用Panel控件分别以矩形、椭圆和圆形的方式动态显示图片,图片的大小由Panel控件的大小决定。【解答】(1) 新建一个Windows应用程序,命名

29、为“ShowImageExe”,调整窗体到适当大小。更改“Form1.cs”为“FromShowImageExe.cs”。(2) 切换到代码方式,添加名称空间引用:using System.Drawing.Drawing2D;(3) 添加四个 Button 控件分别命名为 “buttonOpenFile”、“buttonRectangle”、“buttonEllipse”、“buttonRound”,以及一个 openFileDiolog 和 Panel 控件。在Form类下声明两个私有变量filename和flag,分别用来记录打开的文件名和判断哪个按钮的click时间被触发。private

30、 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控件的paint事件下添加如下代码:private void panel1_Paint(object sender, PaintEventArgs e)if (file

31、name.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.ClientRectangle);break;case 2:g.FillEllipse(mybrush, panel1.ClientRectangle);break;c

32、ase 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();private void buttonEllipse_Click(object sender, EventArgs e)flag = 2;panel1.Refresh();p

33、rivate void buttonRound_Click(object sender, EventArgs e) flag = 3;panel1.Refresh();(8)结果如图所示。第2题 以矩形、椭圆和圆形的方式显示图片运行图3、编写一个Windows窗体应用程序,利用PictureBox控件和Panel控件实现滚动浏览大图片的功能。【解答】由于Picturebox控件在显示图片时不能直接使用滚动条,所以必须借助Panel控件实现以滚动条的方式浏览大图片。具体操作步骤如下:(1) 新建一个Windows应用程序,命名为“scrollBar”,调整窗体到适当大小。更改“Form1.cs”

34、为“FormScrollBar.cs”。(2) 切换到代码方式,添加名称空间引用:using System.Drawing.Drawing2D;(3) 在窗体上分别添加一个button控件命名为“buttonOpenFile”,一个openFileDiolog控件,Picturebox和Panel控件各一个,将Panel控件的AutoScroll 属性设为true。 在“buttonOpenFile”控件的click事件中添加如下代码:private void buttonOpenFile_Click(object sender, EventArgs e)openFileDialog1.Sho

35、wDialog();if (openFileDialog1.FileName.Trim()=)return;tryBitmap mybitmap = new Bitmap(openFileDialog1.FileName);pictureBox1.Image = mybitmap;catch (Exception Err)MessageBox.Show(打开文件错误!,信息提示,MessageBoxButtons.OK, MessageBoxIcon.Information);(5)结果如图所示。第3题 滚动条浏览大图片4、编写一个Windows窗体应用程序,实现对图片进行旋转、缩放和平移的功

36、能。【解答】(1)在窗体上添加六个label控件(其中labell用来显示图片)、一个button控件(用于打开图片文件)和五个numericUpDown控件(分别用来选择图片缩放的比例、图片旋转的角度、图片位移的大小)。(2)在构造函数上方添加代码:private string strfilename=;(3)在button控件的click事件里添加如下代码:private void button1_Click(object sender, EventArgs e) openFileDialog1.ShowDialog();strfilename=openFileDialog1.FileNa

37、me;label1.Refresh();(4)在每一个numericUpDown控件的ValueChanged事件中添加如下代码:label1.Refresh();(5)在label1控件的paint事件中添加如下代码:private void label1_Paint(object sender, PaintEventArgs e) if (this.strfilename.Trim()=)return ;tryBitmap mybitmap = new Bitmap(strfilename);Graphics g = e.Graphics;TextureBrush mybrush = ne

38、w 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, ClientRectangle.Height);float r = (float)(numericUpDownR1.Value);mybrush.RotateTransform(r);g.Fill

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

40、.Show(打开文件错误!,信息提示,MessageBoxButtons.OK, MessageBoxIcon.Information);1、使用保持连接方式编写程序,计算各年级平均成绩,并显示结果。【解答】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

41、习题 9_1 public partial class Form1 : Form public Form1()InitializeComponent();/添加Button按钮在ListBox中显示结果private void button1_Click(object sender, EventArgs e) listBox1.Items.Add(年 级平均成绩勺;string connectionString = Properties.Settings.Default.MyDatabaseConnectionString; 根据连接字符串创建SqlConnection实例 SqlConnec

42、tion conn = new SqlConnection(connectionString);创建SqlCommand实例,并设置SQL语句和使用的连接实例 SqlCommand cmd = new SqlCommand();cmd.CommandText = select substring(学号,1,2) as 年级,avg(成绩)as 平均成绩 from MyTable2 group by substring(学号,1,2); cmd.Connection = conn;try conn.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,计算成绩失败); finally conn.Close()

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号