C#程序源代码辛苦整理.docx
C#程序源代码辛苦整理C#程序设计一、实验名称:C#实验二、实验目的:通过上机实际操作将平时课堂所学具体实现,通过该实验来检查自己的学习成功,并且发现平时学习中没有注意到的问题并解决之,从而加深对该门课程的以及C#语言的了解。三、实验步骤:实验一:C#编程环境实验目的:1.熟悉掌握C#开发环境的安装与配置2.熟悉开发环境,编写控制台和窗口两个版本的hello world范例程序实验内容:实验1-1:编写一个控制台程序,并且输出“hello world!”相关主要代码:using System;using System.Collections.Generic;using System.Text;namespace hello_worldclass SY1_2static void Main(string args)Console.WriteLine("Hello World!");抓图结果:实验1-2:编写一个Windows应用程序,并且输出“hello world!”相关主要代码:namespace hello_world2public partial class Form1 : Formpublic Form1InitializeComponent;private void Form1_Load(object sender, EventArgs e)MessageBox.Show("Hello World", "Message from C#");抓图结果:实验二:C#编程基础实验目的:1.熟悉掌握C#的各种数据类型,常量、变量的表达形式;2.熟悉掌握C#的运算符和表达式;3.熟悉掌握C#的语言,会使用顺序、选择、循环等语句结构编写程序;4.熟悉掌握C#的数组,学会数组的定义、初始化以及数组的应用。实验内容:实验2-1:有红、黄、黑、白四色球各一个,放置在一个编号为1、2、3、4的四个盒子中,每个盒子放置一只球,它们的顺序不知。甲、乙、丙三人猜测放置顺序如下:甲:黑球在1号盒子,黄球在2号盒子;乙:黑球在2号盒子,白球在3号盒子;丙:红球在2号盒子,白球在4号盒子。结果证明甲、乙、丙三人各猜中了一半,给出四色球放置在盒中的情况。相关的主要代码:using System;using System.Collections.Generic;using System.Text;namespace SY2_1class Programstatic void Main(string args)int a, b, c, d;for(a=1;a<=4;a+)for(b=1;b<=4;b+)for(c=1;c<=4;c+)if(a!=b&& b!=c&&c!=a)d=10-a-b-c;if(c=1&&b=4)&&(a=2&&d=3) )Console .Write ("红球放置在0号,黄球放置在1号,",a,b);Console .WriteLine ("黑球放置在0号,白球放置在1号",c,d);Console .Read ;抓图结果:实验2-2:采用筛选法求2-64之间的质数。相关主要代码:using System;public class TestNumSortpublic static void Mainint sieve, w;int i, j, p, k;bool flg = true;sieve = _0x0;p = 3;for (i = 0; i < 32; i+)w = 0x1 << i; w <<= p; j = p;while (j + i < 32)sieve &= w;w <<= p; j += p;k = i + 1;while (sieve >> k) & 0x01) = 0)k+; i+;p = p + 2;Console.WriteLine("2到64之间的素数有");Console.Write("0,4", 2);p = 3; w = 1;for (i = 0; i < 32; i+)if (sieve >> i & 0x01) != 0)Console.Write("0,3", p);p += 2;Console.WriteLine;Console.Read;抓图结果:实验2-3:根据给出的公式编程计算兀的值,直至所加项小于1E-10为止。相关主要代码:using System;using System.Collections.Generic;using System.Text;namespace SY2_3class Programstatic void Main(string args)double sum = 0.5, t, t1, t2, t3, p = 0.5 * 0.5;int odd = 3, even = 2, k;t = t1 = t2 = 1.0;t3= 1/2.0 ;while (t>1e-10)even+=2;t2=1.0/odd ;t1=t1*(odd-2)/(even -2);odd+=2;t3=t3* (1/2.0)*(1/2.0) ;t = t1 * t3 * t2; sum+=t;Console .WriteLine ("nPI=0,10:f8",sum *6);Console .Read ;抓图结果:实验2-4:编程进行卡布列克运算。所谓卡布列克运算,是指任意一个四位数,只要它们各个位上的数字不全相同,就有这样的规律:1)把组成这个四位数的四个数字由大到小排列,形成由这四个数字构成的最大数字2)把组成这个四位数的四个数字由小到大排列,形成由这四个数字构成的最小的四位数3)求出以上两数之差,得到一个新的四位数重复以上过程,总能得到最后的结果是6174。相关的主要代码:using System;using System.Collections.Generic;using System.Text;namespace SY2_4class Programstatic void Main(string args)Console.Write("请输入一个4位整数");string s = Console.ReadLine;int num = Convert.ToInt32(s);int each = new int4;int max, min, i, j, temp;while (num != 6174 && num != 0)i = 0;while (num != 0) each i+=num%10; num=num/10;for(i=0;i<3;i+)for(j=0;j<3-i;j+)if(each j>=each j+1)temp=each j;each j=each j+1;each j+1=temp;min= each 0*1000+each 1*100+each 2*10+each 3 ;max= each 3*1000+each 2*100+each 1*10+each 0 ;num = max -min ;Console .WriteLine ("0-1=2",max,min,num);Console .Read;抓图的结果:实验2-5:数列A=1,1,3,7,17,41有以下性质:a =a =1;a =a +2a (i>1)对于给定的n,数列的各个元素值由数列A的元素生成即以a / a 的分数形式表示,然后对其进行排序相关的主要代码:using System;using System.Collections.Generic;using System.Text;namespace SY2_5class Programstatic void Main(string args)int A = new int11;int ,Fraction=new int2,11;float B=new float 10;int i,n,j,pos,temp;float ftemp;A0=A1=1;Console .Write ("n请输入n(n<=10)值:");string s=Console .ReadLine ;n=Convert .ToInt32 (s);for(i=2;i<n+1;i+)Ai=Ai-2+2*Ai-1;for(i=0;i<n;i+)Bi=(float)Ai/Ai+1;Fraction0, i = Ai;Fraction1, i = Ai+1;for(i=0;i<n-1;i+)for(j=(pos=i)+1;j<n;j+)if(Bj<Bpos)pos=j;if(i!=pos)ftemp =Bpos;Bpos=Bi;Bi=ftemp;temp=Fraction0,pos;Fraction 0,pos=Fraction 0,i;Fraction 0,i=temp ;temp=Fraction 1,pos;Fraction 1,pos=Fraction 1,i;Fraction 1,i=temp;for(i=0;i<n;i+)Console .Write ("0/1 ",Fraction 0,i,Fraction 1,i);Console .Read;抓图的结果:实验三:C#面向对象程序基础实验目的:1.加深理解面向对象编程的概念,如类、对象、实例化等;2.熟练掌握类的声明格式,特别是类的成员定义,构造函数,初始化对象等;3.熟练掌握方法的声明,理解并学会使用方法的参数传递,方法的重载等实验内容:实验3-1:阅读程序相关程序代码:using System;using System.Collections.Generic;using System.Text;namespace SY3_1class Programclass CRectprivate int top, bottom, left, right;public static int total_rects = 0;public static long total_rect_area = 0;public CRectleft = top = right = bottom = 0;total_rects+;total_rect_area += getHeight * getWidth;Console.WriteLine("CRectContructing rectangle number0", total_rects);Console.WriteLine("Total rectangle areas is:0", total_rect_area);public CRect(int x1, int y1, int x2, int y2)left = x1;top = y1;right = x2;bottom = y2;total_rects+;total_rect_area += getHeight * getWidth;Console.WriteLine("CRect(int,int,int ,int)Constructing rectangle number0", total_rects);Console.WriteLine("Total rectangle areas is :0", total_rect_area);public CRect(CRect r)left = r.left;right = r.right;top = r.top;bottom = r.bottom;total_rects+;total_rect_area += getHeight * getWidth;Console.WriteLine("CRect(CRect&)Constructing rectangle number0", total_rects);Console.WriteLine("Total rectangle areas is :0", total_rect_area);public int getHeightreturn (top > bottom ? top - bottom : bottom - top);public int getWidthreturn right > left ? right - left : left - right;public static int getTotalRectsreturn total_rects;public static long getTotalRectanglereturn total_rect_area;public class Test3_1static void Main(string args)CRect rect1 = new CRect(1, 3, 6, 4), rect2 = new CRect(rect1);/拷贝构造,通过重载Console.Write("Rectangle 2:Height:0", rect2.getHeight);Console.WriteLine(",Width:0", rect2.getWidth);CRect rect3 = new CRect;Console.Write("Rectangle 3:Height:0", rect3.getHeight);Console.WriteLine(",Width:0", rect3.getWidth);Console.Write("Total_rects=0", CRect.total_rects);Console.WriteLine(",total_rect_area=0", CRect.total_rect_area);Console.Read;抓图的结果:实验3-2:设计一个图书卡片类Card,用来保存图书馆卡片分类记录。这个类的成员包括书名、作者、馆藏数量。至少提供两个方法,store书的入库处理,show显示图书信息,程序运行时,可以从控制台上输入需要入库图书的总量,根据这个总数创建Card对象数组,然后输入数据,最后可以选择按书名、作者、入库量排序相关的主要代码:using System;using System.Collections.Generic;using System.Text;namespace SY3_2class Cardprivate string title, author;private int total;public Cardtitle = ""author = ""total = 0;public Card(string title, string author, int total)this.title = title;this.author = author;this.total = total;public void store(ref Card card)/使用ref关键字进行引用传递,似乎是它的拷贝构造title = card.title;author = card.author;total = card.total;public void showConsole.WriteLine("Title:0,Author:1,Total:2", title, author, total);public string Title/Title的属性可读可写get return title; set title = value; public string Authorget return author; set author = value; public string Totalget return Total; set total = int.Parse (value); public class Test3_2static void Main(string args)Test3_2 T = new Test3_2;Card books;int index;int i, k;Card card = new Card;Console.Write("请输入需要入库图书的总数:");string sline = Console.ReadLine;int num = int.Parse(sline);books = new Cardnum;for (i = 0; i < num; i+)books i= new Card;index = new intnum;for (i = 0; i < num; i+)Console.Write("请输入书名:");card.Title = Console.ReadLine;Console.Write("请输入作者:");card.Author = Console.ReadLine;Console.Write("请输入入库量:");sline = Console.ReadLine;card.Total = sline;booksi.store(ref card);indexi = i;/应该表示第几本书Console.Write("请选择按什么关键字排序");sline = Console.ReadLine;int choice = int.Parse(sline);switch (choice) case 1:T.sortTitle(books,index);break;case 2:T.sortAuthor(books,index);break;case 3:T.sortTotal(books,index);break;for (i = 0; i < num; i+)k = indexi;books k.show;/和给出的程序不同Console .Read ;void sortTitle(Card book, int index)int i, j, m, n, temp;for (m = 0; m < index.Length - 1; m+)for (n = 0; n < index.Length- m - 1; n+)i = indexn;j = indexn + 1;if (string.Compare(booki.Title, bookj.Title) > 0)temp = indexn;indexn = indexn + 1;indexn + 1 = temp;void sortAuthor(Card book, int index)int i, j, m, n, temp;for(m=0;m<index .Length -1;m+)for (n = 0; n < index.Length -m- 1; n+)i = indexn;j = indexn + 1;if (string.Compare(booki.Author, bookj.Author) > 0)temp = indexn;indexn = indexn+ 1;indexn + 1 = temp;void sortTotal(Card book, int index)int i, j, m, n, temp;for (m = 0; m < index.Length - 1; m+)for (n = 0; n < index.Length - m - 1;n+ )i = indexn;j = indexn + 1;if (int.Parse (booki.Total) > int.Parse (bookj.Total)temp = indexn;indexn = indexn + 1;indexn + 1 = temp;抓图的结果:实验3-3:假设某银行共发出M张储蓄卡,每张储蓄卡拥有唯一的卡号,每天每张储蓄卡至多支持储蓄卡持有者的N笔“存款”或“取款”业务。根据实际发生的业务,实际处理数据,以反映最新情况。设Card卡包含的数据域有:卡号,当前余额,允许当日发生的业务次数,当日实际发生的业务数以及一个数组记录发生的具体业务,它提供的主要方法是store,处理判断是否超过当日允许发生的最大笔数,当前余额是否足以取款以及实现修改当前数据等。当持卡者输入正确的卡号、存款或取款金额后,程序进行相应的处理。若是输入了不正确的数据,程序会提示持卡者重新输入;若输入的卡号为负数时,银行终止当日业务。相关主要代码:using System;using System.Collections.Generic;using System.Text;namespace SY3_3class Programclass Cardpublic long cardNo;/卡号decimal balance;/余额int currentNum;/当日业务实际发生笔数static int number;/每张卡允许当日存款或取款的总次数decimal currentMoney;/存放 当日存款金额,正值代表存款,负值代表取款public CardcurrentMoney = new decimalnumber;public Card(long No, decimal Balance)cardNo = No;balance = Balance;currentMoney = new decimalnumber;public void store(decimal Money, out int status)if (currentNum = number)status = 0;return;/本卡已达当日允许的业务次数if (balance + Money < 0)status = -1;return;currentMoneycurrentNum = Money;balance += Money;currentNum+;status = 1;public void showConsole.WriteLine("卡号:0,当前余额:1,当日发生业务的次数:2", cardNo, balance, currentNum);for (int i = 0; i < currentNum; i+)Console.WriteLine("当时存款/取款的情况:0", currentMoneyi);static public int Number/设置允许当日存款或取款的总次数setnumber = value;public long CardNogetreturn cardNo ;public classTest3_3static void Main(string args)Test3_3 T=new Test3_3 ;Card person;int Num,status,k;long CardNo;decimal Balance,Money;Console .Write ("请输入允许当日存款或取款的总次数:");string sline=Console.ReadLine;Card.Number=int .Parse (sline);Console .Write ("请输入某银行发出的储蓄卡总数:");sline =Console .ReadLine ;Num=int .Parse (sline);person =new Card Num;for(int i=0;i<Num;i+)/初始化Num个存储卡 的卡号等信息Console .Write ("请输入卡号:");sline =Console .ReadLine ;CardNo =long .Parse (sline );Console .Write ("请输入0账户余额:",CardNo );sline=Console .ReadLine ;Balance =decimal .Parse (sline);personi=new Card (CardNo,Balance );while(true)/进行存取款 业务处理Console .WriteLine ("现在正进行存款取款的业务处理,如果输入的卡号<0,则结束业务处理");Console .Write ("请输入卡号:");sline=Console .ReadLine ;CardNo =long .Parse (sline );if(CardNo <0)break ;k=T.Locate(person ,CardNo );if(k=-1)Console .WriteLine ("对不起,不存在0号的存储卡",CardNo );continue ;Console .WriteLine ("请输入卡金额:");sline =Console .ReadLine ;Money =decimal .Parse (sline );person k.store (Money,out status );switch (status )case -1:Console .WriteLine ("存款余额不足,不能完成本次的取款业务");break;case 0:Console .WriteLine ("本卡已达当日允许的业务次数");break;case 1:Console .WriteLine ("成功处理完当前业务");person k.show ;break ;int Locate(Cardperson,long CardNo)int i;for (i = 0; i < person.Length; i+)if (personi.cardNo = CardNo)return i;return -1;抓图结果:实验四:接口实验目的:1)熟练掌握接口的定义和实现2)熟悉集合接口的使用实验内容:实验4-1:编写IEnglishDimensions和IMctricDimensions两个接口,同时以公制单位和英制单位显示框的尺寸。Box类继承IEnglishDimensions和IMctricDimensions两个接口,它们表示不同的度量衡系统。两个接口有相同的成员名Length和Width相关的主要代码:using System;using System.Collections.Generic;using System.Text;namespace jiekouclass Programinterface IEnglishDimensionsfloat Length;float Width;interface IMetricDimensionsfloat Length;float Width;class Box : IEnglishDimensions, IMetricDimensionsfloat lengthInches;float widthInches;public Box(float length, float width)lengthInches = length;widthInches = width;float IEnglishDimensions.Lengthreturn lengthInches;float IEnglishDimensions.Widthreturn widthInches;float IMetricDimensions.Lengthreturn lengthInches * 2.54f;float IMetricDimensions.Widthreturn widthInches * 2.54f;static void Main(string args)Box myBox = new Box(30.0f, 20.0f);IEnglishDimensions eDimensions = (IEnglishDimensions)myBox;IMetricDimensions mDimensions = (IMetricDimensions)myBox;System.Console.WriteLine("Length(in):0", eDimensions.Length);System.Console.WriteLine("Width(in):0", eDimensions.Width);System.Console.WriteLine("Length(out):0", mDimensions.Length);System.Console.WriteLine("Width(out):0", mDimensions.Width);抓图的结果:实验4-2:考虑这样一个水果篮,里面至多可以装有10个苹果和香蕉,它们都派生自一个叫水果的基类。使用集合接口IEnumerable和Ienumerator实现装入水果过程及遍历水果。相关的主要代码:using System;using System.Collections.Generic;using System.Text;using System.Collections;namespace 试验4_2public class Frultpublic virtual string Nameget return ("Frult"); public class Apple : Frultpublic override string Namegetreturn ("Apple");public class Banana : Frultpublic override string Namegetreturn ("Banana");public class FrultBasket : IEnumerablestatic int Max = 10;Frult basket = new FrultMax;int count = 0;internal Frult thisint indexget return (basketindex); set basketindex = value; internal int Countget return (count); public void Add(Frult frult)if (count > Max)Console.WriteLine("超出水果筐容量!");basketcount+ = frult;public IEnumerator GetEnumeratorreturn (new FrultBasketEnumerator(this);public class FrultBasketEnumerator : IEnumeratorFrultBasket frultBasket;int index;