Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I want it by c++ language ,please !! Please note that you are not allowed to use

ID: 3590475 • Letter: I

Question

I want it by c++ language ,please !!

Please note that you are not allowed to use any C/C++ binary/bit operators.Please note that you are not allowed to use any C/C++ binary/bit operators.

1. Write a command line C/C++ program that wl convert binary numbers to decimal and hexadecimal formats. The program should read the input from an input file and display the result in another output file. The first line of the input file contains a number N that represents the number of binary numbers to be converted. The file then contains N lines where each line is a binary number (which may contain a radix point). The output file contains N lines where each line starts with the binary number followed by the decimal equivalent followed by the hexadecimal equivalent. See the below examples for input and output. Please note that you are not allowed to use any C/C++ binary/bit operators. Sample input 4 10110011 101.0101 10001.1 Sample output 10110011 01.0101 10001.1 179 5.3125 5.5 B3 15

Explanation / Answer

Please save this file in convert.cpp, compile and run. Make 1 input.txt file and keep in the same directory. You can modify your code as per your requirement. Put comment in comment section if you have any query.

/*************************/ input.txt

4
101.0101
10001.1
1111
10110011

/*************************/

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <string.h>
#include <math.h>

using namespace std;
char* convert(char* c);

int main(int argc,char* argv[])
{
   ifstream ip("input.txt");
   ofstream opt("output.txt");
   int N;

   ip>>N;
   char arr[N][20];
   for(int i=0;i<N;i++)
   {
       char s[20];
       ip>>s;
//       cout<<":"<<s;
       strcpy(arr[i],s);
   }
   for(int i=0;i<N;i++)
       cout<<arr[i]<<endl;
   for(int i=0;i<N;i++)
   {
       char op[20];
       strcpy(op,convert(arr[i]));
       opt<<op;
   }
   return 0;
}

char* convert(char* c)
{
   char intg[10],frac[10];
   char* ptr=strtok(c,".");
   int count=0;
   while(ptr)
   {
       if(count==0)
           strcpy(intg,ptr);
       else if(count==1)
           strcpy(frac,ptr);
       ptr=strtok(NULL,".");
       count++;
   }

   //calculating integral part of decimal
   int len=strlen(intg)-1;
   int sum=0;
   for(int i=0;i<=len;i++)
   {
       sum=sum+(intg[len-i]-48)*pow(2,i);
   }
//   cout<<"int:"<<sum<<endl;

   //calculating fractional part of decimal
   len=strlen(frac)-1;
   float sumf=0.0;
   for(int i=0;i<=len;i++)
   {
       sumf=sumf+(frac[i]-48)*(pow(2,-(i+1)));
   }
//   cout<<"frac:"<<sumf<<endl;
   double merge=0.0;
   merge=(double)sum+sumf;
   char out[20]; //you can increase the buffer size
   sprintf(out,"%f ",merge); //for writing into hex value try to convert from sum & sumf and return
   cout<<"merge"<<merge<<endl;
   return out;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote