问答题

请使用“答题”菜单或使用VC6打开考生文件夹proj3下的工程proj3,其中包含了类Polyno-mial(“多项式”)的定义。形如5x4+3.4x2-7x+2代数式称为多项式,其中的5为4次项系数,3.4为2次项系数,-7为1次项系数,2为0次项(常数项)系数。此例缺3次项,意味着3次项系数为0,即省略了0x3。在Polynomial中,多项式的各个系数存储在一个名为coef的数组中。例如对于上面的多项式,保存在coef[0]、coef[1]、…、coef[4]中的系数依次为:2.0、-7.0、3.4、0.0、5.0,也即对于i次项,其系数就保存在coef[i]中。作为成员函数重载的运算符“+”用于计算两个多项式的和,并返回作为计算结果的那个多项式。请补充完成文件Polynomial.cpp中重载运算符函数operator+的定义。此程序的正确输出结果应为:
p1+p2的结果:7.3X^4+20.6X^3-41.2X^2-2.4X+5
p2+p3的结果:-2.3X^5+14.6X^4+12.8X^3+2.8X^2+0.2X+1
注意:只需要在operator+的//********333********和//********666********之间填入若干语句,不要改动程序中的其他内容。
//源程序
//主函数
#include"Polynomial.h"
int main()
double p1[]=5.0,3.5,-41.2,7.8,
p2[]=0.0,-5.9,0.0,12.8,7.3,
p3[]=1.0,6.1,2.8,0.0,7.3,-2.3;
Polynomial poly1(p1, sizeof(p1)/sizeof(double)),
poly2(p2, sizeof(p2) /sizeof(double)),
poly3(p3, sizeof(p3) /sizeof(double));
cout<<"p1+p2的结果:"<<(polyl+poly2).tostring()<<endl;
cout<<"p2+p3的结果:"<<(poly2+poly3).tostring()<<endl;
// writeToFile("K:\\b10\\61000102\\");
return 0:

//Polynomial.cpp函数
#include"Polynomial.h"
#include <strstream>
#include <cmath>
const char*Polynomial::tostring() const//将多项式转换成用于输出的字符串
static char s[1000];
s[0]=0;
strstream buf(s,1000);
for(int i=num_of_terms-1;i>=0;i--)
if(coef[i]==0.0) continue, //0系数项不输出
if(coef[i]<0.0) buf<<"-"; //负系数先输出"-"
else if(strlen(s)==0) buf<<" ";
else buf<<"+":
buf<<fabs(coef[i]);
if(i>0)
buf<<"X":
if(i>1)buf<<"^"<<i;


buf<<ends:
return buf.str();
Polynomial Polynomial:: operator+(const Polynomial &x) const
double c[100]=0.0;//存放结果系数的数组,所有元素初始化为零。假定项数不会超过100
//下面的n为两个操作数中项数的较大者
int n=(num_of_terms>=x.num_of_terms num_of_terms:x.num_of_terms);
//将两个多项式的对应系数之和存放到数组c的对应单元中
//********333********
//********333********
while(n>1&&c[n-1]==0.0)n--;//去除无效的(系数为0的)最高次项
return Polynomial(c,n);

【参考答案】

//********333********
for(int i=0;i<n;i++)
c[i]=th......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

多项选择题
请使用“答题”菜单或使用VC6打开考生文件夹proj2下的工程proj2。此工程包含一个程序文件main.cpp,其中的Array是一个表示数组的模板类。Array的成员a用于指向存放数据的数组,size表示该数组的大小。此程序的输出结果应为: 8 29,20,33,12,18,66,25,14 66,33 注意:只能在画线处填写适当的代码,不要改动程序中的其他内容,也不能删除或移动 ************found************。 源程序 #include <iostream> #include <cstdlib> using namespace std; template<class Type> class Array 数组类 Type *a; int size; public:
Array(Type b[],int mm):size(mm)//构造函数
if(size<2) cout<<"数组长度太小,退出运行!\n";exit(1);
//************found************
a=______;//a指向申请的数组空间
for(int i=0; i<size; i++) a[i]=b[i];//************found************
~Array()______; //析构函数
void MaxTwo(Type& x1,Type& x2) const;//由x1和x2带回数组a中的两个最大值
//************found************
int Length() const______;//返回数组长度
Type operator[](int i) const
//下标运算符重载成员函数
if(i<0 || i>=size)cout<<"下标越界!"<<endl;exit(1);
return a[i];;
//由x1和x2带回数组a中的两个最大值
template<class Type>
void Array<Type>::MaxTwo(Type& x1,Type& x2) const //补充完整函数体的内容
//将数组a中头两个数据赋值给x1和x2,使得x1>=x2
//************found************
______(x1=a[0],x2=a[1]):(x1=a[1],x2=a[0]);
for(int i=2;i<size; i++)
if(a[i]>x1)x2=x1; x1=a[i];
else if(a[i]>x2) x2=a[i];
void main()
int s1[8]=29,20,33,12,18,66,25,14;Array<int> d1(s1,8);
int i,a,b;
d1.M axTwo(a,b);
cout<<d1.Length()<<endl;
for(i=0;i<7;i++) cout<<d1[i]<<",";
cout<<d1[7]<<endl;cout<<a<<","<<b<<endl;
问答题
请使用“答题 菜单或使用VC6打开考生文件夹proj1下的工程proj1。此工程包含程序文件main.cpp。程序中位于每个 ERROR************found************下的语句行有错误,请加以更正。更正后程序的输出应该是: 1958年3月23日比2001年11月5日更早 2001年11月5日比1958年3月23日更晚 2001年11月5日与2001年11月5日是同一日期 注意:只能修改每个 ERROR************found************下的那一行,不要改动程序中的其他内容。 源程序 #include <iostream> using namespace std; class Date int year; int month; int day; public: ERROR************found************ Date(int yyyy, int mm, int dd): yyyy(year),mm(month),dd(day) bool isLaterThan(Date dt) const 当前日期晚于日期dt时返回true if(year!=dt.year) return year>dt.year; if(month!=dt.month) return month>dt.month; return day>dt.day; bool isEarlyThan(Date dt) const 当前日期早于日期dt时返回true if(year!=dt.year) return year<dt.year; if(month !=dt.month) return month<dt.month; ERROR************found************ return day>dt.day; void showDate() const cout<<year<< 年 <<month<< 月 <<day<< 日 ; ; ERROR************found************ void compareDate(Date dt1 Date dt2) const if(dt1.isLaterThan(dt2)) dt1.showDate();cout<< 比 ;dt2.showDate();cout<< 更晚 <<endl; else if(dt1.isEarlyThan(dt2)) dt1.showDate();cout<< 比 ;dt2.showDate();cout<< 更早 <<endl; else dt1.showDate();cout<< 与 ;dt2.showDate();cout<< 是同一日期 <<endl; int main() Date dt1(1958,3,23),dt2(2001,11,5); compareDate(dt1,dt2); compareDate(dt2,dt1); compareDate(dt2,dt2); return 0: