#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    2
    Rep Power
    0

    Serious Help Needed


    I have a big problem that hopefully someone can help me with. I wrote the first part of a program that reads in lines of 3 numbers from a data file "values.dat" and draws a rectangles to the screen based on the 3 numbers, the 1st number is the indentation of the rectangle, the 2nd number is the length and the 3rd number is the width.

    Now I have to extend that program and rework it using stacks which I have no understanding of. Can someone please help me. I've included what rules I have to obide by and also the coding from the first part of my program.

    1) Implement in C++ the STACK abstract data type, using
    the ARRAY implementation of the stack. That means, the Class
    and all the methods of the class, whether you need them or not.

    READ IN all the numbers from the file and push them on the stack. Then close the file. And don't reopen the file again in the program.

    No linked list in this program.
    No array anywhere in the program, except in the Stack implementation.

    (3) Now call the rectangle draw function. Call it with numbers
    you get by popping off numbers from the stack. Keep doing this
    until the stack is empty. All rectangles should be drawn. When you pop off the numbers remember that your indentation will be the first number in the stack and the last number out of the stack so the rectangles must be drawn accordingly.

    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <stdlib.h>

    void top(int &spacing, int &length, int &width);
    void bottom(int &spacing, int &length, int &width);
    void sides(int &spacing, int &length, int &width);
    void spa(int &spacing, int &length, int &width);

    int spacing, length, width, ch;
    int rectangle[3];

    void main()
    {
    ifstream in_file; in_file.open("values.dat"); if (in_file.fail()) {
    cout << "\nThe file could not open"
    << "\nPlease make sure the file exists."
    << endl;
    exit(1);
    }

    cout << "\nThe file has been successfully opened for reading." << endl;
    cout << endl;

    while((ch = in_file.peek()) != EOF) { in_file>>rectangle[1]>>rectangle[2]>>rectangle[3];
    spa (rectangle[1],rectangle[2],rectangle[3]);
    top (rectangle[1],rectangle[2],rectangle[3]);
    sides (rectangle[1],rectangle[2],rectangle[3]);
    bottom (rectangle[1],rectangle[2],rectangle[3]);
    cout<<endl;
    cout<<endl;
    }
    }

    void spa(int &spacing, int &length, int &width)
    {
    int spaceone; spaceone = spacing; while (spaceone > 0) { cout<<" ";
    spaceone = spaceone - 1; }
    return;
    }

    void top(int &spacing, int &length, int &width)
    {
    int recwidth; recwidth = width;
    while ( recwidth != 0) { cout<<"*";
    recwidth = recwidth - 1; }
    cout<<endl;
    return;
    }

    void sides(int &spacing, int &length, int &width)
    {
    int reclength; reclength = length - 2; while (reclength > 0) {
    spa (spacing, length, width); int clear;
    clear = width - 2;
    cout<<"*";

    while (clear > 0)
    {
    cout<<" ";
    clear = clear - 1;
    }
    cout<<"*"<<endl;
    reclength = reclength - 1;
    }
    }

    void bottom(int &spacing, int &length, int &width)
    {
    spa (spacing, length, width);
    int secwidth; secwidth = width;
    while ( secwidth != 0) {
    cout<<"*";
    secwidth = secwidth - 1; }
    return;
    }
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    there's plenty of good info on stacks on google, but the short end of it:
    -your class is going to have an array as a private member
    -you will need member functions like push and pop
    -push puts a value in the front of the array
    -pop removes a value from the front of the array
    --a stack is referrred to as LIFO( last in first out). this means that the last piece of data put into the stack is the first one taken out.
  4. #3
  5. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    Also, two things:
    1) Your subject title is unacceptable. You can refer to the sticky thread at the top of this forum for more information on how to post a question.

    2) This sounds a lot like a homework question. If so, then you should go to your intructor and class peers instead of this forum.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    2
    Rep Power
    0
    Sorry for the title of my thread, I just wanted to draw attention to my post so I could maybe get a helping hand. I'm not looking for anyone to give me the answer, I am just looking for some help on the subject, and the reason I posted the code is so people can get a better understanding of what I have done. You are right, it is a homework problem, unfortunately my teacher has been anything but helpful and the class is online so I have no contact with other students, only the teacher.

    So if anyone could tell me where to look for some good info i would appreciate it.
  8. #5
  9. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793

IMN logo majestic logo threadwatch logo seochat tools logo