题目:
定义并实现多项式类(polynomial),实现多项式加,减,乘及求导运算,并用一个交互式主程序测试、使用该多项式类。
程序功能:
显示如下菜单:
s-设置当前多项式
l-调用assign_coef函数(设置多项式的某一项)
t-调用add_to_coef函数(将输入的系数累加到多项式的对应项中)
c-调用clear函数(将当前多项式的各系数清零)
v-显示当前多项式
a-显示所有多项式
d-对当前多项式求导并显示
e-计算当前多项式的值
+-显示A+B的和
-- 显示A-B的差
*-显示A*B的积
q-退出
本程序中共有三个多项式(a,b和c),分别是主函数中定义的含三个多项式的对象数组中的元素:polynomial p[3];多项式的加,减,乘运算针对的是p[0]和p[1]多项式,多项式的系数为double类型,指数为在0到29之间的整数(提示:存放多项式的数组可定义成double coef[30];)
实现代码:
#include<iostream>
using namespace std;
class polynomial
{
public:
double a[30];
int i, j;
polynomial()
{
for (i = 0; i < 30; i++)
a[i] = 0;
i = 0;
j = 0;
}
void assign_coef(int x, int y)
{
for (i = 0; i < 30; i++)
{
if (i == x)
a[i] = y;
}
cout << "当前多项式:";
out();
}
void add_to_coef(int x, int y)
{
for (i = 0; i < 30; i++)
{
if (i == x)
a[i] += y;
}
cout << "当前多项式:";
out();
}
void clear()
{
for (i = 0; i < 30; i++)
a[i] = 0;
}
void dao()
{
a[0] = 0;
for (i = 1; i < 30; i++)
{
a[i - 1] = i * a[i];
}
out();
}
void total(int x)
{
double temp = x, sum = 0;
sum = a[1] * x, +a[0];
for (i = 1; i < 30; i++)
{
temp = x;
for (j = 1; j < i; j++)
temp *= x;
sum += a[i] * temp;
}
cout << sum << endl;
}
void jia(polynomial& x, polynomial& y)
{
double* p;
int flag = 0;
p = new double[30];
for (i = 29; i >= 0; i--, p++)
{
*p = x.a[i] + y.a[i];
if (i == 0)
{
if (*p < 0)cout << *p;
else if (*p > 0)cout << "+" << *p;
}
if (*p != 0 && flag == 0)
{
flag = 1;
cout << *p << "x" << "^" << i;
}
else if (*p < 0)
{
cout << *p << "x" << "^" << i;
}
else if (*p > 0)
cout << "+" << *p << "x" << "^" << i;
}
flag = 0;
cout << endl;
p = p - 30;
delete[]p;
}
void out()
{
int flag = 0;
for (i = 29; i >= 0; i--)
{
if (a[i] != 0)
{
if (flag == 0 && i != 0)
{
cout << a[i] << "x" << "^" << i;
flag = 1;
}
else if (a[i] < 0 && i != 0)
{
cout << a[i] << "x" << "^" << i;
}
else if (a[i] > 0 && i != 0)
cout << "+" << a[i] << "x" << "^" << i;
else if (i == 0)
{
if (flag == 0)
cout << a[i];
else
cout << "+" << a[i];
}
}
}
flag = 0;
cout << endl;
}
void add(polynomial& x, polynomial& y)
{
double* p;
int flag = 0;
p = new double[30];
for (i = 29; i >= 0; i--, p++)
{
*p = x.a[i] - y.a[i];
if (i == 0)
{
if (*p < 0)cout << *p;
else if (*p > 0)cout << "+" << *p;
}
if (*p != 0 && flag == 0)
{
flag = 1;
cout << *p << "x" << "^" << i;
}
else if (*p < 0)
{
cout << *p << "x" << "^" << i;
}
else if (*p > 0)
cout << "+" << *p << "x" << "^" << i;
}
flag = 0;
cout << endl;
p = p - 30;
delete[]p;
}
void cheng(polynomial& x, polynomial& y)
{
int flag = 0;
double arr[60];
for (i = 0; i < 60; i++)
{
arr[i] = 0;
}
for (i = 0; i < 30; i++)
{
if (x.a[i] != 0)
{
for (j = 0; j < 30; j++)
{
if (y.a[j] != 0)
{
arr[i + j] += x.a[i] * y.a[j];
}
}
}
}
for (i = 59; i >= 0; i--)
{
if (arr[i] != 0)
{
if (i == 0)
{
if (arr[i] > 0 && flag == 0)
cout << arr[i];
else
cout << "+" << arr[i];
break;
}
if (flag == 0 && arr[i] != 0)
{
flag = 1;
cout << arr[i] << "x" << "^" << i;
}
else if (arr[i] > 0)
cout << "+" << arr[i] << "x" << "^" << i;
}
}
cout << endl;
}
};
int main()
{
cout << "-------------------------------------指令---------------------------------------------------" << endl;
cout << "s-设置当前多项式" << endl << "l - 调用assign_coef函数(设置多项式的某一项)" << endl << "t - 调用add_to_coef函数(将输入的系数累加到多项式的对应项中)" << endl;
cout << "c - 调用clear函数(将当前多项式的各系数清零)" << endl << "v - 显示当前多项式" << endl;
cout << "a - 显示所有多项式" << endl << "d - 对当前多项式求导并显示" << endl << "e - 计算当前多项式的值" << endl;
cout << " + -显示A + B的和" << endl << "-- 显示A - B的差" << endl << "* -显示A * B的积" << endl << "q - 退出" << endl;
cout << "---------------------------------------------------------------------------------------------" << endl;
cout << ">";
char ch, t;
int n = 0, j, m, k;
polynomial p[3];
cin >> ch;
while (ch != 'q')
{
if (ch == 's')
{
printf("输入想设置的多项式(a,b,c):");
cin >> t;
if (t == 'a')
n = 0;
else if (t == 'b')
n = 1;
else
n = 2;
}
if (ch == 'l')
{
cout << "指数:";
cin >> j;
cout << endl;
cout << "系数:";
cin >> m;
p[n].assign_coef(j, m);
}
if (ch == 't')
{
cout << "指数:";
cin >> j;
cout << endl;
cout << "系数:";
cin >> m;
p[n].add_to_coef(j, m);
}
if (ch == 'c')
p[n].clear();
if (ch == 'v')
p[n].out();
if (ch == 'a')
{
for (int i = 0; i < 3; i++)
{
p[i].out();
}
}
if (ch == 'd')
p[n].dao();
if (ch == 'e')
{
cout << "输入x";
cin >> k;
p[n].total(k);
}
if (ch == '+')
{
p[2].jia(p[0], p[1]);
}
if (ch == '-')
{
p[2].add(p[0], p[1]);
}
if (ch == '*')
{
p[2].cheng(p[0], p[1]);
}
cout << "-------------------------------------指令---------------------------------------------------" << endl;
cout << "s-设置当前多项式" << endl << "l - 调用assign_coef函数(设置多项式的某一项)" << endl << "t - 调用add_to_coef函数(将输入的系数累加到多项式的对应项中)" << endl;
cout << "c - 调用clear函数(将当前多项式的各系数清零)" << endl << "v - 显示当前多项式" << endl;
cout << "a - 显示所有多项式" << endl << "d - 对当前多项式求导并显示" << endl << "e - 计算当前多项式的值" << endl;
cout << " + -显示A + B的和" << endl << "-- 显示A - B的差" << endl << "* -显示A * B的积" << endl << "q - 退出" << endl;
cout << "---------------------------------------------------------------------------------------------" << endl;
cout << ">";
cin >> ch;
}
cout << "程序结束" << endl;
return 0;
}