2014-01-12 51 views
5

我正在通过“加速C++”。我有一个关于问题5-3的问题。它要求:什么是驱动程序功能?

5-3. By using a typedef, we can write one version of the program that implements either a 
vector-based solution or a list-based one. Write and test this version of the program.' 

下一个问题问:

5-4. Look again at the driver functions you wrote in the previous exercise. Note that 
it is possible to write a driver that differs only in the declaration of the type for the data structure 
that holds the input file. If your vector and list test drivers differ in any other way, rewrite them 
so that they differ only in this declaration. 

到底是什么驱动程序的功能?我已经通过创建if语句以及重载函数来处理不同的数据类型,像这样解决5-3:

cout << "Please enter 1 if you would like to use vectors, or 2 if you would like to use lists: "<< endl; 
int choose; 
cin >> choose; 
//CHOOSING TO USE VECTORS 
if (choose == 1){....vector<Student_info> allStudents; 
       vector<Student_info> fail;.......} 

//CHOOSING TO USE LISTS 
else if (choose==2) {....list<Student_info> allStudents; 
        list<Student_info> fail;....} 

//INVALID CHOICE 
else {...invalid number, try again...} 

我没有创造除了那些超载任何额外的功能来处理不同的数据类型。这些驱动程序功能?如果不是的话,我一定在做错了。有人可以点亮一些光线吗? :>

+1

你可能想读这2005线程。 http://bytes.com/topic/c/answers/167496-accelerated-c-clarification-wording-exercises – KeithSmith

+2

驱动程序的功能很可能被写入演示一些图书馆式的代码,解决问题的操作功能。例如,如果你编写了一些类'A',驱动程序函数就是代码中的函数,只是为了显示类“A”的行为如预期。所以,第一个驱动函数本身就是'main',然后是'main'中调用的任何其他函数,它们都是'A'类的客户端。 – LavaScornedOven

+0

对于一本强烈推荐的书,写作往往是非常残酷的。互联网上有多个线程试图解析他们实际上在问什么问题。我一直试图从字面上重新编写问题,以便在没有互联网搜索的情况下理解它们。 (是的,这将是讽刺)这是否显示C++教学法的状态是多么令人遗憾,如果尽管它存在不可否认的缺陷,它仍然位列榜首。 – neuronet

回答

3

里面你的两个if块,多么相似的是,在allStudentsfail操作代码,无论他们是否是listvector?如果你已经正确完成了任务,那么可能几乎没有区别。现在,如果你把这些代码出来,并转移到listvector引用和mytype,你要么建立与typedef vector<Student_info> mytype或代替操作typedef list<Student_info> mytype你将有什么他们调用“驱动程序功能”。这不是你要找到互联网参考的正式名称。他们只是描述驱动器listvector操作以获得答案的代码。

+0

除了typedef之外,这两个代码块是相同的!如果我做了这样的事情: 'if(choose == 1){ typedef vector mytype;如果(选择== 2){ } typedef list } 其他{ 的cout << “无效号码” << ENDL; main(); return 0; } .... MYTYPE ..... ' 将if语句视为驱动程序的功能? – zzz2991

+2

@DavidJhoo我认为这个问题的具体目标是让你把它分成一个*函数*(这可能需要单独编译'typedef'来获得不同的类型),因为我打赌他们接下来要做的事情是介绍*模板*并告诉你如何现在可以有一个“多种类型的驱动程序功能”(使用问题的尴尬语言) –

+0

好的。这就说得通了。我一直在想办法通过函数和引用来定义'mytype'的'typedef'。但我不确定如何做到这一点。我将如何返回一个typedef,或者引用一个可以指定typedef的对象?或者这是一个不正确的做法? – zzz2991

1

在这种特殊情况下,驱动程序代码是一种比较模糊的方式,说测试代码

换句话说,笔者建议你看看你用来验证你在5-3写的代码测试(又名驱动程序)代码。

相关问题