c#控制台应用程序中的简单计算器类
本文关键字:简单 计算器 控制台 应用程序 | 更新日期: 2023-09-27 18:19:13
我是c#的初学者,我有一个家庭作业,所以我需要你的帮助,这是我现在所做的,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleCalculator{
class SimpleCalculator {
double number1;
double number2;
double result;
double stored;
Boolean isStored;
public SimpleCalculator() {}
public double Add()
{
result = (this.number1 + this.number2);
return result;
}
public double Subtract()
{
result = (this.number1 - this.number2);
return result;
}
public double Multiply()
{
result = (this.number1 * this.number2);
return result;
}
public double Divide()
{
result = ((this.number1) /( this.number2));
return result;
}
public double SquareRootNumber1()
{
result = Math.Sqrt(this.number1);
return result;
}
public double SquareRootNumber2()
{
result = Math.Sqrt(this.number2);
return result;
}
public Boolean isMemoryUsed()
{
if (this.stored == 0){
this.isStored=false ;
return isStored;
}
else
this.isStored = true;
return this.isStored;
}
public double MemoryStore(double paramater_store1) {
this.stored = this.stored + paramater_store1;
return this.stored;
}
public double MemoryRecall()
{
if (MemoryStore(this.stored)==0)
return 0;
else
return MemoryStore(this.stored);
}
public double MemoryAdd(double paramater_store2)
{
this.stored = this.stored + paramater_store2 ;
return this.stored;
}
public double MemorySubtract(double paramater_store3)
{
this.stored = this.stored - paramater_store3;
return this.stored;
}
public double MemoryClear()
{
return this.stored = 0;
}
static void Main(string[] args)
{
SimpleCalculator calc = new SimpleCalculator();
calc.number1 = 3.14;
calc.number2 = 5.78;
Console.WriteLine("A is {0}", calc.number1);
Console.WriteLine("B is {0}", calc.number2);
double result = calc.Add();
Console.WriteLine("A + B = {0}", result);
result = calc.Subtract();
Console.WriteLine("A - B = {0}", result);
result = calc.Multiply();
Console.WriteLine("A * B = {0}", result);
Console.WriteLine("Sqrt(A) = {0}", calc.SquareRootNumber1());
Console.WriteLine("Sqrt(B) = {0}", calc.SquareRootNumber2());
Console.WriteLine("A = {0}", calc.number1);
Console.WriteLine("B = {0}", calc.number2);
try
{
result = calc.Divide();
Console.WriteLine("A / B = {0}", result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
calc.number1=3.14;
calc.number2=0 ;
Console.WriteLine("A is {0} ", calc.number1);
Console.WriteLine("B is {0} ", calc.number2);
result = calc.Divide();
try {
result = calc.Divide();
Console.WriteLine("A / B = {0}", result);
}
catch (Exception ex) {
Console.WriteLine("The divisor can’t be zero ",ex.Message);
}
Console.WriteLine("Is memory being used at the beginning? {0}", calc.isMemoryUsed());
calc.MemoryStore(3.16);
Console.WriteLine("Is memory being used after MemoryStore? {0}", calc.isMemoryUsed());
Console.WriteLine("Memory = {0}", calc.MemoryRecall());
calc.MemoryAdd(5.67);
Console.WriteLine("Is memory being used after MemoryAdd? {0}", calc.isMemoryUsed());
Console.WriteLine("Memory = {0}", calc.MemoryRecall());
calc.MemorySubtract(7.89);
Console.WriteLine("Is memory being used after MemorySubtract? {0}", calc.isMemoryUsed());
Console.WriteLine("Memory = {0}", calc.MemoryRecall());
calc.MemoryClear();
Console.WriteLine("Is memory being used after MemoryClear? {0}", calc.isMemoryUsed());
calc.MemoryAdd(5.67);
Console.WriteLine("Is memory being used after MemoryAdd? {0}", calc.isMemoryUsed());
Console.WriteLine("Memory = {0}", calc.MemoryRecall());
calc.MemoryClear();
Console.WriteLine("Is memory being used after MemoryClear? {0}", calc.isMemoryUsed());
calc.MemorySubtract(7.89);
Console.WriteLine("Is memory being used after MemorySubtract? {0}", calc.isMemoryUsed());
Console.WriteLine("Memory = {0}", calc.MemoryRecall());
}
}
}
问题是,我得到错误的结果:这是我的结果,但答案必须是不同的!!!!!!!!但是,应该是这样的有一些方法应该具有这些特性:
以下是一些重要的问题:
意外的结果是一旦你测试内存,对吗?
1) MemoryStore不存储参数,它将参数添加到当前内存内容,这是你想要的吗?
2) Memory recall是这样写的
public double MemoryRecall()
{
if (MemoryStore(this.stored)==0)
return 0;
else
return MemoryStore(this.stored);
}
与
相同public double MemoryRecall()
{
return MemoryStore(this.stored);
}