Microsoft Visual C# 2010 常见各类控制台应用程序.doc_第1页
Microsoft Visual C# 2010 常见各类控制台应用程序.doc_第2页
Microsoft Visual C# 2010 常见各类控制台应用程序.doc_第3页
Microsoft Visual C# 2010 常见各类控制台应用程序.doc_第4页
Microsoft Visual C# 2010 常见各类控制台应用程序.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

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

文档简介

实验2:控制台应用程序练习一、实验目的:、熟悉MicrosoftVisualC#2010常见各类控制台应用程序二、实验内容:1、 数据类型转换/类型转换,包括隐式转换和显式转换using System;class TypeTrans public static void Main() /隐式转换,int到long和float到double Console.WriteLine(隐式转换:); int iValue = int.MaxValue; long lValue = iValue; Console.WriteLine(int 0-long 1, iValue, lValue); float fValue = float.MaxValue; double dValue = fValue; Console.WriteLine(float 0-double 1, fValue, dValue); Console.WriteLine(显式转换:); int iValue2=(int)lValue;/在表示范围之内,转换成功 Console.WriteLine(long 0-int 1,lValue,iValue2); lValue=lValue+1; int iValue3 = (int)lValue;/不在表示范围之内,转换不成功 Console.WriteLine(long 0-int 1, lValue, iValue3); /显式转换溢出检查机制 Console.WriteLine(不使用溢出检查的溢出转换:); byte bValue=(byte)iValue; Console.WriteLine(int 0-byte 1,iValue,bValue); Console.WriteLine(使用溢出检查后的溢出转换:); byte bValue2=checked(byte)iValue);/此处会抛出异常 2、 枚举类型与结构体类型/枚举类型与结构体类型using System;class ID /定义枚举类型 public enum sex male, female ; /定义电话薄的结构体类型 public struct TelBook public string name; public sex sex; public string number; /每一行打印一位用户的电话本信息 public static void TelPrint(TelBook Someone) Console.WriteLine(S + t); Console.WriteLine(Someone.sex + t); Console.WriteLine(Someone.number + tn); public static void Main() TelBook Joey, Rose; J = Joey; Joey.sex = sex.male; Joey.number = 1234567; R = Rose; Rose.sex = sex.female; Rose.number = 7654321; TelPrint(Joey); TelPrint(Rose); Console.ReadKey(); 3、 数组/数组/声明数据时,方括号必须紧跟在类型后面,而不是在标识符后面/数组的大小不是其类型的一部分,声明数组后,可以用任意数组对它赋值,不用管数组长度如何using System;class ArrayApp public static void Main() Console.WriteLine(一维数组演示:); int arr1=new int8,13,36,30,9,23,47,81; int odd=0; int even=0; foreach(int i in arr1) if(i%2=0) even+; else odd+; Console.WriteLine(共有0个偶数,1个奇数,even,odd); Console.WriteLine(二维数组演示:); int , arr2=new int3,44,2,1,7,1,5,4,9,1,3,1,4; for(int r=0;r3;r+) for(int c=0;c4;c+) Console.Write(arr2r,c+t); Console.WriteLine(); Console.ReadKey(); 4、 类举例/从定义上讲,类是一种数据结构,可包含数据成员、函数成员以及其它的嵌套类型/其中的数据成员有常量、字段/函数成员类型有方法、属性、事件、索引器、运算符、实例构造函数、静态构造函数和析构函数等using System;public class Desk protected int length; protected int width; protected int height; public Desk() length = 0; width = 0; height = 0; public void SetInfo(int len, int wid, int hei) length = len; width = wid; height = hei; public void ShowInfo() Console.WriteLine(长0t宽1t高2t, length, width, height); public class Furniture : Desk private int price; public Furniture() price = 0; public void SetInfo(int len, int wid, int hei, int pri) length = len; width = wid; height = hei; price = pri; public void SetPri(int pri) price = pri; public new void ShowInfo() Console.WriteLine(长=0t宽=1t高=2t价格=3, length, width, height, price); class Test public static void Main() Desk desk = new Desk(); desk.ShowInfo(); desk.SetInfo(10, 20, 30); desk.ShowInfo(); Console.ReadKey(); Furniture furniture = new Furniture(); furniture.ShowInfo(); furniture.SetPri(288); furniture.ShowInfo(); furniture.SetInfo(100, 200, 300, 400); furniture.ShowInfo(); Console.ReadKey(); 5、 位运算/位运算using System;class BitCal public static void Main() int oper1=26; int oper2=52; int andres=oper1&oper2; Console.WriteLine(0:x8t与t1:x8t=t2:x8, oper1, oper2, andres); int orres = oper1 | oper2; Console.WriteLine(0:x8t或t1:x8t=t2:x8, oper1, oper2, orres); int xorres = oper1 oper2; Console.WriteLine(0:x8t异或t1:x8t=t2:x8, oper1, oper2, xorres); int notres = oper1; Console.WriteLine(0:x8t的反t=t1:x8, oper1, notres); int leftres = oper1 1; Console.WriteLine(0:x8t右移一位的结果t=t1:x8, oper1, rightres); Console.ReadKey(); 6、 操作符重载/操作符重载using System;class Vector private double xvector; private double yvector; public Vector(double x, double y) xvector = x; yvector = y; public double GetLength() double length = Math.Sqrt(xvector * xvector + yvector * yvector); return length; public static bool operator =(Vector x, Vector y) return (x.xvector = y.xvector) & (x.yvector = y.yvector); public static bool operator !=(Vector x, Vector y) return (!(x = y); public static bool operator (Vector x, Vector y) return x.GetLength() y.GetLength(); public static bool operator (Vector x, Vector y) return x.GetLength() =(Vector x, Vector y) return x.GetLength() = y.GetLength(); public static bool operator =(Vector x, Vector y) return x.GetLength() vec2); Console.WriteLine(向量1tt向量2:t0, vec1 = vec2); Console.WriteLine(向量1tt向量2:t0, vec1 vec2); Console.WriteLine(向量1tt向量2:t0, vec1 = vec2); Console.WriteLine(向量1t!t向量2:t0, vec1 != vec2); Console.ReadKey(); 7、 虚方法/虚方法/如果在声明中包含了修饰符virtual,那么该方法就是一个虚方法。/虚方法的实现会随子类的改变而改变,子类虚方法的实现过程叫做重载/虚方法与其它方法的差别在于虚方法只有在程序运行中才能确定具体的实现方法,而其它方法在编译时就可以确定using System;public class Graph protected double ParaVal; protected double pi = 3.1415926; public Graph(double Para) ParaVal = Para; public virtual double Area() return ParaVal; public class Square : Graph public Square(double Para) : base(Para) public override double Area() return ParaVal * ParaVal; public class EqTriangle:Graph public EqTriangle(double Para):base(Para) public override double Area() return ParaVal*ParaVal*Math.Sin(pi/3)/2; public class Circle : Graph public Circle(double Para) : base(Para) public override double Area() return pi * ParaVal * ParaVal; class Test public static void Main() double length = 6; Graph init = new Graph(length); Graph squa = new Square(length); Graph tria = new EqTriangle(length); Graph circle = new Circle(length); Console.WriteLine(原始图形面积:t0, init.Area(); Console.WriteLine(正方形面积:t0, squa.Area(); Console.WriteLine(等边三角形面积:t0, tria.Area(); Console.WriteLine(圆形面积:t0, circle.Area(); Console.ReadKey(); 8、 链表/链表/C#没有指针,它是用引用来实现这样的功能,可以定义如下类来实现链表中的节点:/ public class Node/ / public Object data;/ public Node next;/ using System;using System.IO;/构成链表的节点定义public class Node public Object data; public Node next; public Node(object d) data = d; next = null; public class List private Node Head = null; private Node Tail = null; private Node Pointer = null; private int Length = 0; /清空整个链表 public void deleteAll() Head = null; Tail = null; Pointer = null; Length = 0; /复位链表,使第一个节点成为当前节点 public void reset() Pointer = null; /判断链表是否为空 public bool isEmpty() return (Length = 0); /判断当前节点是否为最后一个节点 public bool isEnd() if (Length = 0) throw new System.Exception(); else if (Length = 1) return true; else return (cursor() = Tail); /返回当前节点的下一个节点的值,并使其成为当前节点 public Object nextNode() if (Length = 1) throw new System.Exception(); else if (Length = 0) throw new System.Exception(); else Node temp = cursor(); Pointer = temp; if (temp != Tail) return (temp.next.data); else throw new System.Exception(); /返回当前节点的值 public Object currentNode() Node temp = cursor(); return temp.data; /在当前节点前插入一个节点,并使其成为当前节点 public void insert(Object d) Node e = new Node(d); if (Length = 0) Tail = e; Head = e; else Node temp = cursor(); e.next = temp; if (Pointer = null) Head = e; else Pointer.next = e; Length+; /返回链表大小 public int size() return Length; /删除当前结点,并将下一个节点置为当前节点 /如果删除的是最后一个节点,则将第一个节点置为当前节点 public Object remove() Object temp; if (Length = 0) throw new System.Exception(); else if (Length = 1) temp = Head.data; deleteAll(); else Node cur = cursor(); temp = cur.data; if (cur = Head) Head = cur.next; else if (cur = Tail) Pointer.next = null; Tail = Pointer; reset(); else Pointer.next = cur.next; Length-; return temp; /返回当前节点指针 private Node cursor() if (Head = null) throw new System.Exception(); else if (Pointer = null) return Head; else return Pointer.next; /链表简单应用举例 public static void Main() List a = new List(); for (int i = 1; i = 10; i+) a.insert(new IntPtr(i); Console.WriteLine(a.currentNode(); while (!a.isEnd() Console.WriteLine(a.nextNode(); a.reset(); while (!a.isEnd() a.remove(); a.remove(); a.reset(); if (a.isEmpty() Console.WriteLine(链表中无节点!); Console.WriteLine(按回车键退出.); try Console.Read(); catch (IOException e) 9、 回文数/回文数,即顺读和倒读结果相同using System;class Palindrome public static void Main() int num; string x; int sfjx =1; while (sfjx !=0) Console.WriteLine(请输入一下正整数以判断其是否为回文数:); x = Console.ReadLine(); if (x.Length = 0) num = 0; else num = int.Parse(x); Show(num); Console.WriteLine(是否继续(0-结束,其它-继续)?); x = Console.ReadLine(); if (x.Length = 0) sfjx = 1; else sfjx=int.Parse(x); Console.ReadKey(); public static bool isPalin(int val) string s; s = val.ToString(); char x, y; bool flag = true; for (int i = 0; (i s.Length) & flag; i+) x = si; y = ss.Length-1-i; if (x != y) flag = false; return flag; public static void Show(int val) if (isPalin(val) Console.WriteLine(0t是回文数!, val); else Console.WriteLine(0t不是回文数!, val); 10、 汉诺塔/汉诺塔using Sy

温馨提示

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

最新文档

评论

0/150

提交评论