1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
void process(ifstream& infile, char&ch, int&dec, int&c, int&w);
int main()
{
int decimal=0;
char character;
int count=0;
int width;
ifstream infile;
infile.open("INLABVII.DAT");
cout<<"Binary Number Decimal Equivalent"<<endl;
cout<<" ";
infile.get(character);
while (infile)
{
process(infile, character, decimal, count, width);
}
return 0;
}
//precondition
//postcondition
void process(ifstream& infile, char&ch, int&dec, int&c, int&w)
{
int a=0;
///////////////////////////////////
if(ch=='1')
{
dec=2*dec+1;
cout<<ch;
c++;
}
///////////////////////////////////
else if(ch=='0')
{
dec=2*dec;
cout<<ch;
c++;
}
///////////////////////////////////
else if(ch==' ')
{
for (a=0; a<c; a++)
cout<<"\b";
if(c>0 && a>0)
{
cout<<"Bad digit on input"<<endl;
infile.ignore(100, '\n');
dec=0;
c=0;
cout<<" ";
}
}
////////////////////////////////////
else if(ch != '0' && ch != '1' && ch != '\n' && ch != ' ')
{
a=0;
for(a=0; a<c; a++)
{
cout<<"\b";
}
cout<<"Bad digit on input"<<endl;
infile.ignore(100, '\n');
dec=0;
c=0;
cout<<" ";
}
////////////////////////////////////
else if(ch=='\n')
{
w=25-c;
cout<<setw(w)<<dec<<endl<<" ";
c=0;
dec=0;
}
////////////////////////////////////
infile.get(ch);
}
|