J
joesoap
Hey
ok sorry I posted that message twice...Im trying to attach my code...but
Im having trouble!!!
Anyway here we go again...
Im trying to Implement this BitString.h class.(This class must be able
to manipulate a bit-string! A bit string is basically a string with only
'1's and '0's in it!
There are 4 constructors ( they convert a int,char,char*, and a double
all to a bitstring!
ok Now here's my problem.
First of all Im running Linux so Im using a gnu compiler(g++).
I have a test program called assignment2.C...I compile this and it gives
me a segmentation fault!!
I was wondering if anybody might know why.
The code should be attached..so feel free to check it out!!
joesoap
#include "BitString.h"
BitString::BitString(char * number)
{
nBitstring = 0;
for(int i = 0; ; i++)
{
if( number == '\0')
{
break;
}
if((number != '1') && (number != '0'))
{
throw InvalidNumber();
}
nBitstring++;
}
bitstring = number;
}
BitString::BitString(int number)
{
int mod;
nBitstring = 0;
for (int i = 0; ; i++)
{
mod = number % 2;
number = number / 2;
if(mod == 0)
{
bitstring = '\0';
bitstring = '0';
nBitstring++;
}
if(mod == 1)
{
bitstring = '\0';
bitstring = '1';
nBitstring++;
}
if(number == 0)
{
bitstring = '\0';
bitstring = '1';
i++;
nBitstring++;
bitstring = '\0';
break;
}
}
int count = nBitstring - 2;
char temp[count];
for(int i = 0; ;i++)
{
temp = bitstring[count];
if(count == 0)
{
i++;
temp = '\0';
break;
}
count--;
}
bitstring = temp;
//cout<<bitstring<<endl;
}
BitString::BitString(double number)
{
int first;
int last;
double temp1;
int mod;
nBitstring = 0;
first = (int)number;
temp1 = number - first;
last = (int)(temp1 * 100);
for (int i = 0; ; i++)
{
mod = first % 2;
first = first / 2;
if(mod == 0)
{
bitstring = '\0';
bitstring = '0';
nBitstring++;
}
if(mod == 1)
{
bitstring = '\0';
bitstring = '1';
nBitstring++;
}
if(first == 0)
{
bitstring = '\0';
bitstring = '1';
i++;
nBitstring++;
bitstring = '\0';
break;
}
}
int count = nBitstring - 2;
char temp[count];
for(int i = 0; ;i++)
{
temp = bitstring[count];
if(count == 0)
{
i++;
temp = '\0';
break;
}
count--;
}
bitstring = temp;
bitstring[nBitstring-1] = '.';
nBitstring++;
//int dot = nBitstring;
bitstring[nBitstring-1] = '\0';
for (int i = nBitstring-1 ; ; i++)
{
mod = last % 2;
last = last / 2;
if(mod == 0)
{
bitstring = '\0';
bitstring = '0';
nBitstring++;
}
if(mod == 1)
{
bitstring = '\0';
bitstring = '1';
nBitstring++;
}
if(last == 0)
{
bitstring = '\0';
bitstring = '1';
i++;
nBitstring++;
bitstring = '\0';
break;
}
}
//cout<<bitstring<<endl;
/*
int c = nBitstring -(dot+1);
cout<<c<<endl;
char t[c];
for(int i = c; ;i++)
{
t = bitstring[c];
if(c == 0)
{
i++;
t = '\0';
break;
}
c--;
}
bitstring = t;
cout<<bitstring<<endl;*/
}
BitString::BitString(char num)
{
char * a;
a = #
int mod;
int number = 0;
number = atoi(a);
if(number > 9)
cout<<"Only characters from 0 to 9 allowed"<<endl;
nBitstring = 0;
for (int i = 0; ; i++)
{
mod = number % 2;
number = number / 2;
if(mod == 0)
{
bitstring = '\0';
bitstring = '0';
nBitstring++;
}
if(mod == 1)
{
bitstring = '\0';
bitstring = '1';
nBitstring++;
}
if(number == 0)
{
bitstring = '\0';
bitstring = '1';
i++;
nBitstring++;
bitstring = '\0';
break;
}
}
int count = nBitstring - 2;
char temp[count];
for(int i = 0; ;i++)
{
temp = bitstring[count];
if(count == 0)
{
i++;
temp = '\0';
break;
}
count--;
}
bitstring = temp;
//cout<<bitstring<<endl;
}
BitString:perator char()
{
}
BitString:perator int()
{}
BitString:perator char*()
{}
BitString:perator double()
{}
char* BitString:: get()
{
return bitstring;
}
int BitString::HammingDistance(BitString &number)
{throw InvalidNumber();}
istream &operator >>(istream & is , BitString &number)
{
return is;
}
ostream & operator << (ostream & os, BitString &number)
{
for(int i = 0; i < number.nBitstring ; i++)
{
os<<number.bitstring;
}
return os;
}
bool BitString:perator & (BitString & number)
{
for(int i = 0; i < number.nBitstring;i++)
{
if(bitstring != number.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator & (int number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator & (double number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator & (char number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator & (char * number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator | (BitString & number)
{
for(int i = 0; i < number.nBitstring;i++)
{
if(bitstring != number.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator | (int number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator | (double number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator | (char number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator | (char * number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
BitString BitString:perator ^ (BitString &number)
{ throw InvalidNumber();}
char &BitString:perator [] (int number)
{throw InvalidNumber();}
char BitString:perator [] (int number) const
{throw InvalidNumber();}
BitString BitString:perator + (int number)
{throw InvalidNumber();}
BitString BitString:perator + (double number)
{throw InvalidNumber();}
BitString BitString:perator + (char number)
{throw InvalidNumber();}
BitString BitString:perator + (char* number)
{throw InvalidNumber();}
BitString BitString:perator + (BitString &number)
{throw InvalidNumber();}
BitString BitString:perator += (int number)
{throw InvalidNumber();}
BitString BitString:perator += (double number)
{throw InvalidNumber();}
BitString BitString:perator += (char number)
{throw InvalidNumber();}
BitString BitString:perator += (char* number)
{throw InvalidNumber();}
BitString BitString:perator += (BitString &number)
{throw InvalidNumber();}
BitString BitString:perator - (int number)
{throw InvalidNumber();}
BitString BitString:perator - (double number)
{throw InvalidNumber();}
BitString BitString:perator - (char number)
{throw InvalidNumber();}
BitString BitString:perator - (char* number)
{throw InvalidNumber();}
BitString BitString:perator - (BitString &number)
{throw InvalidNumber();}
BitString BitString:perator -= (int number)
{throw InvalidNumber();}
BitString BitString:perator -= (double number)
{throw InvalidNumber();}
BitString BitString:perator -= (char number)
{throw InvalidNumber();}
BitString BitString:perator -= (char* number)
{throw InvalidNumber();}
BitString BitString:perator -= (BitString &number)
{throw InvalidNumber();}
BitString BitString:perator ~()
{throw InvalidNumber();}
BitString BitString:perator >> (BitString &number)
{throw InvalidNumber();}
BitString BitString:perator << (BitString &number)
{throw InvalidNumber();}
#ifndef BITSTRING_H
#define BITSTRING_H
#include <iostream>
#include <istream>
#include <cctype>
#include <cstdio>
#include <stdlib.h>
#include <string.h>
using namespace std;
class BitString
{
private:
char * bitstring;
int nBitstring;
public:
BitString(char *);
BitString(int);
BitString(double);
BitString(char );
class InvalidNumber{};
operator char();
operator int();
operator char*();
operator double();
char* get();
int HammingDistance(BitString &);
friend istream &operator >> (istream & , BitString &number);
friend ostream &operator << (ostream &,BitString &number);
bool operator & (BitString &);
bool operator & (int);
bool operator & (double);
bool operator & (char);
bool operator & (char *);
bool operator | (BitString &);
bool operator | (int);
bool operator | (double);
bool operator | (char);
bool operator | (char *);
BitString operator ^ (BitString &);
char &operator [] (int);
char operator [] (int) const;
BitString operator + (int);
BitString operator + (double);
BitString operator + (char);
BitString operator + (char *);
BitString operator + (BitString &);
BitString operator += (int);
BitString operator += (double);
BitString operator += (char);
BitString operator += (char *);
BitString operator += (BitString &);
BitString operator - (int);
BitString operator - (double);
BitString operator - (char);
BitString operator - (char *);
BitString operator - (BitString &);
BitString operator -= (int);
BitString operator -= (double);
BitString operator -= (char);
BitString operator -= (char *);
BitString operator -= (BitString &);
BitString operator ~();
BitString operator >> (BitString&);
BitString operator << (BitString&);
};
#endif
#include "BitString.h"
#include "BitString.C"
#include <iostream>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
char *t = "100110";
int n = 123;
//try{
//BitString a(t);
//BitString b(n);
//BitString c(123.45);
BitString d('3');
//BitString e(t);
char test;
test = char(d);
//cout<<test<<endl;
cout<<d<<endl;
//}
//catch(BitString::InvalidNumber)
//{
//cout <<"Invalid Binary number, use only the numbers 0 and 1"<<endl;
//}
}
ok sorry I posted that message twice...Im trying to attach my code...but
Im having trouble!!!
Anyway here we go again...
Im trying to Implement this BitString.h class.(This class must be able
to manipulate a bit-string! A bit string is basically a string with only
'1's and '0's in it!
There are 4 constructors ( they convert a int,char,char*, and a double
all to a bitstring!
ok Now here's my problem.
First of all Im running Linux so Im using a gnu compiler(g++).
I have a test program called assignment2.C...I compile this and it gives
me a segmentation fault!!
I was wondering if anybody might know why.
The code should be attached..so feel free to check it out!!
joesoap
#include "BitString.h"
BitString::BitString(char * number)
{
nBitstring = 0;
for(int i = 0; ; i++)
{
if( number == '\0')
{
break;
}
if((number != '1') && (number != '0'))
{
throw InvalidNumber();
}
nBitstring++;
}
bitstring = number;
}
BitString::BitString(int number)
{
int mod;
nBitstring = 0;
for (int i = 0; ; i++)
{
mod = number % 2;
number = number / 2;
if(mod == 0)
{
bitstring = '\0';
bitstring = '0';
nBitstring++;
}
if(mod == 1)
{
bitstring = '\0';
bitstring = '1';
nBitstring++;
}
if(number == 0)
{
bitstring = '\0';
bitstring = '1';
i++;
nBitstring++;
bitstring = '\0';
break;
}
}
int count = nBitstring - 2;
char temp[count];
for(int i = 0; ;i++)
{
temp = bitstring[count];
if(count == 0)
{
i++;
temp = '\0';
break;
}
count--;
}
bitstring = temp;
//cout<<bitstring<<endl;
}
BitString::BitString(double number)
{
int first;
int last;
double temp1;
int mod;
nBitstring = 0;
first = (int)number;
temp1 = number - first;
last = (int)(temp1 * 100);
for (int i = 0; ; i++)
{
mod = first % 2;
first = first / 2;
if(mod == 0)
{
bitstring = '\0';
bitstring = '0';
nBitstring++;
}
if(mod == 1)
{
bitstring = '\0';
bitstring = '1';
nBitstring++;
}
if(first == 0)
{
bitstring = '\0';
bitstring = '1';
i++;
nBitstring++;
bitstring = '\0';
break;
}
}
int count = nBitstring - 2;
char temp[count];
for(int i = 0; ;i++)
{
temp = bitstring[count];
if(count == 0)
{
i++;
temp = '\0';
break;
}
count--;
}
bitstring = temp;
bitstring[nBitstring-1] = '.';
nBitstring++;
//int dot = nBitstring;
bitstring[nBitstring-1] = '\0';
for (int i = nBitstring-1 ; ; i++)
{
mod = last % 2;
last = last / 2;
if(mod == 0)
{
bitstring = '\0';
bitstring = '0';
nBitstring++;
}
if(mod == 1)
{
bitstring = '\0';
bitstring = '1';
nBitstring++;
}
if(last == 0)
{
bitstring = '\0';
bitstring = '1';
i++;
nBitstring++;
bitstring = '\0';
break;
}
}
//cout<<bitstring<<endl;
/*
int c = nBitstring -(dot+1);
cout<<c<<endl;
char t[c];
for(int i = c; ;i++)
{
t = bitstring[c];
if(c == 0)
{
i++;
t = '\0';
break;
}
c--;
}
bitstring = t;
cout<<bitstring<<endl;*/
}
BitString::BitString(char num)
{
char * a;
a = #
int mod;
int number = 0;
number = atoi(a);
if(number > 9)
cout<<"Only characters from 0 to 9 allowed"<<endl;
nBitstring = 0;
for (int i = 0; ; i++)
{
mod = number % 2;
number = number / 2;
if(mod == 0)
{
bitstring = '\0';
bitstring = '0';
nBitstring++;
}
if(mod == 1)
{
bitstring = '\0';
bitstring = '1';
nBitstring++;
}
if(number == 0)
{
bitstring = '\0';
bitstring = '1';
i++;
nBitstring++;
bitstring = '\0';
break;
}
}
int count = nBitstring - 2;
char temp[count];
for(int i = 0; ;i++)
{
temp = bitstring[count];
if(count == 0)
{
i++;
temp = '\0';
break;
}
count--;
}
bitstring = temp;
//cout<<bitstring<<endl;
}
BitString:perator char()
{
}
BitString:perator int()
{}
BitString:perator char*()
{}
BitString:perator double()
{}
char* BitString:: get()
{
return bitstring;
}
int BitString::HammingDistance(BitString &number)
{throw InvalidNumber();}
istream &operator >>(istream & is , BitString &number)
{
return is;
}
ostream & operator << (ostream & os, BitString &number)
{
for(int i = 0; i < number.nBitstring ; i++)
{
os<<number.bitstring;
}
return os;
}
bool BitString:perator & (BitString & number)
{
for(int i = 0; i < number.nBitstring;i++)
{
if(bitstring != number.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator & (int number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator & (double number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator & (char number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator & (char * number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator | (BitString & number)
{
for(int i = 0; i < number.nBitstring;i++)
{
if(bitstring != number.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator | (int number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator | (double number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator | (char number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
bool BitString:perator | (char * number)
{
BitString a(number);
for(int i = 0; i <a.nBitstring;i++)
{
if(bitstring != a.bitstring)
{
return false;
}
else
{
return true;
}
}
}
BitString BitString:perator ^ (BitString &number)
{ throw InvalidNumber();}
char &BitString:perator [] (int number)
{throw InvalidNumber();}
char BitString:perator [] (int number) const
{throw InvalidNumber();}
BitString BitString:perator + (int number)
{throw InvalidNumber();}
BitString BitString:perator + (double number)
{throw InvalidNumber();}
BitString BitString:perator + (char number)
{throw InvalidNumber();}
BitString BitString:perator + (char* number)
{throw InvalidNumber();}
BitString BitString:perator + (BitString &number)
{throw InvalidNumber();}
BitString BitString:perator += (int number)
{throw InvalidNumber();}
BitString BitString:perator += (double number)
{throw InvalidNumber();}
BitString BitString:perator += (char number)
{throw InvalidNumber();}
BitString BitString:perator += (char* number)
{throw InvalidNumber();}
BitString BitString:perator += (BitString &number)
{throw InvalidNumber();}
BitString BitString:perator - (int number)
{throw InvalidNumber();}
BitString BitString:perator - (double number)
{throw InvalidNumber();}
BitString BitString:perator - (char number)
{throw InvalidNumber();}
BitString BitString:perator - (char* number)
{throw InvalidNumber();}
BitString BitString:perator - (BitString &number)
{throw InvalidNumber();}
BitString BitString:perator -= (int number)
{throw InvalidNumber();}
BitString BitString:perator -= (double number)
{throw InvalidNumber();}
BitString BitString:perator -= (char number)
{throw InvalidNumber();}
BitString BitString:perator -= (char* number)
{throw InvalidNumber();}
BitString BitString:perator -= (BitString &number)
{throw InvalidNumber();}
BitString BitString:perator ~()
{throw InvalidNumber();}
BitString BitString:perator >> (BitString &number)
{throw InvalidNumber();}
BitString BitString:perator << (BitString &number)
{throw InvalidNumber();}
#ifndef BITSTRING_H
#define BITSTRING_H
#include <iostream>
#include <istream>
#include <cctype>
#include <cstdio>
#include <stdlib.h>
#include <string.h>
using namespace std;
class BitString
{
private:
char * bitstring;
int nBitstring;
public:
BitString(char *);
BitString(int);
BitString(double);
BitString(char );
class InvalidNumber{};
operator char();
operator int();
operator char*();
operator double();
char* get();
int HammingDistance(BitString &);
friend istream &operator >> (istream & , BitString &number);
friend ostream &operator << (ostream &,BitString &number);
bool operator & (BitString &);
bool operator & (int);
bool operator & (double);
bool operator & (char);
bool operator & (char *);
bool operator | (BitString &);
bool operator | (int);
bool operator | (double);
bool operator | (char);
bool operator | (char *);
BitString operator ^ (BitString &);
char &operator [] (int);
char operator [] (int) const;
BitString operator + (int);
BitString operator + (double);
BitString operator + (char);
BitString operator + (char *);
BitString operator + (BitString &);
BitString operator += (int);
BitString operator += (double);
BitString operator += (char);
BitString operator += (char *);
BitString operator += (BitString &);
BitString operator - (int);
BitString operator - (double);
BitString operator - (char);
BitString operator - (char *);
BitString operator - (BitString &);
BitString operator -= (int);
BitString operator -= (double);
BitString operator -= (char);
BitString operator -= (char *);
BitString operator -= (BitString &);
BitString operator ~();
BitString operator >> (BitString&);
BitString operator << (BitString&);
};
#endif
#include "BitString.h"
#include "BitString.C"
#include <iostream>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
char *t = "100110";
int n = 123;
//try{
//BitString a(t);
//BitString b(n);
//BitString c(123.45);
BitString d('3');
//BitString e(t);
char test;
test = char(d);
//cout<<test<<endl;
cout<<d<<endl;
//}
//catch(BitString::InvalidNumber)
//{
//cout <<"Invalid Binary number, use only the numbers 0 and 1"<<endl;
//}
}