C# 简单计算器设计 & 字符提取和整数整除练习

Posted 一身千寻瀑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 简单计算器设计 & 字符提取和整数整除练习相关的知识,希望对你有一定的参考价值。

简单计算器设计

-----------------------

如图所示的页面,当用户选择不同的运算类型时,下方文本框发生相应的变化。

若存在非法输入,文本框输出结果为“?”。



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 A._2._2_WinForm

    public partial class Form1 : Form
    
        public Form1()
        
            InitializeComponent();
        

        private void label1_Click(object sender, EventArgs e)
        

        

        private void button1_Click(object sender, EventArgs e)
        

            if(radioButton1.Checked)
            
                double sum, number1, number2;
                bool is1 = double.TryParse(textBox1.Text,out number1);
                bool is2 = double.TryParse(textBox2.Text,out number2);
                if (is1 & is2)
                
                    sum = number1 + number2;
                    textBox3.Text = sum.ToString();
                
                else
                    textBox3.Text = "?";
            
            else if (radioButton2.Checked)
            
                double minus, number1, number2;
                bool is1 = double.TryParse(textBox1.Text,out number1);
                bool is2 = double.TryParse(textBox2.Text,out number2);
                if (is1 & is2)
                
                    minus = number1 - number2;
                    textBox3.Text = minus.ToString();
                
                else
                    textBox3.Text = "?";

            
            else if (radioButton3.Checked)
            
                double multiplate, number1, number2;
                bool is1 = double.TryParse(textBox1.Text, out number1);
                bool is2 = double.TryParse(textBox2.Text,out number2);
                if (is1 & is2)
                
                    multiplate = number1 * number2;
                    textBox3.Text = multiplate.ToString();
                
                else
                    textBox3.Text = "?";
            
            else if (radioButton4.Checked)
            
                double divide,number1,number2;
                bool is1 = double.TryParse(textBox1.Text,out number1);
                bool is2 = double.TryParse(textBox2.Text,out number2);
                if (is1 & is2)
                
                    divide = number1 / number2;
                    textBox3.Text = divide.ToString();
                
                else
                    textBox3.Text = "?";
            
            else if (radioButton5.Checked)
            
                double modulo, number1, number2;
                bool is1 = double.TryParse(textBox1.Text, out number1);
                bool is2 = double.TryParse(textBox2.Text, out number2);
                if(is1&is2)
                
                    modulo = number1 % number2;
                    textBox3.Text = modulo.ToString();
                
                else
                    textBox3.Text = "?";
            
            else
            
                DialogResult result = MessageBox.Show("请选择运算类型!");
            

        

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        
            this.label3.Text = "加法";
            this.label4.Text = "+";
        

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        
            this.label3.Text = "减法";
            this.label4.Text = "-";
        

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        
            this.label3.Text = "乘法";
            this.label4.Text = "×";
        

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        
            this.label3.Text = "除法";
            this.label4.Text = "÷";
        

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        
            this.label3.Text = "取模";
            this.label4.Text = "|";
        
    
Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace A._2._2_WinForm

    static class Program
    
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        
    



---------------------------------------------------------------------------------------------


字符提取和整数整除练习

-----------------------

控制台输入如下信息,功能如图所示。

使用两种方法,字符提取法(提取单个字符进行类型转换为整型再逐个相加)和整数整除法(取整和求余数来取得每一位的值再逐个相加)。




Method1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A._2._3_Console.Method1

    class Method1
    
        //字符提取法
        public void func(int number)
        
            string str = number.ToString();
            int sum = 0;
     
            Console.WriteLine("该整数共有0位。", str.Length);
            Console.Write("实现思路1:每一位的值为");
            for (int i = 0; i < str.Length; ++i)
            
                sum += int.Parse(str[i].ToString());
                if(i==str.Length-1)
                    Console.Write(str[i] + ",");
                else
                    Console.Write(str[i]+"、");
            
            Console.WriteLine("这些位之和为0", sum);
        
    


Method2.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A._2._3_Console.Method2

    class Method2
    
        //整数整除法
        public void func(int number)
        
            string str = number.ToString();
            int sum = 0;

            Console.Write("实现思路2:每一位的值为");
            for (int i = 0; i < str.Length; ++i)
            
                if (i == str.Length - 1)
                    Console.Write(str[i] + ",");
                else
                    Console.Write(str[i] + "、");
            

            int[] arr = new int[10000];
            int j = 0;
            while(number / 10 != 0)
            
                arr[j] = number % 10;
                number /= 10;               
                ++j;
            
            arr[j] = number % 10;
            for (int i = j; i >= 0; --i)
            
                sum += arr[i]; 
            
            Console.WriteLine("这些位之和为0", sum);
        
    


Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using A._2._3_Console.Method1;
using A._2._3_Console.Method2;

namespace ch

    class Program
    
        static void Main(string[] args)
        
            Console.Write("请输入一个大于100的整数:");
            int number = int.Parse(Console.ReadLine());

            new Method1().func(number); //字符提取法

            new Method2().func(number); //整数整除法

            Console.WriteLine("按任意键结束");
            Console.ReadKey();
        
    


-------------------

2016.3.14


以上是关于C# 简单计算器设计 & 字符提取和整数整除练习的主要内容,如果未能解决你的问题,请参考以下文章

如何从 NSRange 中提取整数值?

如何利用一条简单语句判断一个整数是否为2的整数次幂

如何在颤动中将十六进制颜色代码解析为它们各自的整数值

字符编码&格式化

从字符串S中提取每个位置的整数[重复]

正则表达式 c# 获取捕获组的子组