ch7

作者: 安东可 | 来源:发表于2017-07-30 16:18 被阅读23次

    一、写出一个类似“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)

    五、

    相关文章

      网友评论

          本文标题:ch7

          本文链接:https://www.haomeiwen.com/subject/gtoukxtx.html