Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<ctype.h>
  4. #define _CRT_SECURE_NO_WARNINGS_
  5. #include <string.h>
  6.  
  7. void antigorner(char*, int, int);
  8. int gorner(char*, int);
  9. void SetError(int errors[][10], int firstBase);
  10.  
  11. int main()
  12. {
  13.     int errors[2][10] = { { 2, 3, 4, 8, 9, 10, 16, 20, 25, 30 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
  14.     FILE* f;
  15.     f = fopen("my.txt", "r");
  16.     int resG, firstNum, firstBase, secondBase;
  17.     char firstSym[10];
  18.     char res[10];
  19.     char bigStr[300];
  20.     char allNumber[300];
  21.     while (fscanf(f, "%s (%d) = %s;",&firstNum, &firstBase, allNumber) != EOF)
  22.     {
  23.         char *pch = strtok(allNumber, ",");
  24.         printf("%d (%d) = ", firstNum, firstBase);
  25.         while (pch != NULL)
  26.         {
  27.             sscanf(pch, "%s(%d)", firstSym, &secondBase); // не верно
  28.             resG = gorner(firstSym, secondBase);
  29.             if (resG != firstNum)
  30.             {
  31.                 antigorner(res, firstNum, secondBase);
  32.                 SetError(errors, firstBase);
  33.                 printf("%s (%d), ", res, secondBase);
  34.             }
  35.             pch = strtok(NULL, ",");
  36.         }
  37.     }
  38.  
  39.     for (int i = 0; i < 2; i++)
  40.     {
  41.         for (int j = 0; j < 10; j++)
  42.         {
  43.             printf("%d\t", errors[i][j]);
  44.         }
  45.  
  46.         printf("\n");
  47.     }
  48.     system("pause");
  49.     return 0;
  50.  
  51. }
  52.  
  53. void antigorner(char* res, int num, int base)
  54. {
  55.     int tmp = 0;
  56.     int n = num;
  57.     int numberofdigits = 0;
  58.     while (n)
  59.     {
  60.         n = n / base;
  61.         numberofdigits++;
  62.     }
  63.  
  64.     char* pres = res;
  65.     while (numberofdigits--)
  66.     {
  67.         *pres = 0;
  68.         pres++;
  69.     }
  70.     *pres = 0;
  71.     while (num)
  72.     {
  73.         *--pres = ((tmp = num % base) < 10) ? (tmp + '0') : (tmp + 'A' - 10);
  74.         num = num / base;
  75.     }
  76. }
  77.  
  78.  
  79. int gorner(char* s, int base)
  80. {
  81.     int res = 0;
  82.     while (*s)
  83.     {
  84.         res = res * base;
  85.         if (isdigit(*s))
  86.         {
  87.             res = res + *s - '0';
  88.             s++;
  89.         }
  90.         else
  91.         {
  92.             res = res + toupper(*s) - 'A' + 10;
  93.             s++;
  94.         }
  95.     }
  96.     return res;
  97. }
  98.  
  99. void SetError(int errors[][10], int firstBase)
  100. {
  101.     int i;
  102.     for (i = 0; i <= 10; i++)
  103.     {
  104.         if (errors[0][i] == firstBase)
  105.         {
  106.             break;
  107.         }
  108.  
  109.     }
  110.     errors[1][i] += 1;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement