当前位置:文档之家› c#程序设计教程第二版李春葆课后编程题答案

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

输入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 https://www.doczj.com/doc/1a7304654.html,ponentModel;

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.B lue,Color.White,Color.Black);

Console.WriteLine("Red={0},Green={1},Blue={2},White={3},Black={4}",(int)Color.Red,(int)Color.Gre

en,(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 (https://www.doczj.com/doc/1a7304654.html,pare(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=13+53+33)

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

if (max1

{

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

{

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 https://www.doczj.com/doc/1a7304654.html,ponentModel;

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)

{

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(https://www.doczj.com/doc/1a7304654.html,, "信息提示", MessageBoxButtons.OK);

if (https://www.doczj.com/doc/1a7304654.html,!="button12") //用户不是单击“=”命令按钮

{

x = Convert.ToDouble(textBox1.Text);

textBox1.Text = "";

s = https://www.doczj.com/doc/1a7304654.html,; //保存用户按键

label1.Text = x.ToString();

}

else//用户单击“=”命令按钮

{

if (label1.Text == "")

MessageBox.Show("输入不正确!!!", "信息提示",MessageBoxButtons.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": //用户刚前面单击“×”命令按钮

textBox1.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.Generic;

using https://www.doczj.com/doc/1a7304654.html,ponentModel;

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.ShowDialog(); //以模式窗体方式调用

}

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 https://www.doczj.com/doc/1a7304654.html,ponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace Proj8_2

{

public partial class Form1 : Form

{

private int n=0;

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

Form2 child = new Form2();

child.MdiParent = this;

child.Show();

n++;

child.Text = "第" + n + "个子窗体";

}

private void button2_Click(object sender, EventArgs e)

{

https://www.doczj.com/doc/1a7304654.html,youtMdi(System.Windows.Forms.MdiLayout.ArrangeIcons); }

private void button3_Click(object sender, EventArgs e)

{

https://www.doczj.com/doc/1a7304654.html,youtMdi(System.Windows.Forms.MdiLayout.Cascade);

}

private void button4_Click(object sender, EventArgs e)

{

https://www.doczj.com/doc/1a7304654.html,youtMdi(System.Windows.Forms.MdiLayout.TileVertical); }

private void button5_Click(object sender, EventArgs e)

{

https://www.doczj.com/doc/1a7304654.html,youtMdi(System.Windows.Forms.MdiLayout.TileHorizontal); }

private void Form1_Load(object sender, EventArgs e)

{

}

}

}

用户登录 P200

using System;

using System.Collections.Generic;

using https://www.doczj.com/doc/1a7304654.html,ponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace Proj8_4

{

public partial class Form1 : Form

{

int n = 0; //保存用户错误输入的次数

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

if (textBox1.Text == "1234" && textBox2.Text == "1234")

{

MessageBox.Show("用户名/口令正确");

this.Close();

//调用其他程序

}

else

{

if (n<3)

{

MessageBox.Show("用户名/口令错误,再次输入");

n++;

}

else

{

MessageBox.Show("已经错误输入三次,退出");

this.Close();

}

}

}

private void Form1_Load(object sender, EventArgs e)

{

}

}

}

中华人民共和国成立60周年。字体,大小,粗体 P200

using System;

using System.Collections.Generic;

using https://www.doczj.com/doc/1a7304654.html,ponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace Proj8_4

{

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

}

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;

//https://www.doczj.com/doc/1a7304654.html, = "楷体_GB2312";

}

private void rbutton3_CheckedChanged(object sender, EventArgs e)

{

Font f = new Font(https://www.doczj.com/doc/1a7304654.html,, 10, textBox1.Font.Style);

textBox1.Font = f;

}

private void rbutton4_CheckedChanged(object sender, EventArgs e)

{

Font f = new Font(https://www.doczj.com/doc/1a7304654.html,, 16, textBox1.Font.Style);

textBox1.Font = f;

}

private void checkBox1_CheckedChanged(object sender, EventArgs e)

{

if (checkBox1.Checked)

{

Font f = new Font(https://www.doczj.com/doc/1a7304654.html,, textBox1.Font.Size, FontStyle.Bold);

textBox1.Font = f;

}

else

{

Font f = new Font(https://www.doczj.com/doc/1a7304654.html,, textBox1.Font.Size, FontStyle.Regular); textBox1.Font = f;

}

}

private void Form2_Load(object sender, EventArgs e)

{

}

}

}

列表框选择乘数进行乘法运算 P200

using System;

using System.Collections.Generic;

using https://www.doczj.com/doc/1a7304654.html,ponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace Proj8_4

{

public partial class Form3 : Form

{

int a=0, b=0;

public Form3()

{

InitializeComponent();

}

private void Form3_Load(object sender, EventArgs e)

{

listBox1.Items.Add("12");

listBox1.Items.Add("10");

listBox1.Items.Add("5");

listBox1.Items.Add("8");

listBox1.Items.Add("22");

listBox1.Items.Add("25");

listBox1.Items.Add("9");

listBox2.Items.Add("9");

listBox2.Items.Add("15");

listBox2.Items.Add("20");

listBox2.Items.Add("32");

listBox2.Items.Add("18");

listBox2.Items.Add("5");

listBox2.Items.Add("7");

listBox2.Items.Add("32");

}

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("{0}×{1}={2}",a,b,a*b));

}

private void listBox3_SelectedIndexChanged(object sender, EventArgs e) {

if (listBox3.SelectedIndex>=0)

listBox3.Items.RemoveAt(listBox3.SelectedIndex);

}

}

}

在组合框输入新项时,自动添加到该组合框列表中并给相应提示。P200

using System;

using System.Collections.Generic;

using https://www.doczj.com/doc/1a7304654.html,ponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace Proj8_4

{

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

}

}

}

相关主题
文本预览
相关文档 最新文档