c++编译的 计算器
问题是 运行之后为什么 显示的是地址 做运算 我应该怎么改正
#include
using namespace std;
#include
class calculator
{
public:
void Setnum1(int num1)
{
m_num1 = num1;
}
int Getnum1(void) const
{
return m_num1;
}
void Setnum2(int num2)
{
m_num2 = num2;
}
int Getnum2(void) const
{
return m_num2;
}
virtual int GetResult() = 0;
int m_num1;
int m_num2;
};
//加
class add :public calculator
{
public:
int GetResult()
{
return Getnum1() + Getnum2();
}
};
//减
class sub :public calculator
{
public:
int GetResult()
{
return Getnum1() - Getnum2();
}
};
//乘
class mul :public calculator
{
public:
int GetResult()
{
return Getnum1() * Getnum2();
}
};
//除
class divi :public calculator
{
public:
int GetResult()
{
if (Getnum2() == 0)
{
cout << "除数不可以为零。" << endl;
}
else
{
return Getnum1() / Getnum2();
}
}
};
class calculatorFactory
{
public:
static calculator* Create(char oper)
{
calculator poper = NULL;
switch (oper)
{
case '+':
poper = new add;
break;
case '-':
poper = new sub;
break;
case '':
poper = new mul;
break;
case '/':
poper = new divi;
break;
}
return poper;
}
};
int main()
{
cout << "请输入一个数字。"<<endl;
int num1;
cin >> num1;
cout << "输入运算符。" << endl;
char oper;
cin >> oper;
cout << "请输入一个数字。" << endl;
int num2;
cin >> num2;
calculator *poper = calculatorFactory::Create(oper);
poper->Setnum1(num1);
poper->Setnum2(num2);
cout << poper->Getnum1() << ' ' << oper << ' ' <Getnum2() << "=" << poper->GetResult() << endl;
system("pause");
return 0;
}
代码还是全放到代码快里面好,复制成文本显示后有些地方就不一样了。
总之,修改编译了一下,没什么问题了
#include<iostream>
using namespace std;
class calculator
{
public:
void Setnum1(int num1)
{
m_num1 = num1;
}
int Getnum1(void) const
{
return m_num1;
}
void Setnum2(int num2)
{
m_num2 = num2;
}
int Getnum2(void) const
{
return m_num2;
}
virtual int GetResult() = 0;
int m_num1;
int m_num2;
};
//加
class add :public calculator
{
public:
int GetResult()
{
return Getnum1() + Getnum2();
}
};
//减
class sub :public calculator
{
public:
int GetResult()
{
return Getnum1() - Getnum2();
}
};
//乘
class mul :public calculator
{
public:
int GetResult()
{
return Getnum1() * Getnum2();
}
};
//除
class divi :public calculator
{
public:
int GetResult()
{
if (Getnum2() == 0)
{
cout << "除数不可以为零。" << endl;
}
else
{
return Getnum1() / Getnum2();
}
}
};
class calculatorFactory
{
public:
static calculator* Create(char oper)
{
calculator *poper = NULL;
switch (oper)
{
case '+':
poper = new add;
break;
case '-':
poper = new sub;
break;
case '*':
poper = new mul;
break;
case '/':
poper = new divi;
break;
}
return poper;
}
};
int main()
{
cout << "请输入一个数字。" << endl;
int num1;
cin >> num1;
cout << "输入运算符。" << endl;
char oper;
cin >> oper;
cout << "请输入一个数字。" << endl;
int num2;
cin >> num2;
calculator *poper = calculatorFactory::Create(oper);
poper->Setnum1(num1);
poper->Setnum2(num2);
cout << poper->Getnum1() << ' ' << oper << ' ' <<poper->Getnum2() << "=" << poper->GetResult() << endl;
system("pause");
return 0;
}
代码修改如下:
#include <iostream>
using namespace std;
class calculator
{
public:
void Setnum1(int num1)
{
m_num1 = num1;
}
int Getnum1(void) const
{
return m_num1;
}
void Setnum2(int num2)
{
m_num2 = num2;
}
int Getnum2(void) const
{
return m_num2;
}
virtual int GetResult() = 0;
int m_num1;
int m_num2;
};
//加
class add :public calculator
{
public:
int GetResult()
{
return Getnum1() + Getnum2();
}
};
//减
class sub :public calculator
{
public:
int GetResult()
{
return Getnum1() - Getnum2();
}
};
//乘
class mul :public calculator
{
public:
int GetResult()
{
return Getnum1() * Getnum2();
}
};
//除
class divi :public calculator
{
public:
int GetResult()
{
if (Getnum2() == 0)
{
cout << "除数不可以为零。" << endl;
}
else
{
return Getnum1() / Getnum2();
}
}
};
class calculatorFactory
{
public:
static calculator* Create(char oper)
{
calculator* poper = NULL;
switch (oper)
{
case '+':
poper = new add;
break;
case '-':
poper = new sub;
break;
case '*':
poper = new mul;
break;
case '/':
poper = new divi;
break;
}
return poper;
}
};
int main()
{
cout << "请输入一个数字。"<<endl;
int num1;
cin >> num1;
cout << "输入运算符。" << endl;
char oper;
cin >> oper;
cout << "请输入一个数字。" << endl;
int num2;
cin >> num2;
calculator *poper = calculatorFactory::Create(oper);
poper->Setnum1(num1);
poper->Setnum2(num2);
cout << poper->Getnum1() << ' ' << oper << ' ' << poper->Getnum2() << "=" << poper->GetResult() << endl;
system("pause");
return 0;
}