Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 6th, 2012  |  syntax: C++  |  size: 2.80 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream.h>
  2. #include <conio.h>
  3. static const int LIMIT=10;
  4.  
  5. class Array
  6. {       public:
  7.         unsigned int arr[LIMIT];
  8.         int kol_elem;
  9.         Array();
  10.         ~Array(){};
  11.         unsigned int& operator[] (int kol_elem);
  12.         virtual void Summa(unsigned int arr_1[LIMIT],unsigned int arr_2[LIMIT]);
  13. };
  14.  
  15. class Hex: public Array
  16. {       public:
  17.         unsigned char hex[LIMIT];
  18.         int dec;
  19.         unsigned int arr_of_hex[LIMIT];
  20.         int HexToDec();
  21.         void Summa(unsigned int arr_of_hex_1[LIMIT],unsigned int arr_of_hex_2[LIMIT]){};
  22. };
  23.  
  24. class Money: public Array
  25. {       public:
  26.         unsigned int arr_of_dec[LIMIT];
  27.         int dec;
  28.         void Summa(unsigned int arr_of_hex_1[LIMIT],unsigned int arr_of_hex_2[LIMIT]){};
  29. };
  30. //============================================================================//
  31. Array::Array()
  32. {
  33.         for(int i=0;i<LIMIT;i++)
  34.                 arr[i]=0;
  35. }
  36.  
  37. unsigned int& Array::operator[] (int kol_elem)
  38. {
  39.         if(kol_elem<0 || kol_elem>=LIMIT)
  40.         {
  41.                 cout<<"Oshibochnui index!!!"<<endl;
  42.                 exit(1);
  43.         }
  44.         return arr[kol_elem];
  45. }
  46.  
  47. void Array::Summa(unsigned int arr_1[LIMIT],unsigned int arr_2[LIMIT])
  48. {
  49.         unsigned int sumarr[LIMIT];
  50.         for (int i=0; i<kol_elem; i++)
  51.         {
  52.                 sumarr[i]=arr_1[i]+arr_2[i];
  53.                 cout<<sumarr[i]<<" ";
  54.         }
  55. }
  56. //============================================================================//
  57. int Hex::HexToDec()
  58. {
  59.         int v;     //вес разряда шестнадцатеричного числа
  60.         int err;   //err==1 - в строке недопустимый символ
  61.         int i;
  62.         err=0;
  63.         strupr(hex); //преобразование введенной строке к верхнему регистру
  64.         dec=0;
  65.         v=1; //вес младшего разряда шестнадцатеричного числа
  66.  
  67.         for (i = strlen(hex)-1; i>=0; i--)
  68.         {
  69.                 if (hex[i]>='0' && hex[i]<='9')
  70.                 dec+=v*(hex[i]-48);
  71.                 else if (hex[i]>='A' && hex[i]<='F')
  72.                         dec+=v*(hex[i]-55);
  73.                          else
  74.                          {
  75.                                 err=1;
  76.                                 break;
  77.                          }
  78.                          v*=16;
  79.         }
  80.         if(!err)
  81.         {
  82.                 printf("Shestnadchaterichnomu chislu %s",hex);
  83.                 printf(" sootvetstvuet desyatichnoe %u\n",dec);
  84.         }
  85.         else
  86.         {
  87.            printf("Stroka %s ne yavlyaetsya ",hex);
  88.            printf("shestnadchaterichnum chislom\n");
  89.         }
  90.         return dec;
  91. }
  92.  
  93. int main()
  94. {
  95.         Array a;
  96.         Hex h;
  97.         Money m;
  98.  
  99.         cout<<"Kol_elem: ";
  100.         cin>>a.kol_elem;
  101.  
  102.         for (int j=0; j<a.kol_elem; j++)
  103.         {
  104.                 cout<<"Vvedite Hex: ";
  105.                 cin>>h.hex;
  106.                 h.HexToDec();
  107.                 h.arr_of_hex[j]=h.dec;
  108.  
  109.         }
  110.         cout<<"Elementu array_of_hex: ";
  111.         for (int j=0; j<a.kol_elem; j++)
  112.         {
  113.            cout<<h.arr_of_hex[j]<<" ";
  114.         }
  115.         cout<<endl<<endl<<"Vvedite elementu array_of_money: ";
  116.         for(int i=0; i<a.kol_elem; i++)
  117.         {
  118.                 cin>>m.arr_of_dec[i];
  119.         }
  120.         cout<<"Elementu array_of_dec: ";
  121.         for(int i=0; i<a.kol_elem; i++)
  122.         {
  123.                 cout<<m.arr_of_dec[i]<<" ";
  124.         }
  125.         cout<<endl<<"Sum_array: ";
  126.         a.Summa(h.arr_of_hex,m.arr_of_dec);
  127.  
  128.  
  129.  
  130.  
  131.  
  132.         getch();
  133.         return 0;
  134. }