问题

计算数学 >> 数据结构
Questions in category: 数据结构 (Data Structure).

关于命令行参数输入的问题

Posted by 120702117 on 2014-04-21 22:56:29 last update 2014-04-21 22:56:29 | Answers (0) | 收藏


问题 1:关于void insert(const T &x,BinaryNode * &t)

   老师您上课讲了*&t,说是加引用是为了改变t所指向的地址,我觉得讲得不是太清楚。我的理解是,比如,

struct BinaryNode
{
    string element;
    BinaryNode* left;
    BinaryNode* right;
    TreeNode(BinaryNode* m=NULL,BinaryNode* n=NULL):left(m),right(n){}
};

insert(x,p);

则BinaryNode*类型的变量只是结构体中的一个成员,void insert(const T &x,BinaryNode * &t)改为void insert(const T &x,BinaryNode * t),则此时的p仅仅表示一个成员的地址,而不是这个结构体的地址。故此时的*&t我认为是代表这个结构体的地址。

问题 2:

  æˆ‘在网上查了相关命令行参数输入的问题,可是还是不懂如何在codeblocks输入参数。。。请老师给出具体的步骤啊

如我下面的例子(c++primer相关章节), ä¿å­˜è·¯å¾„D:\My Documents\c++\argc_argv

#include <iostream>
#include <string>
#include <vector>
#include <ctype.h>
#include<cstdlib>
using namespace std;
const char *const program_name = "comline";
const char *const program_version = "version 0.01 (08/07/97)";
inline void usage( int exit_value = 0 )
{
// 输出一个格式化的用法信息
// 并用 exit_value 退出...
cerr<< "usage:\n"
<< program_name << " "
<< "[-d] [-h] [-v] \n\t"
<< "[-o output_file] [-l limit] \n\t"
<< "file_name\n\t[file_name [file_name [ ... ]]]\n\n"
<< "where [] indicates optional option: \n\n\t"
<< "-h: help.\n\t\t"
<< "generates this message and exits\n\n\t"
<< "-v: version.\n\t\t"
<< "prints version information and exits\n\n\t"
<< "-d: debug.\n\t\tturns debugging on\n\n\t"
<< "-l limit\n\t\t"
<< "limit must be a non-negative integer\n\n\t"
<< "-o ofile\n\t\t"
<< "file within which to write out results\n\t\t"
<< "by default, results written to standard output \n\n"
<< "file_name\n\t\t"
<< "the name of the actual file to process\n\t\t"
<< "at least one file_name is required --\n\t\t"
<< "any number may be specified\n\n"
<< "examples:\n\t\t"
<< "$command chapter7.doc\n\t\t"
<< "$command -d -l 1024 -o test_7_8 "
<< "chapter7.doc chapter8.doc\n\n";
exit( exit_value );
}
int main( int argc, char* argv[] )

{
bool debug_on = false;
bool ofile_on = false;
bool limit_on = false;
int limit = -1;
string ofile;
vector<string> file_names;
cout << "illustration of handling command line arguments:\n"
<< "argc: " << argc << endl;
for ( int ix = 1; ix < argc; ++ix )
{
cout << "argv[ " << ix << " ]: "
<< argv[ ix ] << endl;
char *pchar = argv[ ix ];
switch ( pchar[ 0 ] )
{
case '-':
{
cout << "case \'-\' found\n";
switch( pchar[ 1 ] )
{
case 'd':
cout << "-d found: "
<< "debugging turned on\n";
debug_on = true;
break;
case 'v':
cout << "-v found: "
<< "version info displayed\n";
cout << program_name
<< " :: "
<< program_version
<< endl;
return 0;
case 'h':
cout << "-h found: "
<< "help information\n";
// 这里没必要用 break 了, usage() 可以退出
usage();
case 'o':
cout << "-o found: output file\n";
ofile_on = true;
break;
case 'l':
cout << "-l found: "
<< "resource limit\n";

limit_on = true;
break;
default:
cerr << program_name
<< " : error : "
<< "unrecognized option: - "
<< pchar << "\n\n";
// 这里没必要用 break 了, usage() 可以退出
usage( -1 );
}
break;
}
default: // 或文件名
cout << "default nonhyphen argument: "
<< pchar << endl;
if ( ofile_on ) {
ofile_on = false;
ofile = pchar;
}
else
if ( limit_on ) {
limit_on = false;
limit = atoi( pchar );
if ( limit < 0 ) {
cerr << program_name
<< " : error : "
<< "negative value for limit.\n\n";
usage( -2 );
}
}
else file_names.push_back( string( pchar ));
break;
}
}
if ( file_names.empty() ) {
cerr << program_name
<< " : error : "
<< "no file specified for processing.\n\n";
usage( -3 );
}
if ( limit != -1 )
cout << "User-specifed limit: "
<< limit << endl;
if ( ! ofile.empty() )
cout << "User-specified output file: "
<< ofile << endl;
cout << (file_names.size() == 1 ? "File " : "Files ")
<< "to be processed are the following:\n";
for ( int inx = 0; inx < file_names.size(); ++inx )
cout << "\t" << file_names[ inx ] << endl;
}