A simple programme
Submitted By:
Sanjidur
Rating:





(
Rate It)
Share:
By Email
Visit
Description
Its only an example...
#include<iostream.h>
#include<cstdlib>
//using namespace std;
char a[60],c[60];
class Integer{
int i;
public:
Integer();
Integer(int i);
int setint(int i);
char *toString(int radix);
int isprime();
//char *binary(int b);
};
Integer::Integer(){i=0;}
Integer:: Integer(int i){
this->i = i;
}
int Integer:: setint(int i){
this->i=i;
return 0;
}
char* Integer:: toString(int radix)
{ int j=0,k;
switch(radix){
case 2:
while(i>0)
{
a[j]=(i%2)+48;
i=i/2;j++;
}
break;
case 8:
while(i>0)
{
a[j]=(i%8)+48;
i=i/8;j++;
}
break;
case 16:
while(i>0)
{
int m;
m=i%16;
if(m<=9)a[j]=m+48;
else if(m==10)a[j]='A';
else if(m==11)a[j]='B';
else if(m==12)a[j]='C';
else if(m==13)a[j]='D';
else if(m==14)a[j]='E';
else a[j]='F';
i=i/16;j++;
}
break;
default:
cout<<"Unrecognised number system"<<endl;
}
int l=j-1;
for( k=0;k<j;k++)
{c[k]=a[l];l--;}
return c;
}
int Integer::isprime()
{
return 0;
}
int main()
{
Integer x(2);
x.setint(13);
char *s;
s=(char*)malloc(sizeof(char)*60);
s=x.toString(16);
cout<<s<<endl;
return 0;
}
Comments (1)
Against most coding styles




Posted by: bilderbikkel on Sunday, May 16, 2010
This example shows how not to program. Examples of bad style are
- #include of iostream.h instead of iostream
- use of malloc in C++
- use of globals
- use of C-style casts
- the method IsPrime that always returns zero (instead of returning an int depending on the integer in Integer)
Add Your Rating