C#程序设计实验指导书.docx
《C#程序设计实验指导书.docx》由会员分享,可在线阅读,更多相关《C#程序设计实验指导书.docx(45页珍藏版)》请在冰豆网上搜索。
C#程序设计实验指导书
安徽工业大学C#实验指导书
刘伟uhuhufaji129074157
实验一、C#编程环境
实验1-2、helloworld
staticvoidMain(string[]args)
{
Console.WriteLine("helloworld");
Console.ReadLine();
}
实验1-3
MessageBox.Show("helloworld","messagefromC#");
实验二、C#编程基础
实验2-1
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace实验2_1
{classProgram
{
staticvoidMain(string[]args)
{
inta,b,c,d;//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&&a!
=c)
{
d=10-a-b-c;//盒子编号之和为10
if((c==1^b==2)&&(c==2^d==3)&&(a==2^d==4))
{
Console.Write("红球放在{0}号,黄球放在{1}号",a,b);
Console.WriteLine("黑球放在{0}号,白球放在{1}号",c,d);}}
Console.Read();
}}}
实验2-2
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace实验2_2筛选法
{classProgram
{
staticvoidMain(string[]args)
{
intsieve,w;inti,j,p,k;
boolfig=true;
sieve=~0x0;p=3;
for(i=0;i<32;i++)
{
w=0x1<
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求Π
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace实验2_3求PI
{classProgram
{staticvoidMain(string[]args)
{doublesum=0.5,t,t1,t2,t3,p=0.5*0.5;
intodd=1,even=2,k;
t=t1=t2=1.0;t3=0.5;
while(t>1e-10)
{t1=t1*odd/even;
odd+=2;even+=2;
t2=1.0/odd;
t3=t3*p;
t=t2*t1*t3;
sum+=t;}
Console.WriteLine("\nPI={0,10:
f8}",sum*6);
Console.Read();}}
}
实验2-4
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace实验2_4
{
classProgram
{
staticvoidMain(string[]args)
{
Console.Write("请输入一个4位整数");
intnum=Convert.ToInt32(Console.ReadLine());
int[]each=newint[4];
intmax,min,i,j,temp;
while(num!
=6174&&num!
=0)
{
i=0;
while(num!
=0)
{
each[i++]=num%10;
num/=10;//除以10去掉了0
}
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.ReadLine();
}}}
实验2-5
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace实验2_5
{
classProgram
{
staticvoidMain(string[]args)
{
int[]A=newint[11];
int[,]Fraction=newint[2,10];
float[]B=newfloat[10];
inti,n,j,pos,temp=0;floatftemp;
A[0]=A[1]=1;
Console.Write("\n请输入n(n<=10)的值:
");
n=Covert.toInt32(Console.ReadLine());
for(i=2;iA[i]=A[i-2]+2*A[i-1];
for(i=0;i{
B[i]=(float)A[i]/A[i+1];
Fraction[0,i]=A[i];Fraction[1,i]=A[i+1];
}
for(i=0;i{for(j=(pos=i)+i;jif(B[j]
pos=j;
if(i!
=pos)
{
ftemp=B[pos];
B[pos]=B[i];
B[i]=temp;
temp=Fraction[0,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;iConsole.Write("{0}/{1}",Fraction[0,i],Fraction[1,i]);
Console.Read();
}
}
}
实验三、面向对象编程方法
3.1
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceCSharp实º¦Ì验¨¦指?
导Ì?
书º¨¦3
{
classCRect
{
privateinttop,bottom,left,right;
publicstaticinttotal_recta=0;
publicstaticlongtotal_rect_area=0;
publicintgetHeight()
{
returntop>bottom?
top-bottom:
bottom-top;
}
publicintgetWidth()
{
returnright>left?
right-left:
left-right;
}
publicstaticintgetTotalReca()
{
returntotal_recta;
}
publicstaticlonggetTotalRectArea()
{
returntotal_rect_area;
}
publicCRect()
{
left=top=right=bottom=0;
total_recta++;
total_rect_area+=getHeight()*getWidth();
Console.WriteLine("CRect()constructingrectanglenumber{0}",total_recta);
Console.WriteLine("Totalrectangleareais:
{0}",total_rect_area);
}
publicCRect(intx1,inty1,intx2,inty2)
{
left=x1;top=y1;
right=x2;bottom=y2;
total_recta++;
total_rect_area+=getHeight()*getWidth();
Console.WriteLine("CRect(int,int,int,int)constructingrectanglenumber{0}",total_recta);
Console.WriteLine("totalrectangleareasis:
{0}",total_rect_area);
}
publicCRect(CRectr)
{
left=r.left;right=r.left;
top=r.top;bottom=r.bottom;
total_recta++;
total_rect_area+=getHeight()*getWidth();
Console.WriteLine("CRect(crect&)constructingrectanglenumber{0}",total_recta);
Console.WriteLine("totalrectangleareasis:
{0}",total_rect_area);
}
};
publicclassTest31
{
publicstaticvoidMain()
{
CRectrect1=newCRect(1,3,6,4);CRectrect2=newCRect(rect1);
Console.Write("Rectangle2:
Height:
{0}",rect2.getHeight());
Console.WriteLine(",Width:
{0}",rect2.getWidth());
{
CRectrect3=newCRect();
Console.Write("Rectangle3:
Height:
{0}",rect3.getHeight());
Console.WriteLine(",Width:
{0}",rect3.getWidth());
}
Console.Write("total_recta={0}",CRect.total_recta);
Console.WriteLine("total_rect_area={0}",CRect.total_rect_area);
Console.ReadLine();
}
}
}
运行效果:
将三种方法修改后,源程序如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceCard1
{
classCard
{
privatestringtitle,author;//书名,作者
privateinttotal;//种数
publicCard()
{
title="";author="";total=0;
}
publicCard(stringtitle,stringauthor,inttotal)
{
this.title=title;
this.author=author;
this.total=total;
}
publicvoidstore(refCardcard)
{
title=card.title;author=card.author;total=card.total;
}
publicvoidshow()
{
Console.WriteLine("Title:
{0},author:
{1},Total:
{2}",title,author,total);
}
publicstringTitle//Title属性可读可写
{
get{returntitle;}
set{title=value;}
}
publicstringAuthor
{
get{returnauthor;}
set{author=value;}
}
publicintTotal
{
get{returntotal;}
set{total=value;}
}
}
publicclassTest32
{
voidsort(Card[]books,int[]index,intmethod)
{
if(method==1)
{
inti,j,m,n,temp;
for(m=0;mfor(n=0;n{
i=index[n];j=index[n+1];
if(string.Compare(books[i].Title,books[j].Title)>0)
{
temp=index[n];index[n]=index[n+1];index[n+1]=temp;
}
}
}
elseif(method==2)
{
inti,j,m,n,temp;
for(m=0;mfor(n=0;n{
i=index[n];j=index[n+1];
if(String.Compare(books[i].Author,books[j].Author)>0)
{
temp=index[n];index[n]=index[n+1];index[n+1]=temp;
}
}
}
elseif(method==3)
{
inti,j,m,n,temp;
for(m=0;mfor(n=0;n{
i=index[n];j=index[n+1];
if(books[i].Total>books[i].Total)
{
temp=index[n];index[n]=index[n+1];index[n+1]=temp;
}
}
}
}
publicstaticvoidMain()
{
Test32T=newTest32();
Card[]books;int[]index;
inti,k;
Cardcard=newCard();
Console.Write("请输入需要入库图书的种数:
");
intnum=int.Parse(Console.ReadLine());
books=newCard[num];//新建对象数组赋值给books数组名
for(i=0;ibooks[i]=newCard();
index=newint[num];
for(i=0;i{
Console.Write("请输入书名:
");
card.Title=Console.ReadLine();
Console.Write("请输入作者:
");
card.Author=Console.ReadLine();
Console.Write("请输入入库量:
");
card.Total=int.Parse(Console.ReadLine());
books[i].store(refcard);
index[i]=i;
}
Console.Write("请选择按什么关键字排序:
(1,按书名2,按作者3,按入库量)");
intchoice=Convert.ToInt16(Console.ReadLine());
switch(choice)
{
case1:
T.sort(books,index,1);
break;
case2:
T.sort(books,index,2);
break;
case3:
T.sort(books,index,3);
break;
}
for(i=0;i{
k=index[i];
books[i].show();
}
Console.ReadLine();
}
}
}
3.2
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceCard1
{
classCard
{
privatestringtitle,author;//书名,作者
privateinttotal;//这本书的入库总数
publicCard()
{
title="";author="";total=0;
}
publicCard(stringtitle,stringauthor,inttotal)
{
this.title=title;
this.author=author;
this.total=total;
}
publicvoidstore(refCardcard)
{
title=card.title;author=card.author;total=card.total;
}
publicvoidshow()
{
Console.WriteLine("Title:
{0},author:
{1},Total:
{2}",title,author,total);
}
publicstringTitle//Title属性可读可写
{
get{returntitle;}
set{title=value;}
}
publicstringAuthor
{
get{returnauthor;}
set{author=value;}
}
publicintTotal
{
get{returntotal;}
set{total=value;}
}
}
publicclassTest32
{
voidsortTitle(Card[]book,int[]index)
{
inti,j,m,n,temp;
for(m=0;mfor(n=0;n{
i=index[n];j=index[n+1];
if(string.Compare(book[i].Title,book[j].Title)>0)
{
temp=index[n];index[n]=index[n+1];index[n+1]=temp;
}
}
}
voidsortAuthor(Card[]books,int[]index)
{
inti,j,m,n,temp;
for(m=0;mfor(n=0;n{
i=index[n];j=index