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

Write a program to automatically add comments to code. Specifically, your progra

ID: 3811105 • Letter: W

Question

Write a program to automatically add comments to code. Specifically, your program should prompt the user for the names of an input C++ program file, and output program file. It should read in the input program file and write out a modified version as the output program file. This output program file must have added the required headers to the overall program. each function within the file. each class or structure within the file, and variables within the file. First, you should read in a default header template from a file. and place that header at the top of the output file. EG. you have a file called header.txt with the following text in it (replace all items with your personal data): To help the program to know where each of the required items is in the file, you must add a simple comment before the item so that your program can find and replace the simple comment with a full header template. For example, a comment of://function Will tell your program to put a function header at this spot. The function header must be read from another file, e.g. something like function.txt which might contain a header such as:

Explanation / Answer

program.cpp -------

#include<iostream>
#include<cstdlib>
#include<cstdio>

using namespace std;

void copy(char *, string);
void addComment(char *, char *);

int main()
{
   string input, output;
   cout<<" Name of input file : ";
   cin>>input;
   cout<<" Name of output file : ";
   cin>>output;
   int ilength = input.length(), olength = output.length();
   char *irr = (char *)malloc((ilength + 1)*sizeof(char)), *orr = (char *)malloc((olength + 1) * sizeof(char));
   if(irr == NULL || orr == NULL)
   {
       cout<<" Dynamic memory allocation failed ";
       return -1;
   }
   copy(irr, input), copy(orr, output);
   //cout<<input[0]<<endl;
   //cout<<" "<<irr<<" "<<orr<<endl;
   addComment(irr, orr);
   return 0;
}

void addComment(char *input, char *output)
{
   char header[] = "header.txt", function[] = "function.txt";
   FILE *ip = NULL, *op = NULL, *head = NULL, *func = NULL;
   head = fopen(header, "r"), func = fopen(function, "r"),
   ip = fopen(input, "r"), op = fopen(output, "a");
   if(head == NULL || func == NULL || ip == NULL || op == NULL)
   {
       cout<<" Unable to open file ";
       exit(-2);
   }
   while(feof(head) == 0)
   {
       fputc(fgetc(head), op);
   }
   fputc((int)' ', op), fputc((int)' ', op);
   char t1[] = "// function ", t2[12];
   int c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11,c12;
   fclose(head);
   int i = 0;
   while(feof(ip) == 0)
   {
       c1 = fgetc(ip);
       if(c1 != '/')
       {
           fputc(c1, op);
       }
       else
       {
           c2 = fgetc(ip);
           if(c2 != '/')
           {
               fputc(c1, op), fputc(c2, op);
           }
           else
           {
               c3 = fgetc(ip);
               if(c3 != ' ')
                   fputc(c1, op), fputc(c2, op), fputc(c3, op);
               else
               {
                   c4 = fgetc(ip);
                   if(c4 != 'f')
                       fputc(c1, op), fputc(c2, op), fputc(c3, op), fputc(c4, op);
                   else
                   {
                       c5 = fgetc(ip);
                       if(c5 != 'u')
                           fputc(c1, op), fputc(c2, op), fputc(c3, op), fputc(c4, op), fputc(c5, op);
                       else
                       {
                           c6 = fgetc(ip);
                           if(c6 != 'n')
                               fputc(c1, op), fputc(c2, op), fputc(c3, op), fputc(c4, op), fputc(c5, op), fputc(c6, op);
                           else
                           {
                               c7 = fgetc(ip);
                               if(c7 != 'c')
                                   fputc(c1, op), fputc(c2, op), fputc(c3, op), fputc(c4, op), fputc(c5, op), fputc(c6, op), fputc(c7, op);
                               else
                               {
                                   c8 = fgetc(ip);
                                   if(c8 != 't')
                                       fputc(c1, op), fputc(c2, op), fputc(c3, op), fputc(c4, op), fputc(c5, op), fputc(c6, op), fputc(c7, op), fputc(c8, op);
                                   else
                                   {
                                       c9 = fgetc(ip);
                                       if(c9 != 'i')
                                           fputc(c1, op), fputc(c9, op),fputc(c2, op), fputc(c3, op), fputc(c4, op), fputc(c5, op), fputc(c6, op), fputc(c7, op), fputc(c8, op);
                                       else
                                       {
                                           c10 = fgetc(ip);
                                           if(c10 != 'o')
                                               fputc(c1, op),fputc(c9, op),fputc(c10, op), fputc(c2, op), fputc(c3, op), fputc(c4, op), fputc(c5, op), fputc(c6, op), fputc(c7, op), fputc(c8, op);
                                           else
                                           {
                                               c11 = fgetc(ip);
                                               if(c11 != 'n')
                                                   fputc(c9, op), fputc(c10, op),fputc(c11, op),fputc(c1, op), fputc(c2, op), fputc(c3, op), fputc(c4, op), fputc(c5, op), fputc(c6, op), fputc(c7, op), fputc(c8, op);
                                               else
                                               {
                                                   c12 = fgetc(ip);
                                                   if(c12 != ' ')
                                                       fputc(c12, op),fputc(c9, op), fputc(c10, op),fputc(c11, op),fputc(c1, op), fputc(c2, op), fputc(c3, op), fputc(c4, op), fputc(c5, op), fputc(c6, op), fputc(c7, op), fputc(c8, op);
                                                   else{
                                                       while(feof(func) == 0)
                                                       {
                                                           fputc(fgetc(func),op);
                                                       }
                                                       rewind(func);
                                                       fputc((int)' ',op), fputc((int)' ',op);
                                                   }
                                               }
                                           }
                                       }
                                   }
                               }
                           }
                       }
                   }
               }
           }
       }
      
   }
   fclose(ip), fclose(op), fclose(func);
}

void copy(char *arr, string s)
{
   int i;
   for(i = 0; i < s.length(); i++)
   {
       arr[i] = s[i];
   }
   arr[i] = '';
}

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