[英]queue in c++ ..homework
I have a homework. 我有作业 I try to solve it but I need some help..
我试图解决它,但我需要一些帮助。
124ate
[6 character long] and name like eg Superpan
and production year as integer variable. 124ate
[6个字符长],名称,例如Superpan
和生产年份,都是整数变量。 In your program you shouldn't call Queue ADT explicitly, instead use global functions to call them (implicitly). 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: 我的问题:
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. 当然,您必须自己编写
PrintMenuAndAcceptOneAnswer
和DoOneCommand
。 I would further break down those subroutines -- for example, DoOneCommand
should call DoPrintAllCars
, DoExit
, DoParkOneCar
, etc. 我将进一步细分这些子例程-例如,
DoOneCommand
应该调用DoPrintAllCars
, DoExit
, DoParkOneCar
等。
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.