SimpleCalculator(for lab)
来源:互联网 发布:开网店用什么软件 编辑:程序博客网 时间:2024/05/20 21:44
SimpleCalculator(for lab)
标签(空格分隔): 程序设计实验 c++
本人学院
- SimpleCalculatorfor lab
- 标签空格分隔 程序设计实验 c
- 本人学院
- Description
- 读题
- my answer
- the standard answer
Description:
Write a SimpleCalculator class that has public methods for adding, subtracting, multiplying and dividing two
doubles. SimpleCalculator has a private data member called answer. After performing
an operation, assign the result to answer. And a member function named getAnswer that will retrieve the result of
the last arithmetic operation performed by the object. Also, add a constructor for class SimpleCalculator
that initializes the value of answer to 0.
Note: You may expect the divisor of the divide function will not equal 0.
Test.cpp
// Lab Exercise 1: CalcTest.cpp#include <iostream>using std::cout;using std::cin;using std::endl;#include "SimpleCalculator.h"int main() { double a; double b; cin >> a >> b; SimpleCalculator sc; // instantiate SimpleCalculator cout << "The value of a is: " << a << "\n"; cout << "The value of b is: " << b << "\n\n"; sc.add(a, b); cout << "Adding a and b yields " << sc.getAnswer() << "\n"; sc.subtract(a, b); cout << "Subtracting b from a yields " << sc.getAnswer() << "\n"; sc.multiply(a, b); cout << "Multiplying a by b yields " << sc.getAnswer() << "\n"; sc.divide(a, b); cout << "Dividing a by b yields " << sc.getAnswer() << endl;}
SimpleCalculator.h
// Lab Exercise 1: SimpleCalculator.h// class SimpleCalculator definitionclass SimpleCalculator { public: SimpleCalculator(); void add(double, double); void subtract(double, double); void multiply(double, double); void divide(double, double); double getAnswer() const; private: double answer;}; // end class SimpleCalculator
读题
my answer
#include<iostream>#include"SimpleCalculator.h"using namespace std;SimpleCalculator::SimpleCalculator() { answer = 0;}void SimpleCalculator::add(double a, double b) { answer = a + b;}void SimpleCalculator::subtract(double a, double b) { answer = a - b;}void SimpleCalculator::multiply(double a, double b) { answer = a * b;}void SimpleCalculator::divide(double a, double b) { answer = a / b;}double SimpleCalculator::getAnswer() const { return answer;}
the standard answer
// Lab Exercise 1: SimpleCalculator.cpp#include "SimpleCalculator.h"// default constructorSimpleCalculator::SimpleCalculator() { answer = 0;} // end class SimpleClassculator constructor// function add definitionvoid SimpleCalculator::add(double a, double b) { answer = a + b;} // end function add// function subtract definitionvoid SimpleCalculator::subtract(double a, double b) { answer = a - b;} // end function subtract// function multiply definitionvoid SimpleCalculator::multiply(double a, double b) { answer = a * b;} // end function multiply// function divide definitionvoid SimpleCalculator::divide(double a, double b) { answer = a / b;} // end function divide// return answerdouble SimpleCalculator::getAnswer() const { return answer;} // end function getAnswer
0 0
- SimpleCalculator(for lab)
- SimpleCalculator
- StringProblem(for lab)
- Complex number(for lab)
- poj3224 Go for Lab Cup!
- Account's Balance(for lab)
- ‘孤单北半球’ for DP Lab
- Hands on microsoft lab manual (for 2008)
- POJ 3224 Go for Lab Cup!
- poj 3224 Go for Lab Cup!
- poj 3224 Go for Lab Cup!
- 20140409 Complex again(for lab)
- Valve: The Lab Renderer for Unity
- (15 C++ Lab) Class for Time
- Set(DS)(15C++ for lab)
- POJ 3224 Go for Lab Cup! G++
- Lab
- LAB
- Oracle数据库有哪些应用结构?
- 最短路+定积分 csu1806 Toll
- 数组中出现次数超过一半的数字-基于Partitoin和数据规律
- Android —— GitHub和SourceTree使用
- __declspec(dllimport)的作用
- SimpleCalculator(for lab)
- [Linux] undefined reference to `itoa'
- Financial application- computing future tuition
- Java 之EnumSet与EnumMap
- 浅谈ssh(spring+struts+hibernate)框架的搭建
- nginx使用zookeeper造成进程死锁和崩溃问题和解决方案
- 序列化与反序列化 Socket中处理方法
- 单例模式详解
- swoole1.8.0+版本异步redis安装(本实例为swoole1.8.10版本)详解