简体   繁体   English

在C ++ ..homework中排队

[英]queue in c++ ..homework

I have a homework. 我有作业 I try to solve it but I need some help.. 我试图解决它,但我需要一些帮助。

  1. Use Queue ADT and don't change any of the ADT's functions or procedures. 使用队列ADT,并且不要更改ADT的任何功能或过程。 You have to create a structure car that represent the car that will be in the station waiting queue in order to wash car body. 您必须创建一个代表要在车站等待队列中的汽车的结构汽车,以便清洗车身。 Every car has its ID which looks like 124ate [6 character long] and name like eg Superpan and production year as integer variable. 每辆汽车都有自己的ID,看起来像124ate [6个字符长],名称,例如Superpan和生产年份,都是整数变量。 In your program you shouldn't call Queue ADT explicitly, instead use global functions to call them (implicitly). 在程序中,您不应显式调用Queue ADT,而应使用全局函数(隐式)调用它们。
  2. Your main program should only have declarations and calls to global functions, no structures should be used. 您的主程序应仅具有声明和对全局函数的调用,不应使用任何结构。
  3. Your output should be firstly looks like the following: 首先,您的输出应如下所示:
    1. Park new car 停放新车
    2. Print the number of cars 打印汽车数量
    3. move earliest coming car 最早动车
    4. Print All cars 打印所有汽车
    5. exit 出口

My answer: 我的答案:

#include <iostream>
#include "queue.h"
#include <string>

using namespace std ;

struct car {
    char ID[6];
    int year;
    string name;
}

int main ()
{
    int num;
    car car1;
    Queue <car> q1;

    do {
        cout << "mniu \n"
          << "1. Park new car \n"
          << "2. Print the number of cars \n"
          << "3. move earliest coming car \n"
          << "4. Print All cars \n"
          << "5. exit \n";

        cin >> num;

        switch (num)
        {
        case 1:
            int n;
            cout << "enter id:\n";
            cin >> car1.ID;
            cout << "enter name:\n";
            cin >> car1.name;
            cout << "enter year:\n";
            cin >> car1.year;

            q1.enqueue (car1);
            break;

        case 2:
            cout << "number of cars: " << q1.queueCount();
            break;

        case 3:
            q1.dequeue(car1);
            break;

        case 4:
            while (q1.queueCount()!=0)  
                cout << q1.front();

        case 5:
            cout << "Thank you /n End program";
            break;

        default:
            cout << "failed number. chose from 1 to 5 \n";
        }
    }

    while (num==5);

    return 0;

}

My question: 我的问题:

  1. How can I print the queue node by node without deleting it? 如何逐个打印队列节点而不删除它?
  2. "Your main program should only have declarations and calls to global functions, no structures should be used." “您的主程序只应具有对全局函数的声明和调用,不应使用任何结构。” Does it mean my code is wrong because I write everything in main.. I don't understand it. 这是否表示我的代码是错误的,因为我在main中编写了所有内容。我不理解。
  1. it's a Queue, so you can save the first node in it, and print each element, delete and push it back to the queue, until you see (for the 2nd time!) the 1st element. 它是一个队列,因此您可以在其中保存第一个节点,并打印每个元素,删除并将其推回队列,直到(第2次!)看到第一个元素。
  2. yes, you shouldn't write all the program in main, but use functions and other files, organized by some logical meaning. 是的,您不应该在main中编写所有程序,而应使用按逻辑意义组织的函数和其他文件。

EDIT: 编辑:

1. 1。

car first = q1.dequeue();
q1.enqueue(first);
  //ADD HERE: print first...
while (q1.peek() != first) {
  car element = q1.dequeue();
  q1.enqueue(first);
  //ADD HERE: print element...
}


add your printing where the comments "ADD HERE:..." are 在注释“ ADD HERE:...”所在的位置添加打印

2. you should add functions like getUserInput() and printQueue() to handle all this scenerios, the main will just call these functions 2.您应该添加诸如getUserInput()和printQueue()之类的函数来处理所有这些场景,主体将只调用这些函数

"Your main program should only have declarations and calls to global functions," “您的主程序应该仅具有对全局函数的声明和调用,”

I believe your instructor is encouraging you to divide your code into individual subroutines, each of which does only one thing. 我相信您的老师会鼓励您将代码分成单独的子例程,每个子例程仅做一件事。 In that style, a typical main might be: 以这种风格,典型的main可能是:

int main() {
    MyDataStruct x;
    ReadInput(x);
    ProcessData(x);
    WriteOutput(x);
}

Notice that there are no for loops in main , no add-up-the-sales-and-copmute-the-tax expressions, nothing in main that requires any processing at all. 请注意, main中没有for循环,没有合计销售和税收的表达式,main中根本不需要任何处理。 All of the good stuff is either in your global functions or the methods of your data structures. 所有的好东西都在您的全局函数或数据结构的方法中。

Applying that to your program, your main might look like: 将其应用于您的程序,您的主程序可能看起来像:

int globalExitFlag;
int main() {
    Queue<car> q;
    Car c;
    while( !globalExitFlag ) {
       int cmd;
       cmd = PrintMenuAndAcceptOneAnswer(q, c);
       DoOneCommand(q, c, cmd);
    }
}

Then you can put the guts of your cin reading in PrintMenuAndAcceptOneAnswer , while the printing and queue manipulation lives in DoOneCommand . 然后,您可以将cin的胆量放在PrintMenuAndAcceptOneAnswer ,而打印和队列处理则位于DoOneCommand Of course, you have to write PrintMenuAndAcceptOneAnswer and DoOneCommand yourself. 当然,您必须自己编写PrintMenuAndAcceptOneAnswerDoOneCommand I would further break down those subroutines -- for example, DoOneCommand should call DoPrintAllCars , DoExit , DoParkOneCar , etc. 我将进一步细分这些子例程-例如, DoOneCommand应该调用DoPrintAllCarsDoExitDoParkOneCar等。

Finally, I did put a while in this main() , which I think is appropriate. 最后,我确实在main()放置了一段while ,我认为这是适当的。 Notice the global variable. 注意全局变量。 I assume that when you process the "exit" command, you'll set that variable to indicate the processing should end. 我假设在处理“退出”命令时,将设置该变量以指示处理应结束。

Good luck, and come back to tell us how your assignment works out for you. 祝您好运,然后回来告诉我们您的作业如何为您完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM