实验11_参考答案
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
double SimpleCalculator::add(double a,double b)const
{
return a + b;
}
double SimpleCalculator::substract( double a, double b ) const
{
return a - b;
}
double SimpleCalculator::multiply( double a, double b ) const
using namespace std;
int main()
{
double a=10.0;
double b=20.0;
SimpleCalculator sc;//声明SimpleCalculator类的对象sc
cout << "The value of a is: "<<a<<endl;
cout <<"The value of b is: " << b<< endl;
cout <<“Multiplying a and b yields“<< multiplication << endl;
/*写一条语句,声明变量division,通过SimpleCalculator类对象sc计算a/b,并将结果赋值给division变量*/
cout <<“Dividing a by b yields“<< division<< endl;
double substraction = sc.substract ( a, b );
cout << "Subtracting b from a "<< substraction << endl;
double multiplication = sc.multiply ( a, b );
cout << "Multiplying a and b yields "<< multiplication << endl;
{
return a * b;
}
double SimpleCalculator::divide(double a,double b)const
{
return a / b;
}
// CalcDriver.cpp
#include < iostream>
#include "SimpleCalculator.h"
{wenku.baidu.com
return a*b;
}
//补充divide方法的定义
// CalcDriver.cpp
#include < iostream>
#include“SimpleCalculator.h”
using namespace std;
int main()
{
double a=10.0;
double b=20.0;
// SimpleCalculator.h
class SimpleCalculator {
public:
/*补充add方法的原型声明*/
double subtract ( double, double ) const;
double multiply( double, double ) const;
/*补充divide方法的原型声明*/
实验
请将自己报告部分,文字和程序代码,加粗
完成下面实验内容和实验要求。
1.编程练习:简易计算器
⑴编写一个SimpleCalculator类,包含一些public方法来对两个double型变量进行加减乘除运算。
⑵实例输出:
⑶将下面程序代码补充完整,并运行(注意:共有3个文件:SimpleCalculator.h,SimpleCalculator.cpp,CalcDriver.cpp,此3个文件放到一个工程中进行调试)
};
// SimpleCalculator.cpp
#include“SimpleCalculator.h”
//补充add方法的定义
double SimpleCalculator::subtract( double a, double b ) const
{
return a–b;
}
double SimpleCalculator::multiply( double a, double b ) const
cout <<“Adding a and b yields“<< addition << endl;
double substraction = sc.substract ( a, b );
cout <<“Subtracting b from a“<< subtraction << endl;
double multiplication = sc.multiply ( a, b );
double multiply( double, double ) const;
double divide( double, double ) const;
/*补充divide方法的原型声明*/
};
// SimpleCalculator.cpp
#include "SimpleCalculator.h"
/*写一条语句,声明变量division,通过SimpleCalculator类对象sc计算a/b,并将结果赋值给division变量*/
//声明SimpleCalculator类的对象sc
cout <<“The value of a is:“<<a<<endl;
cout <<”The value of b is:“<< b<< endl;
/*写一条语句,声明变量addition,通过SimpleCalculator类对象sc计算a+b,并将结果赋值给addition变量*/
/*写一条语句,声明变量addition,通过SimpleCalculator类对象sc计算a+b,并将结果赋值给addition变量*/
double addition = sc.add ( a, b );
cout << "Adding a and b yields "<< addition << endl;
return 0;
}
①补充后代码:
// SimpleCalculator.h
class SimpleCalculator {
public:
/*补充add方法的原型声明*/
double add( double, double ) const;
double substract( double, double ) const;
{
return a + b;
}
double SimpleCalculator::substract( double a, double b ) const
{
return a - b;
}
double SimpleCalculator::multiply( double a, double b ) const
using namespace std;
int main()
{
double a=10.0;
double b=20.0;
SimpleCalculator sc;//声明SimpleCalculator类的对象sc
cout << "The value of a is: "<<a<<endl;
cout <<"The value of b is: " << b<< endl;
cout <<“Multiplying a and b yields“<< multiplication << endl;
/*写一条语句,声明变量division,通过SimpleCalculator类对象sc计算a/b,并将结果赋值给division变量*/
cout <<“Dividing a by b yields“<< division<< endl;
double substraction = sc.substract ( a, b );
cout << "Subtracting b from a "<< substraction << endl;
double multiplication = sc.multiply ( a, b );
cout << "Multiplying a and b yields "<< multiplication << endl;
{
return a * b;
}
double SimpleCalculator::divide(double a,double b)const
{
return a / b;
}
// CalcDriver.cpp
#include < iostream>
#include "SimpleCalculator.h"
{wenku.baidu.com
return a*b;
}
//补充divide方法的定义
// CalcDriver.cpp
#include < iostream>
#include“SimpleCalculator.h”
using namespace std;
int main()
{
double a=10.0;
double b=20.0;
// SimpleCalculator.h
class SimpleCalculator {
public:
/*补充add方法的原型声明*/
double subtract ( double, double ) const;
double multiply( double, double ) const;
/*补充divide方法的原型声明*/
实验
请将自己报告部分,文字和程序代码,加粗
完成下面实验内容和实验要求。
1.编程练习:简易计算器
⑴编写一个SimpleCalculator类,包含一些public方法来对两个double型变量进行加减乘除运算。
⑵实例输出:
⑶将下面程序代码补充完整,并运行(注意:共有3个文件:SimpleCalculator.h,SimpleCalculator.cpp,CalcDriver.cpp,此3个文件放到一个工程中进行调试)
};
// SimpleCalculator.cpp
#include“SimpleCalculator.h”
//补充add方法的定义
double SimpleCalculator::subtract( double a, double b ) const
{
return a–b;
}
double SimpleCalculator::multiply( double a, double b ) const
cout <<“Adding a and b yields“<< addition << endl;
double substraction = sc.substract ( a, b );
cout <<“Subtracting b from a“<< subtraction << endl;
double multiplication = sc.multiply ( a, b );
double multiply( double, double ) const;
double divide( double, double ) const;
/*补充divide方法的原型声明*/
};
// SimpleCalculator.cpp
#include "SimpleCalculator.h"
/*写一条语句,声明变量division,通过SimpleCalculator类对象sc计算a/b,并将结果赋值给division变量*/
//声明SimpleCalculator类的对象sc
cout <<“The value of a is:“<<a<<endl;
cout <<”The value of b is:“<< b<< endl;
/*写一条语句,声明变量addition,通过SimpleCalculator类对象sc计算a+b,并将结果赋值给addition变量*/
/*写一条语句,声明变量addition,通过SimpleCalculator类对象sc计算a+b,并将结果赋值给addition变量*/
double addition = sc.add ( a, b );
cout << "Adding a and b yields "<< addition << endl;
return 0;
}
①补充后代码:
// SimpleCalculator.h
class SimpleCalculator {
public:
/*补充add方法的原型声明*/
double add( double, double ) const;
double substract( double, double ) const;