Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "header.h"
- void pascalTab(int ** tab, int n) // tworzenie tablicy pascala
- {
- for(int i=0;i<n;i++)
- {
- tab[i][0] = 0;
- tab[0][i] = 1;
- }
- for(int i=1;i<n;i++)
- {
- for(int j=1;j<n;j++)
- {
- tab[j][i] = tab[j-1][i-1] + tab[j][i-1];
- }
- }
- };
- bool getPolynomial(string line, int * c, int * e, int &word, int &d)
- {
- int size = strlen(line.c_str());
- int i = 1;
- bool cprog = true;
- bool eprog = false;
- word = -1;
- d = 0;
- while(i<size)
- {
- if (line[i] == '.') // jesli natrafimy na kropke to wyswietlamy komunikat i wychodzimy z funkcji
- {
- if(cprog == true)
- {
- cout << "C is not an integer.\n";
- return false;
- }
- if(eprog == true)
- {
- cout << "E is not an integer.\n";
- return false;
- }
- }
- if (i==1 && line[i] == 'n') // jesli pierwszy wyraz w linii to odrazu n to domyslnie c ustawiamy na 1
- {
- word++;
- c[word] = 1;
- cprog = false;
- eprog = true;
- }
- if (line[i] == '-') // jesli mamy znak to wiemy ze dostaniemy kolejna wartosc w wielomianie
- {
- word++;
- cprog = true;
- eprog = false;
- if (line[i+1] == 'n')
- {
- c[word] = -1;
- }
- else
- {
- c[word] = 0;
- }
- }
- if (line[i] == '+')
- {
- word++;
- cprog = true;
- eprog = false;
- if (line[i+1] == 'n')
- {
- c[word] = 1;
- }
- else
- {
- c[word] = 0;
- }
- }
- if (line[i]>='0' && line[i] <='9')
- {
- if (i==1)
- {
- word++;
- }
- if (cprog == true)
- {
- c[word]*=10;
- if (c[word] >= 0)
- c[word]+=line[i] - 48;
- else
- c[word]-=line[i] - 48;
- if (line[i-1] == '-')
- c[word] *= -1;
- }
- if (eprog == true)
- {
- e[word]*=10;
- e[word]+=line[i] - 48;
- if (e[word] > 100)
- {
- cout << "Potega nie miesci sie w przedziale <0,100>\n";
- return false;
- }
- }
- }
- if (line[i] == 'n')
- {
- if (line[i+1] != '^')
- e[word] = 1;
- cprog = false;
- eprog = true;
- }
- if (line[i] == ')')
- {
- if(line[i]-1=='n')
- {
- e[word] = 1;
- }
- break;
- }
- i++;
- }
- i+=2;
- if (line[i] == '-')
- {
- cout << "D is negative integer.\n";
- return false;
- }
- while(i<size)
- {
- if (line[i] == '.')
- {
- cout << "D is not an integer.\n";
- return false;
- }
- d *= 10;
- d += line[i] - 48;
- i++;
- }
- return true;
- };
- void cleanPolynomial(int *c, int *e, int n)
- {
- for(int i=0;i<n;i++)
- {
- c[i] = 0;
- e[i] = 0;
- }
- };
- void calcPolynomial(int *c, int *e, int word, int ** tab, int d)
- {
- if (d==0)
- {
- cout << "You can not divide by zero." << endl;
- return;
- }
- int sum = 0;
- for(int i=0;i<=word;i++)
- {
- sum+=c[i];
- }
- if (sum%d != 0)
- {
- cout << "Not always an integer." << endl;
- return;
- }
- int st = e[0];
- int * temp = new int[st+1];
- int * p2 = new int[st+1];
- for(int i=0;i<=st;i++)
- {
- p2[i] = 0;
- }
- for(int w=0;w<=word;w++)
- {
- int stopien = e[w];
- int wsp = c[w];
- for(int i=0;i<=stopien;i++)
- {
- temp[i] = tab[i][stopien];
- temp[i] *= wsp;
- }
- for(int i=0;i<=stopien;i++)
- {
- p2[i] += temp[i];
- }
- };
- for(int i=0;i<=st;i++)
- {
- for(int j=0;j<=word;j++)
- {
- if (e[j]==i)
- {
- p2[i] -= c[j];
- }
- }
- };
- for(int i=0;i<=st;i++)
- {
- if (p2[i]%d!=0)
- {
- cout << "Not always an integer." << endl;
- return;
- }
- }
- cout << "Always an integer." << endl;
- };
Add Comment
Please, Sign In to add comment