一道C++题目,求问,拜托了
DefineaclassCalculatorlikethis:classCalculator{doubleoperand1,operand2;char*operation...
Define a class Calculator like this:
class Calculator{
double operand1,operand2;
char *operation;
public:
Calculator(double,double,char*);
double doOperation();
};
You must put the class definition in a header file( such asCalculator.h), and implement it in a source-code file( such asCalculator.cpp), and use it to create an object, execute the member function doOperation(), and then display the result inmain().
Hint: the function main() will look like this:
int main(int argc,char *argv[]){
if(argc!=4){
cout << "You must give 4 arguments!" << endl;
return 1;
}
double op1,op2;
char *opcode;
op1=...
op2=...
opcode=...
Calculator one(op1,op2,opcode);
cout << one.doOperation() << endl;
return 0;
}
If we input "calculator div 1.1 2.2" in command line, we will see the result 0.5. 展开
class Calculator{
double operand1,operand2;
char *operation;
public:
Calculator(double,double,char*);
double doOperation();
};
You must put the class definition in a header file( such asCalculator.h), and implement it in a source-code file( such asCalculator.cpp), and use it to create an object, execute the member function doOperation(), and then display the result inmain().
Hint: the function main() will look like this:
int main(int argc,char *argv[]){
if(argc!=4){
cout << "You must give 4 arguments!" << endl;
return 1;
}
double op1,op2;
char *opcode;
op1=...
op2=...
opcode=...
Calculator one(op1,op2,opcode);
cout << one.doOperation() << endl;
return 0;
}
If we input "calculator div 1.1 2.2" in command line, we will see the result 0.5. 展开
2个回答
展开全部
//程序基本测试通过,建议用运算符重载实现运算
//Calc.h
#ifndef _CALCULATOR_H_
#define _CALCULATOR_H_
class Calculator{
private:
double operand1,operand2;
char *operation;
public:
Calculator(double op1,double op2,char* operation);
double doOperation();
};
#endif
//calc.cpp
#include "calc.h"
#include <afx.h>
#include <iostream>
using namespace std;
Calculator::Calculator(double op1,double op2,char* operation){
operand1=op1;
operand2=op2;
this->operation=operation;
}
double Calculator::doOperation(){
CString op;
double result;
op.Format("%s",operation);
if(op=="div")
{
if(operand2==0)
{
cout << "operand2 is zero!" << endl;
result=0;
}
else
result=operand1/operand2;
}
else if(op=="add")
result=operand1+operand2;
else if(op=="sub")
result=operand1-operand2;
else if(op=="mul")
result=operand1*operand2;
return result;
}
//main.cpp
#include "calc.h"
#include <iostream>
using namespace std;
int main(int argc,char* argv[])
{
if(argc!=4){
cout << "You must give 4 arguments!" << endl;
return 1;
}
double op1,op2;
char *opcode;
op1=atof(argv[1]);
op2=atof(argv[2]);
opcode=argv[3];
Calculator one(op1,op2,opcode);
cout << one.doOperation() << endl;
return 0;
}
//Calc.h
#ifndef _CALCULATOR_H_
#define _CALCULATOR_H_
class Calculator{
private:
double operand1,operand2;
char *operation;
public:
Calculator(double op1,double op2,char* operation);
double doOperation();
};
#endif
//calc.cpp
#include "calc.h"
#include <afx.h>
#include <iostream>
using namespace std;
Calculator::Calculator(double op1,double op2,char* operation){
operand1=op1;
operand2=op2;
this->operation=operation;
}
double Calculator::doOperation(){
CString op;
double result;
op.Format("%s",operation);
if(op=="div")
{
if(operand2==0)
{
cout << "operand2 is zero!" << endl;
result=0;
}
else
result=operand1/operand2;
}
else if(op=="add")
result=operand1+operand2;
else if(op=="sub")
result=operand1-operand2;
else if(op=="mul")
result=operand1*operand2;
return result;
}
//main.cpp
#include "calc.h"
#include <iostream>
using namespace std;
int main(int argc,char* argv[])
{
if(argc!=4){
cout << "You must give 4 arguments!" << endl;
return 1;
}
double op1,op2;
char *opcode;
op1=atof(argv[1]);
op2=atof(argv[2]);
opcode=argv[3];
Calculator one(op1,op2,opcode);
cout << one.doOperation() << endl;
return 0;
}
追答
把MFC库加到工程里,VC6是如下配置,别的版本你自己找找
Project->Setting...->General->Microsoft Foundation classes-> Using MFC in a shared DLL
展开全部
// Calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
class Calculator {
double operand1, operand2;
char *operation;
public:
Calculator(double, double, char*);
double doOperation();
};
#endif
// Calculator.cpp
#include "Calculator.h"
#include <cstring>
Calculator::Calculator(double op1, double op2, char *op) {
operand1 = op1;
operand2 = op2;
operation = op;
}
double Calculator::doOperation() {
if (strcmp(operation, "add") == 0) {
return operand1 + operand2;
}
else if (strcmp(operation, "sub") == 0) {
return operand1 - operand2;
}
else if (strcmp(operation, "mul") == 0) {
return operand1 * operand2;
}
else if (strcmp(operation, "div") == 0) {
return operand1 / operand2;
}
}
// main.cpp
#include "Calculator.h"
#include <iostream>
using namespace std;
int main(int argc, char *argv[]){
if (argc != 4){
cout << "You must give 4 arguments!" << endl;
return 1;
}
double op1, op2;
char *opcode;
sscanf(argv[2], "%lf", &op1);
sscanf(argv[3], "%lf", &op2);
opcode = argv[1];
Calculator one(op1, op2, opcode);
cout << one.doOperation() << endl;
return 0;
}
来自:求助得到的回答
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询