天津理工大学C#试验一.doc
精品文档,仅供学习与交流,如有侵权请联系网站删除实验报告 学院(系)名称:计算机科学与工程学院姓名*学号*专业计算机科学与技术班级2015级 班实验项目实验一:C#的数据类型和控制结构课程名称. NET程序设计课程代码0667066实验时间2017年3月30日 12:45-15:45实验地点7-215批改意见成绩教师签字: 一、 实验目的(1) 熟悉Visual Studio 2010开发环境,掌握C#数据类型;(2) 掌握分支语句、循环语句、跳转语句及异常处理语句;(3) 掌握数组的使用;(4) 掌握面向对象的编程思想:类、接口等的使用;(5) 字符串的常用方法。二、 实验环境n 开发环境:PC机,Windows 7操作系统;n 开发工具:Visual Studio 2010以上。三、 实验要求(1) 认真完成实验内容,编写程序代码;(2) 要求所有程序有测试数据,并输出实验的正确结果;(3) 书写并提交实验报告。四、 实验内容(将代码与运行结果展示在此)1、using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 实验1.1 class Program static void Main(string args) Console.WriteLine("第̨²一°?个?C#程¨¬序¨°!ê?!ê?"); Console.ReadLine(); 2、using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 实验1.2 class Program static void Main(string args) int a = 345; float b = 15.3f; bool c = true; char d = 't' string e = "12345" Console.WriteLine("int a = " + a); Console.WriteLine("float b = " + b); Console.WriteLine("bool c = " + c); Console.WriteLine("char d = " + d); Console.WriteLine("string e = " + e); /转Áa换? Console.WriteLine("n类¤¨¤型¨ª转Áa换?n"); Console.WriteLine("n整?型¨ª转Áa化¡¥为a长¡è整?型¨ª:êon"); long l = a; Console.WriteLine("long l = a = " + l); Console.WriteLine("n字Á?符¤?串ä?转Áa化¡¥为a整?型¨ª:êoParse()n"); int transform = int.Parse(e); Console.WriteLine("int transform = e = " + transform); Console.WriteLine("n字Á?符¤?型¨ª转Áa化¡¥为a整?型¨ª:êon"); char h = 'r' int chartoint = h; Console.WriteLine("r的Ì?ASCLL码?为a:êo" + chartoint); /Convert Console.WriteLine("nConvert类¤¨¤:四?舍¦¨¢六¢¨´入¨?五?取¨?偶?"); Console.WriteLine("10.4 = " + Convert.ToInt32(10.4); Console.WriteLine("10.6 = " + Convert.ToInt32(10.6); Console.WriteLine("10.5 = " + Convert.ToInt32(10.5); Console.WriteLine("11.5 = " + Convert.ToInt32(11.5); /显?式º?转Áa换? Console.WriteLine("显?式º?转Áa换?:êo浮?点Ì?型¨ª-整?型¨ª"); double d1 = 10.4, d2 = 10.5, d3 = 10.6; Console.WriteLine("10.4 = " + (int)d1); Console.WriteLine("10.5 = " + (int)d2); Console.WriteLine("10.6 = " + (int)d3); 3、using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 实º¦Ì验¨¦1._3 class Program public static void swamp(ref int x,ref int y) int temp = x; x = y; y = temp; static void Main(string args) int i = 1; int sum1 = 0; while (i <= 100) sum1 += i; i+; Console.WriteLine("1-100所¨´有®D整?数ºy和¨ª为a:" + sum1); int j = 0; int sum2 = 0; while (j < 100) j+; if (j % 2 = 0) continue; sum2 += j; Console.WriteLine("1-100所¨´有®D奇?数ºy和¨ª为a:êo" + sum2); Console.WriteLine("nnswitch()算?成¨¦绩¡§"); int Grade = int.Parse(Console.ReadLine(); int gg = (int)(Grade / 10); switch (gg) case 10: case 9: Console.WriteLine("优®?秀?"); break; case 8: Console.WriteLine("良¢?好?"); break; case 7: Console.WriteLine("中D等̨¨"); break; case 6: Console.WriteLine("及¡ã格?"); break; default: Console.WriteLine("不?及¡ã格?"); break; Console.WriteLine("nntry-catch-finally:"); int bDiv = int.Parse(Console.ReadLine(); int Div = int.Parse(Console.ReadLine(); int result = 0; try result = bDiv / Div; Console.WriteLine(result); catch (Exception ex) Console.WriteLine("0不?能¨¹做Á?除y数ºy!ê?" + ex); Console.WriteLine("nn引°y用®?类¤¨¤型¨ª:êo"); Console.WriteLine("1、¡é数ºy组Á¨¦引°y用®?"); int arr = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ; Console.WriteLine("处ä|理¤¨ª之?前¡ã的Ì?数ºy组Á¨¦:êo"); for (int m = 0; m < arr.Length; m+) Console.Write(arrm + " "); for (int k = 0; k < arr.Length; k+) if (arrk % 7 = 0) arrk = 0; Console.WriteLine("n处ä|理¤¨ª之?后¨®的Ì?数ºy组Á¨¦为a:êo"); for (int n = 0; n < arr.Length; n+) Console.Write(arrn + " "); Console.WriteLine("n2、¡éref引°y用®?:êo"); int a = 13; int b = 18; Console.WriteLine("a = " + a + ",b = " + b); Console.WriteLine("引°y用®?交?换?a和¨ªb的Ì?值¦Ì:êo"); swamp(ref a, ref b); Console.WriteLine("交?换?后¨®:êoa = " + a + ",b = " + b); 4、using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 实º¦Ì验¨¦1._4 class MainClass public interface Body float area(); class Cube : Body public Cube(float x1) a = x1; public float a; void setA(float x) a = x; public float area() return 6 * a * a; class Ball : Body public Ball(float r1) r = r1; float r; void setR(int r1) r = r1; public float area() return (float)(4 * 3.14 * r * r); public static float getArea(Body b) return b.area(); static void Main(string args) Cube c1 = new Cube(2); Ball b1 = new Ball(3); Console.WriteLine("Cube.area = " + getArea(c1); Console.WriteLine("Ball.area = " + getArea(b1); 5、using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 试º?验¨¦1._5 class Program public static int add(int a, int b) return a + b; public static int add(int a, int b,int c) return a + b + c; public static float add(float a, float b) return a + b; public static double add(double a, double b) return a + b; static void Main(string args) int i = 1; int j = 2; int k = 3; float m = 2.5f; float n = 3.7f; double r = 5.0569; double s = 8.2365; Console.WriteLine("0",add(i, j); Console.WriteLine("0", add(i, j,k); Console.WriteLine("0", add(m, n); Console.WriteLine("0", add(r, s); 6、using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 实º¦Ì验¨¦1._6 class Program static void Main(string args) /Contains string s = "葫¨´芦?娃ªT,ê?葫¨´芦?娃ªT,ê?一°?根¨´藤¬¨´上¦?七?朵?花¡§" bool b = s.Contains("葫¨´芦?娃ªT"); Console.WriteLine(b); bool c = s.Contains("");/空?字Á?符¤?串ä?也°2返¤¦Ì回?True Console.WriteLine(c); bool d = s.Contains("哪?吒?"); Console.WriteLine(d); /Split string data = "2017-4-16" string t = data.Split('-'); foreach(string str in t) Console.WriteLine(str); /join string time = string.Join("/",t); Console.WriteLine(time); string s1 = "猴?哥?," string s2 = "你?真?了¢?不?得Ì?" Console.WriteLine(string.Concat(s1, s1, s2); /IndexOf Console.WriteLine(data.IndexOf("4"); /Copy string s3 = "天¬¨¬苍?苍?,ê?野°¡ã茫¡ê茫¡ê,ê?风¤?吹ä¦Ì草Y低̨ª见?牛¡ê羊¨°" string d4 = string.Copy(s3); Console.WriteLine(d4); /Clone string s4 = "猴?哥?,ê?猴?哥?,ê?你?真?了¢?不?得Ì?" object s5 = s4.Clone(); Console.WriteLine(s5.ToString(); 五、 心得体会自从上了大学也学了几门计算机语言了,感觉基础操作大同小异。C#前几章的内容和其他语言也没太大差别,其中的一些方法记住就好了。只不过时间久了不用会忘掉,拿起来看看就会了。C#的过人之处应该就是后边几章吧,记得之前学前端,各种界面都是用代码画出来的,C#中只要添加各种元素就好了,真的好方便。虽然是一门选修,但是老师讲的特别好,希望跟着老师能学到点东西,以后不会丢老师的脸,不丢理工的脸。六、【精品文档】第 12 页