收藏 分销(赏)

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

上传人:xrp****65 文档编号:5910032 上传时间:2024-11-23 格式:DOC 页数:43 大小:440.50KB
下载 相关 举报
c#程序设计教程第二版李春葆课后编程题答案.doc_第1页
第1页 / 共43页
c#程序设计教程第二版李春葆课后编程题答案.doc_第2页
第2页 / 共43页
c#程序设计教程第二版李春葆课后编程题答案.doc_第3页
第3页 / 共43页
c#程序设计教程第二版李春葆课后编程题答案.doc_第4页
第4页 / 共43页
c#程序设计教程第二版李春葆课后编程题答案.doc_第5页
第5页 / 共43页
点击查看更多>>
资源描述

1、输入a,b求c = a + busing 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);

2、 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

3、 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) 强制转换P38using System;using System.Collections.Gene

4、ric;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);

5、 i2 = c; /隐式类型转换 d2 = (int)d; /强制类型转换 c2 = (char)d; /强制类型转换 Console.WriteLine(i2=0:d5,d2=1:f,c2=2, i2, d2, c2); 赋值两同学信息数据,并在图中输出结果P44using System;namespace Proj3_2class Program struct Student/类型声明应放在Main函数的外面 public int xh; /学号 public string xm;/姓名 public string xb;/性别 public int nl;/年龄 public strin

6、g 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

7、.xh, s2.xm, s2.xb, s2.nl, s2.bh); 声明枚举类型color,给两成员赋值,定义三个变量,赋值运算输出相应值。P47using 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(Re

8、d=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

9、=1,c3=2, c1, c2,c3); Console.WriteLine(c1=0,c2=1,c3=2, (int)c1, (int)c2,(int)c3); 位运算符运用P50using 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);

10、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); 输出常用数据类型所用字节数P52using System;using System.Collections.Generic;using System.Text;namespace Proj3_5 class Program static void Main

11、(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(deci

12、mal); 求字符串子串在主串的位置P56using 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

13、.Length, sstr.Length); if (String.Compare(mstr, sstr) != 0) Console.WriteLine(位置:0, mstr.IndexOf(sstr); else Console.WriteLine(两个字符串相同); DataTime结构的使用P59using System;namespace Proj3_7 class Program static void Main(string args) DateTime d1 = DateTime.Now; /定义当前日期时间变量 DateTime d2 = new DateTime(2009,

14、 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); /d

15、3为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 P60using System;using System.Collections.Generic;using System.Text;namespace Proj3_8 class Program stat

16、ic 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 P60using System;using System.Collections.Generic;using System.Text;namespace Proj3_9 class Program static void Main(string args

17、) int a = 3, b = 4, c = 5; Console.WriteLine(0, (+c - 1) & b + c / 2); 声明一个学生结构类型Stud,包含学号,姓名,出生日期成员,定义Stud结构的两个学生变量S1,S2并赋值,求他们出售在星期几及其相差天数P60using System;using System.Collections.Generic;using System.Text;namespace Proj3_10enum WeekDayhz 星期日,星期一,星期二,星期三,星期四,星期五,星期六; class Program struct Stud/结构类型声

18、明应放在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.

19、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结束)分别输出其中奇数和偶数之和 P72using System;using System.Collections.Generic;using System.Text;namespace Proj4_13 c

20、lass 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.

21、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 13) Console.WriteLine(输入的数值太大!); else for (i=0;i=n-1;i+) for (j=1;j15-i;j+) Console.Write( ); /每次循环显示2个空格 c=1; Console.Write(0

22、,c); for (j=1;j=i;j+) c=c*(i-j+1)/j; if (c100) if (c10) 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 clas

23、s 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

24、 static void Main(string args) int i, n, a, b, c; for (i = 100; i a1?a0:a1; max2=a0a1?a1:a0; for (i=2;in;i+) if (max1ai) max2=max1; max1=ai; Console.WriteLine(max1=0,max2=1,max1,max2); 用一个二维数组存放5个考试4门功课的考试成绩,求每个考生的平均成绩 using System;using System.Collections; using System.Collections.Generic;using Sys

25、tem.Text;namespace Proj5_7 class Program static void Main(string args) const int Max = 5; /考生数 int Ave = new intMax; /定义一个一维数组存储考生的总成绩 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; iMax; i+) for(int j=0; j4; j+) Avei += gradei,j; /累加考生成绩 for (in

26、t k = 0; k Max; k+) Console.WriteLine(考生0平均成绩=1 ,k+1, Avek/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); Conso

27、le.Write(学号:t); for (int i = 0; i no.Length; i+) Console.Write(0t,noi); Console.WriteLine(); Console.Write(姓名:t); for (int i = 0; i name.Length; i+) Console.Write(0t, namei); Console.WriteLine(); static void Main(string args) int no = new int 2, 4, 5, 1, 3; string name = new string Smith,John,Mary,C

28、herr,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

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

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

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

关于我们      便捷服务       自信AI       AI导航        获赠5币

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

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

gongan.png浙公网安备33021202000488号   

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

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服