c#数据类型转换

一.类型转换:从根本上说是类型铸造,或者说是把数据从一种类型转换为另一种类型。在 C# 中,类型铸造有两种形式:
1. 隐式类型转换:是 C# 默认的以安全方式进行的转换
2. 显示类型转换 :转换是通过用户使用预定义的函数显式完成的。显式转换需要强制转换运算符。
3. 类型转换方法 :

 

 4. 其他转换方法 
 int.Parse():该方法只能将 string 类型数字字符串强制转换为 int 类型
 (char)Num:整数前使用(char)将 Num 转换为 ASCII 码字符
5. 拆箱和装箱 
 装箱:将值类型转换为引用类型
 拆箱:将引用类型转换为值类型,前提是先将值类型转换为引用类型,也就是拆箱的前提是先装箱

案例:实现简单的计算器

计算器需求分析:
一、界面设计
      1.做一个显示屏
      2.17个按钮(0-9,+-×÷%=,CE)
二、需要实现的功能
      1.输入第一个数字
      2.选择运算类型
      3.输入第二个数字
      4.按下等号计算出结果,结果显示在显示屏上
三、实现步骤
      1.先做界面
        a.显示屏   textbox、listbox、label
        b.使用17个button,button上的文本改成对应的数字符号
      2.
        补充:申请两个int类型变量,第一个num1装第一个数字
                                                       第二个num2装第二个数字
        a.输入第一个数字,当点一个数字按钮,屏幕上显示一个,之前显示的数字在前面呆着
           a1.添加按钮的cilck事件
           a2.事件触发,将按钮代表的数字显示textbox1的text
        b.当输入符号的时候,清除屏幕,但是后台必须记录好第一个数字
           b1.添加符号按钮的click事件
           b2.当点任何一个符号按钮用一个变量装刚才输入的textbox1中的数字
        c.输入第二个数字
        d.按下等号按钮,显示屏上面的文本改变成两个数字的运算结果

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

namespace 计算器2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //1
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text+"1";
        }
        //2
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "2";
        }
        //3
        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "3";
        }
        //4
        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "4";
        }
        //5
        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "5";
        }
        //6
        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "6";
        }
        //7
        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "7";
        }
        //8
        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "8";
        }
        //9
        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "9";
        }
        //0
        private void button10_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + "0";
        }
        //装num1 的盒子
        int num1;
        //装num1 的盒子
        int num2;
        //装符号的盒子
        //方法就是将代码封装成一个代码块进行统一调用
        string fun;
        //+
        //重复代码封装:将重复代码全部选中,右击选择快速操作和重构,
        private void button11_Click(object sender, EventArgs e)
        {
            jisuan();
            fun = "jia";
        }

        private void jisuan()
        {
            num1 = Convert.ToInt32(textBox1.Text);
            textBox1.Text = "";
        }

        //-
        private void button12_Click(object sender, EventArgs e)
        {
            jisuan();
            fun = "jian";
        }
        //*
        private void button13_Click(object sender, EventArgs e)
        {
            jisuan();
            fun = "cheng";
        }
        //÷
        private void button14_Click(object sender, EventArgs e)
        {
            jisuan();
            fun = "chu";
        }
        //%
        private void button15_Click(object sender, EventArgs e)
        {
            jisuan();
            fun = "yu";
        }
        //=
        private void button16_Click(object sender, EventArgs e)
        {
            //textBox1.Text = num1.ToString();//测试num1的数据是否存储
            num2 = int.Parse(textBox1.Text);
            textBox1.Text = "";
            if (fun=="jia")
            {
                textBox1.Text = (num1 + num2).ToString();
            }
            if (fun == "jian")
            {
                textBox1.Text = (num1 - num2).ToString();
            }
            if (fun == "cheng")
            {
                textBox1.Text = (num1 * num2).ToString();
            }
            if (fun == "chu")
            {
                textBox1.Text = (num1 / num2).ToString();
            }
            if (fun == "yu")
            {
                textBox1.Text = (num1 % num2).ToString();
            }
        }
        //清除
        private void button17_Click(object sender, EventArgs e)
        {

                textBox1.Text = "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //设置光标放在某一控件上
            textBox1.TabStop = false;
            textBox1.TabIndex = 0;
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值