C入門經(jīng)典源碼part_第1頁
C入門經(jīng)典源碼part_第2頁
C入門經(jīng)典源碼part_第3頁
C入門經(jīng)典源碼part_第4頁
C入門經(jīng)典源碼part_第5頁
已閱讀5頁,還剩46頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、C#入門源代碼 part11.using System;namespace nsArray public class program public static void Main() DateTime now = DateTime.Now;/System.DateTime Random rand = new Random(int)now.Millisecond);/System.Random產(chǎn)生隨機(jī)數(shù) Array Arr = Array.CreateInstance(typeof(Int32), 10);/創(chuàng)建Array類的一個(gè)實(shí)例 for (int x = Arr.GetLowerBoun

2、d(0); x < Arr.GetUpperBound(0) + 1; +x) Arr.SetValue(rand.Next() % 100, x); /SetValue()成員方法初始化元素 Console.WriteLine("還未排序的數(shù)組元素:"); foreach (int x in Arr) Console.Write(x+" "); int total = 0; Console.WriteLine("rnn這些數(shù)組元素的和與均值:"); foreach (int x in Arr) total += x; Cons

3、ole.WriteLine(total+", "+(double )total /(double )Arr.Length ); Array.Sort(Arr ); Console.WriteLine("rn升序排列后的數(shù)組元素:"); foreach (int x in Arr) Console.Write(x+" "); Array.Reverse(Arr ); Console.WriteLine("rnrn降序排列后的數(shù)組元素:"); foreach (int x in Arr) Console.Write(x

4、+" "); Console.WriteLine (); Console.Read(); 2.using System;public class program public static void Main() int intArr = new int5 10,20,30,40,50; for (int counter = 0; counter < intArr.Length; counter+) Console.WriteLine("intArr"+counter +"="+intArr counter ); char ch

5、arArr = new char 'h','e','l','l','o' for (int counter = 0; counter < charArr.Length; counter+) Console.WriteLine("charArr"+counter +"="+charArr counter ); string strArr = "hello", "csharp" ; foreach (string str in str

6、Arr) Console.WriteLine("strArr="+str); Console.ReadLine(); 3.using System;namespace nsArray public class program public static void Main() DateTime now = DateTime.Now;/System.DateTime Random rand = new Random(int)now.Millisecond);/System.Random產(chǎn)生隨機(jī)數(shù) Array Arr = Array.CreateInstance(typeof(

7、Int32 ),10);/創(chuàng)建Array類的一個(gè)實(shí)例 for (int x = Arr.GetLowerBound(0); x < Arr.GetUpperBound(0) + 1; +x) Arr.SetValue(rand.Next ()%100,x); /SetValue()成員方法初始化元素 int total = 0; Console.Write("Array values are "); foreach (int val in Arr) total += val; Console.Write(val+","); Console.Writ

8、eLine("均值是:0,0:f1",(double )total /(double )Arr.Length );/格式化輸出 Console.ReadLine(); 4.using System;namespace nsArray public class program public static void Main() DateTime now = DateTime.Now;/System.DateTime Random rand = new Random(int)now.Millisecond);/System.Random產(chǎn)生隨機(jī)數(shù) Array iArr = Ar

9、ray.CreateInstance(typeof(Int32), 10);/創(chuàng)建Array類的一個(gè)實(shí)例 for (int x = iArr.GetLowerBound(0); x < iArr.GetUpperBound(0) + 1; +x) iArr.SetValue(rand.Next() % 100, x); /SetValue()成員方法初始化元素 double dArr=new double 8; Array.Copy(iArr ,dArr,dArr.Length ); Console.Write("dArr數(shù)組元素包括:rn "); foreach (

10、double d in dArr) Console.Write("0,4:f1 ",d); Console.Write("rnrn iArr數(shù)組元素包括:rn "); foreach (int x in iArr) Console.Write(x+" "); Console.WriteLine(); Console.ReadLine(); 5.using System;namespace ArrIndex public class program static public void Main() int Arr = new int1

