本文目錄一覽:
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]);
}
原創文章,作者:LLLT,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/139149.html