Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.45 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void printtab(int tab[]);
  4. void add(int tab1[], int tab2[], int result[]);
  5. void soustr(int tab1[], int tab2[], int result[]);
  6. void calcul(int tab1[], int tab2[], int tabresult[], char op);
  7. void divid2(int dividend[], int divisor[], int result[]);
  8. void empty(int tab[]);
  9. void reverse(int tab[]);
  10. int max(int tab1[], int tab2[], int option);
  11. void fill(int tab[], char *var_main);
  12. void copy(int model[], int copy[]);
  13.  
  14. void mult(int tab1[], int tab2[], int result[]) {
  15.  
  16.     int i,j,k,unit1,unit2,mtp,count,indice;
  17.     int taille1=tab1[1];
  18.     int taille2=tab2[1];
  19.     int temp2[1002]= {0};
  20.     int temp3[1002]= {0};
  21.     temp2[1]=1;
  22.     temp3[1]=1;
  23.  
  24.     for(i=0,k=taille2+1 ; i<=taille1 && k>=2 ; i++,k--) {
  25.         unit2=tab2[k];
  26. //printf("unit2: %d\n",unit2);
  27.  
  28.         for(j=taille1+1,count=0; j>=2 && count<taille1; j--,count++) {
  29.             unit1=tab1[j];
  30. //printf("unit1: %d ; ",unit1);
  31.             mtp=unit2*unit1;
  32. //printf("mtp: %d ; ",mtp);
  33.             indice=i+count;
  34. //printf("indice: %d\n",indice);
  35.  
  36.             if(mtp<10) {
  37.                 temp2[2]=mtp;
  38.                 temp2[3]=0;
  39.                 if(indice==0) {
  40.                     temp2[1]=1;
  41.                 } else {
  42.                     temp2[1]=1+indice;
  43.                 }
  44.             } else {
  45.                 temp2[2]=mtp/10;
  46.                 temp2[3]=mtp%10;
  47.                 if(indice==0) {
  48.                     temp2[1]=2;
  49.                 } else {
  50.                     temp2[1]=2+indice;
  51.                 }
  52.             }
  53.  
  54. //printtab(temp2); printf("+"); printtab(temp3); printf("=");
  55.             add(temp2,temp3,result);
  56.             reverse(result);
  57. //printtab(result); printf("\n");
  58.             copy(result,temp3);
  59.         }
  60.     }
  61.     reverse(result);
  62. }
  63.  
  64. void divid(int dividend[], int divisor[], int result[], int option) {
  65.  
  66.     int signe=result[0];
  67.     int previous=0;
  68.     int next=0;
  69.     int taille_divisor=divisor[1];
  70.     int var,i;
  71.  
  72.     while(max(dividend,divisor,0)){
  73.         previous=next;
  74.         divid2(dividend,divisor,result);
  75.         next=divisor[1];
  76. //printf("%d, %d\n",previous,next);
  77.         if(previous-next > 1 && previous!=0){
  78.             var=result[result[1]+1];
  79.             for(i=0; i<previous-next-1; i++){
  80.             result[1]++;
  81.             result[result[1]+i]=0;
  82.             }
  83.             result[result[1]+1]=var;
  84.         }
  85.         divisor[1]=taille_divisor;
  86.     }
  87.  
  88.     if(next>divisor[1]){
  89.         result[1]++;
  90.     }
  91.  
  92.     reverse(result);
  93.     result[0]=signe;
  94.  
  95.     if(option==1){
  96.         empty(result);
  97.         copy(dividend,result);
  98.         reverse(result);
  99.     }
  100. }
  101.  
  102. void divid2(int dividend[], int divisor[], int result[]) {
  103.  
  104.     int temp[1002]={0};
  105.     int count=0;
  106.  
  107. //printf("dividend: "); printtab(dividend);
  108.  
  109.     while(max(dividend,divisor,0)){
  110.         divisor[1]++;
  111.     }
  112.     divisor[1]--;
  113.     while(max(dividend,divisor,0)){
  114.         soustr(dividend,divisor,temp); reverse(temp); copy(temp,dividend); empty(temp);
  115.         count++;
  116.     }
  117.  
  118.     result[1]++;
  119.     result[result[1]+1]=count;
  120.  
  121. //printf("divisor: "); printtab(divisor); printf("result: "); printtab(result);
  122. }
  123.  
  124. void modulo(int tab1[], int tab2[], int result[]) {
  125.  
  126.     if(max(tab2,tab1,0)) {
  127.         copy(tab1,result);
  128.         reverse(result);
  129.         return;
  130.     }
  131.  
  132.     tab1[0]=0;
  133.     tab2[0]=0;
  134.  
  135.     divid(tab1,tab2,result,1);
  136.  
  137.     if(tab1[0]==1 && tab2[0]==0) {
  138.         result[0]=1;
  139.     }
  140.  
  141. }
  142.  
  143. void add(int tab1[], int tab2[], int result[]) {
  144.  
  145.     int i,sum;
  146.     int j=2;
  147.     int retenue=0;
  148.     int taillemax;
  149.     int taille1=tab1[1]+1;
  150.     int taille2=tab2[1]+1;
  151.     if(taille1 >= taille2) {
  152.         taillemax=taille1;
  153.     } else {
  154.         taillemax=taille2;
  155.     }
  156.  
  157.     for(i=taillemax; i>=2; i--) {
  158.         if(taille1<=1) {
  159.             sum=tab2[taille2]+retenue;
  160.         } else if(taille2<=1) {
  161.             sum=tab1[taille1]+retenue;
  162.         } else {
  163.             sum=tab1[taille1]+tab2[taille2]+retenue;
  164.         }
  165.         retenue=0;
  166.         if (sum<10) {
  167.             result[j]=sum;
  168.             j++;
  169.         } else {
  170.             result[j]=sum%10;
  171.             retenue++;
  172.             j++;
  173.         }
  174.         taille1--;
  175.         taille2--;
  176.     }
  177.  
  178.     if(retenue==1) {
  179.         result[j]=1;
  180.         result[1]=taillemax;
  181.     } else {
  182.         result[1]=taillemax-1;
  183.     }
  184.  
  185. }
  186.  
  187. void soustr(int tab1[], int tab2[], int result[]) {
  188.  
  189.     if(max(tab1,tab2,0)==-1) {
  190.         result[0]=0;
  191.         result[1]=1;
  192.         result[2]=0;
  193.         return;
  194.     }
  195.  
  196.     int signe=result[0];
  197.     int taille1=tab1[1]+1;
  198.     int taille2=tab2[1]+1;
  199.     result[0]=0;
  200.  
  201.     if(signe == 2 && max(tab2,tab1,0)) {
  202.         result[0]=1;
  203.     }
  204.     if(signe == 3 && max(tab1,tab2,0)) {
  205.         result[0]=1;
  206.     }
  207.  
  208.     int i,sous;
  209.     int j=2;
  210.     int retenue=0;
  211.     int taillemax;
  212.     if(taille1 >= taille2) {
  213.         taillemax=taille1;
  214.     } else {
  215.         taillemax=taille2;
  216.     }
  217.  
  218.     for(i=taillemax; i>=2; i--) {
  219.         if(taille1<=1) {
  220.             sous=tab2[taille2]-retenue;
  221.         } else if(taille2<=1) {
  222.             sous=tab1[taille1]-retenue;
  223.         } else {
  224.             sous=tab1[taille1]-tab2[taille2]-retenue;
  225.         }
  226. //printf("Retenue= %d ; ", retenue);
  227.         retenue=0;
  228.         if (sous >= 0) {
  229.             result[j]=sous;
  230.             j++;
  231.         } else {
  232.             result[j]=10+sous;
  233.             retenue++;
  234.             j++;
  235.         }
  236. //printf("tab1[%d]=%d - tab2[%d]=%d=%d\n", taille1, tab1[taille1], taille2, tab2[taille2], result[j-1]);
  237.         taille1--;
  238.         taille2--;
  239.     }
  240.  
  241.     while(result[taillemax] == 0) {
  242.         taillemax--;
  243.     }
  244.  
  245.     result[1]=taillemax-1;
  246.  
  247. }
  248.  
  249. void calcul(int tab1[], int tab2[], int tabresult[], char op) {
  250.  
  251.     if(op == '*') {
  252.         if( (tab1[0]==1 && tab2[0]==0) || (tab1[0]==0 && tab2[0]==1) )  {
  253.             tabresult[0]=1;
  254.         }
  255.         if(max(tab1,tab2,0)) {
  256.             mult(tab1,tab2,tabresult);
  257.             return;
  258.         } else {
  259.             mult(tab2,tab1,tabresult);
  260.             return;
  261.         }
  262.     }
  263.     if(op == '/') {
  264.         if(max(tab1,tab2,0)) {
  265.             if( (tab1[0]==1 && tab2[0]==0) || (tab1[0]==0 && tab2[0]==1) ) {
  266.                 tabresult[0]=1;
  267.             }
  268.             divid(tab1,tab2,tabresult,0);
  269.             return;
  270.         } else {
  271.             tabresult[1]=1;
  272.             return;
  273.         }
  274.     }
  275.     if(op == '%') {
  276.         modulo(tab1,tab2,tabresult);
  277.         return;
  278.     }
  279.     if(op == '+') {
  280.         // -X + Y   -> Positif pour Y > X
  281.         // => Y-X  si  Y > X
  282.         if(tab1[0]==1 && tab2[0]==0) {
  283.             if(max(tab2,tab1,0)) {
  284.                 tabresult[0]=0;
  285.                 soustr(tab2,tab1,tabresult);
  286.                 return;
  287.             }
  288.             tabresult[0]=3;
  289.             soustr(tab1,tab2,tabresult);
  290.             return;
  291.         }
  292.         // -X + -Y  -> Négatif
  293.         if(tab1[0]==1 && tab2[0]==1) {
  294.             tabresult[0]=1;
  295.             add(tab1,tab2,tabresult);
  296.             return;
  297.         }
  298.         // X + -Y   -> Positif pour X > Y
  299.         if(tab1[0]==0 && tab2[0]==1) {
  300.             tabresult[0]=2;
  301.             soustr(tab1,tab2,tabresult);
  302.             return;
  303.         }
  304.         add(tab1,tab2,tabresult);
  305.         return;
  306.     }
  307.     if(op == '-') {
  308.         // -X - Y   -> Négatif
  309.         if(tab1[0]==1 && tab2[0]==0) {
  310.             tabresult[0]=1;
  311.             add(tab1,tab2,tabresult);
  312.             return;
  313.         }
  314.         // -X - -Y  -> Positif pour Y > X
  315.         // => Y-X  si  Y > X
  316.         if(tab1[0]==1 && tab2[0]==1) {
  317.             if(!max(tab1,tab2,0)) {
  318.                 tabresult[0]=0;
  319.                 soustr(tab2,tab1,tabresult);
  320.                 return;
  321.             }
  322.             tabresult[0]=3;
  323.             soustr(tab1,tab2,tabresult);
  324.             return;
  325.         }
  326.         // X - -Y   -> Positif
  327.         if(tab1[0]==0 && tab2[0]==1) {
  328.             add(tab1,tab2,tabresult);
  329.             return;
  330.         }
  331.         // X - Y    -> Positif pour X > Y
  332.         if(tab1[0]==0 && tab2[0]==0) {
  333.             if(!max(tab1,tab2,0)) {
  334.                 tabresult[0]=3;
  335.                 soustr(tab2,tab1,tabresult);
  336.                 return;
  337.             }
  338.             tabresult[0]=2;
  339.             soustr(tab1,tab2,tabresult);
  340.             return;
  341.         }
  342.     }
  343.  
  344. }
  345.  
  346. void empty(int tab[]){
  347.     int i;
  348.     for(i=0; i<tab[1]+1; i++){
  349.         tab[i]=0;
  350.     }
  351. }
  352.  
  353. void copy(int model[], int copy[]) {
  354.     empty(copy);
  355.     int taille=model[1]+2;
  356.     int i;
  357.  
  358.     for(i=0; i<taille; i++) {
  359.         copy[i]=model[i];
  360.     }
  361. }
  362.  
  363. void reverse(int tab[]) {
  364.     int i;
  365.     int temp;
  366.     int taille=tab[1]+4;
  367.  
  368.     for(i=2; i<taille/2; i++) {
  369.         temp=tab[i];
  370.         tab[i]=tab[taille-i-1];
  371.         tab[taille-i-1]=temp;
  372.     }
  373. }
  374.  
  375. //Renvoie 1 si X > Y
  376. int max(int tab1[], int tab2[], int option) {
  377.     int i;
  378.     int taille=tab1[1]+1;
  379.     int taille2=tab2[1]+1;
  380.     int verif=1;
  381.  
  382.     if(tab1[0]==0 && tab2[0]==1 && option==1) {
  383.         return 1;
  384.     }
  385.  
  386.     if(tab1[0]==1 && tab2[0]==0 && option==1) {
  387.         return 0;
  388.     }
  389.  
  390.     if(taille > taille2) {
  391.         return 1;
  392.     }
  393.  
  394.     if(taille < taille2) {
  395.         return 0;
  396.     }
  397.  
  398.     for(i=2; i<=taille; i++) {
  399. //printf("Compare: tab1[%d]=%d   et tab2[%d]=%d\n", i, tab1[i], i, tab2[i]);
  400.         if(tab1[i] > tab2[i]) {
  401.             return 1;
  402.         }
  403.         if(tab1[i] < tab2[i]) {
  404.             return 0;
  405.         }
  406.         if(tab1[i] != tab2[i]) {
  407.             verif=0;
  408.         }
  409.     }
  410.     if(verif==1) {
  411.         return -1;
  412.     }
  413.     return 0;
  414. }
  415.  
  416. void printtab(int tab[]) {
  417.     int i=2;
  418.     int taille=tab[1]+2;
  419.  
  420.     if (tab[0]==1) {
  421.         printf("-");
  422.     }
  423.  
  424.     for(i=2; i<taille; i++) {
  425.         //if(i==2) {printf("-");}
  426.         printf("%d", tab[i]);
  427.     }
  428.     printf("\n");
  429. }
  430.  
  431. void fill(int tab[], char *var_main) {
  432.  
  433.     char var=*var_main;
  434.     int i=3;
  435.  
  436.     //Skip espaces avant nombre
  437.     do {
  438.         scanf("%c", &var);
  439. //printf("var: '%c'\n", var);
  440.     } while(var == ' ');
  441.  
  442.     //Attribution négatif(1)
  443.     if(var == '-') {
  444.         tab[0]=1;
  445.         //Skip espaces entre ' - ' et le nombre
  446.         do {
  447.             scanf("%c", &var);
  448. //printf("var: '%c'\n", var);
  449.         } while(var == ' ');
  450.         tab[2]=var - '0';
  451.         //Attribution positif(0)
  452.     } else {
  453.         tab[0]=0;
  454.         tab[2]=var - '0';
  455.     }
  456.  
  457.     //Remplissage tab
  458.     scanf("%c", &var);
  459. //printf("var: '%c'\n", var);
  460.     while(var >= '0' && var <= '9') {
  461.         tab[i]=var - '0';
  462.         i++;
  463.         scanf("%c", &var);
  464. //printf("var: '%c'\n", var);
  465.     }
  466.     tab[1]=i-2;
  467.  
  468.     *var_main=var;
  469.  
  470. }
  471.  
  472.  
  473. int main (int argc, char *argv[]){
  474.  
  475.     int tab1[102]= {0};
  476.     int tab2[102]= {0};
  477.     int tab3[102]= {0};
  478.     int tabresult[1002]= {0};
  479.     int tabfinal[1002]= {0};
  480.     char op1, op2, var;
  481.  
  482.     fill(tab1,&var);
  483.  
  484.     //Skip espaces et récupération 1er opérateur
  485.     while(var == ' ') {
  486.         scanf("%c", &var);
  487.     }
  488.     op1=var;
  489.  
  490. //printtab(tab1);
  491. //printf("operateur1: '%c'\n", op1);
  492.  
  493.     fill(tab2,&var);
  494.  
  495.     //Skip espaces et récupération 2e opérateur
  496.     while(var == ' ') {
  497.         scanf("%c", &var);
  498.     }
  499.     op2=var;
  500.  
  501. //printtab(tab2);
  502. //if(op2!=10) printf("operateur2: '%c'\n", op2);
  503.  
  504.  
  505.     //Test 3e opérateur
  506.     if((int)op2 == 10) {
  507.         calcul(tab1,tab2,tabfinal,op1);
  508. //printf("Before reverse: ");
  509. //printtab(tabfinal);
  510.         reverse(tabfinal);
  511. //printf("Result: ");
  512.  
  513.         printtab(tabfinal);
  514.         return 0;
  515.     }
  516.  
  517.     fill(tab3,&var);
  518.  
  519. //printtab(tab3);
  520.  
  521.  
  522.     //Calcul prioritaire de tab2 et tab3 puis tab1 et result
  523.     if((op2 == '*' || op2=='/' || op2=='%') && (op1 != '*' && op1 != '/' && op1 != '%')) {
  524.         calcul(tab2,tab3,tabresult,op2);
  525. //printf("Before reverse 1: ");
  526. //printtab(tabresult);
  527.         reverse(tabresult);
  528. //printf("Result 1: ");
  529. //printtab(tabresult);
  530.         calcul(tab1,tabresult,tabfinal,op1);
  531. //printf("Before reverse 2: ");
  532. //printtab(tabfinal);
  533.         reverse(tabfinal);
  534. //printf("Result 2: ");
  535.  
  536.         printtab(tabfinal);
  537.         return 0;
  538.     }
  539.  
  540.     //Calcul de tab1 et tab2 puis tabresult et tab 3
  541.     calcul(tab1,tab2,tabresult,op1);
  542. //printf("Before reverse 1: ");
  543. //printtab(tabresult);
  544.     reverse(tabresult);
  545. //printf("Result 1: ");
  546. //printtab(tabresult);
  547.     calcul(tabresult,tab3,tabfinal,op2);
  548. //printf("Before reverse 2: ");
  549. //printtab(tabfinal);
  550.     reverse(tabfinal);
  551. //printf("Result 2: ");
  552.  
  553.     printtab(tabfinal);
  554.     return 0;
  555. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement