C#应用程序设计教程 第二版+课后习题答案
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
(4)
static void Main(string[] args) {
int i, j,k; float s = 1.0f; for (i = 2; i < 11; i++) { k=1; for (j = 2; j <= i; j++) k += j; s += 1.0f / k; } Console.WriteLine("s={0}",s); }
(3)
class Person { public string name; public string sex; public DateTime birthdate; } class Teacher : Person { public string workid; public DateTime beginwork; public double wage; public static int num ; public double WorkYears { get { return DateTime.Now.Year-beginwork.Year ; } } public Teacher() { num++; } public static int GetNum() { return num; }
{ return new Point(p1.x - 1, p1.y - 1); } public static double operator -(Point p1,Point p2) { return Math.Sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y)); } }
(3)
static void Main(string[] args) { int a = 20, b = 16,i=20; do { if (i % a == 0 & i % b == 0) break; i++; } while(true); Console.WriteLine("最小公倍数:{0}",i); i = 16; do { if (a % i == 0 & b % i == 0) break; i--; } while (true); Console.WriteLine("最大公约数:{0}",i); }
(2)
static void Main(string[] args) { int[,] a = {{2,5,18,4 },{3,4,9,2},{4,1,16,8}, {5,2,14,6}};
int i,j,k,max,maxj; for (i = 0; i < 4; i++) { max = a[i,0]; maxj = 0; for (j = 1; j < 4; j++) { if (max < a[i,j]){max = a[i,j]; maxj = j;} } for (k = 0; k < 4; k++) { if (a[k, maxj] < max) break; } if (k == 4) Console.WriteLine("鞍点:第{0}行, 第{1}列",i+1,maxj+1); } }
} public string StudentName { get { return studentname; } set { studentname = value; } } public string BirthPlace { get { return birthplace;} set { birthplace = value; } } public DateTime BirthDate { get { return birthdate;} set { birthdate = value;} } public int Age { get { return DateTime.Now.Year - birthdate.Year+1; } } }
(2)
class Point { public int x,y; public Point(int x1, int y1) { x = x1; y = y1; } public static Point operator ++(Point p1) { return new Point(p1.x + 1, p1.y + 1); } public static Point operator --(Point p1)
(1)B (2) C (3) B (4) B (5) C (6) B (7) D (8) B (9) C (10)A 5.分析并写出下列程序运行结果 (1)a=21,b=9,c=16 y=4 (2)b=41 (3)s=6 (4)a[0]=28 a[1]=15 a[2]=39 a[3]=48 a[4]=39 6.程序填空 (1) ① 3 ② a[i] ③ a[i] ④ a[5-i] ⑤ a[5-i] (2) ① i % 5 == 0 | i % 7 == 0 ② s+ i 7.编程题 (1)
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("Hello C#"); } } }
第二章 1.判断下列符号哪些是合法的C#变量名称 北京 abc _xyz x_y 2.根据要求写出C#表达式 (1) N%3==0 & N%5==0 (2)(char) new Random().Next(67,74) (3)Math.Pow(x,3)*y/(x+y) (4)Math.Sqrt(b*b-4*a*c) 3.判断题 (1)× (2) √ (3) √ (4) √ (5) √ (6) × (7) √ (8) √ (9) √ (10) × (11) √ (12) × 4.选择题
static void Main(string[] args) { int s,i,j; for (i = 2; i < 1000; i++) { s = 0; for (j = 1; j < i; j++) { if (i % j == 0) s += j; } if (i == s) Console.WriteLine("{0}",i); } }
{ return Length * Height; } public double Perimeter() { return 2*(Length +Height ); } }
一半多1个,剩余桃子数量是前一天桃子数量的一半减1,则, 前一天的桃子数量是后一天的桃子数量加1的2倍。 第三章 1.判断题 (1)√ (2) × (3) √ (4) √ (5) √ (6) × (7) √ (8) × (9) × (10) √ (11) √(12) √ (13) √ (14) × (15) √ (16) √ (17) × (18) √(19) √ (20) × (21) ×(22) √(23) √(24) √(25) ×(26) ×(27) × 2.选择题 (1)C (2) B (3) C (4) D (5) C (6) B (7) D (8) C (9) A (10)A (11)B (12) A (13) D (14) B (15) C 3.分析下列程序的运行结果 (1) s=32,s1=32,s2=34 x=11,y=21 (2) x1=3,x2=4 y1=1,y2=4 4.编程题 (1)
class Student { public string studentid; public string studentname; private string birthplace; private DateTime birthdate; public Student(string id, string name) { studentid = id; studentname = name; } public string StudentId { get { return studentid; } set { studentid = value; }
第一章 1.判断题 (1)× (2) √ (3) √ (4) × (5) × (6) √ 2.选择题 (1)C (2) B (3) B (4) C (5) D (6) C 3.编程题
using using using using System; System.Collections.Generic; System.Linq; System.Text;
} public override void Eat() { Console.WriteLine("鸭子吃东西"); ; }Biblioteka Baidu}
(5)
public interface IShape { double Perimeter(); double Area(); } class Circle : IShape { public double Radius { get; set; } public Circle(double r) { Radius = r; } public double Area() { return Math.PI * Radius * Radius; } public double Perimeter() { return 2 * Math.PI * Radius; } } class Rectangle : IShape { public double Length { get; set; } public double Height { get; set; } public Rectangle(double l, double h) { Length = l; Height = h; } public double Area()
}
(4)
abstract class Animal { public string Name { get; set; } public int Color { get; set; } public abstract void Shout(); public abstract void Eat(); } class Tiger : Animal { public override void Shout() { Console.WriteLine("老虎叫"); ; } public override void Eat() { Console.WriteLine("老虎吃东西"); ; } } class Duck : Animal { public void Swim() { Console.WriteLine("游泳"); } public override void Shout() { Console.WriteLine("嘎嘎"); ;
(5)
static void Main(string[] args) { String s = "a,b2>4[AG6p("; int i, n1=0,n2=0,n3=0,n4=0; for(i=0;i<s.Length;i++) { if(s[i]>=65 & s[i]<=90) n1++; if(s[i]>=97 & s[i]<=122) n2++; if(s[i]>=48 & s[i]<=57) n3++; } n4=s.Length -n1-n2-n3; Console.WriteLine("大写字母个数:{0};小写字母个数:{1}; 数字个数:{2};其余字符个数:{3}",n1,n2,n3,n4); }
(6)
static void Main(string[] args) { int i,s = 1; for (i = 0; i < 5; i++) { s = 2 * (s + 1); } Console .WriteLine ("小猴子第一天摘了{0}个桃 子",s); }
说明:由最后一天往前推算。后一天吃了前一天桃子数量的