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

  • 格式:doc
  • 大小:542.00 KB
  • 文档页数:24

文档来源为:从网络收集整理.word版本可编辑.欢迎下载支持.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%good0--good456--00456good5、编写一个控制台应用程序,输出1到5的平方值,要求:1) 用for语句实现。

2) 用while语句实现。

3) 用do-while语句实现。

【解答】using System;using ;using System.Text;namespace outputSquareValue{class Program{static 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;do{Console.WriteLine("{0}的平方值为{1}", k, k * k);} while (k++ < 5);Console.ReadLine();}1文档来源为:从网络收集整理.word版本可编辑.欢迎下载支持.}}6、编写一个控制台应用程序,要求用户输入5个大写字母,如果用户输入的信息不满足要求,提示帮助信息并要求重新输入。

【解答】using System;using ;using System.Text;namespace inputCapitalLetter{class Program{static void Main(){bool ok = false;while (ok == false){Console.Write("请输入5个大写字母:");string str = Console.ReadLine();if (str.Length != 5){Console.WriteLine("你输入的字符个数不是5个,请重新输入。

");}else{ok = true;for (int i = 0; i < 5; i++){char c = str[i];if (c < 'A' || c > 'Z'){Console.WriteLine("第{0}个字符“{1}”不是大写字母,请重新输入。

", i + 1, c);ok = false;break;}}}}}}}2文档来源为:从网络收集整理.word版本可编辑.欢迎下载支持.7、编写一个控制台应用程序,要求完成下列功能。

1) 接收一个整数n。

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

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

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

【解答】using System;using ;using System.Text;namespace testOutput{class Program{static void Main(){while (true){Console.Write("请输入一个整数(负值结束):");string str = Console.ReadLine();try{int i = Int32.Parse(str);if (i < 0) break;for (int j = 1; j <= i; j++) Console.WriteLine(j);}catch{Console.WriteLine("你输入的不是数字或超出整数的表示范围,请重新输入");}}}}}8、编写一个控制台应用程序,求1000之内的所有“完数”。

所谓“完数”是指一个数恰好等于它的所有因子之和。

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

【解答】using System;using ;using System.Text;namespace completeNumber{3文档来源为:从网络收集整理.word 版本可编辑.欢迎下载支持.4class Program{static void Main(string[] args){for (int i = 2; i <= 1000; i++){int s = 1;string str = "1";for (int j = 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、编写一个控制台应用程序,计算234(1)1(1)2!3!4!!nn x x x xx n ++-+-++-L要求精度为10−8。

【解答】using System;class Test3{public static void Main(){int n = 50;文档来源为:从网络收集整理.word版本可编辑.欢迎下载支持.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)将字符串“hello”替换为“me”,输出新字符串。

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

【解答】【解答】using System;class Test4{public static void Main(){string str = "";while (str.Length <= 3){Console.Write("请输入一个长度大于3的字符串:");str = Console.ReadLine();}//(1)Console.WriteLine("字符串的长度为:{0}", str.Length);//(2)int i = str.IndexOf('a');if (i > -1){Console.WriteLine("第一个出现字母a的位置是:{0}", i);}else{Console.WriteLine("字符串中不包含字母a。

");5文档来源为:从网络收集整理.word版本可编辑.欢迎下载支持.}//(3)string str1 = str.Insert(3, "hello"); //在第3个(初始序号为)字符前插入helloConsole.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(arr[j]);}}}1、编写一个控制台应用程序,完成下列功能。

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

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

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

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

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

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

【解答】using System;class Test1{public Test1(){Console.WriteLine(this);}public Test1(string str){Console.WriteLine(str);}public static void Main(){Test1 t1 = new Test1();Test1 t2 = new Test1("This is a string.");Test1[] t3 = new Test1[5];}6文档来源为:从网络收集整理.word版本可编辑.欢迎下载支持. }输出结果:Test1This is a string.2、编写一个控制台应用程序,定义一个类MyClass,类中包含有public、private以及protected数据成员及方法。

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