用C++编写程序,要求如下: ①输入多组数据,总计n*( a+b+2)+1行。其中,第一行整数n代表总计有n组数据,之后依次输入n组数据。每组数据包括a+b+2行,其中第一行是两个整数a和b,分别代表A(x)与B(x)的项数。之后紧跟a行,每行两个整数a1和a2,分别代表A(x)每项的系数和指数,再之后紧跟b行,每行两个整数b1和b2,分别代表B(x)每项的系数和指数,每组数据最后一行为一个字符(+、-、*、'),分别代表多项式的加法、减法、乘法和求导运算。所有数据的绝对值小于100,指数大于等于0。 ②编写的程序在我给出的代码上进行补充 ③当用户输入: 4 1 1 1 0 1 1 + 4 3 7 0 3 1 9 8 5 17 8 1 22 7 -9 8 + 1 1 1 1 1 1 - 1 1 1 1 1 1 ' 输出: 1x^1+1 5x^17+22x^7+11x^1+7 0 1 1 #include <iostream>#include <string> using namespace std; typedef struct LNode{ int coe;int exp;struct LNode *next; }LNode,*LinkList; void CreatePolynomial(LinkList &L,int n){ L=new LNode;L->next=NULL; for(int i=0;i<n;i++){ LinkList p=new LNode;cin>>p->coe>>p->exp; LinkList pre=L,cur=L->next; while(cur&&p->exp<cur->exp){ pre=cur;cur=cur->next;} p->next=cur;pre->next=p;} } void OutputPolynomial(LinkList L){ if(!L||!L->next) cout<<0;LinkList p=L->next; while(p){ if(p==L->next){ if (p->exp!=0) cout<<p->coe<<"x^"<<p->exp; else cout<<p->coe;} else{ if(p->coe>0) cout<<"+"; if(p->exp!=0) cout<<p->coe<<"x^"<<p->exp; else cout<<p->coe;} p=p->next;} cout<<endl;} LinkList Add(LinkList LA,LinkList LB){} void Minus(LinkList LA,LinkList LB){} void Mul(LinkList LA,LinkList LB){} void Diff(LinkList L){ LinkList p=L->next;LinkList r=NULL; while(p){ p->coe*=p->exp;p->exp--; if(p->exp<0){ r=p;p=p->next;delete r;} else{ p=p->next;} } OutputPolynomial(L);} void Opt(LinkList &LA,LinkList &LB,string s){ if(s=="+") OutputPolynomial(Add(LA, LB));if(s=="-") Minus(LA, LB); if(s=="*") Mul(LA, LB);if(s=="'"){ Diff(LA);Diff(LB);} } int main(){ int n;cin>>n; while(n--){ int a,b;cin>>a>>b;LinkList LA,LB;CreatePolynomial(LA,a); CreatePolynomial(LB,b);string s;cin>>s;Opt(LA,LB,s);} return 0;}
时间: 2023-06-18 17:01:55 浏览: 204
以下是补充完整的程序代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
typedef struct LNode{
int coe;
int exp;
struct LNode *next;
}LNode,*LinkList;
// 创建多项式
void CreatePolynomial(LinkList &L,int n){
L = new LNode;
L->next = NULL;
for(int i = 0; i < n; i++){
LinkList p = new LNode;
cin >> p->coe >> p->exp;
LinkList pre = L, cur = L->next;
while(cur && p->exp < cur->exp){
pre = cur;
cur = cur->next;
}
p->next = cur;
pre->next = p;
}
}
// 输出多项式
void OutputPolynomial(LinkList L){
if(!L || !L->next){
cout << 0;
}
LinkList p = L->next;
while(p){
if(p == L->next){
if (p->exp != 0){
cout << p->coe << "x^" << p->exp;
}else{
cout << p->coe;
}
}else{
if(p->coe > 0){
cout << "+";
}
if(p->exp != 0){
cout << p->coe << "x^" << p->exp;
}else{
cout << p->coe;
}
}
p = p->next;
}
cout << endl;
}
// 多项式加法
LinkList Add(LinkList LA, LinkList LB){
LinkList LC = new LNode;
LC->next = NULL;
LinkList pa = LA->next, pb = LB->next, pc = LC;
while(pa && pb){
if(pa->exp > pb->exp){
LinkList p = new LNode;
p->coe = pa->coe;
p->exp = pa->exp;
pc->next = p;
pc = pc->next;
pa = pa->next;
}else if(pa->exp < pb->exp){
LinkList p = new LNode;
p->coe = pb->coe;
p->exp = pb->exp;
pc->next = p;
pc = pc->next;
pb = pb->next;
}else{
LinkList p = new LNode;
p->coe = pa->coe + pb->coe;
p->exp = pa->exp;
pc->next = p;
pc = pc->next;
pa = pa->next;
pb = pb->next;
}
}
while(pa){
LinkList p = new LNode;
p->coe = pa->coe;
p->exp = pa->exp;
pc->next = p;
pc = pc->next;
pa = pa->next;
}
while(pb){
LinkList p = new LNode;
p->coe = pb->coe;
p->exp = pb->exp;
pc->next = p;
pc = pc->next;
pb = pb->next;
}
return LC;
}
// 多项式减法
void Minus(LinkList LA, LinkList LB){
LinkList p = LB->next;
while(p){
p->coe = -p->coe;
p = p->next;
}
OutputPolynomial(Add(LA, LB));
}
// 多项式乘法
void Mul(LinkList LA, LinkList LB){
LinkList LC = new LNode;
LC->next = NULL;
LinkList pa = LA->next;
while(pa){
LinkList pb = LB->next;
while(pb){
LinkList p = new LNode;
p->coe = pa->coe * pb->coe;
p->exp = pa->exp + pb->exp;
LinkList pre = LC, cur = LC->next;
while(cur && p->exp < cur->exp){
pre = cur;
cur = cur->next;
}
if(cur && cur->exp == p->exp){
cur->coe += p->coe;
if(cur->coe == 0){
pre->next = cur->next;
delete cur;
}
}else{
p->next = cur;
pre->next = p;
}
pb = pb->next;
}
pa = pa->next;
}
OutputPolynomial(LC);
}
// 多项式求导
void Diff(LinkList L){
LinkList p = L->next;
LinkList r = NULL;
while(p){
p->coe *= p->exp;
p->exp--;
if(p->exp < 0){
r = p;
p = p->next;
delete r;
}else{
p = p->next;
}
}
OutputPolynomial(L);
}
// 多项式运算
void Opt(LinkList &LA, LinkList &LB, string s){
if(s == "+"){
OutputPolynomial(Add(LA, LB));
}
if(s == "-"){
Minus(LA, LB);
}
if(s == "*"){
Mul(LA, LB);
}
if(s == "'"){
Diff(LA);
Diff(LB);
}
}
int main(){
int n;
cin >> n;
while(n--){
int a, b;
cin >> a >> b;
LinkList LA, LB;
CreatePolynomial(LA, a);
CreatePolynomial(LB, b);
string s;
cin >> s;
Opt(LA, LB, s);
}
return 0;
}
```
阅读全文