您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > c#程序设计教程第二版李春葆课后编程题答案
1输入a,b求c=a+busingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceProj2_1{classProgram{staticvoidMain(string[]args){inta,b,c;Console.Write(a:);a=int.Parse(Console.ReadLine());Console.Write(b:);b=int.Parse(Console.ReadLine());c=a+b;Console.WriteLine(a+b={0},c);}}}usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespaceProj2_2{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){2inta,b,c;a=Convert.ToInt16(textBox1.Text);b=Convert.ToInt16(textBox2.Text);c=a+b;textBox3.Text=Convert.ToString(c);}privatevoidForm1_Load(objectsender,EventArgse){}privatevoidtextBox2_TextChanged(objectsender,EventArgse){}}}强制转换P38usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceProj3_1{classProgram{staticvoidMain(string[]args){inti=65,i1,i2;doubled=66.3456,d1,d2;charc='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;//强制类型转换c2=(char)d;//强制类型转换Console.WriteLine(i2={0:d5},d2={1:f},c2={2},i2,d2,c2);}}}3赋值两同学信息数据,并在图中输出结果P44usingSystem;namespaceProj3_2{classProgram{structStudent//类型声明应放在Main函数的外面{publicintxh;//学号publicstringxm;//姓名publicstringxb;//性别publicintnl;//年龄publicstringbh;//班号}staticvoidMain(string[]args){Students1,s2;//定义两个结构类型变量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赋给s2s2.xh=108;s2.xm=王华;Console.WriteLine(学号:{0},姓名:{1},性别:{2},年龄:{3},班号:{4},s2.xh,s2.xm,s2.xb,s2.nl,s2.bh);}}}声明枚举类型color,给两成员赋值,定义三个变量,赋值运算输出相应值。P47usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceProj3_3{classProgram{enumColor{Red=5,Green,Blue,White=1,Black}//类型声明应放在Main函数的外面staticvoidMain(string[]args){Colorc1,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.Gre4en,(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);}}}位运算符运用P50usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceProj3_4{classProgram{staticvoidMain(string[]args){byteb1,b2,b3;b1=10;b2=(byte)~b1;Console.WriteLine(b2);b3=(byte)(b12);Console.WriteLine(b3);b1=3;b2=6;b3=(byte)(b1&b2);Console.WriteLine(b3);b3=(byte)(b1^b2);Console.WriteLine(b3);b3=(byte)(b1|b2);Console.WriteLine(b3);}}}5输出常用数据类型所用字节数P52usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceProj3_5{classProgram{staticvoidMain(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));}}}求字符串子串在主串的位置P56usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceProj3_6{classProgram{staticvoidMain(string[]args){Stringmstr,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));elseConsole.WriteLine(两个字符串相同);}}}6DataTime结构的使用P59usingSystem;namespaceProj3_7{classProgram{staticvoidMain(string[]args){DateTimed1=DateTime.Now;//定义当前日期时间变量DateTimed2=newDateTime(2009,10,1);//定义一个日期时间变量Console.WriteLine(d1:{0},d1);inti=d1.Year;intj=d1.Month;intk=d1.Day;inth=d1.Hour;intm=d1.Minute;ints=d1.Second;Console.WriteLine(d1:{0}年{1}月{2}日{3}时{4}分{5}秒,i,j,k,h,m,s);Console.WriteLine(d2:{0},d2);Console.WriteLine(相距时间:{0},d2-d1);DateTimed3=d1.AddDays(100);//d3为d1的100天后的日期Console.WriteLine(d3:{0},d3);Console.WriteLine(DateTime.IsLeapYear(i));Console.WriteLine(DateTime.IsLeapYear(d2.Year));}}}设计一个控制台程序,定义变量inta,b;floatx,y。并求表达式(float)(a+b)/+(int)x%(int)yP60usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceProj3_8{classProgram{staticvoidMain(string[]args){inta=2,b=3;floatx=3.5f,y=2.5f;Console.WriteLine({0},(float)(a+b)/2+(int)x%(int)y);}}}7设计一个控制台程序,定义变量inta,b,c;并求表达式(++c-1)&b+c/2P60usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceProj3_9{classProgram{staticvoidMain(string[]args){inta=3,b=4,c=5;Console.WriteLine({0},(++c-1)&b+c/2);}}}声明一个学生结构类型Stud,包含学号,姓名,出生日期成员,定义Stud结构的两个学生变量S1,S2并赋值,求他们出售在星期几及其相差天数P60usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceProj3_10{enumWeekDayhz{星期日,星期一,星期二,星期三,星期四,星期五,星期六};classProgram{structStud//结构类型声明应放在Main函数的外面{publicintxh;//学号publicstringxm;//姓名publicDateTimebirthday;//出生日期}staticvoidMain(string[]args){Studs1,s2;s1.xh=100;s1.xm=李明;s1.bir
本文标题:c#程序设计教程第二版李春葆课后编程题答案
链接地址:https://www.777doc.com/doc-3258159 .html