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

C#程序设计实验报告

实验报告二姓名专业软件工程课程名C#程序设计称一、实验名称:实验2二、实验目的:掌握使用命令行开发简单的C#应用程序掌握使用Visual Studio编写控制台应用程序掌握Visual Studio环境下程序的跟踪调试了解Visual Studio在线帮助的使用掌握应用程序命令行参数的使用三、实验内容及要求利用完成数据的增、删、改、查四、实验材料、工具、或软件Windows XP Professional SP3Visual Studio 2005五、实验步骤、结果(或记录)实验二:程序流程控制2-1输入半径,求对应的圆的周长、面积、对应球体的体积。

运行结果:实验代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _2_1{{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}",r);perimeter = 2 * PI * r;area = PI * r * r;volume = 4 / 3 * PI * Math.Pow(r, 3);Console.WriteLine("圆的周长为={0},面积为={1}",perimeter ,area );Console.WriteLine("球体的体积={0}",volume );Console.ReadLine();}}}2-2求三角形的周长和面积运行结果:实验代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _2_2{{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}",a,b,c);p = a + b + c;h = p / 2;area = Math.Sqrt(h * (h - a) * (h - b) * (h - c));Console.WriteLine("三角形的周长={0},面积为={1}",p,area);}else Console.WriteLine("无法构成三角形!");Console.ReadKey();}}}2-3分段函数的实现运行结果实验代码:using System;using System.Collections.Generic;实验代码: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}",a,b,c);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,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;Console.WriteLine("(方法二)升序值:a={0},b={1},c={2}",a,b,c);Console.ReadKey();}}}2-5求解一元二次方程运行结果:实验代码:using System;using System.Collections.Generic;using System.Text;namespace Console2_5{class Program{static void Main(string[] args){double a, b, c, delta, x1, x2, realPart, imagePart;Console.Write("请输入系数a:");运行结果:实验代码:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication1{class Program{static void Main(string[] args){int i;Console.Write("输入一个数字(1~7):");String s = Console.ReadLine();i = int.Parse(s);switch(i){case 1:Console.WriteLine("对应的星期为:星期一");break;case 2:Console.WriteLine("对应的星期为:星期二");break;case 3:Console.WriteLine("对应的星期为:星期三");break;case 4:Console.WriteLine("对应的星期为:星期四");break;case 5:Console.WriteLine("对应的星期为:星期五");break;case 6:Console.WriteLine("对应的星期为:星期六");break;case 7:Console.WriteLine("对应的星期为:星期日");break;default:Console.WriteLine("输入错误!");break ;}Console.ReadKey();}}}2-7分别使用if语句和switch语句实现多分支结构方法一:运行结果(if语句)using System;using System.Collections.Generic;using System.Text;namespace Console2_7{class Program{static void Main(string[] args){int c; double f = 0;Console.Write("请输入有固定工资收入的党员的月工资:");int salary = int.Parse(Console.ReadLine());if (salary > 1500) c = 15;}实验三:3-1运行结果:实验代码:using System;using System.Collections.Generic; using System.Linq;using System.Text;namespace _3_1{class Program{static void Main(string[] args){int i, n, fac = 1;fac *= i; i++;} while (i <= n);Console.WriteLine(" do.....while循环:{0}!={1}", n, fac);Console.ReadKey();}}}3-2运行结果:实验代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;实验代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Consolejishuhe3_4{class Program{static void Main(string[] args){float n, t,pi;int s;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;Console.WriteLine("pi={0}", pi);Console.ReadKey();}}}3-5运行结果:实验代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _3_7{class Program{static void Main(string[] args){int m, k, i, num = 0;//方法一:利用for循环和break语句Console.WriteLine("方法一:1~100间所有的素数为:");for (m = 2; m <= 100; m++){k = (int)(Math.Sqrt(m));for (i = 2; i <= k; i++)if (m % i == 0) break;if (i == k + 1){Console.Write("{0,5}", m);num++;if (num % 10 == 0) Console.WriteLine();}}//方法二:利用while循环和boollean变量Console.WriteLine("\n方法二:1~100间的所有素数为:");num = 0;for (m = 2; m <= 100; m++){bool flag = true;k = (int)(Math.Sqrt(m));i = 2;while (i <= k && flag == true){if (m % i == 0) flag = false;else i++;}if (flag == true){Console.Write("{0,5},m");num++;if (num % 10 == 0) Console.WriteLine();}}Console.ReadKey();}}}3-9运行结果:实验代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _3_8{class Program{static void Main(string[] args)备注:实验报告的命名格式为:学号--实验序号。

相关主题