一道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.
展开
 我来答
boon1968
2014-05-25 · TA获得超过311个赞
知道小有建树答主
回答量:99
采纳率:0%
帮助的人:99.8万
展开全部
//程序基本测试通过,建议用运算符重载实现运算
//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
porker2008
推荐于2016-03-15 · TA获得超过1.4万个赞
知道大有可为答主
回答量:7066
采纳率:62%
帮助的人:1.1亿
展开全部
// 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;
}
来自:求助得到的回答
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式