收藏 分销(赏)

c#程序设计教程第二版李春葆课后编程题答案.doc

上传人:xrp****65 文档编号:5910032 上传时间:2024-11-23 格式:DOC 页数:43 大小:440.50KB 下载积分:10 金币
下载 相关 举报
c#程序设计教程第二版李春葆课后编程题答案.doc_第1页
第1页 / 共43页
c#程序设计教程第二版李春葆课后编程题答案.doc_第2页
第2页 / 共43页


点击查看更多>>
资源描述
输入a,b求c = a + b using System; using System.Collections.Generic; using System.Text; namespace Proj2_1 { class Program { static void Main(string[] args) { int a, 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); } } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Proj2_2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int a, b, c; a = Convert.ToInt16(textBox1.Text); b = Convert.ToInt16(textBox2.Text); c = a + b; textBox3.Text = Convert.ToString(c); } private void Form1_Load(object sender, EventArgs e) { } private void textBox2_TextChanged(object sender, EventArgs e) { } } } 强制转换P38 using System; using System.Collections.Generic; using System.Text; namespace Proj3_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; //强制类型转换 c2 = (char)d; //强制类型转换 Console.WriteLine("i2={0:d5},d2={1:f},c2={2}", i2, d2, c2); } } } 赋值两同学信息数据,并在图中输出结果P44 using System; namespace Proj3_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; //定义两个结构类型变量 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); } } } 声明枚举类型color,给两成员赋值,定义三个变量,赋值运算输出相应值。P47 using System; using System.Collections.Generic; using System.Text; namespace Proj3_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); } } } 位运算符运用P50 using System; using System.Collections.Generic; using System.Text; namespace Proj3_4 { class Program { static void Main(string[] args) { byte b1, b2, b3; b1 = 10; b2 =(byte) ~b1; Console.WriteLine(b2); b3 = (byte)(b1 << 2); 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); } } } 输出常用数据类型所用字节数P52 using System; using System.Collections.Generic; using System.Text; namespace Proj3_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)); } } } 求字符串子串在主串的位置P56 using System; using System.Collections.Generic; using System.Text; namespace Proj3_6 { 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结构的使用P59 using System; namespace Proj3_7 { class Program { static void Main(string[] args) { DateTime d1 = DateTime.Now; //定义当前日期时间变量 DateTime d2 = new DateTime(2009, 10, 1); //定义一个日期时间变量 Console.WriteLine("d1:{0}",d1); int i = d1.Year; int j = d1.Month; int k = d1.Day; int h = d1.Hour; int m = d1.Minute; int s = 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); DateTime d3 = d1.AddDays(100); //d3为d1的100天后的日期 Console.WriteLine("d3:{0}",d3); Console.WriteLine(DateTime.IsLeapYear(i)); Console.WriteLine(DateTime.IsLeapYear(d2.Year)); } } } 设计一个控制台程序,定义变量int a,b;float x,y。并求表达式(float)(a+b)/+(int)x%(int)y P60 using System; using System.Collections.Generic; using System.Text; namespace Proj3_8 { 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,b, c;并求表达式(++c-1)&b+c/2 P60 using System; using System.Collections.Generic; using System.Text; namespace Proj3_9 { class Program { static void Main(string[] args) { int a = 3, b = 4, c = 5; Console.WriteLine("{0}", (++c - 1) & b + c / 2); } } } 声明一个学生结构类型Stud,包含学号,姓名,出生日期成员,定义Stud结构的两个学生变量S1,S2并赋值,求他们出售在星期几及其相差天数P60 using System; using System.Collections.Generic; using System.Text; namespace Proj3_10 {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); 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); } } } 输入一组整数(以输入0结束)分别输出其中奇数和偶数之和 P72 using System; using System.Collections.Generic; using System.Text; namespace Proj4_13 { class Program { static void Main(string[] args) { int n,s1=0,s2=0; do { n = int.Parse(Console.ReadLine()); if (n%2==1) s1 += n; else s2 += n; } while (n!=0); Console.WriteLine("奇数之和={0}",s1); Console.WriteLine("偶数之和={0}",s2); } } } 输入正整数n,计算s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n) using System; using System.Collections.Generic; using System.Text; namespace Proj4_14 { class Program { static void Main(string[] args) { int n,i,j,s=0; Console.Write("n:"); n = int.Parse(Console.ReadLine()); for (i = 1; i <= n; i++) for (j = 1; j <= i; j++) s += j; Console.WriteLine("s={0}", s); } } } 输出n阶杨辉三角形,n不能大于13 using System; using System.Collections.Generic; using System.Text; namespace Proj4_15 { class Program { static void Main(string[] args) { int i,j,c,n; Console.Write("n:"); n=int.Parse(Console.ReadLine()); if (n>13) Console.WriteLine("输入的数值太大!"); else { for (i=0;i<=n-1;i++) { for (j=1;j<15-i;j++) Console.Write(" "); //每次循环显示2个空格 c=1; Console.Write("{0} ",c); for (j=1;j<=i;j++) { c=c*(i-j+1)/j; if (c<100) if (c<10) Console.Write("{0} ",c); //显示3个空格 else Console.Write("{0} ",c); //显示2个空格 else Console.Write("{0} ",c); //显示1个空格 } Console.WriteLine(); } } } } } 利用π/4 = 1-1/3+1/5-1/7+…+1/(4n-3)-1/(4n-1) using System; using System.Collections.Generic; using System.Text; namespace Proj4_16 { class Program { static void Main(string[] args) { double pi=0.0; int i; for (i=1;i<=2000;i++) if (i%2==1) pi=pi+1.0/(2*i-1); else pi=pi-1.0/(2*i-1); pi=4*pi; Console.WriteLine("π={0}", pi); } } } 输出三个数,其数值刚好等于其每个数字立方和(153=1³+5³+3³) using System; using System.Collections.Generic; using System.Text; namespace Proj4_17 { class Program { static void Main(string[] args) { int i, n, a, b, c; for (i = 100; i <= 999; i++) { n = i; c = n % 10; n = n / 10; b = n % 10; n = n / 10; a = n; if (a * a * a + b * b * b + c * c * c == i) { Console.WriteLine("{0} {1} {2} = {3}", a, b, c,a*a*a+b*b*b+c*c*c); //Console.Write("{0} ", i); } } Console.WriteLine(); } } } 假设十个整数用一个一维数组存放,求其最大值和次大值 using System; using System.Collections.Generic; using System.Text; namespace Proj5_6 { class Program { static void Main(string[] args) { int[] a = new int[10]{1,8,3,4,7,9,6,10,2,5}; int n=10,max1,max2,i; max1=a[0]>a[1]?a[0]:a[1]; max2=a[0]>a[1]?a[1]:a[0]; for (i=2;i<n;i++) if (max1<a[i]) { max2=max1; max1=a[i]; } Console.WriteLine("max1={0},max2={1}",max1,max2); } } } 用一个二维数组存放5个考试4门功课的考试成绩,求每个考生的平均成绩 using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace Proj5_7 { class Program { static void Main(string[] args) { const int Max = 5; //考生数 int[] Ave = new int[Max]; //定义一个一维数组存储考生的总成绩 int[,] grade={{88,75,62,84},{96,85,75,92}, //定义二维数组存储考生成绩 {68,63,72,78},{95,89,76,98}, {76,65,72,63}}; for(int i=0; i<Max; i++) { for(int j=0; j<4; j++) { Ave[i] += grade[i,j]; //累加考生成绩 } } for (int k = 0; k < Max; k++) Console.WriteLine("考生{0}平均成绩={1} ",k+1, Ave[k]/4.0); } } } 用两个一维数组分别存放5个学生的学号和姓名,分别按学号好姓名进行排序,输出排序后结果 using System; using System.Collections.Generic; using System.Text; namespace Proj5_8 { class Program { const int Max = 5; static void disp(int[] no,string[] name,string str) { Console.WriteLine(str); Console.Write("学号:\t"); for (int i = 0; i < no.Length; i++) Console.Write("{0}\t",no[i]); Console.WriteLine(); Console.Write("姓名:\t"); for (int i = 0; i < name.Length; i++) Console.Write("{0}\t", name[i]); Console.WriteLine(); } static void Main(string[] args) { int[] no = new int[] { 2, 4, 5, 1, 3}; string[] name = new string[] {"Smith","John","Mary","Cherr","Tomn"}; disp(no, name,"排序前:"); Array.Sort(no, name); disp(no, name,"按学号排序后:"); Array.Sort(name, no); disp(no, name, "按姓名排序后:"); } } } 计算器 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Prj8_3 { public partial class Form1 : Form { private string s; private double x, y; private Button btn; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { tex
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 应用文书 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服