标题:C#创计算器的问题,求解析,我跟我女朋友编了一天都编不出来0.0
只看楼主
飞鹰JACKEY
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-11-2
结帖率:100%
已结贴  问题点数:20 回复次数:3 
C#创计算器的问题,求解析,我跟我女朋友编了一天都编不出来0.0
设计并实现一个基于面向对象思想的简单计算器。软件界面可参考图。
(1)创建一个C#的工程项目,并在该工程的目录下分别新建两个文本文件“第一组数.txt”和“第二组数.txt”,并打开这两个文件,在其中一文件内输入并保存0,1,2,3,4,5,6,7,8,9另一文件内输入并保存0,10,20,30,40,50,60,70,80,90。注意,输入时每个数占一行,无需标点;

(2)定义一个计算器类Counter,在该类中定义5个函数方法分别实现两个整数的加、减、乘、除运算,和一个浮点数的开平方运算(提示:注意考虑除运算的分母和被开放数的有效性问题);

(3)在Counter类中定义一个名为batchAdd的方法实现两组整数的批量加运算(提示:可以通过设计两个数组作为参数,传递两组的需要计算的数值到类型中相应的函数方法中进行一一计算,结果存储于一个作为返回值的数值)。说明:运算效果形如,第一组数为1,3,5,8,9;第一组数为2,4,6,7,10;经该batchAdd方法处理后返回的结果里的值分别是3,7,11,15,19;

(4)在主调程序中,用程序在工程目录下创建一个文件batchResult.txt,定义计算器的对象实例进行相应计算展示,并将加、减、乘、除和开方的结果显示于界面结果显示的文本框;展示批量加运算时分别从文件“第一组数.txt”和“第二组数.txt”内读出两组数进行批量据计算,并将结果保存到文件batchResul.txt中(一个数占据文本文件的一行)。

搜索更多相关主题的帖子: 文本文件 计算器 女朋友 天都 
2012-11-02 12:10
smart0721
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:106
专家分:468
注 册:2012-2-10
得分:10 
相信第二步5个函数方法,你应该知道怎么写,第三步的批量加方法也类似,只是多个循环,不过是否要求两数组的元素数相等,如果允许不同元素数,那就要考虑多一些,这些写好之后定义实例来调用这些方法,还有文件的创建 和数据流的读入读出。
2012-11-06 23:54
mmxo
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:13
帖 子:189
专家分:1090
注 册:2012-11-7
得分:10 
回复 楼主 飞鹰JACKEY
仓促写了一个,按照我的理解应该能满足你的要求,如有不妥还望指教。不知道如何发图,所以没办法发UI图片上来,就看代码吧。批量加运算时,先打开两个批量文件输入数据保存好,然后点击“批量加运算结果”按钮,就会计算结果并保存到"BatchResult.txt"文件并用记事本打开。
程序代码:
//Counter.cs
using System;
using System.Collections.Generic; 

namespace BatchCalculator
{
    public class Counter
    {
        public static double Add(double value0, double value1)
        {
            return value0 + value1;
        }

        public static List<double> BatchAdd(List<double> values0, List<double> values1)
        {
            var result = new List<double>();
            if (values0.Count != values1.Count)
            {
                if (values0.Count > values1.Count)
                    values0 = values0.GetRange(0, values1.Count);
                else
                    values1 = values1.GetRange(0, values0.Count);
            }
            for (var i = 0; i < values0.Count; i++)
                result.Add(values0[i] + values1[i]);
            return result;
        } 

        public static double Dec(double value0, double value1)
        {
            return value0 - value1;
        } 

        public static double Multiply(double value0, double value1)
        {
            return value0 * value1;
        } 

        public static double Divide(double value0, double value1)
        {
            return value0 / value1;
        } 

        public static double Sqrt(double value0)
        {
            return Math.Sqrt(value0);
        }
    }
}
//FormMain.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using using System.Text;
using System.Windows.Forms; 

namespace BatchCalculator
{
    public partial class FormMain : Form
    {
        #region 常量

        private const string InvalidNumber = "很抱歉,您输入的数值超过了有效范围!";

        #endregion 

        #region 全局字段 

        private bool            _calculateCompleted;
        private List<double>    _numbers;
        private List<string>    _numberStrings;
        private List<string>    _operates;
        private string          _startupPath; 

        #endregion 

        #region 构造函数 

        public FormMain()
        {
            InitializeComponent();
            Init();
        }
        #endregion 

        #region 控件事件

