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

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

Вернуться   Форум программистов > C/C++ программирование > Общие вопросы C/C++
Регистрация

Восстановить пароль
Повторная активизация e-mail

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

Ответ
 
Опции темы Поиск в этой теме
Старый 12.05.2011, 09:30   #1
2008kedr2008
Пользователь
 
Регистрация: 21.11.2010
Сообщений: 14
По умолчанию умножение двух двоичных чисел!!!

Скажите где косяк??? в умножение перегрузил оператор, а как реализовать умножение двух двоичных чисел!!!????
Это Bin.cpp
Код:
Bin Bin:: operator*(Bin& b1){
int t=0;
int i, n1,n2,s;
char f[2];
if(b1.n.size()>n.size()){s=b1.n.size();}  
else{s=n.size();}
 b1.n.resize(s);
 n.resize(s);
for(b1.it=b1.n.begin(), it=n.begin(), i=0; i<s; i++){  
      *f=*b1.it; 
n1=atoi(f); 
 _itoa(n1, f, 10);
  
  *f=*it; 
  n2=atoi(f);
  _itoa(n2, f, 10);
 n1=n1*n2;  
t=0;  
if(n1>1){  
t=n1/2;            
n1=n1%2;
}
_itoa(n1, f, 2);
  *b1.it=*f;
 if(i<b1.n.size()){b1.it++;}  
 else{*b1.it='0';}  
 if(i<n.size()){it++;}
         else{*it='0';}  
}  
  if(t>0){
      _itoa(t, f, 2);
      b1.n.push_back(*f); 
    
}
    return b1;
это bin.h
Код:
#ifndef BIN
#define BIN

#include <list>
#include <iostream>

using namespace std;
class Bin{
      public:
             friend istream& operator>>(istream&, Bin&);
             friend ostream& operator<<(ostream&, const Bin&);
             Bin operator+(Bin&);
             Bin operator-(Bin&);
             Bin operator*(Bin&);
             Bin operator/(Bin&);
             Bin operator%(Bin&);
             int operator>(Bin);
             int operator<(Bin);
             int operator==(Bin);
             int operator!=(Bin);
             int check();
             Bin sign();
             private:
             list <unsigned char> n;
             list <unsigned char>::iterator it;
             long Transfer();
             friend void conclusion(Bin);
};
#endif
это main.cpp
Код:
#include <cstdlib>
#include <iostream>
#include "Bin.h"
using namespace std;

int main(int argc, char *argv[])
{
    string c;
    Bin b1, b2;
    cout<<"Enter the first number:"<<endl;
    cin>>b1;
     if(b1.check()){
    cout<<"Enter the command(+,-,*,/,%,>,>,!=,==)"<<endl;
    cin>>c;
     if(c==">" || c=="<" || c=="==" || c=="!=" || c=="+" || c=="*"|| c=="/"|| c=="-"|| c=="%"){
    cout<<"Enter the second number:"<<endl;
    cin>>b2;
     if(b2.check()){
   //Операция сложение
      if(c=="+"){ 
       b1=b1+b2;
        cout<<"Result of additions:"<<endl<<b1; } 
        // Операция вычитание
         if(c=="-"){
            if(b1>b2){ 
              b1=b1-b2;
              cout<<"Result:"<<endl<<b1;}
            else{cout<<"The inadmissible answer!"<<endl;}}
        //Операция  умножение
            if(c=="*"){
            b1=b1*b2;
            cout<<"Result of multiple:"<<endl<<b1;} 
        //Операция деление
          if(c=="/"){
            b1=b1/b2;
            cout<<"Result of division:"<<endl<<b1;} 
           // Операция остаток
            if(c=="%"){
            b1=b1%b2;
            cout<<"Result:"<<endl<<b1;
          }   
    // Операция больше
    if(c==">"){
    if(b1>b2){
    cout<<"The first number more than the second!"<<endl;
    }
    else{
    cout<<"The first number no more than the second!"<<endl;
    if(b1==b2){cout<<"The numbers are equal!"<<endl;}
    else{cout<<"The first number less than the second!"<<endl;}
         }
         }
    //Операция меньше
    if(c=="<"){
    if(b1>b2){
    cout<<"The first number less than the second!"<<endl;
    }
    else{
    cout<<"The first number no less than the second!"<<endl;
    if(b1==b2){cout<<"The numbers are equal!"<<endl;}
    else{cout<<"The first number more than the secind!"<<endl;}
         }
         }
    // Операция равно
    if(c=="=="){
    if(b1==b2){
    cout<<"The Numbers are equal!"<<endl;
    }
    else{
    cout<<"The numbers are not equal!"<<endl;
    if(b1>b2) {cout<<"The first number more than the second!"<<endl;}
    else{cout<<"The first number less than the second!"<<endl;}
               }
               }
    // Операция не равно
    if(c=="!="){
    if(b1!=b2){
    cout<<"The numbers are not equal!"<<endl;
    if(b1>b2){cout<<"The first number more than the second!"<<endl;}
    else{cout<<"The first number less than the second!"<<endl;}
    }
     else{cout<<"The numbers are equal!"<<endl;}
     }
     }
     else {cout<<"The second number is incorrect!"<<endl;}
     }
     
     else {cout<<" The incorect operations is entered!"<<endl;}
     }
     else {cout<<"The first number is incorrect!"<<endl;}
  
    system("PAUSE");
    return EXIT_SUCCESS;
}
2008kedr2008 вне форума Ответить с цитированием
Старый 12.05.2011, 13:28   #2
onewho
Форумчанин
 
Регистрация: 29.09.2010
Сообщений: 636
По умолчанию

Код не читал, там жесть какая-то ...
можете сделать так - перевести 2 числа из строки в dec , там перемножить - и обратно в bin
Пример:
Код:
	int i;

	char st[]="00000111";
	char *p;

	i=strtol(st,&p,2);

	std::cout << i << '\n';

	i*=3;

	char res[10];

	itoa(i,res,2);

	std::cout << res;
onewho вне форума Ответить с цитированием
Ответ


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

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

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


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Умножение двух длинных целых чисел, представленных двунаправленными связанными списками Rifler Паскаль, Turbo Pascal, PascalABC.NET 0 28.05.2010 19:31
Умножение двух чисел в p-ичной системе счисления - turbo pascal oxygen2007rus Помощь студентам 1 13.12.2009 16:00
сложение 10 двоичных чисел sssvetlaya Assembler - Ассемблер (FASM, MASM, WASM, NASM, GoASM, Gas, RosAsm, HLA) и не рекомендуем TASM 1 10.11.2009 09:32