i++;
expn = (int)get_coef(str + i);
while ((*(str + i) >= '0'&&*(str + i) <= '9') || (*(str + i) == '.'))i++;
}
}
coefs[cnt] = coef;
expns[cnt] = expn;
cnt++;
}
}
typedef struct Polynomial //多项式
{
double coef; //系数
int expn; //指数
struct Polynomial *next;//指针
} Polynomial, *Polyn;
//创建一个头指针为head,项数为m的一元多项式
void CreatPolyn(Polyn head, int m) //建立链表,在插入过程中实现单链表有序
{
for (int i = 0; i < m; i++)
{
Polyn p = (Polyn)malloc(sizeof(struct Polynomial));
p->coef = coefs[i];
p->expn = expns[i];
if (p->coef == 0) free(p);
else
{
Polyn q1, q2;
q1 = head;
q2 = head->next;
while (q2 != NULL && p->expn < q2->expn)
{
q1 = q2;
q2 = q2->next;
}
if (q2 != NULL && p->expn == q2->expn)
{
q2->coef += p->coef;
if (q2->coef == 0)
{
q1->next = q2->next;
free(q2);
}
free(p);
}
else
{
p->next = q2;
q1->next = p;
}
}
}
}
void printPoLlyn(Polyn head) //进行格式化打印输出
{
Polyn q = head->next;
int flag = 0; //记录是否为第一项
if (!q)
{
puts("NULL(0)\t");
return;
}
while (q)
{
if (q->coef > 0 && flag == 1)
{
printf("+");
}
flag = 1;
if (q->coef != 1 && q->coef != -1)
{
printf("%g", q->coef);
if (q->expn == 1) printf("x");
else if (q->expn != 0) printf("x^%d", q->expn);
}
else
{
if (q->coef == 1)
{
if (q->expn == 0) printf("1");
else if (q->expn == 1) printf("x");
else printf("x^%d", q->expn);
}
if (q->coef == -1)
{
if (q->expn == 0) printf("-1");
else if (q->expn == 1) printf("-x");
else printf("-x^%d", q->expn);
}
}
q = q->next;
}
printf("\t\t");
}
int compare(Polyn a, Polyn b)//比较两个多项式的大小
{
if (a&&b) // 多项式a和b均不为空
{
if (a->expn > b->expn) return 1;// a的指数大于b的指数
else if (a->expn < b->expn) return -1;
else return 0;
}
else if (!a&&b) return -1; //a为空,b不为空
else if (a && !b) return 1; //b为空,a不为空
else if (!a && !b)return 0; //a,b均为空
}
void clear(Polyn c)
{
Polyn p, q;
p = c;
while (p->next != NULL)
{
q = p->next;
p->next = q->next;
free(q);
}
c->next = NULL;
}
void addPolyn(Polyn a1, Polyn b1, Polyn c1) //求解a+b
{
Polyn a = a1;
Polyn b = b1;
Polyn c = c1;
clear(c1);
Polyn head, qc;
Polyn qa = a->next;
Polyn qb = b->next;
head = c;
while (qa || qb)
{
qc = (Polyn)malloc(sizeof(Polynomial));
if (compare(qa, qb) == 1)
{
qc->coef = qa->coef;
qc->expn = qa->expn;
qa = qa->next;
}
else if (compare(qa, qb) == 0) //指数相同,直接相加
{
qc->coef = qa->coef + qb->coef;
qc->expn = qa->expn;
qa = qa->next;
qb = qb->next;
}
else
{
qc->coef = qb->coef;
qc->expn = qb->expn;
qb = qb->next;
}
if (qc->coef != 0) //将该节点插入链表中
{
qc->next = c->next;
c->next = qc;
c = qc;
}
}
}
void subPolyn(Polyn a, Polyn b, Polyn c)// a-b可以用a+b来求解,把b改成-b
{
Polyn h = b;
Polyn p = b->next;
while (p)
{
p->coef *= -1;
p = p->next;
}
addPolyn(a, h, c);
for (Polyn i = h->next; i != 0; i = i->next)
{
i->coef *= -1;
}
}
void goto_xy(int x, int y)
{
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = { x,y };
SetConsoleCursorPosition(hOut, pos);
}
void value(Polyn head, int flag) //计算x的值
{
goto_xy(2, 3); printf(" x = ");
double sum = 0, x;
scanf("%lf", &x);
for (Polyn p = head->next; p != 0; p = p->next)
{
double tmp = 1;
int expn = p->expn;
while (expn != 0) //指数不为0
{
if (expn < 0) tmp /= x, expn++;
else if (expn > 0) tmp *= x, expn--;
}
sum += p->coef*tmp;
}
goto_xy(2, 4);
if (flag == 1) printf(" A( %g )的值 = %g", x, sum);
if (flag == 2) printf(" B( %g )的值 = %g", x, sum);
if (flag == 3) printf(" C( %g )的值 = %g", x, sum);
}
void show(Polyn a, Polyn b, Polyn c) //界面实现
{
goto_xy(0, 0); printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
goto_xy(0, 1); printf("┃ 一元稀疏多项式简单计算器 ┃\n");
goto_xy(0, 2); printf("┃━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ┫");
goto_xy(0, 3); printf("┃\n"); goto_xy(50, 3); printf("┃\n");
goto_xy(0, 4); printf("┃\n"); goto_xy(50, 4); printf("┃\n");
goto_xy(0, 5); printf("┃\n"); goto_xy(50, 5); printf("┃\n");
goto_xy(0, 6); printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫");
goto_xy(0, 7); printf("┃★ A :"); goto_xy(7, 7); printPoLlyn(a); goto_xy(50, 7); printf("┃");
goto_xy(0, 8); printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫");
goto_xy(0, 9); printf("┃★ B :"); goto_xy(7, 9); printPoLlyn(b); goto_xy(50, 9); printf("┃");
goto_xy(0, 10); printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫");
goto_xy(0, 11); printf("┃★ C :"); goto_xy(7, 11); printPoLlyn(c); goto_xy(50, 11); printf("┃");
goto_xy(0, 12); printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┫");
goto_xy(0, 13); printf("┃ 按7进行多项式相加 ┃ 按8进行多项式相减 ┃\n");
goto_xy(0, 14); printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━┫");
goto_xy(0, 15); printf("┃ 按0进行多项式输入 ┃ 按enter执行确定换行 ┃\n");
goto_xy(0, 16); printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━┫");
goto_xy(0, 17); printf("┃ 按1计算多项式A的值 ┃ 按2计算多项式B的值 ┃\n");
goto_xy(0, 18); printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━┫");
goto_xy(0, 19); printf("┃ 按3计算多项式C的值 ┃ 按t退出多项式计算器 ┃\n");
goto_xy(0, 20); printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━┫");
goto_xy(0, 21); printf("┃ ┃\n");
goto_xy(0, 22); printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛");
goto_xy(1, 23); printf("【 一元稀疏多项式简单计算器】");
goto_xy(2, 3);
}
void create(Polyn a1, Polyn b1)
{
Polyn a = a1;
Polyn b = b1;
clear(a1);
clear(b1);
goto_xy(2, 3); printf("请输入多项式a : "); getNums();
m = cnt;
CreatPolyn(a, m);
goto_xy(2, 4); printf("请输入多项式b : "); getNums();
m = cnt;
CreatPolyn(b, m);
}
int main()
{
Polyn a = (Polyn)malloc(sizeof(struct Polynomial)); a->next = NULL;
Polyn b = (Polyn)malloc(sizeof(struct Polynomial)); b->next = NULL;
Polyn c = (Polyn)malloc(sizeof(struct Polynomial)); c->next = NULL;
system("一元多项式计算器 ");
system("mode con cols=52 lines=25");
system("color e0");
char ch, ch1;
while (1)
{
system("cls");
show(a, b, c);
ch = _getch();
if (ch == '0')
{
create(a, b);
}
else if (ch == '7')
{
addPolyn(a, b, c);
}
else if (ch == '8')
{
subPolyn(a, b, c);
最后
各位读者,由于本篇幅度过长,为了避免影响阅读体验,下面我就大概概括了整理了,需要的话请**点赞后点击这里免费下载文章资料!**
");
system("color e0");
char ch, ch1;
while (1)
{
system("cls");
show(a, b, c);
ch = _getch();
if (ch == '0')
{
create(a, b);
}
else if (ch == '7')
{
addPolyn(a, b, c);
}
else if (ch == '8')
{
subPolyn(a, b, c);
最后
各位读者,由于本篇幅度过长,为了避免影响阅读体验,下面我就大概概括了整理了,需要的话请**点赞后点击这里免费下载文章资料!**
[外链图片转存中…(img-TAR1XJXs-1628430789334)]
[外链图片转存中…(img-IWIYzvjQ-1628430789337)]
[外链图片转存中…(img-XLFhr4St-1628430789339)]
|