include stdio h include stdlib h include conio
Програмна реалізація алгоритму #include <stdio. h> #include <stdlib. h> #include <conio. h> #include <math. h> typedef struct el { int n; float *a; } EL; typedef struct st { EL p; struct st *ps; } STACK;
void Add(STACK**, EL); void Del(STACK**); EL proizv(EL); EL delen(EL, EL); //функції опрацювання стеку //знаходження похідної //знаходження остачі від //ділення int sps(STACK*, float); //кількість змін знаку void find(STACK*, float, int); //пошук коренів void main() { clrscr(); STACK *F=NULL; int n, i, tochn=-1; EL A; printf("max stepin="); scanf("%d", &A. n);
A. a=(float*)calloc(A. n+1, sizeof(float)); for (i=0; i<=A. n; i++) { printf("%d ", i); scanf("%f", &A. a[i]); } Add(&F, A); Add(&F, proizv(F->p)); while (F->p. n>0) Add(&F, delen(F->p, F->ps->p)); printf("tochn="); while (tochn<0 || tochn>6) tochn=getch()-48; printf("%dn", tochn); find(F, -50, tochn); while (F!=NULL) Del(&F); if (sps(&F, 50)!=sps(&F, -50)) printf("nplus/minus %. *f", tochn, pow(0. 1, tochn)); getch();
// Функції для роботи зі стеком void Add(STACK **p, EL w) { STACK *q; q=(st*)malloc(sizeof(st)); q->ps=*p; q->p=w; *p=q; } void Del(STACK **p) { STACK *q; q=*p; *p=q->ps; free((q->p). a); free(q); }
while (t. n>=p. n) { q=t. a[t. n]/p. a[p. n]; Q=t. n-p. n; for (i=t. n; i>=0; i--) t. a[i]-=p. a[i-Q]*q; for (i=t. n; i>=0; i--) if(fabs(t. a[i])>0. 0000001) break; if (i==-1) i=0; t. n=i; } t. a=(float*)realloc(t. a, (t. n+1)*sizeof(float)); //перерозподілення динамічної пам'яті for (i=0; i<=t. n; i++) t. a[i]=-t. a[i]; return t; }
//Знаходження кількості змін знаку int sps(STACK *f, float k) { int i, kol=0, R=0; float r; while (f!=NULL) { r=0; if (k==0) r=f->p. a[0]; else for (i=0; i<=f->p. n; i++) r+=f->p. a[i]*pow(k, i); if (R==1 && r<0) {kol++; R=-1; } else if (R==-1 && r>0) { kol++; R=1; } else if (r>0) R=1; else if (r<0) R=-1; f=f->ps; } return kol; }
//Пошук коренів на проміжку від x 1 до x 2 void find(STACK *f, float x 1, float x 2, int tochn) { if (sps(f, x 1)==sps(f, x 2)) return; if (fabs(x 1 -x 2)<pow(0. 1, tochn)) { printf ("%. *f ", tochn, x 1); //виведення значення return; } find(f, x 1, (x 1+x 2)/2, tochn); find(f, (x 1+x 2)/2, x 2, tochn); }
- Slides: 14