Написать классы для готовых программ - C (СИ)

Узнай цену своей работы

Формулировка задачи:

Приветствую уважаемые посетители и, прошу помогите переделать задачу с использованием классов (7 часов уже решаю задачи на си, мозг уже не думает((() Есть задача (код ниже), нада её переделать и применить классы. (Вот такое:
class Circle (
public:
Circle () ;
void SetRadius(void) ;
void GetRadius(void) ;
~Circle () ;
private:
void CalculateArea(void);
int radius;
int color;
};
) Вот мой код:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <string.h>
#include <iostream.h>
/*
str1= 123,43 ==> с1=123 dc=43 ==> 12300+43 ==>12343 ==> 12343+3512=15855 
str2=35,12 ==> c2=35 dc2=12 ==> 3500+12 ==> 3512    ==>
==>   43 =  2 дроб числа
==>   12 =  2 дроб числа
==> 15855 /10/10 = 158 *10 *10 ==> 15855-15800=55  15800/10/10,55 ==> Ответ 158,55
*/
 
char str1[12],str2[12];//вводимое 1 число и 2 строкой
    int c1,c2,d1,d2,otv,otv1,otv2,ch1,ch2;//числа разбиваються на c1 d1 и c2 d2 соответственно
    //otv - часть ответа целого, otv1 - часть ответа дробной части
    int count1=0,count2=0,count;//счетчики целых и дробных частей для разбивки
    char chois;
        int dc,dc2;//вспомогательные переменные
        int cc,cc2;
        int ch1,ch2;
        int i;
    void ENTER()
    {
            printf("BBEDUTE 1 4UCLO CLEDYUSHUM OBPA3OM:\n");
            printf("CELAYA 4ACTb,DPOBHAA 4ACTb.\n");
            gets(str1);
        printf("BBEDUTE 2 4UCLO TAKUM JE OBPA3OM.\n");
        gets(str2);
        printf("1e 4UCLO - %s\n",str1);
        printf("2e 4UCLO - %s\n",str2);
        printf("BUBEPUTE DEUCTBUE\n");
        printf("+ - *\n");
        scanf("%c",&chois);
    }
int PABOTA()
{
    int i=0,j=0,si=0,sj=0;
    char odun[12];//вспомог строка для дроби 1 числа
    char dva[12];//вспомог строка для дроби 2 числа
c1=atoi(&str1);//берем целую часть 1 числа
c2=atoi(&str2);//берем целую часть 2 числа
while(str1[i]!=',')//отсчитываем целую часть числа
{
    i++;
}
i++;
while(str1[i]!='\0')//отсчитываем дробную часть числа
{
    odun[j]=str1[i];
    i++;
    j++;
}
d1=atoi(&odun);//берем дробную часть 1 числа
while(str2[si]!=',')//отсчитываем целую часть числа
{
    si++;
}
si++;
while(str2[si]!='\0')//отсчитываем целую часть числа
{   
    dva[sj]=str2[si];
    si++;
    sj++;
}
d2=atoi(&dva);//берем дробную часть 2 числа
return 1;
 }
int PLUS()
{
dc=d1;
dc2=d2;
ch1=c1;
ch2=c2;
while(dc!=0)//по принципу 100.51==> 10051 ==> 100.51
{
dc=dc/10;
count1++;
}
while(dc2!=0)
{
dc2=dc2/10;
count2++;
}
if(count1>count2)
{
count=count1;
}
else
{
count=count2;
}
dc=d1;
dc2=d2;
////////////////////////////////////////
for(i=0;i<(count+count);i++)
{
ch1*=10;
ch2*=10;
}
for(i=0;i<count;i++)
{
dc*=10;
dc2*=10;
}
cc=dc+ch1;
cc2=dc2+ch2;
otv=cc+cc2;
otv1=otv;
otv2=otv1;
for(i=0;i<count;i++)
{
    otv/=10;
    otv1/=10;
    otv2/=10;
}
for(i=0;i<count;i++)
{
otv2/=10;
}
for(i=0;i<count;i++)
{
otv2*=10;
}
otv1=otv1-otv2;
for(i=0;i<count;i++)
{
    otv=otv/10;
}
return 1;
}
int MINUS()
{
dc=d1;
dc2=d2;
ch1=c1;
ch2=c2;
while(dc!=0)
{
dc=dc/10;
count1++;
}
while(dc2!=0)
{
dc2=dc2/10;
count2++;
}
if(count1>count2)
{
count=count1;
}
else
{
count=count2;
}
dc=d1;
dc2=d2;
////////////////////////////////////////
for(i=0;i<(count+count);i++)
{
ch1*=10;
ch2*=10;
}
for(i=0;i<count;i++)
{
dc*=10;
dc2*=10;
}
cc=dc+ch1;
cc2=dc2+ch2;
otv=cc-cc2;
otv1=otv;
otv2=otv1;
for(i=0;i<count;i++)
{
    otv/=10;
    otv1/=10;
    otv2/=10;
}
for(i=0;i<count;i++)
{
otv2/=10;
}
for(i=0;i<count;i++)
{
otv2*=10;
}
otv1=otv1-otv2;
for(i=0;i<count;i++)
{
    otv=otv/10;
}
return 1;
}
 
int PRO()
{
dc=d1;
dc2=d2;
ch1=c1;
ch2=c2;
while(dc!=0)
{
dc=dc/10;
count1++;
}
while(dc2!=0)
{
dc2=dc2/10;
count2++;
}
if(count1>count2)
{
count=count1;
}
else
{
count=count2;
}
dc=d1;
dc2=d2;
////////////////////////////////////////
for(i=0;i<(count1+count1);i++)
{
ch1*=10;
}
for(i=0;i<(count2+count2);i++)
{
ch2*=10;
}
for(i=0;i<count1;i++)
{
dc*=10;
}
for(i=0;i<count2;i++)
{
dc2*=10;
}
cc=dc+ch1;
cc2=dc2+ch2;
otv=cc*cc2;
otv1=otv;
otv2=otv1;
for(i=0;i<(count2+count1);i++)
{
    otv=otv/10;
    otv1=otv1/10;
    otv2=otv2/10;
}
for(i=0;i<(count2+count1);i++)
{
    otv2/=10;
}
for(i=0;i<(count2+count1);i++)
{
    otv2*=10;
}
otv1=otv1-otv2;
for(i=0;i<(count2+count1);i++)
{
    otv=otv/10;
}
return 1;
}
  void BIBOD()
{
printf("%d,%d%c%d,%d=%d,%d\n",c1,d1,chois,c2,d2,otv,otv1);
}
main()
{   
ENTER();
PABOTA();
if(chois=='+')
PLUS();
if(chois=='-')
MINUS();
if(chois=='*')
PRO();
else
{
printf("BU HE BUbPALU DEUCTBUE!\n\a");
exit(0);
}
BIBOD();
}
По возможности с пояснениями, буду оч. благодарен.

Решение задачи: «Написать классы для готовых программ»

textual
Листинг программы
#include <iostream>
#include <sstream>
#include <string>
 
class Number {
 public:
  Number() : value_("") {}
  explicit Number(std::string value) : value_(value) {}
  std::string Value() const {
    return value_;
  }
  void Value(const std::string &value) {
    value_ = value;
  }
  // оператор сложения
  Number operator+(const Number &other) {
    std::string result;
    // тут надо сложить две строки
    // value_ и other.Value() как числа с плавающей
    // запятой в переменную result
    // заглушка преобразовыванием в флоат и суммированием
    std::istringstream string_a(value_), string_b(other.Value());
    float a, b;
    string_a >> a;
    string_b >> b;
    std::ostringstream string_result;
    string_result << (a + b);
    result = string_result.str();
 
    return Number(result);
  }
  // по аналогии для остальных операторов
 private:
  std::string value_;
};
 
int main(int argc, char *argv[]) {
  Number a("1.12345"), b("2.221");
  // пример использования оператора сложения
  Number c = a + b;
  std::cout << c.Value() << std::endl;
  return 0;
}

Объяснение кода листинга программы

  1. Создание класса Number.
  2. Создание конструкторов для класса Number.
  3. Реализация метода Value() для класса Number.
  4. Реализация оператора сложения для класса Number.
  5. Реализация метода main() для программы.
  6. Создание объектов класса Number для использования в программе.
  7. Пример использования оператора сложения для объектов класса Number.
  8. Вывод результата на экран.
  9. Завершение программы.

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.375 из 5
Похожие ответы