首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++ Primer上的一个程序解决办法

2012-02-19 
C++ Primer上的一个程序#include iostream#include vector#include string.husingnamespacestd#inc

C++ Primer上的一个程序
#include <iostream>
#include <vector>
#include <string.h>

using   namespace   std;

#include <ctype.h>

const   char   *const   program_name= "comline ";
const   char   *const   program_version= "version   0.01   (08/07/97) ";

inline   void   usage(int   exit_value=0)
{
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;
    std::vector <string,std::allocator>   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   into   displayed\n ";
   
    cout < <program_name
    < < ":: "
    < <program_version
    < <endl;
   
    return   0;
   
    case 'h ':
    cout < < "-h   found: "
    < < "help   information\n ";
   
    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 ";
   
    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-specified   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;
}
会报错,怎么修改??

------解决方案--------------------


先把include文件名的.h删了再说
[解决办法]

#include <iostream>
#include <vector>
#include <string>

using namespace std;

#include <ctype.h>

const char *const program_name= "comline ";
const char *const program_version= "version 0.01 (08/07/97) ";

inline void usage(int exit_value=0)
{
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;
std::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 into displayed\n ";

cout < <program_name
< < ":: "
< <program_version
< <endl;

return 0;

case 'h ':
cout < < "-h found: "
< < "help information\n ";

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 ";

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-specified 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;
}


热点排行