Thread: Pretty Print Program

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    Question Pretty Print Program HELP!!!

    I am working on a program that has me stumped(pointless college programs). This program must place line numbers at the front of each line of an existing program that uses imperative data types only in this format (0001,0002,0003,--->hypothetically 9999). Then show level numbers( everytime a "{" is reached one level) and then export all of this to a table showing all identifiers and every line they occur on. I have prototyped most of the basic ideas of the program but I still have know idea how to proceed from there. I know that I will need three classes One for the line numbers one to get the input from the program being read in and one for the table. I am using a array of linked lists for the table and I am trying to use tokens for the reading and checking of the input class. Right now I have changed the line number into a char string and incremented the last then reset the last and incremented the second and so on. It is still very rough and any help would be greatlly appreciated.
    Last edited by 2TheGrindStone; 11-15-2001 at 08:23 AM.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    4
    Not looking for a handout just some help please.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > existing program that uses imperative data types only in this format
    A 'before and after' example of this would help us to see what you're getting at.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    4
    This is the sample program
    #include <iostream.h>
    #include <iomanip.h>

    const char EOL = '\n';

    void mean( const float answer[], int arraySize )
    {
    int total = 0;

    cout << "********" << EOL << " Mean" << EOL
    << "********" << EOL;

    for ( int j = 0; j < arraySize; j++ )
    total += answer[ j ];

    cout << "The mean is the average value of the data"
    << EOL
    << "items. The mean is equal to the total of"
    << EOL
    << "all the data items divided by the number"
    << EOL
    << "of data items (" << arraySize
    << "). The mean value for\nthis run is: "
    << total << " / " << arraySize << " = "
    << setiosflags( ios::fixed | ios::showpoint )
    << setprecision( 4 )
    << ( float ) total / arraySize
    << EOL << EOL;
    }

    void median( int answer[], int size )
    {
    cout << EOL << "********" << EOL << " Median"
    << EOL << "********" << EOL
    << "The unsorted array of responses is";

    printArray( answer, size );
    bubbleSort( answer, size );
    cout << EOL << EOL << "The sorted array is";
    printArray( answer, size );
    cout << EOL << EOL << "The median is element " << size / 2
    << " of" << EOL << "the sorted " << size
    << " element array." << EOL
    << "For this run the median is "
    << answer[ size / 2 ] << EOL << EOL;
    }

    void mode( int freq[], int answer[], int size )
    {
    int rating;
    int largest = 0;
    int modeValue = 0;

    cout << EOL << "********" << EOL << " Mode"
    << EOL << "********" << EOL;

    for ( rating = 1; rating <= 9; rating++ )
    freq[ rating ] = 0;

    for ( int j = 0; j < size; j++ )
    ++freq[ answer[ j ] ];

    cout << "Response"<< setw( 11 ) << "Frequency"
    << setw( 19 ) << "Histogram"
    << EOL << EOL << setw( 55 )
    << "1 1 2 2" << EOL << setw( 56 )
    << "5 0 5 0 5" << EOL << EOL;

    for ( rating = 1; rating <= 9; rating++ )
    {
    cout << setw( 8 ) << rating << setw( 11 )
    << freq[ rating ] << " ";

    if ( freq[ rating ] > largest ) {
    largest = freq[ rating ];
    modeValue = rating;
    }

    for ( int h = 1; h <= freq[ rating ]; h++ )
    cout << '*';

    cout << EOL;
    }

    cout << "The mode is the most frequent value." >> EOL
    << "For this run the mode is " << modeValue
    << " which occurred " << largest
    << " times." << endl;
    }

    void bubbleSort( int a[], int size )
    {
    int hold;

    for ( int pass = 1; pass < size; pass++ )

    for ( int j = 0; j < size - 1; j++ )

    if ( a[ j ] > a[ j + 1 ] )
    {
    hold = a[ j ];
    a[ j ] = a[ j + 1 ];
    a[ j + 1 ] = hold;
    }
    }

    void printArray( const int a[], int size )
    {
    for ( int j = 0; j < size; j++ )
    {

    if ( j % 20 == 0 )
    cout << endl;

    cout << setw( 2 ) << a[ j ];
    }
    }


    int main()
    {
    const int responseSize = 99;
    int frequency[ 10 ] = {
    0
    };

    float response[ responseSize ] =
    {
    6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
    7, 8, 9, 5, 9, 8, 7, 8, 7, 8,
    6, 7, 8, 9, 3, 9, 8, 7, 8, 7,
    7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
    6, 7, 8, 7, 8, 7, 9, 8, 9, 2,
    7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
    5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
    7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
    7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
    4, 5, 6, 1, 6, 5, 7, 8, 7
    };

    mean( response, responseSize );
    median( response, responseSize );
    mode( frequency, response, responseSize );

    return 0;
    }
    This is what I need to get to

    The formatted version of the program should contain four-digit line numbers, so that all lines line-up, e.g.,
    0001 #include <iostream.h>
    0002 #include "LineBuffer.h"
    0003
    0004 void main()
    0005 { level 1
    0006 <statement>;
    0007 while (<condition>)
    { level 2

    } level 2
    0008 } level 1
    0009 <statement>;
    0010 etc.
    any help is appreciated

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    4
    The main issue that I need help with is a get token function to read in tokens to be checked for type ie int char float

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    Probably best to think of the input file as a text file rather than a file with different variable types. Rather than trying to use tokens I would merely read the file a single char at a time. A simple program diagram would be to:

    declare an int variable to hold the line number starting at 1
    declare an int variable to hold the level number starting at 0
    declare an ifstream to read the input file
    declare an ofstream to write to the output file

    use setwidth and fill left ostream manipulators to allow you to output the line variable as 0001 as opposed to 1 and send first line number to output file before starting to read the file.

    until EOF found read each char from input file one char at a time
    if input char is new line char increment line number by one and send new line number to output file using above style
    if input char is { output the char, increment the level number by one and output the word "level", a space and the level number
    if input char is } output the char, the word "level", a space and the level number, then decrease the level number by one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is it that my program does not print for case 1?
    By Jasper in forum C Programming
    Replies: 10
    Last Post: 07-02-2009, 12:57 PM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  3. Program to print last non zero digit of n!
    By Tanuj_Tanmay in forum C Programming
    Replies: 14
    Last Post: 04-22-2009, 10:13 AM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  5. why does my program print off the wrong answers?
    By kl3pt0 in forum C Programming
    Replies: 18
    Last Post: 06-10-2004, 06:52 PM