当前位置:文档之家› C#程序设计实验报告2

C#程序设计实验报告2

实验报告二
运行结果:
实验代码:
using System;
using ;
using ;
namespace _2_1
{
class Program
{
static void Main(string[] args)
{
const double PI = ;
double r, perimeter, area, volume; ("请输入半径:");
String s = ();
r = (s);
("圆的半径为={0}",r);
perimeter = 2 * PI * r;
area = PI * r * r;
volume = 4 / 3 * PI * (r, 3);
("圆的周长为={0},面积为={1}",perimeter ,area ); ("球体的体积={0}",volume );
();
}
}
}
2-2求三角形的周长和面积
运行结果:
实验代码:
using System;
using ;
using ;
namespace _2_2
{
class Program
{
static void Main(string[] args)
}
2-3分段函数的实现
运行结果
实验代码:
using System;
using ;
using ;
namespace _2_3_2
{
class Program
{
static void Main(string[] args)
{
double x, y;
Console .Write ("请输入x:");
string s = ();
x = (s);
y = (x * x - 3 * x) / (x + 1) + 2 * + (x);
if(x<0)
y=(-5*x)+6*Math .Sqrt (Math .Abs (x)+Math .Pow ,4)-Math .Pow (x+1,3));
Console .WriteLine ("方法一:x={0},y={1}",x,y);
if(x>=0)
y = (x * x - 3 * x) / (x + 1) + 2 * + (x);
if(x<0)
y=(-5*x)+6*Math .Sqrt (Math .Abs (x)+Math .Pow ,4)-Math .Pow (x+1,3)); Console .WriteLine ("方法二:x={0},y={1}",x,y);
if(x>=0)
y = (x * x - 3 * x) / (x + 1) + 2 * + (x);
else
y=(-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);
y=(x>=0)(x * x - 3 * x) / (x + 1) + 2 * + (x):(-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);
Console .ReadKey ();
}
}
}
2-4三个数比较大小
运行结果:
实验代码:
using System;
using ;
namespace Console2_5
{
class Program
{
static void Main(string[] args)
{
double a, b, c, delta, x1, x2, realPart, imagePart; ("请输入系数a:");
String s = ();
a = (s);
("请输入系数b:");
s = ();
imagePart=(-delta)/(2*a);
("此方程有两个不等实根:{0}+{1}i和{0}-{1}i",realPart,imagePart);
}
}
}
();
}
}
}
2-6switch语句实现多重分支
运行结果:
实验代码:
using System;
using ;
namespace ConsoleApplication1
{
class Program
case 6:
("对应的星期为:星期六"); break;
case 7:
("对应的星期为:星期日"); break;
default:
("输入错误!");
break ;
}
();
}
}
}
2-7分别使用if语句和switch语句实现多分支结构方法一:
运行结果(if语句)
using System;
using ;
namespace Console2_7
else if (salary > 600 && salary <= 800) f = / 100 * salary;
else if (salary > 800 && salary <= 1500) f = / 100 * salary;
else if (salary > 1500) f = / 100 * salary;
else ("月工资输入有误!");
("月工资={0},交纳党费={1}", salary, f);
();
}
}
}
实验三:
3-1
运行结果:
实验代码:
using System;
using ;
using ;
namespace _3_1
{
();
}
}
}
3-2
运行结果:
实验代码:
using System;
using ;
using ;
namespace _3_2
{
class Program
{
static void Main(string[] args)
while (f % 2 != 0)
{
("请输入总脚数(必须是偶数):");
s = ();
f = (s);
}
riteLine ("方法二:鸡:{0}只,兔:{1}只",c ,r);
solution =true ;
}
if (!solution) ("方法二:无解,请重新运行测试!");
Console .ReadKey ();
}
}
}
3-4利用级数和求PI
运行结果:
实验代码:
using System;
(s);
}
adRight(8); }
(s);
}
();
}
}
}
3-7
运行结果:
实验代码:
using System; using ;
using ; namespace _3_7
实验代码:
using System;
using ;
using ;
namespace _3_8
{
class Program
{
static void Main(string[] args) {
int i, j, k;
("请输入被除数:");
try
{
String s = ();
i = (s);
("请输入除数:");
s = ();
j = (s);
k = i / j;
备注:实验报告的命名格式为:学号-姓名-实验序号。

如3-刘闽-实验一。

相关主题