第一次作业

计算器实验报告

一、基本功能描述

       简易计算器包括基本的四则运算(加、减、乘、除)及开方运算。

二、实验环境

       1.操作系统:Windows11

       2.开发工具:Visual Studio 2022

三、功能结构图

 

四、程序流程图

 

     1、加法

     2、减法

 

    3、乘法

    4、除法

 

    5、开方

五、各功能代码块

1、加法

#include<iostream>
using namespace std;

int main()
{
int a,b;
cout<<"请输入第一个数字:";
cin>>a;
cout<<"请输入第二个数字:";
cin>>b;
if(a>0&&b>0)
{
  cout<<a+b<<endl; 
}
else
   cout<<"请重新输入";
return 0; 
 }

2、减法

#include<iostream>
using namespace std;

int main()
{
int a,b;
cout<<"请输入第一个数字:";
cin>>a;
cout<<"请输入第二个数字:";
cin>>b;
if(a>0&&b>0&&a>=b)
{
  cout<<a-b<<endl; 
}
else
   cout<<"请重新输入";
return 0; 
 }

3、乘法

#include<iostream>
using namespace std;

int main()
{
int a,b;
cout<<"请输入第一个数字:";
cin>>a;
cout<<"请输入第二个数字:";
cin>>b;
if(a>0&&b>0)
{
  cout<<a*b<<endl; 
}
else
   cout<<"请重新输入";
return 0; 
 }

4、除法

#include<iostream>
using namespace std;

int main()
{
int a,b;
cout<<"请输入第一个数字:";
cin>>a;
cout<<"请输入第二个数字:";
cin>>b;
if(a>0&&b>0&&b!=0)
{
  cout<<a/b<<endl; 
}
else
   cout<<"请重新输入";
return 0; 
 }

5、开方

#include <stdio.h>
#include <math.h>
double pingfanggen(double x)
{
double a=x/2;
double b=(a+x/a)/2;
while(fabs(a-b)>0.0000001)//取绝对值 
{
a=b;
b=(a+x/a)/2; 
}
return b;
}
int main()
{
double x=0;
double result=0;
printf("输入需要计算的数字:");
scanf("%lf",&x);
if(x<0)
{
    printf("输入有误!"); 
}
else{
    result=pingfanggen(x);
    printf("结果是:%lf",result);
}
return 0;
}

六、软件设计

    1.设计思路

首先我们需要确定简易计算器的功能,主要包括加、减、乘、除及求根号的运算功能;其次我们需要确定计算器的界面设计。

计算器的界面设计应当尽量简单明了,不需要太花哨的效果。一般来说,界面分为两部分,上面是显示屏,下面是按键。在按键的设计中,我们需要显示出数字0~9、加减乘除等符号,此外我们还需要设计清除键和等号键,以便于进行清除和计算数据。

     2.设计流程图

 

   3.界面设计

1)筛选模板C#、Windows桌面,选择Windows窗体应用。

2)进入界面后拖动控件,Button为按钮,点击单个控件,右下角可修改其具体属性,TextBox为显示数据的地方。

3)修改名称、调整位置,右下角属性中可修改控件的名称,调整位置。

七、结论

   1、调试报告

     2、测试结果

  1)输入:36+25=

       输出:

(2) 输入:52-36=

 输出:

3)输入:12*5=

 输出:

4)输入:36/2=

     输出:

5)输入:√324

     输出:

3、关键源代码

using System.Text;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Threading.Tasks;

using System.Collections.Generic;

using System.ComponentModel;

using System.Runtime.InteropServices;

using System.Security.Cryptography.X509Certificates;

using System;

 

namespace WinFormsApp1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private double LeftNum, RightNum, Result;

        String Flag;

 

        private void button1_Click(object sender, EventArgs e)

        {

            textBox1.Text += "1";

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            textBox1.Text += "2";

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            textBox1.Text += "3";

        }

 

        private void button5_Click(object sender, EventArgs e)

        {

            textBox1.Text += "4";

        }

 

        private void button6_Click(object sender, EventArgs e)

        {

            textBox1.Text += "5";

        }

 

        private void button7_Click(object sender, EventArgs e)

        {

            textBox1.Text += "6";

        }

 

        private void button9_Click(object sender, EventArgs e)

        {

            textBox1.Text += "7";

        }

 

        private void button10_Click(object sender, EventArgs e)

        {

            textBox1.Text += "8";

        }

 

        private void button11_Click(object sender, EventArgs e)

        {

            textBox1.Text += "9";

        }

 

        private void button14_Click(object sender, EventArgs e)

        {

            textBox1.Text += "0";

        }

 

        private void button4_Click(object sender, EventArgs e)

        {

            LeftNum = Convert.ToDouble(textBox1.Text);

            Flag = "+";

            textBox1.Text = "";

        }

        private void button8_Click(object sender, EventArgs e)

        {

            LeftNum = Convert.ToDouble(textBox1.Text);

            Flag = "-";

            textBox1.Text = "";

        }

 

        private void button12_Click(object sender, EventArgs e)

        {

            LeftNum = Convert.ToDouble(textBox1.Text);

            Flag = "X";

            textBox1.Text = "";

        }

 

        private void button16_Click(object sender, EventArgs e)

        {

            LeftNum = Convert.ToDouble(textBox1.Text);

            Flag = "-";

            textBox1.Text = "";

        }

 

        private void button13_Click(object sender, EventArgs e)

        {

            LeftNum = Convert.ToDouble(textBox1.Text);

            Flag = "/";

            textBox1.Text = "";

        }

 

        private void From1_load(object sender, EventArgs e)

        {

 

        }

//牛顿迭代法解决算数平方根

        private void button17_Click_1(object sender, EventArgs e)

        {

            LeftNum = Convert.ToDouble(textBox1.Text);

            Flag = "sqrt";

            textBox1.Text = "";

            

                double a = LeftNum / 2;

                double b = (a + LeftNum / a) / 2;

                while (a - b > 0.0000001 || a - b < (-0.0000001))//取绝对值

                {

                    a = b;

                    b = (a + (LeftNum / a)) / 2;

                 }

                Result = b;

 

            

            textBox1.Text = Result.ToString();

        }

      

//加减乘除运算

        private void button15_Click(object sender, EventArgs e)

        {

           

            RightNum = Convert.ToDouble(textBox1.Text);

            if (Flag == "+")

                Result = LeftNum + RightNum;

            else if (Flag == "-")

                Result = LeftNum - RightNum;

            else if (Flag == "X")

                Result = LeftNum * RightNum;

            else if (Flag == "/")

                Result = LeftNum / RightNum;

            

 

           textBox1.Text = Result.ToString();

        }

       

    }

}

八、实验心得

    在大家的共同努力下,我们将此程序成功设计出来。在设计的过程中,我们学会使用visual studio 2022创建简易计算机界面,使用visio 绘制程序流程图,并了解到要设计一个大型程序,查找资料是至关重要的,在他人的基础上,再根据自己所学进行修改与调试,最后设计出自己想要的程序,这过程艰辛,但我认为只要我们持之以恒,成功指日可待。另外平时扎实的基础也很关键,当我们面对这么一个比较有难度的程序,可能会望而却步,看他人的程序都是个难点,更别说让我们自己去设计。为了解决此类问题,最好就是多向同学和老师请教,积极查找资料寻找解决办法。                                                                                                                                                                  

                                                                                                                                                                                                                                                  小组成员:陈菲、程雯静、吝佳妮

posted @ 2023-10-12 23:34  吝佳妮  阅读(62)  评论(0)    收藏  举报