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

上传人:b****6 文档编号:6264564 上传时间:2023-01-04 格式:DOCX 页数:35 大小:359.76KB
下载 相关 举报
C程序设计及应用课后题答案.docx_第1页
第1页 / 共35页
C程序设计及应用课后题答案.docx_第2页
第2页 / 共35页
C程序设计及应用课后题答案.docx_第3页
第3页 / 共35页
C程序设计及应用课后题答案.docx_第4页
第4页 / 共35页
C程序设计及应用课后题答案.docx_第5页
第5页 / 共35页
点击查看更多>>
下载资源
资源描述

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

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

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

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

4、分别写出下列语句执行的结果。

1)Console.WriteLine("{0}--{0:

p}good",12.34F);

2)Console.WriteLine("{0}--{0:

####}good",0);

3)Console.WriteLine("{0}--{0:

00000}good",456);

【解答】

12.34--1,234.00%good

0--good

456--00456good

5、编写一个控制台应用程序,输出1到5的平方值,要求:

1)用for语句实现。

2)用while语句实现。

3)用do-while语句实现。

【解答】

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceoutputSquareValue

{

classProgram

{

staticvoidMain()

{

//用for语句实现

for(inti=1;i<=5;i++)

{

Console.WriteLine("{0}的平方值为{1}",i,i*i);

}

//用while语句实现

intj=0;

while(j++<5)

{

Console.WriteLine("{0}的平方值为{1}",j,j*j);

}

//用do-while语句实现

intk=1;

do

{

Console.WriteLine("{0}的平方值为{1}",k,k*k);

}while(k++<5);

Console.ReadLine();

}

}

}

6、编写一个控制台应用程序,要求用户输入5个大写字母,如果用户输入的信息不满

足要求,提示帮助信息并要求重新输入。

【解答】

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceinputCapitalLetter

{

classProgram

{

staticvoidMain()

{

boolok=false;

while(ok==false)

{

Console.Write("请输入5个大写字母:

");

stringstr=Console.ReadLine();

if(str.Length!

=5)

{

Console.WriteLine("你输入的字符个数不是5个,请重新输入。

");

}

else

{

ok=true;

for(inti=0;i<5;i++)

{

charc=str[i];

if(c<'A'||c>'Z')

{

Console.WriteLine("第{0}个字符“{1}”不是大写字母,请重新输入。

",i+1,c);

ok=false;

break;

}

}

}

}

}

}

}

7、编写一个控制台应用程序,要求完成下列功能。

1)接收一个整数n。

2)如果接收的值n为正数,输出1到n间的全部整数。

3)如果接收的值为负值,用break或者return退出程序。

4)转到

(1)继续接收下一个整数。

【解答】

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespacetestOutput

{

classProgram

{

staticvoidMain()

{

while(true)

{

Console.Write("请输入一个整数(负值结束):

");

stringstr=Console.ReadLine();

try

{

inti=Int32.Parse(str);

if(i<0)break;

for(intj=1;j<=i;j++)Console.WriteLine(j);

}

catch

{

Console.WriteLine("你输入的不是数字或超出整数的表示范围,请重新输入");

}

}

}

}

}

8、编写一个控制台应用程序,求1000之内的所有“完数”。

所谓“完数”是指一个数恰好

等于它的所有因子之和。

例如,6是完数,因为6=1+2+3。

【解答】

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespacecompleteNumber

{

classProgram

{

staticvoidMain(string[]args)

{

for(inti=2;i<=1000;i++)

{

ints=1;

stringstr="1";

for(intj=2;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);

}

else

{

s+=j;

str+=string.Format("+{0}",j);

}

}

}

if(s==i)Console.WriteLine("{0}={1}",i,str);

}

Console.ReadLine();

}

}

}

3、编写一个控制台应用程序,计算

要求精度为10−8。

【解答】

usingSystem;

classTest3

