ch7

一、写出一个类似“Hello ,world!”的函数,他把一个名字作为参数,并写出“hello,name”.修改这程序,使他能够以一系列名字作为参数,并且对每个名字分别说hello.

#include "stdafx.h"
#include<iostream>
using namespace std;
int main(int argc , char **argv){
    if (argc < 2)
        cout << "Hi, why not pass me argumets?" << endl;
    else
    {
        for (int k = 1; k != argc; ++k)
            cout << "Hello, " << argv[k] << " "<<endl;
    }
}

【相关文档】
1.VS2010 设置main函数输入参数

二、写一个程序,它读入任意多个由命令行参数提供名字的文件,并将它们一个接着一个写入cout,因为这个程序拼接起来它的输入去产生输出,你可称呼他为cat.

#include "stdafx.h"
#include<iostream>
#include<fstream>
using namespace std;


void cat_stream(istream & input){
    char ch;
    while (input.get(ch)){
        cout.put(ch);
    }
}
int main(int argc, char **argv){

    if (argc < 2){
        cout << "Hi, why not pass me argumets?" << endl;
        cout << "now input:" << endl;
        cat_stream(cin);
    }
    else
    {
        for (int k = 1; k != argc; ++k){
            ifstream infile(argv[k]);
            cat_stream(infile);
        }
    }

}

【相关文档】
1.C++文件读写详解(ofstream,ifstream,fstream)

  1. c++文件流基本用法(fstream, ifstream, ostream)

三、

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;


struct Tnode{
    string word;
    int count;
    Tnode * left;
    Tnode * right;
};

Tnode* new_Tnode(string const & word){
    Tnode * node = new Tnode;
    node->word = word;
    node->count = 1;
    node->left = node->right = NULL;
    return node;
}

void enter_word(Tnode * & root, string const & word){
    if (root != 0){
        Tnode * node = root;
        while (1)
        {
            int order = word.compare(node->word); //比较单词与节点单词的大小
            if (order == 0){  //如果相等,则本节点次数加一
                ++(node->count);
                break;
            }
            else{
                Tnode * &next = (order < 0) ? node->left : node->right;
                if (next == 0){//不存在子节点则添加
                    next = new_Tnode(word);
                    break;
                }
                else{
                    node = next;
                }
            }
        }
    }
    else{//创建第一个节点
        root = new_Tnode(word);
    }

}

void write_output(ostream &output, Tnode *node,
    bool indent, int spaces = 2) {
    if (node) {
        write_output(output, node->left, indent, spaces + 2);
        if (indent)
        for (int k = 0; k != spaces; ++k)
            cout << ' ';
        output << node->word
            << " (" << node->count << ")\n";
        write_output(output, node->right, indent, spaces + 2);
    }
}

int main(){
    cout << "Enter words terminated by \"$done\" " << endl;

    Tnode *tree=NULL;
    while(1){
        string word;
        cin >> word;
        if (word == "$done"){
            break;
        }
        enter_word(tree, word);
    }
    write_output(cout, tree, true);
    return 0;
}

【需要注意得是,函数传入的参数类型,有的需要引用类型。有的则不需要】

四、


#include "stdafx.h"
#include<iostream>
#include<cassert>
#include<stdexcept>
#include<stdarg.h>  //处理可变参数的函数
using namespace std;


void Myerror(char const *fmt, ...){//参数至少一个
    assert(fmt);
    va_list p;   //创建一个char类型指针
    va_start(p, fmt); //读取参数列表
    for (char const *s = fmt; *s; ++s){
        if (*s != '%')
            cerr.put(*s); //无缓冲输出流
        else{
            switch (*(++s))
            {   //函数输出类型选择
            case '%': cerr.put('%'); break;
            case 's': cerr << va_arg(p, char const *); break;
            case 'c': cerr << va_arg(p, char); break;
            case 'd': cerr << va_arg(p, int); break;
            default:
                throw domain_error(string("panic!!"));
            }
        }
    }
    va_end(p);
}

int main() {
    //输入四个参数
    Myerror("A string \"%s\", a single character \'%c\', an integer %d\n"
        "and a percent-symbol \'%%\'.\n",
        "Hello World", '$', 12345);
    return 0;
}

【相关文档】
1.stdarg.h头文件详解
2.#include <stdarg.h>
3.C++中cout和cerr的区别?
4.深入C语言可变参数(va_arg,va_list,va_start,va_end,_INTSIZEOF)

五、

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,707评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,204评论 19 139
  • 简介 C++98/03的设计目标:一、比C语言更适合系统编程(且与C语言兼容)。二、支持数据抽象。三、支持面向对象...
    认真学计算机阅读 10,748评论 0 53
  • ———————————————回答好下面的足够了---------------------------------...
    恒爱DE问候阅读 5,739评论 0 4
  • 说起这组片子的制作团队,可能是史上最清贫的剧组。从编剧、导演、灯光、后期、摄影、演员,全都是西山居世游的同事。投入...
    Mr少年客阅读 3,762评论 9 50

友情链接更多精彩内容