n0va_sa

Middle-Term-Factorization

Jan 31st, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. using namespace std;
  6. void find_the_Number(int,int,int,int);
  7. int gcd(int,int);
  8. char problem[20], beg[20],mid[10],end[10];char extr;
  9. int main(){
  10.     int temp;
  11.     cin >> problem; // 6x^2
  12.     cin >> mid; // +5x
  13.     cin >> end;//-6
  14.     char *ptr;
  15.     int a,b,c;
  16.     a = strtol(problem, &ptr, 10); b = strtol(mid, &ptr, 10);c = strtol(end, &ptr, 10);
  17.     for(int i=0;i<strlen(mid);i++){ // extracting the mid char
  18.         if(mid[i] >= 97 && mid[i] <= 122){
  19.             extr = mid[i];
  20.             break;
  21.         }
  22.     }
  23.     if(a == 0) a = 1;
  24.     if(b == 0 ) b = 1;
  25.     cout << " A = " <<a << " b = " << b << "C = "<<c<<endl;
  26.     long int res = a*c;
  27.     strcpy(beg,problem);
  28.     strcat(problem,mid);//Making It The Total String 6x^2 + 5x - 6
  29.     strcat(problem,end);//Same
  30.     find_the_Number(res,b,a,c);
  31. }
  32. void find_the_Number(int number,int diff,int begNum,int endNum){
  33.     bool negativeBit = false;
  34.     int co =0; int flag=0;
  35.     int arr[20];
  36.     if (number < 0){
  37.         negativeBit = true; number = -number;
  38.     }
  39.     for(int i=1; i <= number; ++i){
  40.         if (number%i == 0) arr[co++] = i;
  41.     }// (ax^2 + bx + c) //
  42.     int a , b;
  43.     for(int k = 0;k<co;k++){ // product should be equal to res,and the diffrence
  44.         for(int s= 0;s<co;s++){ /*should be equal to b*/
  45.             if(negativeBit){
  46.                 if(arr[k] - arr[s] == diff && arr[k] * arr[s] == number){
  47.                 a = arr[k]; b = -arr[s];
  48.                 flag = 1;
  49.                 break;}
  50.                 if(flag == 1)break;
  51.             }
  52.             else{
  53.                 if(arr[k] + arr[s] == diff && arr[k] * arr[s] == number){
  54.                 a = arr[k]; b = arr[s];
  55.                 flag = 1;
  56.                 break;}
  57.                 if(flag == 1)break;
  58.             }      
  59.         }
  60.     }
  61.     system ("cls");
  62.     cout << endl<<endl;
  63.     cout << problem;cout << endl;
  64.     cout << endl;
  65.     printf("%s %+d%c %+d%c %s\n",beg,a,extr,b,extr,end);
  66.     cout << endl;
  67.     int com1 = gcd(begNum,a);
  68.     int com2 = gcd(endNum,b);
  69.     printf("%+d%c(%+d%c%+d)%+d(%+d%c%+d)\n",com1,extr,begNum/com1,extr,a/com1,com2,b/com2,extr,endNum/com2);
  70.     cout << endl;
  71.     printf("(%+d%c%+d)(%+d%c%+d)\n",com1,extr,com2,begNum/com1,extr,a/com1);
  72.     cout << endl;
  73. }
  74. int gcd(int a ,int b){
  75.     int rem = 10;
  76.     while(rem != 0){
  77.         rem = b%a;
  78.         b = a;
  79.         a = rem;
  80.     }
  81.     return b;
  82. }
Add Comment
Please, Sign In to add comment