0% found this document useful (0 votes)
8 views3 pages

Xulidathuc

The document defines structures for monomials (DonThuc) and polynomials (DaThuc) in C++. It includes functions for inputting, outputting, sorting by degree, adding/removing monomials, and simplifying polynomials. The main function demonstrates taking in two polynomials, adding them together, and outputting the simplified sum polynomial.

Uploaded by

Hiếu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Xulidathuc

The document defines structures for monomials (DonThuc) and polynomials (DaThuc) in C++. It includes functions for inputting, outputting, sorting by degree, adding/removing monomials, and simplifying polynomials. The main function demonstrates taking in two polynomials, adding them together, and outputting the simplified sum polynomial.

Uploaded by

Hiếu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <bits/stdc++.

h>
using namespace std;
const int MAX = 1000;
struct DonThuc{
float heso;
int bac;
};
struct DaThuc{
DonThuc p[MAX];
int count;
};
void NhapDonThuc(DonThuc &p){
cout << "Nhap He So : "; cin >> p.heso;
cout << "Nhap Bac : "; cin >> p.bac;
}
void NhapDaThuc(DaThuc &u){
DonThuc d;
u.count = 0;
do{
NhapDonThuc(d);
if(d.heso != 0){
u.p[u.count] = d;
u.count++;
}
}while(d.heso != 0);
}

void XuatDonThuc(DonThuc p){


if(p.heso == 1){
cout << "x^" << p.bac;
return;
}
if(p.heso == -1){
cout << "-x^" << p.bac;
return;
}
if(p.bac == 0){
cout << p.heso;
return;
}
if(p.bac == 1){
if(p.heso == 1){
cout << "x";
return;
}
if(p.heso == -1){
cout << "-x";
return;
}
cout << p.heso << "x";
return;
}
cout << p.heso << "x^" << p.bac;
}
void XuatDaThuc(DaThuc u){
if(u.count == 0) return;
for(int i = 0; i < u.count; i++){
XuatDonThuc(u.p[i]);
if(u.p[i + 1].heso > 0 && i < u.count - 1){
cout << "+";
}
}
}
void SapXep(DaThuc &u){
for(int i = 0; i < u.count - 1; i++){
for(int j = i + 1; j < u.count; j++){
if(u.p[i].bac < u.p[j].bac){
swap(u.p[i],u.p[j]);
}
}
}
}
void XoaDonThuc(DaThuc &u,int vitrixoa){
if(u.count <= 0 || vitrixoa > u.count) return;
for(int i = vitrixoa; i < u.count - 1; i++){
u.p[i] = u.p[i + 1];
}
u.count--;
}
void ThemDonThuc(DaThuc &u,int vitrithem, DonThuc dtthem){
if(u.count >= MAX) return;
for(int i = u.count; i > vitrithem; i--){
u.p[i] = u.p[i - 1];
}
u.p[vitrithem] = dtthem;
u.count++;

}
void SuaDonThuc(DaThuc &u,int vitrisua, DonThuc dtmoi){
if(u.count <= 0) return;
for(int i = 0; i < u.count; i++){
if(i == vitrisua){
u.p[i] = dtmoi;
}
}
}
void RutGon(DaThuc &u){
SapXep(u);
for(int i = 0; i < u.count; i++){
if(u.p[i].bac == u.p[i+1].bac){
u.p[i].heso += u.p[i+1].heso;
XoaDonThuc(u,i+1);
}
}
SapXep(u);
}
void cong2dt(DaThuc u, DaThuc q, DaThuc &t){
if(u.count <= 0 && q.count <= 0) return;
RutGon(u);
RutGon(q);
t.count = 0;
for(int i = 0 ; i < u.count; i++){
t.p[t.count] = u.p[i];
t.count++;
}
for(int i = 0 ; i < q.count; i++){
t.p[t.count] = q.p[i];
t.count++;
}
RutGon(t);
}
int main(){
DaThuc q,p,t;
cout << "Nhap Q(x)=" << endl;
NhapDaThuc(q);
cout << "Nhap P(x)=" << endl;
NhapDaThuc(p);
cong2dt(q,p,t);
cout << "T(x)=";
XuatDaThuc(t);
return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy