01/10/2018, 12:14
Cho em hỏi về danh sách liên kết, em sai chỗ nào vậy ạ
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include<fstream>
using namespace std;
class Dathuc{
private:
int heso;
int somu;
Dathuc *next;
Dathuc *ptr;
public:
Dathuc *hPtr1, *hPtr2, *tong,*hieu,*tich,*giatri1,*giatri2;
Dathuc * buildNode(int heso, int somu) {
ptr = new Dathuc;
ptr->heso=heso;
ptr->somu = somu;
ptr->next = NULL;
return ptr;
}
/* Chèn du li?u vào l?nh decending - d?a vào giá tr? s? mu */
void polynomial_insert(Dathuc ** myNode, int heso, int somu) {
Dathuc *lPtr, *pPtr, *qPtr = *myNode;
lPtr = buildNode(heso, somu);
/* chèn nút m?i vào v? trí thích h?p */
if (*myNode == NULL || (*myNode)->somu < somu) {
*myNode = lPtr;
(*myNode)->next = qPtr;
return;
}
/* d?t nút m?i gi?a hai nút ho?c cu?i nút */
while (qPtr) {
pPtr = qPtr;
qPtr = qPtr->next;
if (!qPtr) {
pPtr->next = lPtr;
break;
}
else if ((somu < pPtr->somu) && (somu > qPtr->somu)){
lPtr->next = qPtr;
pPtr->next = lPtr;
break;
}
}
return;
}
/* chèn nút m?i v?i k?t qu? d? li?u vào danh sách d?u ra (n1) */
void polynomial_add(Dathuc **n1, int heso, int somu) {
Dathuc *x = NULL, *temp = *n1;
if (*n1 == NULL || (*n1)->somu<somu) {
/* thêm vào ? phía tru?c */
*n1 = x = buildNode(heso, somu);
(*n1)->next = temp;
} else {
while (temp) {
if (temp->somu == somu) {
/* c?p nh?t giá tr? d?ng doanh nghi?p m?t mình */
temp->heso= temp->heso + heso;
return;
}
if (temp->somu > somu && (!temp->next || temp->next->somu < somu)) {
/* chèn vào gi?a ho?c cu?i */
x = buildNode(heso, somu);
x->next = temp->next;
temp->next = x;
return;
}
temp = temp->next;
}
x->next = NULL;
temp->next = x;
}
}
void Tich(Dathuc **n1, Dathuc *n2, Dathuc *n3) {
Dathuc * temp;
int heso, somu;
temp = n3;
/* n?u c? hai danh sách d?u vào v?ng m?t, sau dó danh sách d?u ra là NULL */
if (!n2 && !n3)
return;
/* input list 1(n2) is absent, then output list is input list2 (n3) */
if (!n2) {
*n1 = n3;
} else if (!n3) {
/*
* danh sách n3 v?ng m?t, sau dó o / p danh sách là n2
*/
*n1 = n2;
} else {
while (n2) {
while (n3) {
/* multiply coefficient & add exponents */
heso = n2->heso * n3->heso;
somu = n2->somu + n3->somu;
n3 = n3->next;
/*chèn d? li?u thao tác ? trên vào danh sách o / p */
polynomial_add(n1, heso, somu);
}
n3 = temp;
n2 = n2->next;
}
}
return;
}
void Tong(Dathuc **n1,Dathuc *n2,Dathuc *n3){
Dathuc * temp;
int heso, somu;
temp = n3;
/* n?u c? hai danh sách d?u vào v?ng m?t, sau dó danh sách d?u ra là NULL */
if (!n2 && !n3)
return;
/* input list 1(n2) is absent, then output list is input list2 (n3) */
if (!n2) {
*n1 = n3;
}
else if (!n3) {
/*
* danh sách n3 v?ng m?t, sau dó o / p danh sách là n2
*/
*n1 = n2;
} else {
while (n2) {
while (n3) {
while(n2->somu=n3->somu)
heso = n2->heso + n3->heso;
somu = n2->somu=n3->somu;
n3 = n3->next;
/*chèn d? li?u thao tác ? trên vào danh sách o / p */
polynomial_add(n1, heso, somu);
}
n3 = temp;
n2 = n2->next;
}
}
return;
}
void Tru(Dathuc **n1,Dathuc *n2,Dathuc *n3){
Dathuc * temp;
int heso, somu;
temp = n3;
/* n?u c? hai danh sách d?u vào v?ng m?t, sau dó danh sách d?u ra là NULL */
if (!n2 && !n3)
return;
/* input list 1(n2) is absent, then output list is input list2 (n3) */
if (!n2) {
*n1 = n3;
}
else if (!n3) {
/*
* danh sách n3 v?ng m?t, sau dó o / p danh sách là n2
*/
*n1 = n2;
} else {
while (n2) {
while (n3) {
while(n2->somu=n3->somu)
heso = n2->heso - n3->heso;
somu = n2->somu=n3->somu;
n3 = n3->next;
/*chèn d? li?u thao tác ? trên vào danh sách o / p */
polynomial_add(n1, heso, somu);
}
n3 = temp;
n2 = n2->next;
}
}
return;
}
int TinhGiaTri(Dathuc **n1,Dathuc *n2,int x){
int T=0;
Dathuc * temp;
int heso, somu;
temp = n2;
/* n?u c? hai danh sách d?u vào v?ng m?t, sau dó danh sách d?u ra là NULL */
/* input list 1(n2) is absent, then output list is input list2 (n3) */
if (!n2) {
*n1 = 0;
}
else
{
while (n2) {
T+= n2->heso * pow(x,n2->somu);
n2 = n2->next;
/*chèn d? li?u thao tác ? trên vào danh sách o / p */
polynomial_add(n1, heso, somu);
}
n2 = temp;
n2 = n2->next;
}
return T;
}
/* xóa danh sách d?u vào cho tru?c */
Dathuc *polynomial_deleteList(Dathuc *ptr) {
Dathuc *temp;
while (ptr){
temp = ptr->next;
free(ptr);
ptr = temp;
}
return NULL;
}
friend ostream & operator <<(ostream &out,Dathuc *ptr){
int i = 0;
int dem=0;
while (ptr) {
if(ptr->somu != 0 || ptr->somu != 1 ){
if(ptr->somu > 0 && dem==0 ){
cout<<ptr->heso<<"x^"<<ptr->somu;
dem++;
}
else if (ptr->heso > 0 && dem==1 )
cout<<" + "<<ptr->heso<<"x^"<<ptr->somu;
else if(ptr->heso < 0)
cout<<ptr->heso<<"x^"<<ptr->somu;
}
else if (ptr->somu == 0){
if(ptr->heso > 0 && dem==0 ){
cout<<ptr->heso;
dem++;
}
else if (ptr->heso > 0 && dem==1 )
cout<<" + "<<ptr->heso;
else if(ptr->heso < 0)
cout<<ptr->heso;
}
else if( ptr->somu == 1 ){
if(ptr->heso > 0 && dem==0 ){
cout<<ptr->heso<<"x ";
dem++;
}
else if (ptr->heso > 0 && dem==1 )
cout<<"+ "<<ptr->heso<<"x ";
else if(ptr->heso < 0)
cout<<ptr->heso<<"x ";
}
ptr = ptr->next;
i++;
}
printf("
");
return out;
}
friend istream & operator >>(istream &in,Dathuc *ptr){
cout<<"
Nhap he so:";
in>>ptr->heso;
cout<<"
Nhap so mu:";
in>>ptr->somu;
return in;
}
//Ham menu
void Menu(){
int luachon;
while(true)
{
system("cls");
cout<<"
================Menu==============";
cout<<"
1.Nhap vao hai da thuc";
cout<<"
2.Xuat danh sach";
cout<<"
3.Tinh gia tri cua da thuc";
cout<<"
4.Tong,Hieu,Tich cua hai da thuc";
cout<<"
5.Ghi da thuc re file";
cout<<"
Nhap lua chon cua ban:";
cin>>luachon;
if(luachon==1){
int n,m;
cout<<"Nhap bac da thuc 1:"; cin>>n;
cout<<"
Nhap da thuc 1:
";
for(int i=0;i<n;i++){
cin>>hPtr1;
polynomial_insert(&hPtr1, heso, somu);
}
cout<<"
Nhap so bac da thuc 2"; cin>>m;
for(int i=0;i<m;i++){
cin>>hPtr2;
polynomial_insert(&hPtr2,heso,somu);
}
}
else if(luachon ==2){
cout<<"
Da thuc 1 la: "; cout<<hPtr1;
cout<<"
Da thuc 2 la: "; cout<<hPtr2;
}
else if(luachon==3){
int x;
cout<<"Nhap gia tri cua x:"; cin>>x;
cout<<"
Gia tri cua da thuc 1 la:
";
TinhGiaTri(&giatri1,hPtr1,x);
cout<<"Gia tri cua da thuc 2 la:
";
TinhGiaTri(&giatri2,hPtr2,x);
}
else if(luachon == 4){
cout<<"
Tong cua hai da thuc la:";
Tong(&tong,hPtr2,hPtr1);
cout<<tong;
cout<<"
Tich cua hai da thuc la:";
Tich(&tich,hPtr2,hPtr1);
cout<<tich;
cout<<"
Hieu cua hai da thuc la:";
Tru(&hieu,hPtr2,hPtr1);
cout<<hieu<<endl;
}
else if (luachon ==5){
ofstream fileout("Dathuc.txt");
fileout<<"
Tong hai da thuc:"; fileout<<tong;
fileout<<"
Hieu hai da thuc:"; fileout<<hieu;
fileout<<"
Tich hai da thuc:"; fileout<<tich;
fileout.close();
}
else {
break;
}
}
}
};
int main()
{
Dathuc ptr;
ptr.Menu();
}
Bài liên quan