Advertisement
mrnn_

4302 - Tetszőleges és tízes számrendszerek közötti átváltás

Nov 23rd, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6.  
  7. char szamok[37]={'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  8.  
  9. int nyilkeres(char* str){
  10.     int i;
  11.     for (i = 0; i < strlen(str); i++)
  12.     {
  13.         if (str[i]=='-')
  14.         {
  15.             if (str[i-1]=='<' && i!=0)
  16.             {
  17.                 return 2;
  18.             }
  19.             else if (str[i+1]=='>')
  20.             {
  21.                 return 1;
  22.             }
  23.         }
  24.     }
  25.     return 0;
  26. }
  27.  
  28. long long int poww(long long int a, long long int b)
  29.     {
  30.         long long int ret=1;
  31.         int i;
  32.         for (i = 0; i != b; i++)
  33.         {
  34.             ret*=a;
  35.         }
  36.         return ret;
  37.     };
  38.    
  39. bool szrbeli_e(long long int bol, long long int ba, char szam[]){
  40.     int i,j,ok=0;
  41.     if (!((bol >=2 && bol <= 36 && ba==10) || (ba >=2 && ba <= 36 && bol==10)))
  42.     {
  43.         return false;
  44.     }
  45.    
  46.     for (i = 0; szam[i]; i++)
  47.     {
  48.         for (j = 0; j < bol; j++)
  49.         {
  50.             if (szam[i]==szamok[j] ||szam[i]== tolower(szamok[j]))
  51.             {
  52.                 ok++;
  53.                 break;
  54.             }
  55.         }
  56.     }
  57.     return (ok==i);
  58. }
  59.  
  60. int char_to_num(char c){
  61.     int i;
  62.     for (i = 0; szamok[i]; i++)
  63.     {
  64.         if (c==tolower(szamok[i]) || c==toupper(szamok[i]))
  65.         {
  66.             return i;
  67.         }
  68.     }
  69.     return -1;
  70. }
  71.  
  72. long long int conv_to_10(long long int bol, char szam[]){
  73.     int i;
  74.     long long int ret=0;
  75.     for (i = 0; szam[i]; i++)
  76.     {
  77.         ret+=(char_to_num(szam[i]))*poww(bol, strlen(szam)-i-1);
  78.     }
  79.     return ret;
  80. }
  81.  
  82. char* conv_from_10(long long int ba, long long int forras){
  83.     char* rev;
  84.     rev = (char*)malloc(65*sizeof(char));
  85.     int i=0;
  86.     while (forras / ba > 0)
  87.     {
  88.         rev[i++]=tolower(szamok[forras%ba]);
  89.         forras/=ba;
  90.     }
  91.     rev[i++]=tolower(szamok[forras]);
  92.     rev[i]=0;
  93.     char* ret;
  94.     ret = (char*)malloc(65*sizeof(char));  
  95.     for (i = 0; i<strlen(rev); i++)
  96.     {
  97.         ret[i]=rev[strlen(rev)-i-1];
  98.     }
  99.     return ret;
  100. }
  101.  
  102. int main(int argc, char **argv)
  103. {
  104.     FILE* fbe;
  105.     FILE* fki;
  106.     fki = fopen("ki.txt", "w");
  107.     long long int bol, ba;
  108.     char szam[65];
  109.     if (!(fbe = fopen("be.txt", "r")))
  110.     {
  111.         fprintf(fki, "HIBA\n");
  112.         fclose(fki);
  113.         return 0;
  114.     }
  115.     char str[65];
  116.     fscanf(fbe, "%s", str);
  117.     fclose(fbe);
  118.     fbe = fopen("be.txt", "r");
  119.     switch (nyilkeres(str))
  120.     {
  121.         //1ből
  122.         case 1:
  123.             fscanf(fbe, "%lld->%lld %s", &bol, &ba, szam);
  124.             break;
  125.         //2ból
  126.         case 2:
  127.             fscanf(fbe, "%lld<-%lld %s", &ba, &bol, szam);
  128.             break;
  129.         case 0:
  130.         default:
  131.             fprintf(fki, "HIBA\n");
  132.             fclose(fki);
  133.             return 0;
  134.     }
  135.     if (szrbeli_e(bol, ba, szam))
  136.     {
  137.         fprintf(fki, "%s\n", conv_from_10(ba, conv_to_10(bol, szam)));
  138.         fclose(fki);
  139.     }
  140.     else
  141.     {
  142.         fprintf(fki, "HIBA\n");
  143.         fclose(fki);
  144.     }
  145.     fclose(fbe);
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement