本文目录一览:
C语言编写一元多项式相乘,求解!!
你提的这个要求实在是太高了。你的这个大作业肯定是不会有人满足你的。因为从编写程序的角度上讲,使用任何一种编程语言编写带有一定复杂功能的任何程序,实际本质上都是一项艰苦的脑力劳动。即:除了编写程序之外,更多的时间和精力是需要放在调试程序上面的。
另外,你的要求中涉及到在编程过程中需要用到的很多计算机软件的理论知识。例如:数据结构和算法的设计(例如:链表的创建、排序等)、整个程序总体的设计风格、以及即使你在集成编程环境下面录入完全部的程序源代码,但是程序肯定是必须要经过一系列的不断调试(例如:在可疑的语句处设置断点、单步跟踪等)、编译、链接,直到运行出最终的正确结果。
故你的这个 C 语言大作业,别的任何人无法帮助你实现你的程序功能,只能够依靠自己的刻苦努力来完成它了。
如何用C语言实现两个一元多项式的相加和相乘?
没有别的好办法,你看这样行不行,不行你自己再想想吧
#include
void
main()
{
int
a1,b1,c1,d1,e1,f1,a2,b2,c2,d2,e2,f2;
printf("ax^5+bx^4+cx^3+dx^2+ex+f=0\n");
printf("请输入:a
b
c
d
e
f\n");
printf("第一个:");
scanf("%d%d%d%d%d%d",a1,b1,c1,d1,e1,f1);
printf("第二个:");
scanf("%d%d%d%d%d%d",a2,b2,c2,d2,e2,f2);
printf("两式相加后得:\n");
printf("%dx^5+%dx^4+%dx^3+%dx^2+%dx+%d=0\n",a1+a2,b1+b2,c1+c2,d1+d2,e1+e2,f1+f2);
}
由于变量太多!输出时要注意哦
希望回答对你有帮助!
C语言,多项式相乘
#include stdio.h
#include stdlib.h
typedef struct node {
int coefficient, power;
struct node* next;
}term;
term* new_term(int coefficient, int power) {
term* t = (term*)malloc(sizeof(term));
t-next = NULL;
t-coefficient = coefficient;
t-power = power;
return t;
}
void free_term(term* t) {
free(t);
}
typedef struct list {
term head;
}polynomial;
void init_polynomial(polynomial* p) {
p-head.next = NULL;
}
void clear_polynomial(polynomial* p) {
term* t = p-head.next;
term* del;
while (t != NULL) {
del = t;
t = t-next;
free_term(del);
}
p-head.next = NULL;
}
void insert_polynomial(polynomial* p, term* t) {
t-next = p-head.next;
p-head.next = t;
}
void sort(polynomial* p) {
term* t;
term* next;
int finish = 0, temp;
while (!finish) {
finish = 1;
t = p-head.next;
while (t != NULL) {
next = t-next;
if (next != NULL) {
if (t-power next-power) {
temp = t-coefficient;
t-coefficient = next-coefficient;
next-coefficient = temp;
temp = t-power;
t-power = next-power;
next-power = temp;
finish = 0;
}
}
t = next;
}
}
}
void combine(polynomial* p) {
term* t = p-head.next;
term* next;
while (t != NULL) {
next = t-next;
if (next != NULL next-power == t-power) {
t-coefficient += next-coefficient;
t-next = next-next;
free_term(next);
}
else {
t = next;
}
}
}
void multiply(polynomial* p1, polynomial* p2, polynomial* p3) {
term* t1 = p1-head.next;
term* t2;
clear_polynomial(p3);
init_polynomial(p3);
while (t1 != NULL) {
t2 = p2-head.next;
while (t2 != NULL) {
insert_polynomial(p3, new_term(t1-coefficient*t2-coefficient, t1-power + t2-power));
t2 = t2-next;
}
t1 = t1-next;
}
sort(p3);
combine(p3);
}
void input(polynomial* p) {
int coef, power;
char c;
init_polynomial(p);
while (true) {
scanf("%d%d", coef, power);
insert_polynomial(p, new_term(coef, power));
c = getchar();
if (c == '\n') break;
}
sort(p);
combine(p);
}
void output(polynomial* p) {
term* t = p-head.next;
while (t != NULL) {
printf("%d %d ", t-coefficient, t-power);
t = t-next;
}
}
int main() {
int i;
polynomial p[3];
for (i = 0; i 3; i++) {
init_polynomial(p[i]);
}
for (i = 0; i 2; i++) {
input(p[i]);
}
multiply(p[0], p[1], p[2]);
output(p[2]);
}