实验报告二
姓 名 专业 软件工程 课程名C#程序设计 称 一、实验名称: 实验2 二、实验目的: 掌握使用命令行开发简单的C#应用程序 掌握使用Visual Studio编写控制台应用程序 掌握Visual Studio环境下程序的跟踪调试 了解Visual Studio在线帮助的使用 掌握应用程序命令行参数的使用 三、实验内容及要求 利用ADO.NET完成数据的增、删、改、查 四、实验材料、工具、或软件 Windows XP Professional SP3 Visual Studio 2005 五、实验步骤、结果(或记录) 实验二:程序流程控制 2-1输入半径,求对应的圆的周长、面积、对应球体的体积。 运行结果: 实验代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _2_1 { class Program { static void Main(string[] args) { const double PI = 3.14159; double r, perimeter, area, volume; Console.Write (\请输入半径:\ String s = Console.ReadLine(); r = double.Parse(s); Console.WriteLine(\圆的半径为={0}\ perimeter = 2 * PI * r; area = PI * r * r; volume = 4 / 3 * PI * Math.Pow(r, 3); Console.WriteLine(\圆的周长为={0},面积为={1}\ Console.WriteLine(\球体的体积={0}\ Console.ReadLine(); } } } 2-2求三角形的周长和面积 运行结果: 实验代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _2_2 { class Program { static void Main(string[] args) { double a, b, c, p, h, area; Console.Write(\请输入三角形的边a:\ string s = Console.ReadLine(); a = double.Parse(s); Console .Write (\请输入三角形的边b:\ s = Console.ReadLine(); b = double.Parse(s); Console.Write(\请输入三角形的边c:\ s = Console.ReadLine(); c = double.Parse(s); if (a > 0 && b > 0 && c > 0 && a + b > c && a + c > b && b + c > a) { Console.WriteLine(\三角形三边分别为:a={0},b={1},c={2}\ p = a + b + c; h = p / 2; area = Math.Sqrt(h * (h - a) * (h - b) * (h - c)); Console.WriteLine(\三角形的周长={0},面积为={1}\ } else Console.WriteLine(\无法构成三角形!\ Console.ReadKey(); } } } 2-3分段函数的实现 运行结果 实验代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _2_3_2 { class Program { static void Main(string[] args) { double x, y; Console .Write (\请输入x:\ string s = Console.ReadLine(); 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}\ if(x>=0) 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}\ if(x>=0) y = (x * x - 3 * x) / (x + 1) + 2 * Math.PI + Math.Sin(x); else 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}\ 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 .WriteLine (\方法四:x={0},y={1}\ Console .ReadKey (); } } } 2-4三个数比较大小 运行结果: 实验代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _2__4 { class Program { static void Main(string[] args) { int a, b, c, a1, b1, c1, t, Nmax, Nmin, Nmid; Random rMun = new Random(); a = rMun.Next(101); b = rMun.Next(101); c= rMun.Next(101); Console.WriteLine(\原始值:a={0},b={1},c={2}\ a1 = a; b1 = b; c1 = c; if (a > b) { t = a; a = b; b = t; } if (a > c) { t = a; a = c; c = t; } if (b > c) { t = b; b = c; c = t; } Console.WriteLine((\方法一)升序值:a={0},b={1},c={2}\ 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; Console.WriteLine(\方法二)升序值:a={0},b={1},c={2}\