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

A pretty print program takes a program, which may not be indented in any particu

ID: 644898 • Letter: A

Question

A pretty print program takes a program, which may not be indented in any particular way, and produces a copy with the same program indented so that bracket pairs ({ and }) line up with inner pairs indented more than outer pairs, so that if-else statements are indented as we have been doing, and so that other indenting is as we have been doing.

Write a program that reads a C++ program from one text file and produces a pretty print version of the program in a second text file. To make it easier, simply do this for the body of the program, ignoring the declarations, and assume thatt all substatements of complex statements (other than compound statements themselves) are compound statements (that is, are enclosed by brackets).

The data to be used is the following below:

#include <stdio.h>


main(){FILE   *fd,*fd2;
   int   i,j,index;
   char   filename[20];
   char   c, s[80];
   char   *ptr,*device = {"AMLSX"};
   int   garbagecnt,ok=1;


   printf("Enter message file: ");
   gets(filename);

   fd = fopen( filename, "r");
   if (fd < 0) {printf("error in opening file -> %s ",filename);
       exit(1);}

   printf("Enter real message file: ");
   gets(filename);

   fd2 = fopen( filename, "r");
   if (fd2 < 0) {printf("error in opening file -> %s ",filename);
       exit(1);}

   while (ok)
   {
      if ( fgets(s, 100, fd) != NULL)
      {   ok = 1;

       garbagecnt = rand() % 15;
       for ( j=1; j<garbagecnt; j++)
       {   c = (rand() % 96) + 32;
            if ( c != '%')
            putchar(c);}
       index = rand() % 5;
       printf("%c%c", '$',device[index]);
       ptr = s;
       printf("%02d", strlen(s));
       while ( *ptr && *ptr!=' ')
       {putchar( *ptr++);}

       if ( (garbagecnt % 2) == 0 )
          if (fgets(s, 100, fd2) != NULL){   ok = 1;
                ptr = s;
                printf("$Y");
                printf("%02d", strlen(s));

                while ( *ptr && *ptr!=' ')
                   putchar( *ptr++);}
           else   ok=0;

       garbagecnt = rand() % 8;
       for ( j=1; j<garbagecnt; j++){   c = (rand() % 96) + 32;
            if ( c != '%')
               putchar(c);} } else   ok=0;}putchar(' ');fclose(fd);}

Explanation / Answer

#include
#include
#include
#include

void main()
{
char ch[50],str[50],st[100],ar[100];
fstream f,f1;
cout<<"Enter filename to be formatted";
gets(ch);
cout<<"Enter filename for new file";
gets(str);
f.open(ch,ios::in);
f1.open("xyz.txt",ios::out);
f.seekg(0);
while(!f.eof())
{

f.getline(st,100);
f1< }
f.close();
f1.close();
f.open("xyz.txt",ios::in);
f1.open("chg.txt",ios::out);
f.seekg(0);
int j=0;
while(!f.eof())
{
f.getline(st,100);
for(int i=0;st[i]!='';i++)
{ ar[j++]=st[i];
if(st[i]=='#')
{
while(st[i]!='>')
{ar[j++]=st[i];
i++;
}
ar[j++]=' ';}
if(st[i]=='{')
{ar[j++]=' ';}
if(st[i]==';')
{ ar[j++]=' ';}
ar[j++]=st[i];
}

f1< }
f.close();

f1.close();
f.open("chg.txt",ios::in);
f1.open(str,ios::out);
f.seekg(0);
while(!f.eof())
{
f.getline(ar,100);
cout< f1< }
f1.close();
f.close();
getch();
}

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