        void ButCalculator_Click(object sender, EventArgs e)
        {
            var but = (Button) sender;
            var command = but.Text;
          
            switch (command)
            {
                case "0":
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "9":
                    {
                        if (_calculateCompleted)
                        {
                            _calculateCompleted = false;
                            _numberStrings.Clear();
                        }
                        _numberStrings.Add(command);
                        TbResult.Text = string.Concat(_numberStrings);
                    }
                    break; 

                case "Clear":
                    {
                        ClearInnerData();
                        TbResult.Clear();
                    }
                    break; 

                case "*":
                case "/":
                case "+":
                case "-":
                    {
                        if (_numberStrings.Count == 0) break;
                        double number;
                        if (!double.TryParse(string.Concat(_numberStrings), out number))
                        {
                            MessageBox.Show(InvalidNumber, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            _numberStrings.Clear();
                            break;
                        }
                        _operates.Add(command);
                        _numbers.Add(number);
                        _numberStrings.Clear();
                    }
                    break; 

                case "=":
                    {
                        if (_numberStrings.Count > 0)
                            _numbers.Add(double.Parse(string.Concat(_numberStrings)));
                        CalculateProcess();
                    }
                    break;
            }
        }
        void ButEditBatchDataFile_Click(object sender, EventArgs e)
        {
            var but = (Button) sender;
            var targetFile = but.Tag.ToString();
            if (targetFile == "BatchResult.txt")
            {
                var numberArray0 = GetNumberArrayFromFile(string.Concat(_startupPath, "第一组数.txt"));
                var numberArray1 = GetNumberArrayFromFile(string.Concat(_startupPath, "第二组数.txt"));
                try
                {
                    var batchAddResultArray = Counter.BatchAdd(numberArray0, numberArray1);
                    var sw = new StreamWriter(targetFile, false);
                    foreach (var number in batchAddResultArray)
                        sw.WriteLine(number);
                    sw.Close();
                }
                catch (Exception)
                {
                    MessageBox.Show(InvalidNumber, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
          
            var filePath = string.Concat(_startupPath, targetFile);
            if (!File.Exists(filePath))
                File.CreateText(filePath);
            Process.Start("notepad.exe", string.Concat(_startupPath, but.Tag.ToString()));
        } 

        #endregion 

        #region 私有方法 

        private void CalculateProcess()
        {
            if (_numbers.Count == 0) return;
            var number = _numbers[0];
            for (var i = 0; i < _operates.Count; i++)
            {
                var nextOperateNumber = _numbers[i + 1];
                try
                {
                    switch (_operates[i])
                    {
                        case "+":
                            number = Counter.Add(number, nextOperateNumber);
                            break;
                        case "-":
                            number = Counter.Dec(number, nextOperateNumber);
                            break;
                        case "*":
                            number = Counter.Multiply(number, nextOperateNumber);
                            break;
                        case "/":
                            number = Counter.Divide(number, nextOperateNumber);
                            break;
                    }
                }
                catch
                {
                    MessageBox.Show(InvalidNumber, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    break;
                }
            }
            TbResult.Text = number.ToString(CultureInfo.InvariantCulture);
            ClearInnerData();
            foreach (var c in number.ToString(CultureInfo.InvariantCulture))
                _numberStrings.Add(c.ToString(CultureInfo.InvariantCulture));
            _calculateCompleted = true;
        } 

        private void ClearInnerData()
        {
            _numberStrings.Clear();
            _numbers.Clear();
            _operates.Clear();
        } 

        private void Init()
        {
            _startupPath = string.Concat(Application.StartupPath, Path.DirectorySeparatorChar);
            _numberStrings = new List<string>();
            _numbers = new List<double>();
            _operates      = new List<string>();
            foreach (Button but in GbBatchData.Controls)
                but.Click += ButEditBatchDataFile_Click;
            foreach (var control in Controls)
            {
                if (!(control is Button)) continue;
                var but = (Button) control;
                but.Click += ButCalculator_Click;
            }
        }
      
        #endregion 

        #region 静态私有方法 

        private static List<double> GetNumberArrayFromFile(string filePath)
        {
            var result = new List<double>();
            var sr = new StreamReader(filePath, Encoding.Default);
            while (true)
            {
                var line = sr.ReadLine();
                if (string.IsNullOrWhiteSpace(line)) break;
                double Number;
                if (!double.TryParse(line.Trim(' '), out Number))
                    continue;
                result.Add(Number);
            }
            return result;
        } 

        #endregion
    }
}
//FormMain.Designer.cs
namespace BatchCalculator
{
    partial class FormMain
    {
        private  components = null;
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))components.Dispose();
            base.Dispose(disposing);
        }
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.GbBatchData = new System.Windows.Forms.GroupBox();
            this.button3 = new System.Windows.Forms.Button();
            this.TbResult = new System.Windows.Forms.TextBox();
            this.button4 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.button6 = new System.Windows.Forms.Button();
            this.button7 = new System.Windows.Forms.Button();
            this.button8 = new System.Windows.Forms.Button();
            this.button9 = new System.Windows.Forms.Button();
            this.button10 = new System.Windows.Forms.Button();
            this.button11 = new System.Windows.Forms.Button();
            this.button12 = new System.Windows.Forms.Button();
            this.button13 = new System.Windows.Forms.Button();
            this.button14 = new System.Windows.Forms.Button();
            this.button15 = new System.Windows.Forms.Button();
            this.button16 = new System.Windows.Forms.Button();
            this.button17 = new System.Windows.Forms.Button();
            this.button18 = new System.Windows.Forms.Button();
            this.button19 = new System.Windows.Forms.Button();
            this.GbBatchData.SuspendLayout();
            this.SuspendLayout();
            this.button1.Location = new System.Drawing.Point(6, 22);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(131, 23);
            this.button1.TabIndex = 0;
            this.button1.Tag = "第一组数.txt";
            this.button1.Text = "编辑批量文件一(&A)";
            this.button1.UseVisualStyleBackColor = true;
            this.button2.Location = new System.Drawing.Point(6, 51);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(131, 23);
            this.button2.TabIndex = 1;
            this.button2.Tag = "第二组数.txt";
            this.button2.Text = "编辑批量文件二(&B)";
            this.button2.UseVisualStyleBackColor = true;
            this.GbBatchData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.GbBatchData.Controls.Add(this.button3);
            this.GbBatchData.Controls.Add(this.button1);
            this.GbBatchData.Controls.Add(this.button2);
            this.GbBatchData.Location = new System.Drawing.Point(12, 65);
            this.GbBatchData.Name = "GbBatchData";
            this.GbBatchData.Size = new System.Drawing.Size(143, 107);
            this.GbBatchData.TabIndex = 2;
            this.GbBatchData.TabStop = false;
            this.GbBatchData.Text = "批数据文件";
            this.button3.Location = new System.Drawing.Point(6, 79);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(131, 23);
            this.button3.TabIndex = 2;
            this.button3.Tag = "BatchResult.txt";
            this.button3.Text = "批量加运算结果(&C)";
            this.button3.UseVisualStyleBackColor = true;
            this.TbResult.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.TbResult.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.TbResult.Location = new System.Drawing.Point(18, 12);
            this.TbResult.Name = "TbResult";
            this.TbResult.ReadOnly = true;
            this.TbResult.Size = new System.Drawing.Size(365, 30);
            this.TbResult.TabIndex = 3;
            this.TbResult.Text = "0";
            this.TbResult.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            this.button4.Location = new System.Drawing.Point(169, 58);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(49, 23);
            this.button4.TabIndex = 4;
            this.button4.Tag = "";
            this.button4.Text = "7";
            this.button4.UseVisualStyleBackColor = true;
            this.button5.Location = new System.Drawing.Point(224, 58);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(49, 23);
            this.button5.TabIndex = 5;
            this.button5.Text = "8";
            this.button5.UseVisualStyleBackColor = true;
            this.button6.Location = new System.Drawing.Point(279, 58);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(49, 23);
            this.button6.TabIndex = 6;
            this.button6.Text = "9";
            this.button6.UseVisualStyleBackColor = true;
            this.button7.Location = new System.Drawing.Point(334, 58);
            this.button7.Name = "button7";
            this.button7.Size = new System.Drawing.Size(49, 23);
            this.button7.TabIndex = 7;
            this.button7.Text = "Clear";
            this.button7.UseVisualStyleBackColor = true;
            this.button8.Location = new System.Drawing.Point(334, 87);
            this.button8.Name = "button8";
            this.button8.Size = new System.Drawing.Size(49, 23);
            this.button8.TabIndex = 11;
            this.button8.Text = "*";
            this.button8.UseVisualStyleBackColor = true;
            this.button9.Location = new System.Drawing.Point(279, 87);
            this.button9.Name = "button9";
            this.button9.Size = new System.Drawing.Size(49, 23);
            this.button9.TabIndex = 10;
            this.button9.Text = "6";
            this.button9.UseVisualStyleBackColor = true;
            this.button10.Location = new System.Drawing.Point(224, 87);
            this.button10.Name = "button10";
            this.button10.Size = new System.Drawing.Size(49, 23);
            this.button10.TabIndex = 9;
            this.button10.Text = "5";
            this.button10.UseVisualStyleBackColor = true;
            this.button11.Location = new System.Drawing.Point(169, 87);
            this.button11.Name = "button11";
            this.button11.Size = new System.Drawing.Size(49, 23);
            this.button11.TabIndex = 8;
            this.button11.Text = "4";
            this.button11.UseVisualStyleBackColor = true;
            this.button12.Location = new System.Drawing.Point(334, 116);
            this.button12.Name = "button12";
            this.button12.Size = new System.Drawing.Size(49, 23);
            this.button12.TabIndex = 15;
            this.button12.Text = "/";
            this.button12.UseVisualStyleBackColor = true;
            this.button13.Location = new System.Drawing.Point(279, 116);
            this.button13.Name = "button13";
            this.button13.Size = new System.Drawing.Size(49, 23);
            this.button13.TabIndex = 14;
            this.button13.Text = "3";
            this.button13.UseVisualStyleBackColor = true;
            this.button14.Location = new System.Drawing.Point(224, 116);
            this.button14.Name = "button14";
            this.button14.Size = new System.Drawing.Size(49, 23);
            this.button14.TabIndex = 13;
            this.button14.Text = "2";
            this.button14.UseVisualStyleBackColor = true;
            this.button15.Location = new System.Drawing.Point(169, 116);
            this.button15.Name = "button15";
            this.button15.Size = new System.Drawing.Size(49, 23);
            this.button15.TabIndex = 12;
            this.button15.Text = "1";
            this.button15.UseVisualStyleBackColor = true;
            this.button16.Location = new System.Drawing.Point(334, 144);
            this.button16.Name = "button16";
            this.button16.Size = new System.Drawing.Size(49, 23);
            this.button16.TabIndex = 19;
            this.button16.Text = "=";
            this.button16.UseVisualStyleBackColor = true;
            this.button17.Location = new System.Drawing.Point(279, 144);
            this.button17.Name = "button17";
            this.button17.Size = new System.Drawing.Size(49, 23);
            this.button17.TabIndex = 18;
            this.button17.Text = "-";
            this.button17.UseVisualStyleBackColor = true;
            this.button18.Location = new System.Drawing.Point(224, 144);
            this.button18.Name = "button18";
            this.button18.Size = new System.Drawing.Size(49, 23);
            this.button18.TabIndex = 17;
            this.button18.Text = "+";
            this.button18.UseVisualStyleBackColor = true;
            this.button19.Location = new System.Drawing.Point(169, 144);
            this.button19.Name = "button19";
            this.button19.Size = new System.Drawing.Size(49, 23);
            this.button19.TabIndex = 16;
            this.button19.Text = "0";
            this.button19.UseVisualStyleBackColor = true;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(395, 182);
            this.Controls.Add(this.button16);
            this.Controls.Add(this.button17);
            this.Controls.Add(this.button18);
            this.Controls.Add(this.button19);
            this.Controls.Add(this.button12);
            this.Controls.Add(this.button13);
            this.Controls.Add(this.button14);
            this.Controls.Add(this.button15);
            this.Controls.Add(this.button8);
            this.Controls.Add(this.button9);
            this.Controls.Add(this.button10);
            this.Controls.Add(this.button11);
            this.Controls.Add(this.button7);
            this.Controls.Add(this.button6);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.TbResult);
            this.Controls.Add(this.GbBatchData);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FormMain";
            this.ShowIcon = false;
            this.Text = "Batch Calculator by ";
            this.GbBatchData.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.GroupBox GbBatchData;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.TextBox TbResult;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button5;
        private System.Windows.Forms.Button button6;
        private System.Windows.Forms.Button button7;
        private System.Windows.Forms.Button button8;
        private System.Windows.Forms.Button button9;
        private System.Windows.Forms.Button button10;
        private System.Windows.Forms.Button button11;
        private System.Windows.Forms.Button button12;
        private System.Windows.Forms.Button button13;
        private System.Windows.Forms.Button button14;
        private System.Windows.Forms.Button button15;
        private System.Windows.Forms.Button button16;
        private System.Windows.Forms.Button button17;
        private System.Windows.Forms.Button button18;
        private System.Windows.Forms.Button button19;
    }
}


 

[ 本帖最后由 mmxo 于 2012-11-9 17:22 编辑 ]

为提高中华编程水平而奋斗
2012-11-07 11:25
张京奎
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2012-11-10
得分:0 
高手就是猛
2012-11-11 10:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-385608-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.073099 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved