c#程序设计 第二章练习
例题:
设计一个控制台应用程序说明类型转换的应用:
using System; namespace proj2_1 { class Program { static void Main(string[] args) { int i = 65, i1, i2; double d = 66.3456, d1, d2; char c = 'A', c1, c2; Console.WriteLine("i={0:d5},d={1:f},c={2}", i, d, c); i1 = (int)d; //强制类型转换 d1 = i; //隐式类型转换 c1 = (char)i; //强制类型转换 Console.WriteLine("i1={0:d5},d1={1:f},c1={2}", i1, d1, c1); i2 = c; //隐式类型转换 d2 = (int)d; //强制类型转换,转换成整数后再隐式转换为double类型 c2 = (char)d; //强制类型转换 Console.WriteLine("i2={0:d5},d2={1:f},c2={2}", i2, d2, c2); } } }

设计一个控制台应用程序说明结构类型的应用:
using System; namespace proj2_2 { class Program { struct Student //结构类型声明应放在Main函数的外面 { public int xh; //学号 public string xm; //姓名 public string xb; //性别 public int nl; //年龄 public string bh; //班号 }; static void Main(string[] args) { Student s1,s2; //定义两个结构类型变量 //Student s1 = new Student(); //Student s2 = new Student(); s1.xh = 101; s1.xm = "李明"; s1.xb = "男"; s1.nl = 20; s1.bh = "07001"; Console.WriteLine("学号:{0},姓名:{1},性别:{2},年龄:{3},班号:{4}", s1.xh, s1.xm, s1.xb, s1.nl, s1.bh); s2 = s1; //将结构变量s1赋给s2 s2.xh = 108; s2.xm = "王华"; Console.WriteLine("学号:{0},姓名:{1},性别:{2},年龄:{3},班号:{4}", s2.xh, s2.xm, s2.xb, s2.nl, s2.bh); Console.WriteLine("学号:{0},姓名:{1},性别:{2},年龄:{3},班号:{4}", s1.xh, s1.xm, s1.xb, s1.nl, s1.bh); //Console.WriteLine(typeof(Student)); } } }

设计一个控制台程序说明枚举类型的应用:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace proj2_3 { class Program { enum Color { Red=5, Green, Blue, White=1, Black } //类型声明应放在Main函数的外面 static void Main(string[] args) { Color c1,c2,c3; Console.WriteLine("Red={0},Green={1},Blue={2},White={3},Black={4}",Color.Red,Color.Green,Color.Blue,Color.White, Color.Black); Console.WriteLine("Red={0},Green={1},Blue={2},White={3},Black={4}",(int)Color.Red,(int)Color.Green, (int)Color.Blue,(int)Color.White,(int)Color.Black); c1 = Color.Red; c2 = c1 + 1; c3 = c2 + 1; Console.WriteLine("c1={0},c2={1},c3={2}", c1, c2,c3); Console.WriteLine("c1={0},c2={1},c3={2}", (int)c1, (int)c2,(int)c3); } } }

设计一个控制台程序说明为运算符的应用:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace proj2_4 { class Program { static void Main(string[] args) { byte b1, b2, b3; b1 = 10; b2 = (byte)~b1; //~b1的结果为int,需强制转换成byte类型 Console.WriteLine(b2); b3 = (byte)(b1 << 2); //b1<<b2结果为int类型,需强制转换成byte类型 Console.WriteLine(b3); b1 = 3; b2 = 6; b3 = (byte)(b1 & b2); //b1&b2结果为int类型,需强制转换成byte类型 Console.WriteLine(b3); b3 = (byte)(b1 ^ b2); //b1^b2结果为int类型,需强制转换成byte类型 Console.WriteLine(b3); b3 = (byte)(b1 | b2); //b1|b2结果为int类型,需强制转换成byte类型 Console.WriteLine(b3); } } }

设计一个控制台程序输出常用数据类型所占的字节数:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace proj2_5 { class Program { static void Main(string[] args) { Console.WriteLine("byte类型所占字节数:{0}", sizeof(byte)); Console.WriteLine("char类型所占字节数:{0}", sizeof(char)); Console.WriteLine("int类型所占字节数:{0}", sizeof(int)); Console.WriteLine("float类型所占字节数:{0}", sizeof(float)); Console.WriteLine("double类型所占字节数:{0}", sizeof(double)); Console.WriteLine("decimal类型所占字节数:{0}", sizeof(decimal)); } } }

设计一个控制台程序求用户输入的子串在主串中的位置:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace proj2_7 { class Program { static void Main(string[] args) { String mstr, sstr; Console.Write("输入主串:"); mstr = Console.ReadLine(); Console.Write("输入子串:"); sstr = Console.ReadLine(); Console.WriteLine("主串长度={0},子串长度={1}", mstr.Length, sstr.Length); if (String.Compare(mstr, sstr) != 0) //使用静态方法 Console.WriteLine("位置:{0}", mstr.IndexOf(sstr)); //使用非静态方法 else Console.WriteLine("两个字符串相同"); } } }

设计一个控制台程序说明DataTime结构的使用:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace proj2_8 { class Program { static void Main(string[] args) { DateTime d1 = DateTime.Now; //定义当前日期时间变量 DateTime d2 = new DateTime(2008, 10, 1);//定义一个日期时间变量 Console.WriteLine("d1:{0}", d1); int i = d1.Year; //求d1的年 int j = d1.Month; //求d1的月 int k = d1.Day; //求d1的日 int h = d1.Hour; //求d1的时 int m = d1.Minute; //求d1的分 int s = d1.Second; //求d1的秒 Console.WriteLine("d1:{0}年{1}月{2}日{3}时{4}分{5}秒", i, j, k, h, m, s); Console.WriteLine("d2:{0}", d2); Console.WriteLine("相距时间:{0}", d1 - d2); DateTime d3 = d1.AddDays(100); //d3为d1的100天后的日期 Console.WriteLine("d3:{0}", d3); Console.WriteLine("d1年是否为闰年:{0}", DateTime.IsLeapYear(i));//静态方法调用 Console.WriteLine("d2年是否为闰年:{0}", DateTime.IsLeapYear(d2.Year)); } } }

编程题:
设计控制台程序,定义变量“int a=2,b=3;float x=3.5f,y=2.5f;”,并求表达式(float)(a+b)/2+(int)x%(int)y的值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace exci2_1 { class Program { static void Main(string[] args) { int a = 2, b = 3; float x = 3.5f, y = 2.5f; Console.WriteLine("{0}", (float)(a + b) / 2 + (int)x % (int)y); } } }

设计控制台程序,定义变量“int a=3,b=4,c=5;”,并求表达式(++a-1)&b+c/2的值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace exci2_2 { class Program { static void Main(string[] args) { int a = 3, b = 4, c = 5; Console.WriteLine("{0}", (++a - 1) & b + c / 2); } } }

实验题:
设计控制台程序,声明一个学生结构类型Stud,包含学号,姓名和出生日期成员;
定义Stud结构的两个学生变量s1和s2并赋值,求他们出生在星期几以及他们出生相差的天数。
using System; using System.Collections.Generic; using System.Text; namespace experment2 { enum WeekDayhz { 星期日, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六 }; class Program { struct Stud //结构类型声明应放在Main函数的外面 { public int xh; //学号 public string xm; //姓名 public DateTime birthday; //出生日期 } static void Main(string[] args) { Stud s1, s2; s1.xh = 100; s1.xm = "李明"; s1.birthday = new DateTime(1985, 10, 18); s2.xh = 200; s2.xm = "王丽"; s2.birthday = new DateTime(1986, 2, 16); Console.WriteLine("李明的生日为{0}-{1}-{2}", s1.birthday.Year, s1.birthday.Month, s1.birthday.Day); Console.WriteLine("王丽的生日为{0}-{1}-{2}", s2.birthday.Year, s2.birthday.Month, s2.birthday.Day); int i = (int)s1.birthday.DayOfWeek; Console.WriteLine("{0}出生在{1}", s1.xm, (WeekDayhz)i); i = (int)s2.birthday.DayOfWeek; Console.WriteLine("{0}出生在{1}", s2.xm, (WeekDayhz)i); Console.WriteLine("{0}和{1}相差{2}天", s1.xm, s2.xm, s2.birthday - s1.birthday); } } }

ending
越努力越幸运!

浙公网安备 33010602011771号