设计要求:
(1)完善计算器程序,改为可对实数操作。
(2)完善程序,改为多个操作数基本四则运算,遇到0为止。
(3)增加函数,完成四则混合运算,增加相应的主菜单选项。
(4)添加语句,使四则运算具有测试功能。
(5)可扩充其功能。
#include <process.h>
#include <iostream.h>
#include <stdio.h>
#include <cmath>
template <class T>
class operate {
private:
T add(T x, T y);
T sub(T x, T y);
T div(T x, T y);
T mult(T x, T y);
T fartocel(T x);
T celtofar(T x);
T sqroot(T x);
T pow(T x, T y);
T log(T x);
public:
T number1,number2,answer,val;
void calcadd();
void calcsub();
void calcdiv();
void calcmult();
void calcfartocel();
void calcceltofar();
void calcsroot();
void calcpow();
void calclog();
void menu();
};
template<class T> T operate<T>::add(T x, T y)
{
val = x + y;
return val;
}
template<class T>T operate<T>::sub(T x, T y)
{
val = x - y;
return val;
}
template<class T>T operate<T>::div(T x, T y)
{
val = x / y;
return val;
}
template<class T>T operate<T>::mult(T x, T y)
{
val = x * y;
return val;
}
template<class T>T operate<T>::fartocel(T x)
{
T cel;
cel = ((x - 32) * 5) / 9;
return cel;
}
template<class T>T operate<T>::celtofar(T x)
{
T f;
f = x * 9 / 5 + 32;
return f;
}
template<class T>T operate<T>::sqroot(T x)
{
T g;
g = sqrt(x);
return g;
}
template<class T>T operate<T>::pow(T x, T y)
{
T h;
h = pow(x,y);
return h;
}
template<class T>T operate<T>::log(T x)
{
T k;
k = log(x);
return k;
}
template<class T>void operate<T>::calcadd()
{
cout << "The Add Function\n";
cout << "First number: ";
cin >> number1;
cout << "Second number: ";
cin >> number2;
answer = add(number1,number2);
cout << number1 << " + " << number2 << " = " << answer << endl;
}
template<class T>void operate<T>::calcsub()
{
cout << "The Subtract Function\n";
cout << "First number: ";
cin >> number1;
cout << "Second number: ";
cin >> number2;
answer = sub(number1,number2);
cout << number1 << " - " << number2 << " = " << answer << endl;
}
template<class T>void operate<T>::calcdiv()
{
cout << "The Divide Function\n";
cout << "First number: ";
cin >> number1;
cout << "Second number: ";
cin >> number2;
answer = div(number1,number2);
cout << number1 << " / " << number2 << " = " << answer << endl;
}
template<class T>void operate<T>::calcmult()
{
cout << "The Multiply Function\n";
(1)完善计算器程序,改为可对实数操作。
(2)完善程序,改为多个操作数基本四则运算,遇到0为止。
(3)增加函数,完成四则混合运算,增加相应的主菜单选项。
(4)添加语句,使四则运算具有测试功能。
(5)可扩充其功能。
#include <process.h>
#include <iostream.h>
#include <stdio.h>
#include <cmath>
template <class T>
class operate {
private:
T add(T x, T y);
T sub(T x, T y);
T div(T x, T y);
T mult(T x, T y);
T fartocel(T x);
T celtofar(T x);
T sqroot(T x);
T pow(T x, T y);
T log(T x);
public:
T number1,number2,answer,val;
void calcadd();
void calcsub();
void calcdiv();
void calcmult();
void calcfartocel();
void calcceltofar();
void calcsroot();
void calcpow();
void calclog();
void menu();
};
template<class T> T operate<T>::add(T x, T y)
{
val = x + y;
return val;
}
template<class T>T operate<T>::sub(T x, T y)
{
val = x - y;
return val;
}
template<class T>T operate<T>::div(T x, T y)
{
val = x / y;
return val;
}
template<class T>T operate<T>::mult(T x, T y)
{
val = x * y;
return val;
}
template<class T>T operate<T>::fartocel(T x)
{
T cel;
cel = ((x - 32) * 5) / 9;
return cel;
}
template<class T>T operate<T>::celtofar(T x)
{
T f;
f = x * 9 / 5 + 32;
return f;
}
template<class T>T operate<T>::sqroot(T x)
{
T g;
g = sqrt(x);
return g;
}
template<class T>T operate<T>::pow(T x, T y)
{
T h;
h = pow(x,y);
return h;
}
template<class T>T operate<T>::log(T x)
{
T k;
k = log(x);
return k;
}
template<class T>void operate<T>::calcadd()
{
cout << "The Add Function\n";
cout << "First number: ";
cin >> number1;
cout << "Second number: ";
cin >> number2;
answer = add(number1,number2);
cout << number1 << " + " << number2 << " = " << answer << endl;
}
template<class T>void operate<T>::calcsub()
{
cout << "The Subtract Function\n";
cout << "First number: ";
cin >> number1;
cout << "Second number: ";
cin >> number2;
answer = sub(number1,number2);
cout << number1 << " - " << number2 << " = " << answer << endl;
}
template<class T>void operate<T>::calcdiv()
{
cout << "The Divide Function\n";
cout << "First number: ";
cin >> number1;
cout << "Second number: ";
cin >> number2;
answer = div(number1,number2);
cout << number1 << " / " << number2 << " = " << answer << endl;
}
template<class T>void operate<T>::calcmult()
{
cout << "The Multiply Function\n";


