Shiva.K
شنبه 15 خرداد 1389, 08:46 صبح
لطفآ اصلاحش کنید که اینقدر Error نده. مرسی:تشویق:
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <string.h>
void menu();
void convert binary to decimal();
int main()
{
menu();
getch();
return 0;
}
//******************************************
void menu()
{int c;
clrscr();
cout<<"1- Convert binary to decimal\n"
<<"2- Convert decimal to hexadecimal\n"
<<"3- Sum of 2 binary numbers\n"
<<"4- Check out to be binary\n"
<<"5- Exit!\n";
cout<<"\nPlease enter your choice's number:";
cin>>c;
switch(c)
{
case 1 :Convert binary to decimal(); break;
case 2 :Convert decimal to hexadecimal;
int b,c=0,a[16],i=0;
cout<<"Enter decimal number:";
cin>>b;
while (b>15)
{
a[i]=b%16;
b=b/16;
i++;
C++;
}
a[i]=b;
for (i=c;i>=0;--i)
{
if (a[i]==10)
cout<<"A";
else if (a[i]==11)
cout<<"B";
else if (a[i]==12)
cout<<"C";
else if (a[i]==13)
cout<<"D";
else if (a[i]==14)
cout<<"E";
else if (a[i]==15)
cout<<"F";
else
cout<<"%d",a[i];
} cout
cout<<"If you want to go back to the menu press enter.";
if(getch()=='\r')
menu();
} break;
case 3 :
int n,r;
bool flag= true;
cout<<"Enter your number:";
cin>>n;
while (n>0){
r=n%10;//for sum
n=n/10;
if (r!=0 && r!=1){
flag=false;
break;
}
}//for count
if(flag)
cout<<"The num is binary."<<endl;
else
cout<<"The num is not binary!"<<endl;
cprintf("If you want to go back to the menu press enter.");
if(getch()=='\r')
menu();
}
break;
case 4 :
int n1,n2,i=0,k=0,r1,r2,r3,s1=0,s2=0,sum;
cout<<"Enter binary number 1:";
cin>>n1;
do{
r1=n1%10;
s1+=r1*pow(2,i);
n1=n1/10;
i++;}while(n1>0);
cout<<"Enter binary number 2:";
cin>>n2;
do{
r2=n2%10;
s2+=r2*pow(2,k);
n2=n2/10;
k++;}while(n2>0);
sum=s1+s2;
int j=0,a[20];
while(sum>=1)//or while(sum>0)
{
r3=sum%2;
a[j]=r3;
sum/=2;
j++;
} cout<<"The sum of tow binary number is:";
for(j--;j>=sum;--j) //for(--j or j--;j>=0;j--)
cout<<a[j];
cprintf("If you want to go back to the menu press enter.");
if(getch()=='\r')
menu();
}
break;
case 5 :break;/*exit*/ }
//******************************************
void Convert binary to decimal()
{
int n;
cout<<"Enter binary number:";
cin>>n;
do
{
r=n%10;
s+=r*pow(2,i);
n=n/10;
i++;}while(n>0);
cout<<"Enter converted number is:"<<s<<endl;
cout<<"If you wanna go back to the menu press enter.";
if(getch()=='\r')
menu();
}
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <string.h>
void menu();
void convert binary to decimal();
int main()
{
menu();
getch();
return 0;
}
//******************************************
void menu()
{int c;
clrscr();
cout<<"1- Convert binary to decimal\n"
<<"2- Convert decimal to hexadecimal\n"
<<"3- Sum of 2 binary numbers\n"
<<"4- Check out to be binary\n"
<<"5- Exit!\n";
cout<<"\nPlease enter your choice's number:";
cin>>c;
switch(c)
{
case 1 :Convert binary to decimal(); break;
case 2 :Convert decimal to hexadecimal;
int b,c=0,a[16],i=0;
cout<<"Enter decimal number:";
cin>>b;
while (b>15)
{
a[i]=b%16;
b=b/16;
i++;
C++;
}
a[i]=b;
for (i=c;i>=0;--i)
{
if (a[i]==10)
cout<<"A";
else if (a[i]==11)
cout<<"B";
else if (a[i]==12)
cout<<"C";
else if (a[i]==13)
cout<<"D";
else if (a[i]==14)
cout<<"E";
else if (a[i]==15)
cout<<"F";
else
cout<<"%d",a[i];
} cout
cout<<"If you want to go back to the menu press enter.";
if(getch()=='\r')
menu();
} break;
case 3 :
int n,r;
bool flag= true;
cout<<"Enter your number:";
cin>>n;
while (n>0){
r=n%10;//for sum
n=n/10;
if (r!=0 && r!=1){
flag=false;
break;
}
}//for count
if(flag)
cout<<"The num is binary."<<endl;
else
cout<<"The num is not binary!"<<endl;
cprintf("If you want to go back to the menu press enter.");
if(getch()=='\r')
menu();
}
break;
case 4 :
int n1,n2,i=0,k=0,r1,r2,r3,s1=0,s2=0,sum;
cout<<"Enter binary number 1:";
cin>>n1;
do{
r1=n1%10;
s1+=r1*pow(2,i);
n1=n1/10;
i++;}while(n1>0);
cout<<"Enter binary number 2:";
cin>>n2;
do{
r2=n2%10;
s2+=r2*pow(2,k);
n2=n2/10;
k++;}while(n2>0);
sum=s1+s2;
int j=0,a[20];
while(sum>=1)//or while(sum>0)
{
r3=sum%2;
a[j]=r3;
sum/=2;
j++;
} cout<<"The sum of tow binary number is:";
for(j--;j>=sum;--j) //for(--j or j--;j>=0;j--)
cout<<a[j];
cprintf("If you want to go back to the menu press enter.");
if(getch()=='\r')
menu();
}
break;
case 5 :break;/*exit*/ }
//******************************************
void Convert binary to decimal()
{
int n;
cout<<"Enter binary number:";
cin>>n;
do
{
r=n%10;
s+=r*pow(2,i);
n=n/10;
i++;}while(n>0);
cout<<"Enter converted number is:"<<s<<endl;
cout<<"If you wanna go back to the menu press enter.";
if(getch()=='\r')
menu();
}