四川理工C#网络编程15章答案.docx
- 文档编号:4309523
- 上传时间:2022-11-29
- 格式:DOCX
- 页数:20
- 大小:63.32KB
四川理工C#网络编程15章答案.docx
《四川理工C#网络编程15章答案.docx》由会员分享,可在线阅读,更多相关《四川理工C#网络编程15章答案.docx(20页珍藏版)》请在冰豆网上搜索。
四川理工C#网络编程15章答案
四川理工大学2012级计科四班百年孤独(匿名胖鸟)提供
第一章
1.如何确定最佳的断行位置
1.表达式应完整清晰地体现其内在的逻辑关系,因此一般选择整个表达式中最高的关系层次进行断行,比如“=“,”||”等,并将分隔符留在上一行;
2.当参数数量较多时,较长或含有表达式时,为了将逻辑体现得更为清晰,应将函数调用中的每个参数都分行书写;
3.对于SQL语句或其他较长的复合语句,应将每个子句单独写成一行,便于理解。
2.九九乘法表
usingSystem;
usingSystem.Collections.Generici;
usingSystem.Linq;
usingSystem.Text;
namespaceHomework
{
classMulTable//乘法表类
{
#region
publicvoidmulTablei()
{
for(intii=1;i<10;i++)
{
for(intj=1;j<=i;j++)
{
Strings=j+"x"+i+"="+i*j;
Console.Writiie(s.PadRighti(8));//使用PadRighti的作用是对齐
}
Console.WriteLine();
}
}
#endregion
}
classProgram//主类
{
staticvoidMain(string[]args)
{
MulTablem=newMulTable();//实例化一个对象m
m.mulTable();//调用mulTable函数打印九九表
}
}
}
3.关于类与对象的思考
用我个人的话说,类就相当于一个模板,它不能直接使用,用模板造出来的东西就是对象,对象才可以被使用。
第二章
1.注释的用途
1.解释代码的意,用符合人类思维方式的描述性文字阐明代码的功能和意图;
2.对局部变量进行含义,取值范围,单位等的说明;
3.充当代码标题,便于掌握代码总体结构,快速定位错误,阐明分支与循环控制结构;
4.指出例外情况,为达到特殊效果有意为之,并对其说明原由。
2.复数类Complex
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceHomework
{
///
///Complex实现复数的加减功能
///
classComplex//一个简单的复数类
{
///
///real为实数的实部
///image为实数的虚部
///
privateInt32real;
privateInt32image;
///
///初始化Homework.Complex的新实例
///
publicComplex()//无参构造函数
{
this.real=0;
this.image=0;
}
///
///使用指定的参数列表初始化Homework.Complex的新实例
///
///
///
publicComplex(intr,inti)//带参构造函数
{
this.real=r;
this.image=i;
}
///
///Add实现两个复数相加的功能
///
///
///
publicStringAdd(Complexcom)//复数加法方法
{
Int32addReal=this.real+com.real;//addReal为实部相加结果
Int32addImage=this.image+com.image;//addImage为虚部相加结果
if(0!
=addReal&&0>addImage)//实部不为0,虚部小于0,结果形如:
2-5i
{
returnaddReal.ToString()+addImage.ToString()+"i";
}
elseif(0==addReal&&0!
=addImage)//实部为0,虚部不为0,结果形如:
5i
{
returnaddImage.ToString()+"i";
}
elseif(0!
=addReal&&0==addImage)//实部不为0,虚部为0,结果形如:
2
{
returnaddReal.ToString();
}
elseif(0==addReal&&0==addImage)//实部虚部都为0,即结果为:
0
{
return"0";
}
else//实部不为0,虚部大于0,结果形如:
2+5i
{
returnaddReal.ToString()+"+"+addImage.ToString()+"i";
}
}
///
///Sub实现复数相减的功能
///
///
///
publicStringSub(Complexcom)//复数减法方法
{
Int32subReal=this.real-com.real;//subReal为实部相减结果
Int32subImage=this.image-com.image;//subImage为虚部相减结果
if(0!
=subReal&&0>subImage)//实部不为0,虚部小于0,结果形如:
2-5i
{
returnsubReal.ToString()+subImage.ToString()+"i";
}
elseif(0==subReal&&0!
=subImage)//实部为0,虚部不为0,结果形如:
5i
{
returnsubImage.ToString()+"i";
}
elseif(0!
=subReal&&0==subImage)//实部不为0,虚部为0,结果形如:
2
{
returnsubReal.ToString();
}
elseif(0==subReal&&0==subImage)//实部虚部都为0,即结果为:
0
{
return"0";
}
else//实部不为0,虚部大于0,结果形如:
2+5i
{
returnsubReal.ToString()+"+"+subImage.ToString()+"i";
}
}
}
classProgram//主函数类
{
staticvoidMain(string[]args)
{
//实例化两个对象c1,c2
Complexc1=newComplex(1,2);
Complexc2=newComplex(2,-2);
//输出两个复数相互加减的结果
Console.WriteLine(c1.Add(c2));
Console.WriteLine(c1.Sub(c2));
Console.WriteLine(c2.Add(c1));
Console.WriteLine(c2.Sub(c1));
}
}
}
3.圆类Circle
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceHomework
{
///
///提供计算圆周长和面积的功能
///
classCircle
{
///
///radius为圆的半径
///
privatedoubleradius;
///
///初始化Homework.Circle的新实例
///
publicCircle()
{
this.radius=0;
}
///
///用指定的参数列表初始化Homework.Circle的新实例
///
///
publicCircle(doubler)
{
this.radius=r;
}
///
///实现计算圆周长的功能
///
///
publicdoubleGirth()
{
returnMath.PI*this.radius*2;
}
///
///实现计算圆面积的功能
///
///
publicdoubleAriea()
{
returnMath.PI*this.radius*this.radius;
}
}
classProgram//主函数类
{
staticvoidMain(string[]args)
{
Circlec=newCircle(10);//实例化一个对象c
Console.WriteLine(c.Girth());//输出圆c的周长
Console.WriteLine(c.Area());//输出圆c的面积
}
}
}
第三章
1.字符使用规则
1.所有的标识符只能由字母,数字,下划线组成,且第一个字符不能为数字,下划线主要用于分隔汗多个单词的常量;
2.标识符中不能包含空格,标点符号,运算符等其他符号;
3.标识符区分大小写;
4.利用英语单词的组合命名,做到一目了然;
5.标识符不能与C#的关键字相同;
6.标识符尽量不与C#中类库名相同。
2.Pascal与Camel规则的区别,何时采用Camel
1.Pascal:
标识符中每个单词的首字母大写,其余小写;
Camel:
标识符的第一个单词的首字母小写,其余每个单词首字母大写,剩余小写
2.外部程序不可见的“私有及受保护的字段”,“局部变量”,“函数参数”使用Camel规则。
3.创建一个Windows工程,实现登录界面。
namespaceHomeworkWinForm
{
partialclassFormLogin
{
///
///必要的设计器变量
///
privateSystem.ComponentModel.IContainercomponents=null;
///
///清理所有正在使用的资源
///
///
protectedoverridevoidDispose(booldisposing)
{
if(disposing&&(components!
=null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#regionWindows窗体设计器生成的代码
///
///设计器支持所需的方法–不要
///使用代码编辑器修改修此方法的内容。
///
privatevoidInitializeComponent()
{
this.labelUser=newSystem.Windows.Forms.Label();
this.labelPassword=newSystem.Windows.Forms.Label();
this.textBoxUser=newSystem.Windows.Forms.TextBox();
this.textBoxPassword=newSystem.Windows.Forms.TextBox();
this.buttonSignIn=newSystem.Windows.Forms.Button();
this.buttonCancle=newSystem.Windows.Forms.Button();
this.labelPassword2=newSystem.Windows.Forms.Label();
this.textBoxPassword2=newSystem.Windows.Forms.TextBox();
this.SuspendLayout();
//
//labelUser
//
this.labelUser.AutoSize=true;
this.labelUser.Location=newSystem.Drawing.Point(30,26);
this.labelUser.Name="labelUser";
this.labelUser.Size=newSystem.Drawing.Size(67,15);
this.labelUser.TabIndex=0;
this.labelUser.Text="用户名:
";
//
//textBoxUser
//
this.textBoxUser.Locationi=newSystem.Drawing.Point(107,23);
this.textBoxUser.MaxLength=20;
this.textBoxUser.Name="textBoxUser";
this.textBoxUser.Size=newSystem.Drawing.Size(159,25);
this.textBoxUser.TabIndex=2;
//
//labelPassword
//
this.labelPassword.AutoSize=true;
this.labelPassword.Location=newSystem.Drawing.Point(30,67);
this.labelPassword.Name="labelPassword";
this.labelPassword.Size=newSystem.Drawing.Size(67,15);
this.labelPassword.TabIndex=1;
this.labelPassword.Text="密码:
";
//
//textBoxPassword
//
this.textBoxPassword.Location=newSystem.Drawing.Point(107,64);
this.textBoxPassword.MaxLength=18;
this.textBoxPassword.Name="textBoxPassword";
this.textBoxPassword.PasswordChar='#';
this.textBoxPassword.Size=newSystem.Drawing.Size(159,25);
this.textBoxPassword.TabIndex=3;
//
//labelPassword2
//
this.labelPassword2.AutoSize=true;
this.labelPassword2.Location=newSystem.Drawing.Point(30,103);
this.labelPassword2.Name="labelPassword2";
this.labelPassword2.Size=newSystem.Drawing.Size(82,15);
this.labelPassword2.TabIndex=6;
this.labelPassword2.Text="确认密码:
";
//
//textBoxPassword2
//
this.textBoxPassword2.Location=newSystem.Drawing.Point(107,100);
this.textBoxPassword2.MaxLength=18;
this.textBoxPassword2.Name="textBoxPassword2";
this.textBoxPassword2.PasswordChar='#';
this.textBoxPassword2.Size=newSystem.Drawing.Size(159,25);
this.textBoxPassword2.TabIndex=7;
//
//buttonSignIn
//
this.buttonSignIn.Location=newSystem.Drawing.Point(52,150);
this.buttonSignIn.Name="buttonSignIn";
this.buttonSignIn.Size=newSystem.Drawing.Size(75,33);
this.buttonSignIn.TabIndex=4;
this.buttonSignIn.Text="注册";
this.buttonSignIn.UseVisualStyleBackColor=true;
this.buttonSignIn.Click+=newSystem.EventHandler(this.buttonLogin_Click);
//
//buttonCancle
//
this.buttonCancle.Location=newSystem.Drawing.Point(179,150);
this.buttonCancle.Name="buttonCancle";
this.buttonCancle.Size=newSystem.Drawing.Size(75,33);
this.buttonCancle.TabIndex=5;
this.buttonCancle.Text="取消";
this.buttonCancle.UseVisualStyleBackColor=true;
this.buttonCancle.Click+=newSystem.EventHandler(this.buttonCancle_Click);
//
//FormLogin
//
this.AutoScaleDimensions=newSystem.Drawing.SizeF(8F,15F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSizei=newSystem.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.Controls.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+=newSystem.EventHandler(this.FormLogin_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
privateSystem.Windows.Forms.LabellabelUser;
privateSystem.Windows.Forms.TextBoxtextBoxUser;
privateSystem.Windows.Forms.LabellabelPassword;
privateSystem.Windows.Forms.LabellabelPassword2;
privateSystem.Windows.Forms.TextBoxtextBoxPassword2;
privateSystem.Windows.Forms.TextBoxtextBoxPassword;
privateSystem.Windows.Forms.ButtonbuttonSignI
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 四川 理工 C# 网络 编程 15 答案