四川理工C#网络编程(1—5章)答案_第1页
四川理工C#网络编程(1—5章)答案_第2页
四川理工C#网络编程(1—5章)答案_第3页
四川理工C#网络编程(1—5章)答案_第4页
四川理工C#网络编程(1—5章)答案_第5页
已阅读5页,还剩7页未读, 继续免费阅读

下载本文档

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

kok电子竞技:文档简介

1、四川理工大学2012级计科四班 百年孤独(匿名胖鸟) 提供第一章1如何确定最佳的断行位置1. 表达式应完整清晰地体现其内在的逻辑关系,因此一般选择整个表达式中最高的关系层次进行断行,比如“=“,”|”等,并将分隔符留在上一行;2. 当参数数量较多时,较长或含有表达式时,为了将逻辑体现得更为清晰,应将函数调用中的每个参数都分行书写;3. 对于SQL语句或其他较长的复合语句,应将每个子句单独写成一行,便于理解。2九九乘法表using System;using System.Collections.Generici;using System.Linq;using System.Text;namesp

2、ace Homework class MulTable /乘法表类#region public void mulTablei() for (int ii = 1; i 10; i+) for (int j = 1; j = i; j+) String s = j + x + i + = + i * j; Console.Writiie(s.PadRighti(8); /使用PadRighti的作用是对齐 Console.WriteLine(); #endregion class Program /主类 static void Main(string args) MulTable m = new

3、 MulTable();/实例化一个对象m m.mulTable();/调用mulTable函数打印九九表 3关于类与对象的思考用我个人的话说,类就相当于一个模板,它不能直接使用,用模板造出来的东西就是对象,对象才可以被使用。第二章1注释的用途1. 解释代码的意,用符合人类思维方式的描述性文字阐明代码的功能和意图;2. 对局部变量进行含义,取值范围,单位等的说明;3. 充当代码标题,便于掌握代码总体结构,快速定位错误,阐明分支与循环控制结构;4. 指出例外情况,为达到特殊效果有意为之,并对其说明原由。2复数类Complexusing System;using System.Collection

4、s.Generic;using System.Linq;using System.Text;namespace Homework / / Complex 实现复数的加减功能 / class Complex/一个简单的复数类 / / real 为实数的实部 / image 为实数的虚部 / private Int32 real; private Int32 image; / / 初始化Homework.Complex的新实例 / public Complex()/无参构造函数 this.real = 0; this.image = 0; / / 使用指定的参数列表初始化Homework.Comp

5、lex的新实例 / / 形参实部 / 形参虚部 public Complex(int r, int i)/带参构造函数 this.real = r; this.image = i; / / Add实现两个复数相加的功能 / / 类型为Complex的形参 / 返回一个a+bi形式的字符串 public String Add(Complex com)/复数加法方法 Int32 addReal = this.real + com.real;/addReal为实部相加结果 Int32 addImage = this.image + com.image;/addImage为虚部相加结果 if (0 !

6、= addReal & 0 addImage)/实部不为0,虚部小于0,结果形如:2 - 5i return addReal.ToString() + addImage.ToString() + i; else if (0 = addReal & 0 != addImage)/实部为0,虚部不为0,结果形如:5i return addImage.ToString() + i; else if (0 != addReal & 0 = addImage)/实部不为0,虚部为0,结果形如:2 return addReal.ToString(); else if (0 = addReal & 0 =

7、addImage)/实部虚部都为0,即结果为:0 return 0; else/实部不为0,虚部大于0,结果形如:2+5i return addReal.ToString()+ + + addImage.ToString() + i; / / Sub实现复数相减的功能 / / 类型为Complex的形参 / 返回一个形如a+bi的字符串 public String Sub(Complex com)/复数减法方法 Int32 subReal = this.real - com.real;/subReal为实部相减结果 Int32 subImage = this.image - com.image

8、;/subImage为虚部相减结果 if (0 != subReal & 0 subImage)/实部不为0,虚部小于0,结果形如:2 - 5i return subReal.ToString() + subImage.ToString() + i; else if (0 = subReal & 0 != subImage)/ 实部为0,虚部不为0,结果形如:5i return subImage.ToString() + i; else if (0 != subReal & 0 = subImage)/ 实部不为0,虚部为0,结果形如:2 return subReal.ToString();

9、else if (0 = subReal & 0 = subImage)/ 实部虚部都为0,即结果为:0 return 0; else/实部不为0,虚部大于0,结果形如:2+5i return subReal.ToString() + + + subImage.ToString() + i; class Program/主函数类 static void Main(string args) /实例化两个对象c1,c2 Complex c1 = new Complex(1, 2); Complex c2 = new Complex(2, -2); /输出两个复数相互加减的结果 Console.Wr

