Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 16.06.2012, 06:38   #1
Xronikov
Пользователь
 
Аватар для Xronikov
 
Регистрация: 10.06.2012
Сообщений: 17
По умолчанию С++ прегрузка оперций

Здравствуйте уважаемые эксперты.
Задание: Создать класс Decimal для работы с безнаковыми целыми десятичными числами, используя для представления числа массив из 100 элементов типа unsigned char, ка-ждый из которых является десятичной цифрой. Младшая цифра имеет меньший индекс. Реальный размер массива задается как аргумент конструктора инициализации. Реализо-вать арифметические операции, аналогичные встроенным для целых чисел в C++, и опе-рации сравнения. Помогите пожалуйста исправить ошибку и написать этот класс с помощью конструктора и деструктора.
[C++ Error] Unit1.cpp(39): E2333 Class member 'Decimal:utvl() const' declared outside its class.
[C++ Error] Unit1.cpp(40): E2040 Declaration terminated incorrectly

Код:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#pragma hdrstop

#pragma argsused
const int SZ = 100;
class Decimal
{
 private:
         char vlstr [SZ];
         int vlen;
         Decimal multdigit (const int) const;
         Decimal mult10 (const Decimal) const;
 public:
         Decimal (): vlen (0)
         {
           vlstr [0] = '\0';
         }
         Decimal (const char S[SZ])
         {
          strcpy (vlstr, S);
          vlen = strlen (S);
          }
          Decimal (const unsigned long n)
          {
          ltoa (n,vlstr, 10);
          strrev (vlstr);
          vlen = strlen (vlstr);
         }
   void putvl () const;
   void getvl ();
   Decimal operator + (const Decimal);
   Decimal operator * (const Decimal);
 };
 void Decimal :: putvl() const;
 {
     char temp [SZ0];
     strcpy (temp,vlstr);
     cout << strrev (temp);
   }
   void Decimal :: getvl()
   {
      cin >> vlstr;
      vlen = strlen(vlstr);
      strrev (vlstr);
   }
   Decimal Decimal :: operator + (const Decimal V)
   {
      vlen temp [SZ];
      int j;
      int maxlen = (vlen> v.vlen)? vlen : v.vlen;
      int carry = 0;
      for(j = 0; j < max len; j++)
   {
      int d1 = (j > vlen -1)? 0 : vlstr [j] - '0';
      int d2 = (j > v.vlen -1)? 0 : v.vlstr [j]- '0';
      int digitsum = d1 + d2 + carry;
      if (digitsum >= 10)
           {digitsum -= 10; carry = 1;}
      else
        carry = 0;
      temp [j] = digitsum + '0';
   }
   if (carry == 1)
       temp [j++] = '1';
       temp [j] = '\0';
       return Decimal (temp);
   }
   Decimal Decimal :: operator * (const Decimal V)
   {
      Decimal pprod;
      Decimal tempsum;
      for (int j = 0; j < v.vlen; j++)
      {
        int digit = v.vlstr[j] - '0';
        pprod = multdigit (digit);
        for (int k = 0; k<j; k++)
         pprod = mult 10 (pprod);
         tempsum += pprod;
      return tempsum;
   }
   Decimal Decimal :: mult 10 (cout Decimal V)
   { cler temp [SZ];
     for (int j = v.vlen -1; j >= 0; j--)
          temp [j + 1] = v.vlen [j];
     temp [0] = '0';
     temp [v.vlen + 1] = '\0';
     return Decimal (temp);
   }
     Decimal Decimal :: multdigit (const int d2)
     {
       char temp [SZ];
       int j, carry = 0;
       for (j = 0; j < vlen; j++)
       {
           int d1 = vlstr [j] - '0';
           int digitprod = d1 * d2;
           digitprod += carry;
     if (digitprod >= 10)
     {
         carry = digitprod / 10;
         digitprod -= carry *10;
    }
    else
         carry = 0;
    temp [j] = digitprod + '0';
    }
    if (carry != 0)
        temp [j++] = carry + '0';
        temp [j] = '\0';
        return Decimal (temp);
    }
    void main (void)
    {
       unsigned long nuumb, j;
       Decimal fact = 1;
       cin >> numb;
       for (j = numb; j > 0; j--)
       fact = fact * j;
       fact putvl ();
       getch ();
    }

Последний раз редактировалось Xronikov; 16.06.2012 в 06:49.
Xronikov вне форума Ответить с цитированием
Старый 16.06.2012, 08:57   #2
p51x
Старожил
 
Регистрация: 15.02.2010
Сообщений: 15,821
По умолчанию

Код:
 void Decimal :: putvl() const;
 {
Точка с запятой здесь зачем?
p51x вне форума Ответить с цитированием
Старый 16.06.2012, 09:44   #3
Xronikov
Пользователь
 
Аватар для Xronikov
 
Регистрация: 10.06.2012
Сообщений: 17
По умолчанию

Испраил ту ошибку, но взамен новая вылезла.
[C++ Error] Unit1.cpp(86): E2108 Improper use of typedef 'Decimal'
[C++ Error] Unit1.cpp(86): E2379 Statement missing ;
[C++ Warning] Unit1.cpp(128): W8070 Function should return a value

Код:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#pragma hdrstop

#pragma argsused
const int SZ = 100;
class Decimal
{
 private:
         char vlstr [SZ];
         int vlen;
         Decimal multdigit (const int) const;
         Decimal mult10 (const Decimal) const;
 public:
         Decimal (): vlen (0)
         {
           vlstr [0] = '\0';
         }
         Decimal (const char S[SZ])
         {
          strcpy (vlstr, S);
          vlen = strlen (S);
          }
          Decimal (const unsigned long n)
          {
          ltoa (n,vlstr, 10);
          strrev (vlstr);
          vlen = strlen (vlstr);
         }
   void putvl () const;
   void getvl ();
   Decimal operator + (const Decimal);
   Decimal operator * (const Decimal);
 };
 void Decimal :: putvl () const
 {
     char temp [SZ];
     strcpy (temp,vlstr);
     cout << strrev (temp);
   }
   void Decimal :: getvl()
   {
      cin >> vlstr;
      vlen = strlen(vlstr);
      strrev (vlstr);
   }
   Decimal Decimal :: operator + (const Decimal V)
   {
      char temp [SZ];
      int j;
      int maxlen = (vlen> V.vlen)? vlen : V.vlen;
      int carry = 0;
      for(j = 0; j < maxlen; j++)
   {
      int d1 = (j > vlen -1)? 0 : vlstr [j] - '0';
      int d2 = (j > V.vlen -1)? 0 : V.vlstr [j]- '0';
      int digitsum = d1 + d2 + carry;
      if (digitsum >= 10)
           {digitsum -= 10; carry = 1;}
      else
        carry = 0;
      temp [j] = digitsum + '0';
   }
   if (carry == 1)
       temp [j++] = '1';
       temp [j] = '\0';
       return Decimal (temp);
   }
   Decimal Decimal :: operator * (const Decimal V)
   {
      Decimal pprod;
      Decimal tempsum;
      for (int j = 0; j < V.vlen; j++)
      {
        int digit = V.vlstr[j] - '0';
        pprod = multdigit (digit);
        for (int k = 0; k<j; k++)
         pprod = mult10 (pprod);
         tempsum = pprod;
      return tempsum;
   }
   Decimal Decimal:: mult10  (const Decimal V)
   {
  char temp [SZ];
     for (int j = v.vlen -1; j >= 0; j--)
          temp [j + 1] = v.vlen [j];
     temp [0] = '0';
     temp [v.vlen + 1] = '\0';
     return Decimal (temp);
   }
     Decimal Decimal :: multdigit (const int d2)
     {
       char temp [SZ];
       int j, carry = 0;
       for (j = 0; j < vlen; j++)
       {
           int d1 = vlstr [j] - '0';
           int digitprod = d1 * d2;
           digitprod += carry;
     if (digitprod >= 10)
     {
         carry = digitprod / 10;
         digitprod -= carry *10;
    }
    else
         carry = 0;
    temp [j] = digitprod + '0';
    }
    if (carry != 0)
        temp [j++] = carry + '0';
        temp [j] = '\0';
        return Decimal (temp);
    }
    void main (void)
    {
       unsigned long nuumb, j;
       Decimal fact = 1;
       cin >> numb;
       for (j = numb; j > 0; j--)
       fact = fact * j;
       fact putvl ();
       getch ();
       }
       }
Xronikov вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц

Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
прегрузка операций класс Матрица ArtGoN Общие вопросы C/C++ 3 15.11.2010 22:20