我有作业要做。我试着解决这个问题,但我需要一些帮助。
124ate 6,字符长,名字类似,例如Superpan和生产年份作为整数变量。在您的程序中,不应该显式调用队列ADT,而是使用全局函数来调用它们,(implicitly).H 115打印所有cars
我的答案是:
#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;
}我的问题是:
发布于 2011-03-24 14:40:51
编辑:
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...
}在注释“添加这里:.”的地方添加您的打印是
发布于 2011-03-24 15:13:07
“您的主程序只应该有声明和对全局函数的调用,”
我相信你的导师鼓励你把你的代码分成单独的子程序,每个子程序只做一件事。按照这种风格,典型的main可能是:
int main() {
MyDataStruct x;
ReadInput(x);
ProcessData(x);
WriteOutput(x);
}请注意,在for中没有main循环,没有增加销售额和复制税的表达式,根本不需要任何处理。所有的好东西要么在全局函数中,要么在数据结构的方法中。
将其应用到您的程序中,您的主程序可能如下所示:
int globalExitFlag;
int main() {
Queue<car> q;
Car c;
while( !globalExitFlag ) {
int cmd;
cmd = PrintMenuAndAcceptOneAnswer(q, c);
DoOneCommand(q, c, cmd);
}
}然后,您可以将cin读取的核心放在PrintMenuAndAcceptOneAnswer中,而打印和队列操作则驻留在DoOneCommand中。当然,您必须自己编写PrintMenuAndAcceptOneAnswer和DoOneCommand。我将进一步分解这些子程序--例如,DoOneCommand应该调用DoPrintAllCars、DoExit、DoParkOneCar等等。
最后,我在这个while中添加了一个main(),我认为这是合适的。注意全局变量。我假设,当您处理"exit“命令时,您将设置该变量以指示应该结束的处理。
祝你好运,回来告诉我们你的任务是如何完成的。
https://stackoverflow.com/questions/5420831
复制相似问题