一元稀疏多项式计算器源代码
更新时间:2024-06-03 04:35:01 阅读量: 综合文库 文档下载
#include
float coef; int exp;
struct node *next; }Lnode, *polynmial;
void create(polynmial &L); //输入并建立多项式L void display(polynmial L); //显示,输出多项式L void sort(polynmial &L); //多项式L按指数排序 void reverse(polynmial &L); //逆置 void select(); //用户选择加减操作
void add(polynmial La, polynmial Lb, polynmial &Lc); //多项式void subtract(polynmial La, polynmial Lb, polynmial &Ld); //多项式
void create(polynmial &L) //输入并建立多项式L {
int i, n;
static struct node *p; scanf(\
L = (struct node *)malloc (sizeof(struct node)); L->next = NULL; for(i = 0; i < n; i++) {
p = (struct node *)malloc(sizeof(struct node)); scanf(\ p->next = L->next; L->next = p; } }
void display(polynmial L)//显示,输出多项式L {
struct node *p, *q; int flag = 0; int k = 0; q = L->next; while(q) {
if(q->coef != 0) k++;
q = q->next;
La,Lb相加
La减去LbLd ,结果给
}
printf(\p = L->next; if(p->coef != 0) {
printf(\ flag++; }
for(p = p->next; p; p = p->next) {
if(p->coef != 0) {
printf(\ flag++; } }
if(flag == 0)
printf(\else
printf(\}
void sort(polynmial &L)//多项式L按指数排序 {
polynmial p, q, r, u; p = L->next;
L->next = NULL; while(p != NULL) {
r = L;
q = L->next;
while((q != NULL) && (q->exp <= p->exp)) {
r = q;
q = q->next; }
u = p->next; r->next = p; p->next = q; p = u; } }
void reverse(polynmial &L)//逆置
{
polynmial H;
static struct node *p, *q, *s;
H = (struct node*)malloc(sizeof(struct node)); H->next = NULL;
p = (struct node*)malloc(sizeof(struct node)); s = L->next;
p->coef = s->coef; p->exp = s->exp; p->next = s->next; while(s) {
p->coef = s->coef; p->exp = s->exp; p->next = s->next; q = H->next; H->next = p; p->next = q;
p = (struct node*)malloc(sizeof(struct node)); s = s->next; }
p = H->next; q = L->next; while(p) {
q->coef = p->coef; q->exp = p->exp; q = q->next; p = p->next; } }
void select() //用户选择加减操作 {
printf(\请选择加减操作\\n\
printf(\两个一元多项式相加\\n\printf(\两个一元多项式相减\\n\}
void add(polynmial La, polynmial Lb, polynmial &Lc)//{
struct node *pa, *pb; static struct node *pc;
Lc = (struct node*)malloc(sizeof(struct node));
La,Lb多项式相加
pa = La->next; pb = Lb->next; Lc->next = NULL; while(pa && pb) {
pc = (struct node*)malloc(sizeof(struct node)); if(pa->exp < pb->exp) {
pc->next = Lc->next; Lc->next = pc;
pc->coef = pa->coef; pc->exp = pa->exp; pa = pa->next; } else
if(pa->exp == pb->exp) {
pc->next = Lc->next; Lc->next = pc; pc->exp = pa->exp;
pc->coef = pa->coef + pb->coef; pa = pa->next; pb = pb->next; } else {
pc->next = Lc->next; Lc->next = pc;
pc->coef = pb->coef; pc->exp = pb->exp; pb = pb->next; } }
while(pa) {
pc = (struct node*)malloc(sizeof(struct node)); pc->next = Lc->next; Lc->next = pc;
pc->coef = pa->coef; pc->exp = pa->exp; pa = pa->next; }
while(pb) {
pc = (struct node*)malloc(sizeof(struct node)); pc->next = Lc->next; Lc->next = pc;
pc->coef = pb->coef; pc->exp = pb->exp; pb = pb->next; } }
void subtract(polynmial La, polynmial Lb, polynmial &Ld)//多项式{
struct node *pa, *pb; static struct node *pd;
Ld = (struct node*)malloc(sizeof(struct node)); pa = La->next; pb = Lb->next; Ld->next = NULL; while(pa && pb) {
pd = (struct node*)malloc(sizeof(struct node)); if(pa->exp < pb->exp) {
pd->next = Ld->next; Ld->next = pd;
pd->coef = pa->coef; pd->exp = pa->exp; pa = pa->next; } else
if(pa->exp == pb->exp) {
pd->next = Ld->next; Ld->next = pd; pd->exp = pa->exp;
pd->coef = pa->coef - pb->coef; pa = pa->next; pb = pb->next; } else {
pd->next = Ld->next; Ld->next = pd;
pd->coef = pb->coef; pd->exp = pb->exp;
La减去Lb,结果给Ld
pb = pb->next; } }
while(pa) {
pd = (struct node*)malloc(sizeof(struct node)); pd->next = Ld->next; Ld->next = pd;
pd->coef = pa->coef; pd->exp = pa->exp; pa = pa->next; }
while(pb) {
pd = (struct node*)malloc(sizeof(struct node)); pd->next = Ld->next; Ld->next = pd;
pd->coef = -pb->coef; pd->exp = pb->exp; pb = pb->next; } }
int main() {
int sign;
polynmial La, Lb, Lc, Ld;
printf(\请输入第一个多项式:\\n\create(La); sort(La);
printf(\请输入第二个多项式:\\n\create(Lb); sort(Lb);
select();
scanf(\switch(sign) {
case 1:
printf(\多项式之和为:\\n\ add(La, Lb, Lc); sort(Lc);
reverse(Lc); display(Lc); break; default:
printf(\多项式之差为:\\n\ subtract(La, Lb, Ld); sort(Ld); reverse(Ld); display(Ld); break; } int a;
printf(\是否继续进行运算?scanf(\if(a==1) main(); else if (a==0) return 0; }
,是;2,否\1
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 多项式
- 稀疏
- 一元
- 计算器
- 源代码