{

publicstaticvoidMain()

{

intn=50;

doublex=3;

doubles=0;

doublea=1;

for(inti=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)将字符串“hello”替换为“me”,输出新字符串。

(5)以字符“m”为分隔符,将字符串分离,并输出分离后的字符串。

【解答】

【解答】

usingSystem;

classTest4

{

publicstaticvoidMain()

{

stringstr="";

while(str.Length<=3)

{

Console.Write("请输入一个长度大于3的字符串:

");

str=Console.ReadLine();

}

//

(1)

Console.WriteLine("字符串的长度为:

{0}",str.Length);

//

(2)

inti=str.IndexOf('a');

if(i>-1)

{

Console.WriteLine("第一个出现字母a的位置是:

{0}",i);

}

else

{

Console.WriteLine("字符串中不包含字母a。

");

}

//(3)

stringstr1=str.Insert(3,"hello");//在第3个(初始序号为)字符前插入hello

Console.WriteLine("插入hello后的结果为:

{0}",str1);

//(4)

stringstr2=str1.Replace("hello","me");

Console.WriteLine("将hello替换为me后的结果为:

{0}",str2);

//(5)

string[]arr=str2.Split('m');

Console.WriteLine("以m为分隔符分离后的字符串有:

");

for(intj=0;j

{

Console.WriteLine(arr[j]);

}

}

}

1、编写一个控制台应用程序,完成下列功能。

(1)创建一个类,用无参数的构造函数输出该类的类名。

(2)增加一个重载的构造函数,带有一个string类型的参数,在此构造函数中将传递的字符串打印出来。

(3)在Main方法中创建属于这个类的一个对象,不传递参数。

(4)在Main方法中创建属于这个类的另一个对象,传递一个字符串“Thisisastring.”。

(5)在Main方法中声明类型为这个类的一个具有5个对象的数组,但不要实际创建分配到数组里的对象。

(6)写出运行程序应该输出的结果。

【解答】

usingSystem;

classTest1

{

publicTest1()

{

Console.WriteLine(this);

}

publicTest1(stringstr)

{

Console.WriteLine(str);

}

publicstaticvoidMain()

{

Test1t1=newTest1();

Test1t2=newTest1("Thisisastring.");

Test1[]t3=newTest1[5];

}

}

输出结果:

Test1

Thisisastring.

2、编写一个控制台应用程序,定义一个类MyClass,类中包含有public、private以及protected数据成员及方法。

然后定义一个从MyClass类继承的类MyMain,将Main方法放在MyMain中,在Main方法中创建MyClass类的一个对象,并分别访问类中的数据成员及方法。

要求注明在试图访问所有类成员时哪些语句会产生编译错误。

【解答】

usingSystem;

classMyClass

{

publicinti;

privateintj;

protectedintk;

publicvoidmethod1()

{

Console.WriteLine("publicmethod.");

}

privatevoidmethod2()

{

Console.WriteLine("privatemethod.");

}

protectedvoidmethod3()

{

Console.WriteLine("protectedmethod.");

}

}

classMyMain:

MyClass

{

publicstaticvoidMain()

{

MyClasst=newMyClass();

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数据。

在相同的文件里创建第二个类,用一个方法操纵第一个类里的protected数据。

【解答】

usingSystem;

classClass1

{

protectedinti=5;

protectedvoidMyMethod()

{

Console.WriteLine("protectedmethod.");

}

}

classClass2:

Class1

{

privatevoidNewMethod()

{

Console.WriteLine(this.i);

this.i+=10;

Console.WriteLine(this.i);

}

publicstaticvoidMain()

{

Class2t=newClass2();

t.NewMethod();

}

}

3、编写一个控制台应用程序,完成下列功能,并回答提出的问题。

(1)创建一个类A,在构造函数中输出“A”,再创建一个类B,在构造函数中输出“B”。

(2)从A继承一个名为C的新类,并在C内创建一个成员B。

不要为C创建构造函数。

(3)在Main方法中创建类C的一个对象,写出运行程序后输出的结果。

(4)如果在C中也创建一个构造函数输出“C”,整个程序运行的结果又是什么?

【解答】

usingSystem;

publicclassA

{

publicA()

{

Console.WriteLine("A");

}

}

publicclassB

{

publicB()

{

Console.WriteLine("B");

}

}

publicclassC:

A

{

Bnewb=newB();

}

classMainClass

{

publicstaticvoidMain()

{

Cnewc=newC();

Console.ReadLine();

}

}

输出结果:

B

A

如果在C中也创建一个构造函数输出“C”,即添加:

publicC()

{

Console.WriteLine("C");

}

则整个程序运行的结果为:

B

A

C

4、编写一个控制台应用程序,完成下列功能,并写出运行程序后输出的结果。

(1)创建一个类A,在A中编写一个可以被重写的带int类型参数的方法MyMethod,并在该方法中输出传递的整型值加10后的结果。

(2)再创建一个类B,使其继承自类A,然后重写A中的MyMethod方法,将A中接收的整型值加50,并输出结果。

(3)在Main方法中分别创建类A和类B的对象,并分别调用MyMethod方法。

【解答】

usingSystem;

publicclassA

{

publicvirtualvoidMyMethod(intnum)

{

num+=10;

Console.WriteLine(num);

}

}

publicclassB:

A

{

publicoverridevoidMyMethod(intnum)

{

num+=50;

Console.WriteLine(num);

}

}

classMainClass

{

publicstaticvoidMain()

{

Anewa=newA();

newa.MyMethod

(2);

Bnewb=newB();

newb.MyMethod

(2);

Console.ReadLine();

}

}

输出结果:

12

52

5、假设Node类的每一个节点包括有两个字段:

m_data(引用节点的数据)和m_next(引用链接列表中的下一项)。

这两个字段都是由构造函数方法设置的。

该类有两个功能,第一个功能是通过名为Data和Next的只读属性访问m_data和m_next字段。

第二个功能是对System.Object的ToString虚拟方法进行重写。

试分别用类和泛型两种方法编写程序实现上述功能。

【解答】

usingSystem;

classNode

{

Objectm_data;

Nodem_next;

publicNode(Objectdata,Nodenext)

{

m_data=data;

m_next=next;

}

//访问结点数据

publicObjectData

{

get{returnm_data;}

}

//访问下一个结点

publicNodeNext

{

get{returnm_next;}

}

//获取结点数据描述

publicoverrideStringToString()

{

returnm_data.ToString();

}

}

//链表结点类的泛型定义

classNode

{

Tm_data;

Nodem_next;

publicNode(Tdata,Nodenext)

{

m_data=data;

m_next=next;

}

//访问结点数据

publicTData

{

get{returnm_data;}

set{m_data=value;}

}

//访问下一个结点

publicNodeNext

{

get{returnm_next;}

set{m_next=value;}

}

//获取结点数据描述

publicoverrideStringToString()

{

returnm_data.ToString();

}

}

//使用结点类型或泛型结点类型

classLinkedList

{

staticvoidMain(string[]args)

{

////创建整数链表

//Nodehead=newNode(5,null);

//head=newNode(10,head);

//head=newNode(15,head);

////遍历链表求整数和

//Int32sum=0;

//for(Nodecurrent=head;current!

=null;

//current=current.Next)

//{

//sum+=(Int32)current.Data;

//}

////输出结果

//Console.WriteLine("Sumofnodes={0}",sum);

//用泛型创建整数链表

Nodehead=newNode(5,null);

head=newNode(10,head);

head=newNode(15,head);

//遍历求和

Int32sum=0;

for(Nodecurrent=head;current!

=null;

current=current.Next)

{

sum+=current.Data;

}

//输出

Console.WriteLine("Sumofnodes={0}",sum.ToString());

}

}

4、设计一个Windows应用程序,窗体上有一个TextBox控件、一个Button控件。

要求:

每当用户单击按钮时,文本框都会增加一行文字来反映单击的次数,例如,“第3次单击按钮”。

【解答】

1)窗体界面如图6-1所示;

2)窗体中主要控件属性设置如表6-1;

表6-1窗体中的主要控件属性

控件

Name属性

功能

其它属性

TextBox控件

textBox1

显示信息

ScrollBars=Vertical;Multiline=True

Button控件

Button1

触发添加信息事件

Button2

触发结束添加事件

3)主要事件代码。

……

inti=1;

boolAdd=true;

……privatevoidbutton1_Click(objectsender,EventArgse)

{

if(Add)textBox1.Text+="第"+i+"次单击按钮\r\n";

i++;

}

privatevoidbutton2_Click(objectsender,EventArgse)

{

Add=false;

}

5、编写一段程序,向名为listBox1的ListBox控件中,自动添加10个随机数,每个数占一项。

【解答】

主要代码如下。

publicpartialclassForm1:

Form

{

intm=1;

……

privatevoidbutton1_Click(objectsender,EventArgse)

{

for(inti=m;i

{

listBox1.Items.Add(i);

}

m=m+10;

}

}

2、编写程序用Directory类提供的方法确定指定的目录是否存在,如果不存在,则创建该目录。

然后在其中创建一个文件,并将一个字符串写到文件中。

【解答】

程序清单如下:

usingSystem;

usingSystem.IO;

classTest

{

publicstaticvoidMain()

{

stringpath=@"c:

\MyDir";

try

{

if(!

Directory.Exists(path))

{

Directory.CreateDirectory(path);

}

StreamWritersw=File.CreateText(path+@"\myfile.txt");

sw.WriteLine("ThisisaString!

");

sw.close();

}

catch(Exceptione)

{

Console.WriteLin

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

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1