forked from thantrieu/LearnC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbai4.19.c
More file actions
62 lines (51 loc) · 931 Bytes
/
bai4.19.c
File metadata and controls
62 lines (51 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
void nhap(int *a, int *n) {
*n = -1;
while(*n < 0) {
printf("Nhap bac cua da thuc: ");
scanf("%d", n);
}
int i;
for(i = 0; i <= *n; i++) {
printf("he so x^%d = ", i);
scanf("%d", &a[i]);
}
}
void khoiTao(int *a, int n) {
int i;
for(i = 0; i < n; i++) {
a[i] = 0;
}
}
void tong(int *a, int m, int *b, int n, int *s, int *k) {
int i;
*k = (m > n) ? m : n;
for(i = 0; i <= *k; i++) {
s[i] = a[i] + b[i];
}
}
void hienThi(int *a, int n) {
int i;
for(i = n; i >= 0; i--) {
printf("%dx^%d", a[i], i);
if(i > 0 && a[i-1] > 0) {
printf(" + ");
} else if(a[i - 1] < 0) {
printf(" ");
}
}
}
int main() {
int p[100], q[100], s[100];
int m, n, k;
khoiTao(p, 100);
khoiTao(q, 100);
printf("Thong tin da thuc P(x): \n");
nhap(p, &m);
printf("Thong tin da thuc Q(x): \n");
nhap(q, &n);
tong(p, m, q, n, s, &k);
printf("S(x) = ");
hienThi(s, k);
return 0;
}