C#程序设计实验报告2_第1页
C#程序设计实验报告2_第2页
C#程序设计实验报告2_第3页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

2、逾人半径:5自的半径为巧圆的周 =31-4159.圆的 =78.53975 球体的体积=392.£9875实验代码:using System;using System.Collecti on s.Ge neric;using System.L inq;using System.Text;n amespace _2_1class Programstatic void Main(string args)const double PI = 3.14159;double r, perimeter, area, volume;Console.Write ("请输入半径:”);Stri

3、ng s = Con sole.ReadL in e();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 );Con sole.ReadL in e();2

4、-2求三角形的周长和面积运行结果:was ra Jura Ju实验代码:using System;using System.Collecti on s.Ge neric;using System.L inq;using System.Text;n amespace _2_2 class Programstatic void Main(string args)double a, b, c, p, h, area;Console.Write(”请输入三角形的边 a:");stri ng s = Con sole.ReadL in e();a = double.Parse(s);Conso

5、le .Write ("请输入三角形的边 b:");s = Con sole.ReadL in e();b = double.Parse(s);Console.Write(”请输入三角形的边 c:");s = Con sole.ReadL in e();c = double.Parse(s);if (a > 0 && b > 0 && c > 0 && a + b > c && a + c > b && b + c > a)Console.Write

6、Line("三角形三边分别为: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(” 无法构成三角形!”);Con sole.ReadKey();2-3分段函数的实现nJx=5y=6 x=-5,y=6 x=5,y=6 x=5 u=G运行结果-990927699183129909276991f

7、i312-99092769918312实验代码:using System;using System.Collecti on s.Ge neric; using System.L inq;using System.Text;n amespace _2_3_2class Programstatic void Main(string args)double x, y;Con sole .Write ("请输入 x:");stri ng s = Con sole.ReadL in e();x = double.Parse(s);y = (x * x - 3 * x) / (x +

8、1) + 2 * Math.PI + Math.Si n( 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 = (x * x - 3 * x) / (x + 1) + 2 * Math.PI + Math.Si n(x);if(x<0)y=Math.Log(-5*x)+6*Math.Sqrt (Math .Abs (x)

9、+Math .Pow(Math.E,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 * Math.PI + Math.Si n(x);elsey=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);y=(x&g

10、t;=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",x,y);Co nsole .ReadKey ();2-4三个数比较大小运行结果:实验代码:using System;using System.Collecti on s.Ge neric;using System.L inq;using S

11、ystem.Text;n amespace _2_4class Programstatic void Main(string args)int a, b, c, a1, b1, c1, t, Nmax, Nmin, Nmid;Ran dom rMun = new Ran dom();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

12、= 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.Mi n( Math.Mi n(a, b), c);Nmid = a + b + c - Nmax - Nmi n;a = Nmi n; b = Nmid;a=0,b=1,c=2&qu

13、ot;,a,b,c);c = Nma x;Console.WriteLine("(方法二)升序值: Co nsole.ReadKey();2-5求解一元二次方程 运行结果:b C 1 系系茅无 K1A1A程 >请注i冃0 0 6认奈数b描荣相命实根为:1肓揃人筮数孔:1请糊入塞数b: -11匕労程右两个不等实根为 了和边、系数” L働系薮-1 输入亲薮c; 0-5程有商个不等实根为=0-S+0-S1和取弓实验代码:using System;using System.Collecti on s.Ge neric;using System.Text;n amespace Con s

14、ole2_5class Programstatic void Main(string args)double a, b, c, delta, x1, x2, realPart, imagePart;Console.Write("请输入系数 a:");String s = Con sole.ReadL in e();a = double.Parse(s);Console.Write(” 请输入系数 b:");s = Con sole.ReadL in e();b = double.Parse(s);Console.Write(” 请输入系数 c:");s

15、= Con sole.ReadL in e();c = double.Parse(s);if(a=0)if(b=O) Console.WriteLine("此方程无解!");else Console.WriteLine("此方程的解为:0",-c/b);elsedelta=b*b-4*a*c;if(delta>0)x1= (-b+Math.Sqrt(delta)/(2*a);x2=(-b-Math.Sqrt(delta)/(2*a);Console.WriteLine("此方程有两个不等实根:0 , 1",x1,x2);else

16、if(delta =0)Console.WriteLine("此方程有两个相等实根:0",-b/(2*a);elserealPart=-b/(2*a);imagePart=Math.Sqrt(-delta)/(2*a);Co nsole.WriteLi ne(”此方程有两个不等实根:0+1i 和 0-1i",realPart,imagePart);Co nsole.ReadKey();2-6switch语句实现多重分支运行结果:z fil«:/C:/VSS/2-6/2-6/hin实验代码:using System;using System.Collect

17、i on s.Ge neric; using System.Text;n amespace Con soleApplicati on1class Programstatic void Main(string args)int i;Console.Write(” 输入一个数字(17):");String s = Con sole.ReadL in e();i = in t.Parse(s);switch(i)case 1:Console.WriteLine("对应的星期为:星期一"); break;case 2:Console.WriteLine("对应的

18、星期为:星期二 ”); 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.W

19、riteLine("输入错误!”);break ;Co nsole.ReadKey();2-7分别使用if语句和switch语句实现多分支结构 方法一:运行结果(if语句)using System;using System.Collecti on s.Ge neric;using System.Text;n amespace Con sole2_7class Programstatic void Main(string args)int c; double f = 0;Console.Write("请输入有固定工资收入的党员的月工资:");int salary =

20、 in t.Parse(C on sole.ReadL in e();if (salary > 1500) c = 15;法二运行结果:else c = (salary - 1) / 100;switch (c)case 0:case 1:case 2:case 3:f = 0.5 / 100 * salary; break;case 6:case 7:f = 1.5 / 100 * salary; break;case 8:case 9:case 10:case 11:case 12:case 13:case 14:f = 2.0 / 100 * salary; break;case

21、15:f = 3.0 / 100 * salary; break;Console.WriteLine("月工资=0,交纳党费=1", salary, f); Co nsole.ReadKey();代码:(if语句)using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace_2_7_2class Programstatic void Main( string args)int c; double f = 0;Console .Write("请输

22、入有固定收入的党员的月工资:”);int salary = int .Parse( Console .ReadLine();if (salary > 0 && salary <= 400) f = 0.5 / 100 * salary;elseif(salary >400&& salary <= 600) f = 1.0/ 100*salary;elseif(salary >600&& salary <= 800) f = 1.5/ 100*salary;elseif(salary >800&&a

23、mp; salary <= 1500) f = 2.0 / 100* salary;elseif(salary >1500) f = 3.0 / 100 * salary;else Console .WriteLine("月工资输入有误!”);Console .WriteLine("月工资=0,交纳党费=1" , salary, f); Console .ReadKey();实验三:3-1运行结果:实验代码:using System;using System.Collections.Generic;using System.L inq;using Sy

24、stem.Text;n amespace _3_1class Programstatic void Main(string args)int i, n, fac = 1;stri ng s;n 二-1;while (n < 0)Console.Write(”请输入非负整数n:");s = Con sole.ReadL in e();n 二 in t.Parse(s);/方法一:for循环for (i = 1; i <= n; i+) fac *= i;Console.WriteLine(”For 循环:0! =1", n, fac);/方法二:while语句循环

25、i = 1; fac = 1;while (i <= n) fac *= i+;Console.WriteLine(”while 循环:0 ! =1", n, fac);/方法三:do.while语句循环i = 1; fac = 1;dofac *= i; i+; while (i <= n);=1", n, fac);Console.WriteLine(”do.while 循环:0 !Con sole.ReadKey();3-2运行结果:如 f£le = /C = /VSS/3-2/3-2/biiVDebuE/3-2-EM11235813Z13455

26、891442333776109971597259441816765实验代码:using System;using System.Collections.Generic;using System.L inq;using System.Text;n amespace _3_2 class Programstatic void Main(string args)int f1 = 1, f2 = 1, f3, num = 2;Console.Write("0,5t0,5t", f1, f2);f3 = f1 + f2;while (f3 <= 10000)Co nsole.W

27、rite("0,5t", f3);nu m+;if (num % 5 = 0) Con sole.WriteLi ne();fl = f2;f2 = f3;f3 = fl + f2;Con sole.ReadKey();3-3、鸡兔同笼问题运行结果:代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace_3_3class Programstatic void Main( string args)int c, r;Console .Write(

28、"请输入总头数:"); string s = Console .ReadLine(); int h = int .Parse(s);int f = 1;while (f % 2 != 0)Console .Write("请输入总脚数(必须是偶数):”);s =Console .ReadLine();f =int .Parse(s);/方法一:利用循环bool solution = false ;for (c = 0; c <= h; C+)r = h - c;if (2 * c + 4 * r = f)Console.WriteLine("方法一:

29、鸡:0只,兔:1只",c ,r ); solution =true ;if (!solution) Console .Write("方法一:无解,请重新运行测试!");/方法二:解方程r = f / 2 - h;c = h - r;solution =false ;if (r>=0&&c>=0)Console .WriteLine ("方法二:鸡:0只,兔:1只",c ,r);solution =true ;if (!solution) Console .WriteLine("方法二:无解,请重新运行测试!

30、”);Console .ReadKey ();3-4利用级数和求PI 运行结果:西 f il&:/C:/TSS/3-4/3'4/biii/pi=3.141594实验代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespaceConsolejishuhe3_4class Programstatic void Main( string args)float n, t,pi;int s;pi = 0; t = 1; n = 1; s = 1;while (

31、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;namespaceConsoleG_3_6class Programstatic void Main( string args)int m,

32、n, r, m1, n1;RandomrNum = new Random);m1 = rNum.Next(101);n1 = rNum.Next(101);,m1, n1);Console .WriteLine("整数 1=0,整数 2=1"if (m1 > n1)m = m1; n = n1;elsem = n1; n = m1;dor =m %n;m =n ;n =r ; while (r !=0);Console .WriteLine ("最大公约数=0,最小公约数=1" ,m ,m1 *n1 /m );Console .ReadKey ()

33、;3-6打印九九乘法表 运行结果:代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace_3_5class Programstatic void Main( string args)string s;/Console .WriteLine("九九乘法表”);for ( int i = 1; i <= 9; i+)s =""for ( int j = 1; j <= i; j+)s += (string .Format(

34、"0*1=2", i, j, i * j).PadRight(8);Console .WriteLine(s);/Console .WriteLine("");九九乘法表");Console .WriteLine("for ( int i = 1; i <= 9; i+)s += s.PadRight(8 * (i - 1) + 1);for ( int j = i ; j <= 9; j+)s += (string .Format( "0*1=2", i, j, i * j).PadRight(8);

35、Console .WriteLine(s);Console .ReadKey();3-7运行结果:file: /C = /TS S/3- 7/ 3- 7/bin/Debug/3- 7. EJE刁法一:IF囱间所有的素数为*235711131719232931374143475359616?717983899711001的所有素数为=C0J.5J - m£9- m0- 5J,m£Wi, 53SJ .nCS-'EJ -m-CQl-5.0,5- inB-bz , nt<0,E>j.itiC 0,5, m<0,亠5> ,11<05> ,

36、in-0,5y05>Pra<0,51- mt BSJmCa. 5j -n实验代码:using System;using System.Collectio ns.Ge neric; using Syste m. Li nq;using System.Text;n amespace _3_7class Programstatic void Main( stri ng args)int m, k, i, num = 0;方法一:利用for循环和break语句Console.WriteLine("方法一:1T00 间所有的素数为:"); for (m = 2; m <= 100; m+)k = (in t)(Math.Sqrt(m

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论