c#程序设计教程(课后习题答案代码)
更新时间:2023-10-30 15:35:01 阅读量: 综合文库 文档下载
判断是否是闰年 课本63
用户输入整数反向显示 课本67或68 乘法表 课本69
判断从键盘输入大于3的整数是否为素数 课本70 求输入所以数其中正数的和 课本70
求 1平方+2平方+……+n平方 小于等于1000 的最大n 课本71或72 读入一组数(以0结束),分别求奇数和偶数和 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+….+n)
int n,i,j,s=0;
Console.Write(\);
n = int.Parse(Console.ReadLine()); for (i = 1; i <= n; i++) for (j = 1; j <= i; j++) s += j;
Console.WriteLine(\, s); 杨辉三角
static void Main(string[] args) {int i,j,c,n;
Console.Write(\);
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(\,c); for (j=1;j<=i;j++) { c=c*(i-j+1)/j; if (c<100) if (c<10)
Console.Write(\,c); //显示3个空格 else
Console.Write(\,c); //显示2个空格 else
Console.Write(\,c); }
Console.WriteLine(); } 计算π的值
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); 求水仙花数
static void Main(string[] args)
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(\, a, b, c,a*a*a+b*b*b+c*c*c); //Console.Write(\ } }
Console.WriteLine(); }
假设10个整数用一维数组存放,求最大值和次大值 static void Main(string[] args)
{ int[] a = new int[10]{1,8,10,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
{ int i, n, a, b, c;
Console.WriteLine(\,max1,max2); }
用一个二维数组存放5个考生4门功课的考试成绩,求每位考生的平均成绩 课本89页 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
{ 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个学生的学号和姓名,分别按学号和姓名排序 课本89页上级实验5 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(\,no[i]); Console.WriteLine(); Console.Write(\姓名:\\t\);
for (int i = 0; i < name.Length; i++) Console.Write(\, name[i]); Console.WriteLine(); }
static void Main(string[] args)
{ int[] no = new int[] { 2, 4, 5, 1, 3};
string[] name = new string[] {\,\,\,\,\}; disp(no, name,\排序前:\); Array.Sort(no, name);
disp(no, name,\按学号排序后:\); Array.Sort(name, no);
disp(no, name, \按姓名排序后:\); } 课本124页8 class Program
{ static void Main(string[] args) { Person p1 = new Person(2, 50);
Animal a1 = new Animal(); p1.show(); a1.show(); } }
public class Person
{public int legs;
//定义人类 //腿的只数
protected float weight; //重量
public Person() //默认构造函数
{ }
public Person(int legs1,float weight1)//自定义方法F { legs= legs1;
weight = weight1; } }
课本124页9
//定义了一个委托,委托在传递方法时,方法必须带两个int型的参数。 public delegate int Call(int num1, int num2); //在Delegates类的内部定义Math类和TestDelegates类。 class Math
{ public int fun1(int num1, int num2) { return num1*num1+num2*num2; }
public int fun2(int num1, int num2) { return num1*num1-num2*num2; } }
class Program
public void show()
{ Console.WriteLine(\某人有{0}只腿,重量为{1}kg\, legs, weight); } }
//定义动物类 //重量
{ public int num; //腿的条数 private float weight; { }
public Animal(int n,float w) //Animal类带2个参数的构造函数 { num = n; weight = w; }
public void show()
{ Console.WriteLine(\某动物有{0}只脚,重量为{1}kg\, num, weight); }
public Animal() //Animal类的默认构造函数
class Animal
{ static void Main(string[] args) { int result; Call objCall;
//委托的对象 //Math类的对象 //将委托实例化
Math objMath = new Math(); result = objCall(5, 3);
objCall = new Call(objMath.fun1); Console.WriteLine(\结果为{0}\, result); objCall = new Call(objMath.fun2); result = objCall(5, 3); }
} 课本124页10 class List
{ private int Max = 100; //存储最多元素 private int num = 0; //存储的实际元素个数 private object[] list; //存储元素数组 public List() //构造函数 { list = new object[Max]; }
public void add(object obj) //添加一个元素 { list[num] = obj; num++; }
public void delete(int pos) //删除一个元素 { for (int i = pos + 1; i < num; i++) list[i - 1] = list[i]; num--; }
public object get(int pos) //获取指定位置的元素 { if (pos < num)
return list[pos]; else
return null; }
public int getnum() //获取实际元素个数 { return num; }
public string disp() //获取所有元素 { string s = \;
for (int i = 0; i < num; i++) s += list[i] + \; return s; }
//将委托实例化
Console.WriteLine(\结果为{0}\, result);
private void rbutton1_CheckedChanged(object sender, EventArgs e) { Font f = new Font(\宋体\,textBox1.Font.Size,textBox1.Font.Style); textBox1.Font = f; }
private void rbutton2_CheckedChanged(object sender, EventArgs e)
{ Font f = new Font(\楷体_GB2312\, textBox1.Font.Size, textBox1.Font.Style); textBox1.Font = f;
//textBox1.Font.Name = \楷体_GB2312\ }
private void rbutton3_CheckedChanged(object sender, EventArgs e) { Font f = new Font(textBox1.Font.Name, 10, textBox1.Font.Style); textBox1.Font = f; }
private void rbutton4_CheckedChanged(object sender, EventArgs e) { Font f = new Font(textBox1.Font.Name, 16, textBox1.Font.Style); textBox1.Font = f; }
private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked)
{ Font f = new Font(textBox1.Font.Name, textBox1.Font.Size, FontStyle.Bold); textBox1.Font = f; } else
{ Font f = new Font(textBox1.Font.Name, textBox1.Font.Size, FontStyle.Regular); textBox1.Font = f;
} } } 课本200页 5
public partial class Form3 : Form { int a=0, b=0; public Form3()
{InitializeComponent(); }
private void Form3_Load(object sender, EventArgs e) { listBox1.Items.Add(\); listBox1.Items.Add(\); listBox1.Items.Add(\); listBox1.Items.Add(\); listBox1.Items.Add(\); listBox1.Items.Add(\); listBox1.Items.Add(\); listBox2.Items.Add(\); listBox2.Items.Add(\); listBox2.Items.Add(\); listBox2.Items.Add(\);
listBox2.Items.Add(\); listBox2.Items.Add(\); listBox2.Items.Add(\); listBox2.Items.Add(\); }
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { a = int.Parse(listBox1.Text); }
private void listBox2_SelectedIndexChanged(object sender, EventArgs e) { b = int.Parse(listBox2.Text);
listBox3.Items.Add(string.Format(\×{1}={2}\,a,b,a*b)); }
private void listBox3_SelectedIndexChanged(object sender, EventArgs e) { if (listBox3.SelectedIndex>=0)
listBox3.Items.RemoveAt(listBox3.SelectedIndex); }
} 课本200页6
public partial class Form4 : Form { public Form4()
{ InitializeComponent(); }
private void Form4_Load(object sender, EventArgs e) { comboBox1.Items.Add(\北京\); comboBox1.Items.Add(\上海\); comboBox1.Items.Add(\天津\); comboBox1.Items.Add(\广州\); comboBox1.Items.Add(\武汉\); comboBox1.Items.Add(\沈阳\); comboBox1.Items.Add(\合肥\); comboBox1.Items.Add(\长沙\); comboBox1.Items.Add(\重庆\); }
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e) { //MessageBox.Show(e.KeyChar.ToString()); if (e.KeyChar == 13) //按Enter键后判断 { if (comboBox1.Items.Contains(comboBox1.Text)) label2.Text = \你的输入已在组合框!\; else
{ comboBox1.Items.Add(comboBox1.Text);
label2.Text = \你的输入新项已添加到组合框中!\; } }}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{ MessageBox.Show(string.Format(\你选择了'{0}'项\,comboBox1.Text)); }} 课本200页上机实验8
public partial class Form5 : Form { struct StudType { public int no; public string name; public string sex; public string sclass; public DateTime rq; }
StudType[] stud = new StudType[10]; //定义一个结构类型的数组 public Form5()
{ InitializeComponent();}
private void Form5_Load(object sender, EventArgs e) { //给定义的数组赋初值
stud[0].no = 1; stud[0].name = \王华\; stud[0].sex = \男\; stud[0].rq = new DateTime(1980,2,10); stud[0].sclass = \; stud[1].no = 2; stud[1].name = \李强\; stud[1].sex = \男\; stud[1].rq = new DateTime(1981,10,4); stud[1].sclass = \; stud[2].no = 3; stud[2].name = \张丽\; stud[2].sex = \女\; stud[2].rq = new DateTime(1980,3,2); stud[2].sclass = \; stud[3].no = 4; stud[3].name = \汪洋\; stud[3].sex = \男\; stud[3].rq = new DateTime(1980,1,5); stud[3].sclass = \; stud[4].no = 5; stud[4].name = \江华\; stud[4].sex = \男\; stud[4].rq = new DateTime(1980,3,10); stud[4].sclass = \; stud[5].no = 6; stud[5].name = \李英\; stud[5].sex = \女\; stud[5].rq = new DateTime(1980,6,2); stud[5].sclass = \; stud[6].no = 7; stud[6].name = \胡军\; stud[6].sex = \男\; stud[6].rq = new DateTime(1981,10,9); stud[6].sclass = \; stud[7].no = 8; stud[7].name = \刘驰\; stud[7].sex = \女\; stud[7].rq = new DateTime(1982,5,2); stud[7].sclass = \; stud[8].no = 9; stud[8].name = \宋仁\; stud[8].sex = \男\; stud[8].rq = new DateTime(1980,8,3); stud[8].sclass = \; stud[9].no = 10; stud[9].name = \许兵\; stud[9].sex = \男\; stud[9].rq = new DateTime(1980,11,8); stud[9].sclass = \; //将学号都添加到组合框中 for(int i = 0;i<10;i++)
comboBox1.Items.Add(stud[i].no); } private void button1_Click(object sender, EventArgs e) { if (comboBox1.Text != \)
{ int i = 0; //查找指定学号的学生记录
while (Convert.ToInt32(comboBox1.Text) != stud[i].no) i = i + 1; if (i >= 10)
MessageBox.Show(\没有该学号的记录\);
else
{ textBox1.Text = stud[i].no.ToString(); //显示找到的学生记录 textBox2.Text = stud[i].name; textBox3.Text = stud[i].sex; textBox4.Text = stud[i].sclass;
textBox5.Text = stud[i].rq.ToString();}
}}} 课本236页2
public partial class Form1 : Form {public Form1()
{InitializeComponent();}
private void menu11_Click(object sender, EventArgs e)
{ Font f = new Font(\宋体\,label1.Font.Size,label1.Font.Style); label1.Font = f; }
private void menu12_Click(object sender, EventArgs e)
{ Font f = new Font(\仿宋_GB2312\, label1.Font.Size, label1.Font.Style); label1.Font = f; }
private void menu13_Click(object sender, EventArgs e)
{ Font f = new Font(\黑体\, label1.Font.Size, label1.Font.Style); label1.Font = f; }
private void menu14_Click(object sender, EventArgs e)
{ Font f = new Font(\幼圆\, label1.Font.Size, label1.Font.Style); label1.Font = f; }
private void menu15_Click(object sender, EventArgs e)
{ Font f = new Font(\楷体_GB2312\, label1.Font.Size, label1.Font.Style); label1.Font = f; }
private void menu21_Click(object sender, EventArgs e)
{ Font f = new Font(label1.Font.Name, 28, label1.Font.Style); label1.Font = f; }
private void menu22_Click(object sender, EventArgs e)
{Font f = new Font(label1.Font.Name, 20, label1.Font.Style); label1.Font = f; }
private void menu23_Click(object sender, EventArgs e)
{ Font f = new Font(label1.Font.Name, 16, label1.Font.Style); label1.Font = f; }
private void menu24_Click(object sender, EventArgs e)
{ Font f = new Font(label1.Font.Name, 12, label1.Font.Style); label1.Font = f; }
private void menu25_Click(object sender, EventArgs e)
{Font f = new Font(label1.Font.Name, 8, label1.Font.Style); label1.Font = f; }
}
课本237页上机实验9
public partial class Form2 : Form
{ public Form2()
{InitializeComponent();}
private void Form2_Load(object sender, EventArgs e) { treeView1.Indent = 20;
treeView1.Nodes.Add(\哺乳动物\); treeView1.Nodes[0].Nodes.Add(\豹子\); treeView1.Nodes[0].Nodes.Add(\老虎\); treeView1.Nodes[0].Nodes.Add(\北极熊\); treeView1.Nodes[0].Nodes.Add(\狼\); treeView1.Nodes[0].Nodes.Add(\大象\); treeView1.Nodes[0].Nodes.Add(\犀牛\); treeView1.Nodes.Add(\鱼类\);
treeView1.Nodes[1].Nodes.Add(\鲨鱼\); treeView1.Nodes[1].Nodes.Add(\热带鱼\); treeView1.Nodes[1].Nodes.Add(\金鱼\); treeView1.Nodes.Add(\鸟类\);
treeView1.Nodes[2].Nodes.Add(\天鹅\); treeView1.Nodes[2].Nodes.Add(\猫头鹰\); treeView1.Nodes[2].Nodes.Add(\翠鸟\); }
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {string s;
s = e.Node.Text; listView1.Items.Clear();
listView1.LargeImageList = imageList1; listView1.SmallImageList = imageList2; switch(s)
{ case \哺乳动物\:
listView1.Items.Add(\豹子\, 0); listView1.Items.Add(\老虎\, 0); listView1.Items.Add(\北极熊\, 0); listView1.Items.Add(\狼\, 0); listView1.Items.Add(\大象\, 0); listView1.Items.Add(\犀牛\, 0); break; case \鱼类\:
listView1.Items.Add(\鲨鱼\, 0); listView1.Items.Add(\热带鱼\, 0); listView1.Items.Add(\金鱼\, 0); break; case \鸟类\:
listView1.Items.Add(\天鹅\, 0); listView1.Items.Add(\猫头鹰\, 0); listView1.Items.Add(\翠鸟\, 0); break; }}
{ return string.Format(\, no, name); } } public Form2()
{ InitializeComponent();}
private void button1_Click(object sender, EventArgs e) { Myclass[] st = new Myclass[3]; int i;
string mystr=\; try
{ //for (i = 0; i < 3; i++) 加上本for循环就不会出错 // st[i] = new Myclass(); st[0].input(1, \王华\); st[1].input(3, \曾丽\); st[2].input(2, \陈山\); for (i = 0; i < 3; i++)
mystr = mystr + st[i].getstud() + \; textBox1.Text = mystr; }
catch (Exception ex)
{ string str = ex.Message + \ + ex.Source + \ + ex.HelpLink ; MessageBox.Show(str);
} } } 课本288上机实验12
public partial class Form3 : Form { public Form3()
{ InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { string path = textBox1.Text; try
{ string[] dirs = Directory.GetDirectories(path); label2.Text = \子文件夹个数为:\ + dirs.Length; }
catch(Exception ex)
{ label2.Text = \不存在指定的文件夹!\; } }
private void Form3_Load(object sender, EventArgs e)
{ label2.Text = \;}}
第十三章课后题和本章例题相似,代码太长这里不写了。
正在阅读:
c#程序设计教程(课后习题答案代码)10-30
冷轧带钢生产工艺及发展状况10-11
ktv工作计划03-28
县住建局上半年推进农村房屋安全隐患排查整治工作部署06-05
110812 - 李书记现场办公TJK工作汇报材料 - 图文10-12
最新高考人物作文素材整理5篇03-28
社戏说课稿08-11
构造论文01-22
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- c#
- 课后
- 习题
- 程序设计
- 答案
- 代码
- 教程
- 川人社发12号 关于2012年增加企业退休人员基本养老金的通知
- 不给人添麻烦是美德吗?(罗振宇-反方)
- 对广州流动商贩问题的探讨
- 广东省海洋功能区划文本(粤府〔2008〕57号)
- CCS3.3使用图解
- MC1648 - PLL 高频信号发生器
- 超市收银系统分析
- 2015年上海徐汇区中考英语一模试卷和答案 - 图文
- 《抄表核算收费员》中级工理论试卷
- 推优入党团员评议活新闻稿
- 动火证审批表、二级、三级动火许可证范本
- 穿越百年沧桑,领悟红色经典
- 2013年高考理科数学全国新课标卷1(附答案)
- 船舶辅机
- 拆迁户调查问卷表
- 2013年德育工作总结
- 苏宁易购物流实习报告
- 2014计算机类专业课试卷及答案
- 家庭教育中国研究现状
- 中国移动客户优惠购机协议