目录
实现两个多项式(顺序表)相加运算
#include<iostream>
#include<string>
using namespace std;
#define MAXSIZE 100
typedef int ElemType;
class Polynomial
{
public:
ElemType *data;
int length;
public:
Polynomial()
{
data = new ElemType[MAXSIZE];
length = 0;
cout << "初始化成功" << endl;
}
~Polynomial()
{
delete data;
cout << "销毁成功" << endl;
}
//创建多项式
void CreatePoly(int n)
{
int a;
for(int i = 0 ; i < n ; ++i)
{
cin >> a;
data[i] = a;
}
length = n;
}
//打印多项式
void PrintPoly()
{
string s;
for(int i = 0 ; i < length ; ++i)
{
if(data[i] != 0)
{
if(i != length - 1)
cout << data[i] << "x^" << i << " + ";
else
cout << data[i] << "x^" << i << " ";
}
}
cout << endl;
}
};
void add(Polynomial &La , Polynomial &Lb , Polynomial &Lc)
{
int c = min(La.length , Lb.length);
for(int i = 0 ; i < c ; ++i)
Lc.data[i] = La.data[i] + Lb.data[i];
if(c < La.length)
{
for(int i = c ; i < La.length ; ++i)
Lc.data[i] = La.data[i];
}
else
{
for(int i = c ; i < Lb.length ; ++i)
Lc.data[i] = Lb.data[i];
}
Lc.length = max(La.length , Lb.length);
return ;
}
int main()
{
Polynomial La , Lb , Lc;
La.CreatePoly(5);
Lb.CreatePoly(7);
cout << "La = ";
La.PrintPoly();
cout << "Lb = ";
Lb.PrintPoly();
add(La , Lb , Lc);
cout << "Lc = ";
Lc.PrintPoly();
return 0;
}
实现两个多项式(链表)相加运算
#include<iostream>
using namespace std;
struct pNode
{
float coef;
int expn;
struct pNode *next;
};
class Polynomial
{
public:
pNode *L;
Polynomial()
{
L = new pNode({0.0 , 0 , NULL});
cout << "初始化成功" << endl;
}
~Polynomial()
{
delete L;
cout << "销毁成功" << endl;
}
void CreatePoly(int n)
{
pNode *p = L;
for(int i = 0 ; i < n ; ++i)
{
pNode *temp = new pNode({0.0 , 0 , NULL});
cin >> temp->coef >> temp->expn;
p->next = temp;
p = p->next;
}
}
//按照指数进行查找,找到返回指数的地址,没有找到返回NULL
pNode *GetExpn(int exp)
{
pNode *p = L->next;
while (p)
{
if(p->expn == exp)
return p;
p = p->next;
}
return NULL;
}
void PrintPoly()
{
pNode *p = L->next;
while (p)
{
if(p->next)
{
cout << p->coef << "x^" << p->expn << " + ";
p = p->next;
}
else
{
cout << p->coef << "x^" << p->expn << " ";
p = p->next;
}
}
cout << endl;
}
};
void add(Polynomial &La , Polynomial &Lb , Polynomial &Lc)
{
pNode *p1 = La.L->next , *p2 = Lb.L->next , *p3 = Lc.L;
while (p1)
{
pNode *temp = new pNode({0.0 , 0 , NULL});
if(p2 = Lb.GetExpn(p1->expn))
{
temp->coef = p1->coef + p2->coef;
temp->expn = p1->expn;
}
else
{
temp->coef = p1->coef;
temp->expn = p1->expn;
}
p3->next = temp;
p3 = p3->next;
p1 = p1->next;
}
}
int main()
{
Polynomial La , Lb , Lc;
La.CreatePoly(4);
cout << "La = ";
La.PrintPoly();
Lb.CreatePoly(3);
cout << "Lb = ";
Lb.PrintPoly();
add(La , Lb , Lc);
cout << "Lc = ";
Lc.PrintPoly();
return 0;
}
图书信息管理系统
#include<iostream>
using namespace std;
#include <stdlib.h>
struct ElemType
{
char id[20];
char name[50];
int price;
};
struct Node
{
ElemType data;
Node *next;
};
class LinkList
{
public:
Node *L;
public:
LinkList()
{
L = new Node;
L->next = NULL;
cout << "初始化成功!" << endl;
}
//销毁单链表
~LinkList()
{
Node *p;
while (L)
{
p = L;
L = L->next;
delete p;
}
cout << "销毁单链表成功" << endl;
}
//判断链表是否为空
void ListEmpty()
{
if(L->next)
cout << "链表不为空" << endl;
else
cout << "链表为空" << endl;
}
//清空链表
void ClearList()
{
Node *p , *q;
p = L->next;
while (p)
{
q = p->next;
delete p;
p = q;
}
L->next = NULL;
cout << "清空链表成功" << endl;
}
//求链表表长
int ListLength()
{
Node *p;
int count = 0;
p = L->next;
while (p)
{
p = p->next;
count++;
}
cout << "链表表长为:" << count << endl;
return count;
}
//取链表中第i个元素的值
int GetElem(int i , ElemType& data)
{
Node *p;
int j = 1;
p = L->next;
while (j < i && p)
{
p = p->next;
j++;
}
if(j > i || !p)
{
cout << "所取元素不存在" << endl;
}
data = p->data;
cout << "书的id :" << data.id << "书名为: " << data.name <<"书的价格为: " << data.price << endl;
return 0;
}
//按值查找元素所在位置,返回元素地址
Node *LocateElem(ElemType data)
{
Node *p = NULL;
p = L->next;
while (p && p->data.id != data.id)
{
p = p->next;
}
if(p)
{
cout << "查找数据书名id:" << data.id << "地址为: " << p << endl;
return p;
}
else
{
cout << "查找数据书名id" << data.id << "不存在" << endl;
return NULL;
}
}
//按值查找书名id所在位置,返回元素位置序号
int LocateElem_i(ElemType data)
{
Node *p = NULL;
int j = 1;
p = L->next;
while (p && p->data.id != data.id)
{
p = p->next;
j++;
}
if(p)
{
cout << "查找数据" << data.id << "元素位置序号为: " << j << endl;
return j;
}
else
{
cout << "查找数据" << data.id << "不存在" << endl;
return -1;
}
}
//在L中第i个元素之前插入数据元素data
void ListInsert(int i , ElemType data)
{
Node *p = L;
int j = 1;
while(p && j < i-1)
{
p = p->next;
j++;
}
Node *s;
s = new Node;
s->data = data;
s->next = p->next;
p->next = s;
}
//将线性表中第i个数据元素删除
int ListDelete(int i)
{
Node *p = L , *q;
int j = 1;
while(p && j < i-1)
{
p = p->next;
j++;
}
if(!(p->next) || j > i -1)
{
cout << "删除位置不合理" << endl;
return -1;
}
q = p->next;
p->next = q->next;
delete q;
return 0;
}
//头插法
void CreateList(int n)
{
for(int i = n;i > 0 ; i--)
{
Node *p = new Node;
cin >> p->data.id >> p->data.name >> p->data.price;
p->next = L->next;
L->next = p;
}
}
//尾插法
void CreateList_R(int n)
{
Node *r;
r = L;
for(int i = 0 ; i < n ; i++)
{
Node *p = new Node;
cin >> p->data.id >> p->data.name >> p->data.price;
r->next = p;
p->next = NULL;
r = p;
}
}
};
int main()
{
LinkList L;
L.CreateList_R(1);
L.ListEmpty();
cout << (L.L->next)->data.id << endl;
return 0;
}