10、iteLine(c1.Add(c2); Console.WriteLine(c1.Sub(c2); Console.WriteLine(c2.Add(c1); Console.WriteLine(c2.Sub(c1); 3圆类Circleusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Homework / / 提供计算圆周长和面积的功能 / class Circle / / radius为圆的半径 / private double radius; / / 初始

11、化Homework.Circle的新实例 / public Circle() this.radius = 0; / / 用指定的参数列表初始化Homework.Circle的新实例 / / 形参为圆的半径,double类型 public Circle(double r) this.radius = r; / / 实现计算圆周长的功能 / / 返回圆的周长,类型为double public double Girth() return Math.PI * this.radius * 2; / / 实现计算圆面积的功能 / / 返回圆的面积,类型为double public double Ariea

12、() return Math.PI * this.radius * this.radius; class Program/主函数类 static void Main(string args) Circle c = new Circle(10);/实例化一个对象c Console.WriteLine(c.Girth();/输出圆c的周长 Console.WriteLine(c.Area();/输出圆c的面积 第三章1字符使用规则1. 所有的标识符只能由字母,数字,下划线组成,且第一个字符不能为数字,下划线主要用于分隔汗多个单词的常量;2. 标识符中不能包含空格,标点符号,运算符等其他符号;3.

13、标识符区分大小写;4. 利用英语单词的组合命名,做到一目了然;5. 标识符不能与C#的关键字相同;6. 标识符尽量不与C#中类库名相同。2Pascal与Camel规则的区别,何时采用Camel1. Pascal: 标识符中每个单词的首字母大写,其余小写;Camel: 标识符的第一个单词的首字母小写,其余每个单词首字母大写,剩余小写2. 外部程序不可见的“私有及受保护的字段”,“局部变量”,“函数参数”使用Camel规则。3创建一个Windows工程,实现登录界面。namespace HomeworkWinForm partial class FormLogin / / 必要的设计器变量 / p

14、rivate System.ComponentModel.IContainer components = null; / / 清理所有正在使用的资源 / / 如果应释放托管资源,为 true;否则为false protected override void Dispose(bool disposing) if (disposing & (components != null) components.Dispose(); base.Dispose(disposing); #region Windows 窗体设计器生成的代码 / / 设计器支持所需的方法 不要 / 使用代码编辑器修改修此方法的内容

15、。 / private void InitializeComponent() this.labelUser = new System.Windows.Forms.Label(); this.labelPassword = new System.Windows.Forms.Label(); this.textBoxUser = new System.Windows.Forms.TextBox(); this.textBoxPassword = new System.Windows.Forms.TextBox(); this.buttonSignIn = new System.Windows.Fo

16、rms.Button(); this.buttonCancle = new System.Windows.Forms.Button(); this.labelPassword2 = new System.Windows.Forms.Label(); this.textBoxPassword2 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); / / labelUser / this.labelUser.AutoSize = true; this.labelUser.Location = new System.Drawing.

17、Point(30, 26); this.labelUser.Name = labelUser; this.labelUser.Size = new System.Drawing.Size(67, 15); this.labelUser.TabIndex = 0; this.labelUser.Text = 用户名:; / / textBoxUser / this.textBoxUser.Locationi = new System.Drawing.Point(107, 23); this.textBoxUser.MaxLength = 20; this.textBoxUser.Name = t

18、extBoxUser; this.textBoxUser.Size = new System.Drawing.Size(159, 25); this.textBoxUser.TabIndex = 2; / / labelPassword / this.labelPassword.AutoSize = true; this.labelPassword.Location = new System.Drawing.Point(30, 67); this.labelPassword.Name = labelPassword; this.labelPassword.Size = new System.D

19、rawing.Size(67, 15); this.labelPassword.TabIndex = 1; this.labelPassword.Text = 密码:; / / textBoxPassword / this.textBoxPassword.Location = new System.Drawing.Point(107, 64); this.textBoxPassword.MaxLength = 18; this.textBoxPassword.Name = textBoxPassword; this.textBoxPassword.PasswordChar = #; this.

20、textBoxPassword.Size = new System.Drawing.Size(159, 25); this.textBoxPassword.TabIndex = 3; / / labelPassword2 / this.labelPassword2.AutoSize = true; this.labelPassword2.Location = new System.Drawing.Point(30, 103); this.labelPassword2.Name = labelPassword2; this.labelPassword2.Size = new System.Dra

21、wing.Size(82, 15); this.labelPassword2.TabIndex = 6; this.labelPassword2.Text = 确认密码:; / / textBoxPassword2 / this.textBoxPassword2.Location = new System.Drawing.Point(107, 100); this.textBoxPassword2.MaxLength = 18; this.textBoxPassword2.Name = textBoxPassword2; this.textBoxPassword2.PasswordChar =

22、 #; this.textBoxPassword2.Size = new System.Drawing.Size(159, 25); this.textBoxPassword2.TabIndex = 7; / / buttonSignIn / this.buttonSignIn.Location = new System.Drawing.Point(52, 150); this.buttonSignIn.Name = buttonSignIn; this.buttonSignIn.Size = new System.Drawing.Size(75, 33); this.buttonSignIn

23、.TabIndex = 4; this.buttonSignIn.Text = 注册; this.buttonSignIn.UseVisualStyleBackColor = true; this.buttonSignIn.Click += new System.EventHandler(this.buttonLogin_Click); / / buttonCancle / this.buttonCancle.Location = new System.Drawing.Point(179, 150); this.buttonCancle.Name = buttonCancle; this.bu

24、ttonCancle.Size = new System.Drawing.Size(75, 33); this.buttonCancle.TabIndex = 5; this.buttonCancle.Text = 取消; this.buttonCancle.UseVisualStyleBackColor = true; this.buttonCancle.Click += new System.EventHandler(this.buttonCancle_Click); / / FormLogin / this.AutoScaleDimensions = new System.Drawing

25、.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSizei = new System.Drawing.Size(316, 209); this.Controls.Add(this.textBoxPassword2); this.Controls.Add(this.labelPassword2); this.Controls.Add(this.buttonCancle); this.Controls.Add(this.buttonSignIn); this.Cont

26、rols.Add(this.textBoxPassword); this.Controls.Add(this.textBoxUser); this.Controls.Add(this.labelPassword); this.Controls.Add(this.labelUser); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = FormLogin; this.ShowIcon = false; this.ShowInTaskbar = false; this.Text = 注册界面; this.Load +=

27、new System.EventHandler(this.FormLogin_Load); this.ResumeLayout(false); this.PerformLayout(); #endregion private System.Windows.Forms.Label labelUser; private System.Windows.Forms.TextBox textBoxUser; private System.Windows.Forms.Label labelPassword; private System.Windows.Forms.Label labelPassword2

28、; private System.Windows.Forms.TextBox textBoxPassword2; private System.Windows.Forms.TextBox textBoxPassword; private System.Windows.Forms.Button buttonSignIn; private System.Windows.Forms.Button buttonCancle; 第四章1. 判断double型的d1是否等于3.14的表达式if (Math.Abs(d1 - 3.14) = 90 & score = 80) Console.WriteLin

29、e(良好); else if (score = 70) Console.WriteLine(中等); else if (score = 60) Console.WriteLine(及格); else Console.WriteLine(不及格); void Score_Grade(float score)/switch实现等级转换 int i = (int)score/10; switch(i) case 10: case 9: Console.WriteLine(优秀); break; case 8: Console.WriteLine(良好); break; case 7: Console

30、.WriteLine(中等); break; case 6: Console.WriteLine(及格); break; case 5: case 4: case 3: case 2: case 1: case 0: Console.WriteLine(不及格); break; default: Console.WriteLine(Error.); break; 2. C#的四种循环结构,使用选择四种循环结构为:while, do while, for, foreach四种使用选择:1,迭代变量为数值型或次数清楚时用for,否则使用while 2,循环变量需要在循环体内修改或次数不可预知时用w

31、hile 3,当循环次数可为0次时使用for或while,否则可使用do while 4,当循环不知次数且须遍历时使用foreach最好3. 抄写素数程序并注释,画出流程图int m, k, n;/m为外循环变量,k为内循环变量,n为一个过渡变量bool isPrime = true;/isPrime判断m是否为素数Console.Write(2 + t + 3 + t);/输出自然数中最前的两个素数2,3for (m = 5; m 100; m += 2)/外循环,因为大于2的素数不可能是偶数,所以循环变量每次加2 isPrime = true;/每次进入外层for循环就将isPrime设置

32、为true n = (int)Math.Sqrt(m);/将m开方后赋值给n并放在外层for循环,可以减少计算次数 for (k = 3; k = n; k += 2)/内层for循环 if (m % k = 0)/判断m%k是否为0从而判断m是否为素数 isPrime = false;/如果m%k等于0则说明m不是素数,将isPrime赋值为false break; / 跳出内循环,进入外循环的下一轮,判断下一个m是否为素数 if (isPrime = true)/每次内循环结束后判断isPrime的值的真假, Console.Write(m + t);/如果为真就说明m是素数,然后输出m值。 流程图Word做图好难,拙图请谅解NNisPrime =false breakYisPrime = truen =(int)Math.sqrt(m)k =3K=n ?YYNYN结束m +=2misPrime = trueisPrime=true?K+=2m%k=0?m100?m = 52 3开始

温馨提示

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

kok电子竞技:最新文档

评论

0/150

提交评论