11、2 29, 82, 42, 46, 54, 65, 50, 42, 5, 94, 19, 34 ; Console.WriteLine("42第一次出現(xiàn)在下標(biāo)位置:"+Array.IndexOf (Arr,42); Console.WriteLine("42最后一次出現(xiàn)在下標(biāo)位置:"+Array.LastIndexOf (Arr,42); int x = 0; while (x = Array.IndexOf(Arr, 42, x) > 0) Console.WriteLine("42在下標(biāo)" + x + "處出現(xiàn)&q

12、uot;); +x; /+x x = Arr.Length - 1; while (x = Array.LastIndexOf(Arr, 42, x) >= 0) Console.WriteLine("42在下標(biāo)" + x + "處出現(xiàn)"); -x; /-x Console.ReadLine (); 6.using System;class Star public string name; public int brightness; public Star(string name, int brightness) = name

13、; this.brightness = brightness; public class program public static void Main() Star galaxy = new Star10; galaxy1 = new Star("Sun",3); galaxy2 = new Star("Moon",1); Console.WriteLine("元素個(gè)數(shù)="+galaxy.Length ); for (int x = 0; x < galaxy.Length; x+) if (galaxyx != null)

14、Console.WriteLine("galaxy"+x+".name:"+ ); Console.WriteLine("galaxy"+x+".brightness="+galaxyx.brightness ); Console.ReadLine(); 7.using System;class Star public string name; public int brightness; public Star(string name, int brightness)

15、= name; this.brightness = brightness; public class program public static void Main() Star, , galaxy = new Star10, 5, 3; galaxy1, 3, 2 = new Star("Sun",3); galaxy4, 1, 2 = new Star("Moon",1); Console.WriteLine("維數(shù)="+galaxy.Rank+",元素個(gè)數(shù)="+galaxy .Length ); for(in

16、t x=0;x<galaxy.GetLength (0);x+) for(int y=0;y<galaxy.GetLength (1);y+) for (int z = 0; z < galaxy.GetLength(2); z+) if (galaxyx, y, z != null) Console.WriteLine("galazy"+x+","+y+","+z+".name="+galaxy x,y, ); Console.WriteLine("galazy"

17、 + x + "," + y + "," + z + ".brightness=" + galaxyx, y, z.brightness); Console.ReadLine(); 8.using System;namespace ArrayTest class Employee private int empID; public Employee(int empID) this.empID = empID; public override string ToString() return empID.ToString(); publ

18、ic class ClassArray public void Run() int intArr; Employee empArr; intArr = new int5; empArr = new Employee3; for (int i = 0; i < empArr.Length; i+) empArri = new Employee(i+5); Console.WriteLine("The int array."); for (int i = 0; i < intArr.Length; i+) Console.WriteLine(intArri.ToSt

19、ring(); Console.WriteLine("nThe employee array."); for (int i = 0; i < empArr.Length; i+) Console.WriteLine(empArri.ToString(); static void Main() ClassArray c = new ClassArray(); c.Run(); Console.ReadLine(); 9.using System;namespace base_ public class Person protected string ssn = &quo

20、t;111-222-333-444" protected string name = "張三" public virtual void GetInfo() Console.WriteLine("姓名:0",name ); Console.WriteLine("編號(hào):0",ssn ); public class Employee : Person public string id = "ABC567" public override void GetInfo() base.GetInfo(); Consol

21、e.WriteLine("成員id:0",id ); class program public static void Main() Employee e = new Employee(); e.GetInfo(); Console.Read();/ 10.using System;namespace whilebreak class program static void Main(string args) for (int i = 0; i < 3; i+) Console.WriteLine("外部循環(huán):"+i); Console.Write

22、("內(nèi)部循環(huán):"); int t = 0; while (t <100) if (t = 10) break; /t為10時(shí)就已經(jīng)跳出了while循環(huán)體,所以參與循環(huán)的數(shù)壓根就沒超過10 /所以可寫成:while(t<=10). Console.Write(t+" "); t+; Console.WriteLine(); Console.WriteLine("循環(huán)結(jié)束"); Console.ReadLine(); 11.using System;using System.Collections;using System.C

23、ollections.Generic;namespace Collection class Product string pName; int pNum; string pMaker; string pTime; int pSales; double pPrice; public Product(string _pName, int _pNum, string _pMaker, string _pTime, int _pSales, double _pPrice) pName = _pName; pNum = _pNum; pMaker = _pMaker; pTime = _pTime; p

24、Sales = _pSales; pPrice = _pPrice; public string _pName get return pName; set pName = value; public int _pNum get return pNum; set pNum = value; public string _pMaker get return pMaker; set pMaker = value; public string _pTime get return pTime; set pTime = value; public int _pSales get return pSales

25、; set pSales = value; public double _pPrice get return pPrice; set pPrice = value; class Computer : Product public Computer(string pname, int pnum, string pmaker, string ptime, int psales, double pprice, string type) : base(pname, pnum, pmaker, ptime, psales, pprice) _type = type; public string _typ

26、e get return _type; set public void PlayGames() Console.WriteLine("Play Computer Games!"); class Printer:Product string category; public string _Category get return category; set category = value; public Printer(string pName, int pNum, string pMaker, string pTime, int pSales, double pPrice

27、, string cat) : base(pName, pNum, pMaker, pTime, pSales, pPrice) category = cat; public void takePrint() Console.WriteLine("Print PictureS!"); class Camera : Product string focus; public string _focus get return focus; set focus = value; public Camera(string pName, int pNum, string pMaker,

28、 string pTime, int pSale, double pPrice, string foc) : base(pName, pNum, pMaker, pTime, pSale, pPrice) focus = foc; public void takePhoto() Console.WriteLine("Take Photos!"); class Phone : Product string frequency; public string _frequecy get return frequency; set frequency = value; public

29、 Phone(string pName, int pNum, string pMaker, string pTime, int pSales, double pPrice, string freq) : base(pName, pNum, pMaker, pTime, pSales, pPrice) frequency = freq; public void takeCall() Console.WriteLine("Take Phonecall!"); class Mp3 : Product string Mp3type; public string _Mp3type g

30、et return Mp3type; set Mp3type = value; public Mp3(string pName, int pNum, string pMaker, string pTime, int pSales, double pPrice, string Mt) : base(pName, pNum, pMaker, pTime, pSales, pPrice) Mp3type = Mt; public void listenMusic() Console.WriteLine("Listen to the Music!"); class program

31、static void Main(string args) Mp3 m1 = new Mp3("翠竹", 100, "深圳恒業(yè)", "2007-10-23", 21, 403, "國(guó)產(chǎn)"); Mp3 m2 = new Mp3("lopo", 100, "深圳恒業(yè)", "2007-10-23", 33, 830, "進(jìn)口"); Phone p1 = new Phone("摩托羅拉v988", 1000, "深

32、圳創(chuàng)業(yè)", "2006-10-23", 233, 1830, "GSM"); Phone p2 = new Phone("摩托羅拉v1902", 1000, "深圳創(chuàng)業(yè)", "2006-10-23", 33, 80, "CDMA"); Phone p3 = new Phone("熊貓v3211", 1000, "深圳創(chuàng)業(yè)", "2006-3-23", 33, 1230, "CDMA");

33、 Computer c1 = new Computer("宏基3211", 1000, "深圳創(chuàng)業(yè)", "2006-3-23", 33, 8230, "筆記本"); Computer c2 = new Computer("宏基TC003", 1000, "深圳創(chuàng)業(yè)", "2006-3-23", 33, 4230, "臺(tái)式機(jī)"); ArrayList paList = new ArrayList();/產(chǎn)品數(shù)組列表 paList.Add(

34、p1); paList.Add(p2); paList.Add(p3); paList.Add(m1); paList.Add(m2); paList.Add(c1); paList.Add(c2); ArrayList spaList = new ArrayList();/抽樣數(shù)組列表 spaList.Add(Product)paList1);/變換成Product類型很有必要,否則最后一項(xiàng)屬性會(huì)不同 spaList.Add(Product)paList2); spaList.Add(Product)paList3); foreach (Product mySample in spaList

35、) Console.WriteLine("采樣產(chǎn)品 " + mySample._pName + " 銷售數(shù):" + mySample._pSales); Computer c3 = new Computer("IBM Re6", 1000, "聯(lián)想", "2007-73-23", 33, 8230, "筆記本"); paList.Add(c3); foreach (Product pro in paList) Console.WriteLine("產(chǎn)品 "

36、 + pro._pName + " 總數(shù)量:" + pro._pNum); Console.ReadLine(); 12.using System;namespace C_Exception class Ridio public void TurnOn(bool on) if (on) Console.WriteLine("Accelerate."); else Console.WriteLine("Speed is undercontrol."); class Car public const int MaxSpeed = 100;

37、 private int curSpeed; private string petName; private bool carIsDead; private Ridio theMusicBox = new Ridio(); public Car() public Car(string name, int cursp) curSpeed = cursp; petName = name; public void CrankTunes(bool state) theMusicBox.TurnOn(state); public void Accelerate(int delta) if (carIsD

38、ead) Console.WriteLine("0 不能正常工作.", petName); else curSpeed += delta; if (curSpeed < MaxSpeed) Console.WriteLine("0 已經(jīng)過熱了.", petName); curSpeed = 0; carIsDead = true; throw new Exception(string.Format("0 已經(jīng)太熱了.", petName); else Console.WriteLine("=>汽車當(dāng)前速度為:0&

39、quot;, curSpeed); class program static void Main(string args) Console.WriteLine("=汽車控制程序="); Console.WriteLine("=>創(chuàng)建一個(gè)Car,開始加速."); Car mycar = new Car("長(zhǎng)安發(fā)動(dòng)機(jī)", 20); mycar.CrankTunes(true); try for (int i = 0; i < 10; i+) mycar.Accelerate(10); catch(Exception e) Co

40、nsole.WriteLine("n=ERROR="); Console.WriteLine("方法:0", e.TargetSite); Console.WriteLine("消息:0", e.Message); Console.WriteLine("源碼:0", e.Source); Console.Read(); 13.using System;using System.IO;namespace C_File class Customer string custName; string custID; str

41、ing custAccount; public Customer(string id, string name, string account) custName = name; custID = id; custAccount = account; public Customer(string id, string name) custID = id; custName = name; public string _custID get return custID; set custID = value; public string _custName get return custName

42、; set custName = value; public string _custAcount get return custAccount; set custAccount = value; class BankStaff public void Desposit(string custName, string custNum, int money, string staffID) Console.WriteLine("銀行業(yè)務(wù)員" + staffID + "為用戶" + custName + "從賬戶" + custNum +

43、 "存款" + money); public string Register(string staffID, string custName, string custID) Console.WriteLine("銀行業(yè)務(wù)員" + staffID + "為用戶" + custName + "注冊(cè)賬戶" + "quot;); return "quot; public void UnRegister(string custNa

44、me, string custID, string custNum, string staffID) Console.WriteLine("銀行業(yè)務(wù)員" + staffID + "刪除用戶" + custName + "的賬戶:" + custNum); public void FetchMoney(string staffID, string custName, string custNum, int money) Console.WriteLine("銀行業(yè)務(wù)員" + staffID + "為用戶&q

45、uot; + custName + "從賬戶" + custNum + "取款" + money); public void Login(string staffID, string pw) Console.WriteLine("銀行職員 " + staffID + "登陸銀行系統(tǒng)!"); class LogInfo public LogInfo() File.CreateText("D:My DocumentsC#testtestfile.txt"); public void WriteLog

46、Info(string log) using (StreamWriter writer = new StreamWriter("D:My DocumentsC#testtestlog.txt", true) writer.WriteLine(log); writer.Close(); public void ReadLogInfo() Console.WriteLine("讀取日志信息:n"); using (StreamReader sr = new StreamReader ("D:My DocumentsC#testtestlog.txt") string input = null; while (input = sr.ReadLine() != null) Console.WriteLine(input); sr.Close(); class program static void Main(string a

溫馨提示

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

評(píng)論

0/150

提交評(píng)論