C#程序设计实验报告2要点Word格式.docx
《C#程序设计实验报告2要点Word格式.docx》由会员分享,可在线阅读,更多相关《C#程序设计实验报告2要点Word格式.docx(27页珍藏版)》请在冰豆网上搜索。
![C#程序设计实验报告2要点Word格式.docx](https://file1.bdocx.com/fileroot1/2023-1/2/d2f39d71-716d-49e9-99b5-02b295c65a70/d2f39d71-716d-49e9-99b5-02b295c65a701.gif)
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace_2_1
{
classProgram
{
staticvoidMain(string[]args)
constdoublePI=3.14159;
doubler,perimeter,area,volume;
Console.Write("
请输入半径:
"
);
Strings=Console.ReadLine();
r=double.Parse(s);
Console.WriteLine("
圆的半径为={0}"
r);
perimeter=2*PI*r;
area=PI*r*r;
volume=4/3*PI*Math.Pow(r,3);
圆的周长为={0},面积为={1}"
perimeter,area);
球体的体积={0}"
volume);
Console.ReadLine();
}
}
2-2求三角形的周长和面积
namespace_2_2
doublea,b,c,p,h,area;
Console.Write("
请输入三角形的边a:
strings=Console.ReadLine();
a=double.Parse(s);
Console.Write("
请输入三角形的边b:
s=Console.ReadLine();
b=double.Parse(s);
请输入三角形的边c:
c=double.Parse(s);
if(a>
0&
&
b>
c>
a+b>
c&
a+c>
b&
b+c>
a)
三角形三边分别为:
a={0},b={1},c={2}"
a,b,c);
p=a+b+c;
h=p/2;
area=Math.Sqrt(h*(h-a)*(h-b)*(h-c));
三角形的周长={0},面积为={1}"
p,area);
elseConsole.WriteLine("
无法构成三角形!
Console.ReadKey();
2-3分段函数的实现
运行结果
namespace_2_3_2
doublex,y;
请输入x:
x=double.Parse(s);
y=(x*x-3*x)/(x+1)+2*Math.PI+Math.Sin(x);
if(x<
0)
y=Math.Log(-5*x)+6*Math.Sqrt(Math.Abs(x)+Math.Pow(Math.E,4)-Math.Pow(x+1,3));
Console.WriteLine("
方法一:
x={0},y={1}"
x,y);
if(x>
=0)
y=Math.Log(-5*x)+6*Math.Sqrt(Math.Abs(x)+Math.Pow(Math.E,4)-Math.Pow(x+1,3));
方法二:
else
y=Math.Log(-5*x)+6*Math.Sqrt(Math.Abs(x)+Math.Pow(Math.E,4)-Math.Pow(x+1,3));
方法三:
y=(x>
=0)?
(x*x-3*x)/(x+1)+2*Math.PI+Math.Sin(x):
Math.Log(-5*x)+6*Math.Sqrt(Math.Abs(x)+Math.Pow(Math.E,4)-Math.Pow(x+1,3));
方法四:
Console.ReadKey();
2-4三个数比较大小
namespace_2__4
inta,b,c,a1,b1,c1,t,Nmax,Nmin,Nmid;
RandomrMun=newRandom();
a=rMun.Next(101);
b=rMun.Next(101);
c=rMun.Next(101);
原始值:
a1=a;
b1=b;
c1=c;
b)
t=a;
a=b;
b=t;
c)
a=c;
c=t;
if(b>
t=b;
b=c;
Console.WriteLine(("
(方法一)升序值:
),a,b,c);
a=a1;
b=b1;
c=c1;
Nmax=Math.Max(Math.Max(a,b),c);
Nmin=Math.Min(Math.Min(a,b),c);
Nmid=a+b+c-Nmax-Nmin;
a=Nmin;
b=Nmid;
c=Nmax;
(方法二)升序值:
2-5求解一元二次方程
namespaceConsole2_5
doublea,b,c,delta,x1,x2,realPart,imagePart;
请输入系数a:
请输入系数b:
请输入系数c:
if(a==0)
if(b==0)Console.WriteLine("
此方程无解!
此方程的解为:
{0}"
-c/b);
else
delta=b*b-4*a*c;
if(delta>
{
x1=(-b+Math.Sqrt(delta))/(2*a);
x2=(-b-Math.Sqrt(delta))/(2*a);
此方程有两个不等实根:
{0},{1}"
x1,x2);
if(delta==0)Console.WriteLine("
此方程有两个相等实根:
-b/(2*a));
realPart=-b/(2*a);
imagePart=Math.Sqrt(-delta)/(2*a);
{0}+{1}i和{0}-{1}i"
realPart,imagePart);
2-6switch语句实现多重分支
namespaceConsoleApplication1
inti;
输入一个数字(1~7):
i=int.Parse(s);
switch(i)
case1:
对应的星期为:
星期一"
break;
case2:
星期二"
case3:
星期三"
case4:
星期四"
case5:
星期五"
case6:
星期六"
case7:
星期日"
default:
输入错误!
break;
2-7分别使用if语句和switch语句实现多分支结构
运行结果(if语句)
namespaceConsole2_7
intc;
doublef=0;
请输入有固定工资收入的党员的月工资:
intsalary=int.Parse(Console.ReadLine());
if(salary>
1500)c=15;
elsec=(salary-1)/100;
switch(c)
case0:
f=0.5/100*salary;
f=1.5/100*salary;
case8:
case9:
case10:
case11:
case12:
case13:
case14:
f=2.0/100*salary;
case15:
f=3.0/100*salary;
月工资={0},交纳党费={1}"
salary,f);
法二
代码:
(if语句)
namespace_2_7_2
请输入有固定收入的党员的月工资:
salary<
=400)f=0.5/100*salary;
elseif(salary>
400&
=600)f=1.0/100*salary;
600&
=800)f=1.5/100*salary;
800&
=1500)f=2.0/100*salary;
1500)f=3.0/100*salary;
月工资输入有误!
月工资={0},交纳党费={1}"
实验三:
3-1
namespace_3_1
inti,n,fac=1;
strings;
n=-1;
while(n<
0)
请输入非负整数n:
n=int.Parse(s);
//方法一:
for循环
for(i=1;
i<
=n;
i++)fac*=i;
For循环:
{0}!
={1}"
n,fac);
//方法二:
while语句循环
i=1;
fac=1;
while(i<
=n)fac*=i++;
while循环:
//方法三:
do...while语句循环
do
fac*=i;
i++;
}while(i<
=n);
do.....while循环:
3-2
namespace_3_2
intf1=1,f2=1,f3,num=2;
{0,5}\t{0,5}\t"
f1,f2);
f3=f1+f2;
while(f3<
=10000)
{0,5}\t"
f3);
num++;
if(num%5==0)Console.WriteLine();
f1=f2;
f2=f3;
3-3、鸡兔同笼问题
namespace_3_3
intc,r;
请输入总头数:
inth=int.Parse(s);
intf=1;
while(f%2!
=0)
请输入总脚数(必须是偶数):
f=int.Parse(s);
利用循环
boolsolution=false;
for(c=0;
c<
=h;
c++)
r=h-c;
if(2*c+4*r==f)
方法一:
鸡:
{0}只,兔:
{1}只"
c,r);
solution=true;
if(!
solution)Console.Write("
无解,请重新运行测试!
解方程
r=f/2-h;
c=h-r;
solution=false;
if(r>
=0&
c>
方法二:
c,r);
solution=true;
solution)Console.WriteLine("
3-4利用级数和求PI
运行结果:
namespaceConsolejishuhe3_4
floatn,t,pi;
ints;
pi=0;
t=1;
n=1;
s=1;
while(Math.Abs(t)>
=Math.Pow(10,-6))
pi+=t;
n+=2;
s=-s;
t=s/n;
pi*=4;
pi={0}"
pi);
3-5
namespaceConsoleG_3_6