c程序设计教程第二版李葆课后编程题答案_第1页
c程序设计教程第二版李葆课后编程题答案_第2页
c程序设计教程第二版李葆课后编程题答案_第3页
c程序设计教程第二版李葆课后编程题答案_第4页
c程序设计教程第二版李葆课后编程题答案_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

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

文档简介

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; cons

2、ole.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 bu

3、tton1_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) 强制转换p38using

4、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; /

5、强制类型转换 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); 赋值两同学信息数据,并在图中输出结果p44using system;namespace proj3_2class program struct student/类型声明应放在main函数的外面 public in

6、t 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", s

7、1.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,给两成员赋值,定义三个变量,赋值运算输出相应值。p47using system;using system.collections.generic;using system.text;namespace pro

8、j3_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

9、,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); 位运算符运用p50using syste

10、m;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 =

11、(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(string args) console.writeline("byte类型所占字节数:0", sizeof(byte); console.writelin

12、e("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); 求字符串子串在主串的位

13、置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

14、,子串长度=1", mstr.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; /

15、定义当前日期时间变量 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&

16、quot;,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

17、p60using 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 p60using

18、 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并赋值,求他们出售在星期几及其相差天数p60using system;using sys

19、tem.collections.generic;using system.text;namespace proj3_10enum 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

20、.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); con

21、sole.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 class program static void main(string args) int n,s1=0,s2=0; do n = int.parse(console.readline(); if (n%2=1)

22、 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;

23、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(st

24、ring 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-

25、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;namespac

26、e 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;u

27、sing 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*

28、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 int101,8,3,4,7,9,6,10,2,5; int n=10,max1,max2,i; max1=a0>a1?a0:a1; max2=a

29、0>a1?a1:a0; for (i=2;i<n;i+) if (max1<ai) max2=max1; max1=ai; 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(

30、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; i<max; i+) for(int j=0; j<4; j+) avei += gradei,j; /累加考生成绩 for (int k = 0; k < max; k+) console.writeline("考

31、生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); console.write("学号:t"); for (

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

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

34、 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, eventa

35、rgs e) textbox1.text = "" label1.text = "" private void buttond_click(object sender, eventargs e) btn = (button)sender; textbox1.text = textbox1.text + btn.text; private void buttonop_click(object sender, eventargs e) btn = (button)sender; /messagebox.show(btn.name, "信息提示&qu

36、ot;, messageboxbuttons.ok); if (btn.name!="button12") /用户不是单击“=”命令按钮 x = convert.todouble(textbox1.text); textbox1.text = "" s = btn.name; /保存用户按键 label1.text = x.tostring(); else /用户单击“=”命令按钮 if (label1.text = "") messagebox.show("输入不正确!", "信息提示",me

37、ssageboxbuttons.ok); else y = convert.todouble(textbox1.text); switch(s) case "button13": /用户刚前面单击“+”命令按钮 textbox1.text = (x + y).tostring(); break; case "button14": /用户刚前面单击“-”命令按钮 textbox1.text = (x - y).tostring(); break; case "button15": /用户刚前面单击“×”命令按钮 textbox

38、1.text = (x * y).tostring(); break; case "button16": /用户刚前面单击“÷”命令按钮 if (y = 0) messagebox.show("除零错误!", "信息提示",messageboxbuttons.ok); else textbox1.text = (x / y).tostring(); break; label1.text = textbox1.text; 模式窗体,无模式窗体调用using system;using system.collections.gen

39、eric;using system.componentmodel;using system.data;using system.drawing;using system.text;using system.windows.forms;namespace proj8_1 public partial class form1 : form public form1() initializecomponent(); private void button1_click(object sender, eventargs e) form myform = new form1_1(); myform.sh

40、owdialog(); /以模式窗体方式调用 private void button2_click(object sender, eventargs e) form myform = new form1_2(); myform.show(); /以无模式窗体方式调用 private void form1_load(object sender, eventargs e) private void form1_load_1(object sender, eventargs e) 窗体设计using system;using system.collections.generic;using system.componentmodel;using system.data;using system.drawi

温馨提示

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

评论

0/150

提交评论