第四版c#程序設(shè)計教程(課后習題答案代碼)_第1頁
第四版c#程序設(shè)計教程(課后習題答案代碼)_第2頁
第四版c#程序設(shè)計教程(課后習題答案代碼)_第3頁
第四版c#程序設(shè)計教程(課后習題答案代碼)_第4頁
第四版c#程序設(shè)計教程(課后習題答案代碼)_第5頁
已閱讀5頁,還剩21頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、判斷是否是閏年 課本63用戶輸入整數(shù)反向顯示 課本67或68乘法表 課本69判斷從鍵盤輸入大于3的整數(shù)是否為素數(shù) 課本70求輸入所以數(shù)其中正數(shù)的和 課本70求 1平方+2平方+n平方 小于等于1000 的最大n 課本71或72讀入一組數(shù)(以0結(jié)束),分別求奇數(shù)和偶數(shù)和static void Main(string args) int n,s1=0,s2=0; do n = int.Parse(Console.ReadLine(); if (n%2=1) s1 += n; else s2 += n; while (n!=0); Console.WriteLine("奇數(shù)之和=0&quo

2、t;,s1); Console.WriteLine("偶數(shù)之和=0",s2); 輸入正整數(shù)n計算S=1+(1+2)+(1+2+3)+(1+2+.+n)int n,i,j,s=0; Console.Write("n:"); n = int.Parse(Console.ReadLine(); for (i = 1; i <= n; i+) for (j = 1; j <= i; j+) s += j; Console.WriteLine("s=0", s);楊輝三角static void Main(string args) i

3、nt i,j,c,n; Console.Write("n:"); n=int.Parse(Console.ReadLine(); if (n>13) Console.WriteLine("輸入的數(shù)值太大!"); else for (i=0;i<=n-1;i+) for (j=1;j<15-i;j+) Console.Write(" "); /每次循環(huán)顯示2個空格 c=1; Console.Write("0 ",c); for (j=1;j<=i;j+) c=c*(i-j+1)/j; if (

4、c<100) if (c<10) Console.Write("0 ",c); /顯示3個空格 else Console.Write("0 ",c); /顯示2個空格 else Console.Write("0 ",c); /顯示1個空格 Console.WriteLine(); 計算的值double pi=0.0; int i; for (i=1;i<=2000;i+) if (i%2=1) pi=pi+1.0/(2*i-1); else pi=pi-1.0/(2*i-1); pi=4*pi; Console.Wr

5、iteLine("=0", pi);求水仙花數(shù)static void Main(string args) int i, n, a, b, c; for (i = 100; i <= 999; i+) n = i; c = n % 10; n = n / 10; b = n % 10; n = n / 10; a = n; if (a * a * a + b * b * b + c * c * c = i) Console.WriteLine("0 1 2 = 3", a, b, c,a*a*a+b*b*b+c*c*c); /Console.Writ

6、e("0 ", i); Console.WriteLine(); 假設(shè)10個整數(shù)用一維數(shù)組存放,求最大值和次大值static void Main(string args) int a = new int101,8,10,4,7,9,6,10,2,5; int n=10,max1,max2,i; max1=a0>a1?a0:a1; max2=a0>a1?a1:a0; for (i=2;i<n;i+) if (max1<ai) max2=max1; max1=ai; Console.WriteLine("max1=0,max2=1",

7、max1,max2); 用一個二維數(shù)組存放5個考生4門功課的考試成績,求每位考生的平均成績 課本89頁static void Main(string args) const int Max = 5; /考生數(shù) int Ave = new intMax; /定義一個一維數(shù)組存儲考生的總成績 int, grade=88,75,62,84,96,85,75,92, /定義二維數(shù)組存儲考生成績 68,63,72,78,95,89,76,98, 76,65,72,63; for(int i=0; i<Max; i+) for(int j=0; j<4; j+) Avei += gradei,

8、j; /累加考生成績 for (int k = 0; k < Max; k+) Console.WriteLine("考生0平均成績=1 ",k+1, Avek/4.0); 用倆個一維數(shù)組分別存放5個學(xué)生的學(xué)號和姓名,分別按學(xué)號和姓名排序 課本89頁上級實驗5class Program const int Max = 5; static void disp(int no,string name,string str) Console.WriteLine(str); Console.Write("學(xué)號:t"); for (int i = 0; i &

9、lt; no.Length; i+) Console.Write("0t",noi); Console.WriteLine(); Console.Write("姓名:t"); for (int i = 0; i < name.Length; i+) Console.Write("0t", namei); Console.WriteLine(); static void Main(string args) int no = new int 2, 4, 5, 1, 3; string name = new string "

10、Smith","John","Mary","Cherr","Tomn" disp(no, name,"排序前:"); Array.Sort(no, name); disp(no, name,"按學(xué)號排序后:"); Array.Sort(name, no); disp(no, name, "按姓名排序后:"); 課本124頁8class Program static void Main(string args) Person p1 = new Pe

11、rson(2, 50); Animal a1 = new Animal(); p1.show(); a1.show(); public class Person /定義人類public int legs; /腿的只數(shù) protected float weight; /重量 public Person() /默認構(gòu)造函數(shù) public Person(int legs1,float weight1)/自定義方法F legs= legs1; weight = weight1; public void show() Console.WriteLine("某人有0只腿,重量為1kg"

12、, legs, weight); class Animal /定義動物類 public int num; /腿的條數(shù) private float weight; /重量 public Animal() /Animal類的默認構(gòu)造函數(shù) public Animal(int n,float w) /Animal類帶2個參數(shù)的構(gòu)造函數(shù) num = n; weight = w; public void show() Console.WriteLine("某動物有0只腳,重量為1kg", num, weight); 課本124頁9/定義了一個委托,委托在傳遞方法時,方法必須帶兩個int

13、型的參數(shù)。 public delegate int Call(int num1, int num2); /在Delegates類的內(nèi)部定義Math類和TestDelegates類。 class Math public int fun1(int num1, int num2) return num1*num1+num2*num2; public int fun2(int num1, int num2) return num1*num1-num2*num2; class Program static void Main(string args) int result; Call objCall;

14、/委托的對象 Math objMath = new Math();/Math類的對象 objCall = new Call(objMath.fun1); result = objCall(5, 3);/將委托實例化 Console.WriteLine("結(jié)果為0", result); objCall = new Call(objMath.fun2); result = objCall(5, 3);/將委托實例化 Console.WriteLine("結(jié)果為0", result); 課本124頁10class List private int Max =

15、100; /存儲最多元素 private int num = 0; /存儲的實際元素個數(shù) private object list; /存儲元素數(shù)組 public List() /構(gòu)造函數(shù) list = new objectMax; public void add(object obj) /添加一個元素 listnum = obj; num+; public void delete(int pos) /刪除一個元素 for (int i = pos + 1; i < num; i+) listi - 1 = listi; num-; public object get(int pos) /

16、獲取指定位置的元素 if (pos < num) return listpos; else return null; public int getnum() /獲取實際元素個數(shù) return num; public string disp() /獲取所有元素 string s = "" for (int i = 0; i < num; i+) s += listi + " " return s; class Program static void Main(string args) List list = new List(); list.a

17、dd("abc"); list.add(1.23); list.add(2); list.add('a'); Console.WriteLine("元素序列:0",list.disp(); Console.WriteLine("元素個數(shù):0",list.getnum(); Console.WriteLine("位置1的元素:0",list.get(1); Console.WriteLine("刪除位置2的元素"); list.delete(2); Console.WriteLin

18、e("元素序列:0", list.disp(); 課本124頁11public class Student private string name; private int eng, math, sum; public int psum get return sum; public void inscore() Console.Write("姓名:"); name = Console.ReadLine(); Console.Write("英語:"); eng = int.Parse(Console.ReadLine(); Consol

19、e.Write("數(shù)學(xué):"); math = int.Parse(Console.ReadLine(); sum = eng + math; public void display() Console.WriteLine("t0t1t2t3", name, eng, math, sum); class Program const int Max = 100; static void sort(int n, params Student p)/采用冒泡排序法排序 int i, j; bool exchange; Student tmp; for (i =

20、0; i < n - 1; i+) exchange = false; for (j = n - 2; j >= i; j-) if (pj + 1.psum > pj.psum) tmp = pj + 1;/pj+1<->pj pj + 1 = pj; pj = tmp; exchange = true; if (exchange = false) break; static void Main(string args) int n, i; Student p = new StudentMax; /定義對象引用數(shù)組 Console.Write("n:&

21、quot;); n = int.Parse(Console.ReadLine(); for (i = 0; i < n; i+) /創(chuàng)建對象引用的實例 pi = new Student(); for (i = 0; i < n; i+) Console.WriteLine("輸入第0個學(xué)生數(shù)據(jù):", i + 1); pi.inscore(); Console.WriteLine("排序前:"); Console.WriteLine("t姓名t英語t數(shù)學(xué)t總分"); for (i = 0; i < n; i+) Con

22、sole.Write("序號0:", i + 1); pi.display(); sort(n, p); /按總降序排序 Console.WriteLine("排序后:"); Console.WriteLine("t姓名t英語t數(shù)學(xué)t總分"); for (i = 0; i < n; i+) Console.Write("第0名:", i + 1); pi.display(); 課本124上機實驗6 class Student /學(xué)生類 int sno; /學(xué)號 string sname; /姓名 Course

23、 course; /Course類對象數(shù)組 int score; /課程成績數(shù)組 double sgpa1; /常見GPA值 double sgpa2; /標準GPA值 public int psno /psno屬性可讀可寫 get return sno; set sno = value; public string psname /psname屬性可讀可寫 get return sname; set sname = value; public void setcourse(params Course course1) /設(shè)置課程 course = new Coursecourse1.Leng

24、th; for (int i = 0; i < course1.Length; i+) coursei = course1i; public void setscore(int score1) /設(shè)置分數(shù) score = new intscore1.Length; for (int i = 0; i < score1.Length; i+) scorei = score1i; public void computegpa() /根據(jù)課程的學(xué)分以及學(xué)生成績計算GPA int i; double s, sumc = 0, sumgpa1 = 0, sumgpa2 = 0; for (i

25、 = 0; i < score.Length; i+) if (scorei >= 90) s = 4.0; /點數(shù) else if (scorei >= 80) s = 3.0; else if (scorei >= 70) s = 2.0; else if (scorei >= 60) s = 1.0; else s = 0.0; sumgpa1 += coursei.pcredits * s; sumgpa2 += coursei.pcredits * scorei; sumc += coursei.pcredits; sgpa1 = sumgpa1 / s

26、umc; sgpa2 = sumgpa2 * 4 / sumc / 100; public void dispstud() /輸出學(xué)生信息 Console.WriteLine("學(xué)號:0t姓名:1", sno, sname); Console.WriteLine(" 課程名t學(xué)分t分數(shù)"); for (int i = 0; i < course.Length; i+) Console.WriteLine(" 0t1t2", coursei.pcname, coursei.pcredits, scorei); public voi

27、d dispgpa() /輸出GPA Console.WriteLine("常見算法GPA=0:n,標準算法GPA=1:n", sgpa1, sgpa2); class Course /課程類 string cname; /課程名 int credits; /課程學(xué)分 public Course() public Course(string name, int xf) /構(gòu)造函數(shù) cname = name; credits = xf; public string pcname /pcname屬性,課程名可讀可寫 get return cname; set cname = v

28、alue; public int pcredits /pcredits屬性,課程學(xué)分可讀可寫 get return credits; set credits = value; class Program static void Main(string args) Course course1 = new Course new Course("課程1",4),new Course("課程2",3), new Course("課程3",2),new Course("課程4",6),new Course("課程

29、5",3); int score1 = new int 92, 80, 98, 70, 89 ; Student s1 = new Student(); s1.psno = 1; s1.psname = "王華" s1.setcourse(course1); s1.setscore(score1); putegpa(); s1.dispstud(); s1.dispgpa(); 課本157頁7using System;using System.Collections.Generic;using System.Text;namespace Proj7_15 publ

30、ic class Employee private double bsalary= 1000; private double psalary; private int n; public int pn get return n; set n = value; public double compsalary() Console.Write("工作年數(shù):"); pn = int.Parse(Console.ReadLine(); psalary = bsalary+30*pn; return psalary; public class UEmployee : Employee

31、 new public double compsalary() return 1.5 * psalary(); class Program static void Main(string args) Employee emp1 = new Employee(); Console.WriteLine("該普通職工工資:0", psalary(); UEmployee emp2 = new UEmployee(); Console.WriteLine("該本科生職工工資:0", psalary(); 課本157頁8public class Employee

32、/普通職工類 private double bsalary = 1000; /基本工資 private double psalary; /實際工資 private int n; /工作年數(shù) public int pn get return n; set n = value; public virtual double compsalary() /計算普通員工工資 Console.Write("工作年數(shù):"); pn = int.Parse(Console.ReadLine(); psalary = bsalary + 30 * pn; return psalary; pub

33、lic class UEmployee : Employee /本科生職工類 public override double compsalary() return 1.5 * psalary(); public class GEmployee : Employee /研究生職工類 public override double compsalary() return 2 * psalary(); class Program static void Main(string args) Employee emp1 = new Employee(); Console.WriteLine("該

34、普通職工工資:0", psalary(); UEmployee emp2 = new UEmployee(); Console.WriteLine("該本科生職工工資:0", psalary(); GEmployee emp3 = new GEmployee(); Console.WriteLine("該研究生職工工資:0", psalary(); 課本157頁9public class Person /人類 private int no; /編號 private string name; /姓名 public void input() Con

35、sole.Write(" 編號:"); no = int.Parse(Console.ReadLine(); Console.Write(" 姓名:"); name = Console.ReadLine(); public void disp() Console.WriteLine(" 編號:0",no); Console.WriteLine(" 姓名:0",name); public class Student : Person /學(xué)生類 private string sclass; /班號 private in

36、t degree; /成績 public void input() base.input(); Console.Write(" 班號:"); sclass = Console.ReadLine(); Console.Write(" 成績:"); degree = int.Parse(Console.ReadLine(); new public void disp() base.disp(); Console.WriteLine(" 班號:0",sclass); Console.WriteLine(" 成績:0",d

37、egree); public class Teacher : Person /教師類 private string prof; /職稱 private string depart; /部門 public void input() base.input(); Console.Write(" 職稱:"); prof = Console.ReadLine(); Console.Write(" 部門:"); depart = Console.ReadLine(); new public void disp() base.disp(); Console.Write

38、Line(" 職稱:0", prof); Console.WriteLine(" 部門:0", depart); class Program static void Main(string args) Student s1 = new Student(); Teacher t1 = new Teacher(); Console.WriteLine("輸入一個學(xué)生數(shù)據(jù):"); s1.input(); Console.WriteLine("輸入一個教師數(shù)據(jù):"); t1.input(); Console.WriteLine("顯示一個學(xué)生數(shù)據(jù):"); s1.disp(); Console.WriteLine("顯示一個教師數(shù)據(jù):"); t1.d

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論