C#程序设计经典教程(第三版)实验二

若转载,请保留文章出处和原作者,感谢。

1.设计一个简单的Windows应用程序,在文本框中随意输人一个日期,单击“确定”时显示“这一天是星期几”,如图2-10所示。

核心代码提示:

enum WeekDay{星期天,星期一,星期二,星期三,星期四,星期五,星期六};

DateTime dt = Convert.ToDateTime(txtDate.Text);

WeekDay wd = (WeekDay)dt.DayOfWeek; lblShow.Text = "这一天是" + wd + "。";

2.设计一个简单的计算器,实现两个数的加、减、乘、除、求幂等计算,运行效果如图2-11所示。

3.设计一个简单的Windows程序,输入5个数字,然后排序并输出,如图2-12所示。

内容如下:

1.

代码如下(注意:没有对文本框的内容进行非空检测,如果需要检测,需要自己加上):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        enum WeekDay { 星期日, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六 };
        private void button1_Click(object sender, EventArgs e)
        {
            DateTime dt = Convert.ToDateTime(textBox1.Text);
            WeekDay wd = (WeekDay)dt.DayOfWeek;
            lblshow.Text = "这一天" + wd + "。";

        }

    }
}

2.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double a = Convert.ToDouble(textBox1.Text);
            double b = Convert.ToDouble(textBox2.Text);
            lblshow.Text = a + "+" + b + "=";
            lblshow.Text += a + b;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            double a = Convert.ToDouble(textBox1.Text);
            double b = Convert.ToDouble(textBox2.Text);
            lblshow.Text = a + "-" + b + "=";
            lblshow.Text += a - b;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            double a = Convert.ToDouble(textBox1.Text);
            double b = Convert.ToDouble(textBox2.Text);
            lblshow.Text = a + "x" + b + "=";
            lblshow.Text += a * b;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            double a = Convert.ToDouble(textBox1.Text);
            double b = Convert.ToDouble(textBox2.Text);
            lblshow.Text = a + "/" + b + "=";
            lblshow.Text += a / b;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            double a = Convert.ToDouble(textBox1.Text);
            double b = Convert.ToDouble(textBox2.Text);
            lblshow.Text = a + "的" + b + "次方=";
            lblshow.Text += Math.Pow(a,b);
        }
    }
}

3.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        double[] arr = new double[5];
        int i = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            arr[i] = Convert.ToDouble(textBox1.Text);
            lblshow1.Text += arr[i]+" ";
            i++;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Array.Sort(arr);
            lblshow2.Text = "\n排序后的序列:";
            lblshow2.Text += arr[0] +" "+ arr[1] +" "+ arr[2] +" "+ arr[3] +" "+ arr[4];
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lblshow1.Text = "排序前的序列:";
        }
    }
}

  • 14
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值