ch7

一、寫出一個類似“Hello ,world!”的函數(shù),他把一個名字作為參數(shù),并寫出“hello,name”.修改這程序,使他能夠以一系列名字作為參數(shù),并且對每個名字分別說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;
    }
}

【相關(guān)文檔】
1.VS2010 設(shè)置main函數(shù)輸入?yún)?shù)

二、寫一個程序,它讀入任意多個由命令行參數(shù)提供名字的文件,并將它們一個接著一個寫入cout,因?yàn)檫@個程序拼接起來它的輸入去產(chǎn)生輸出,你可稱呼他為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);
        }
    }

}

【相關(guān)文檔】
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); //比較單詞與節(jié)點(diǎn)單詞的大小
            if (order == 0){  //如果相等,則本節(jié)點(diǎn)次數(shù)加一
                ++(node->count);
                break;
            }
            else{
                Tnode * &next = (order < 0) ? node->left : node->right;
                if (next == 0){//不存在子節(jié)點(diǎn)則添加
                    next = new_Tnode(word);
                    break;
                }
                else{
                    node = next;
                }
            }
        }
    }
    else{//創(chuàng)建第一個節(jié)點(diǎn)
        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;
}

【需要注意得是,函數(shù)傳入的參數(shù)類型,有的需要引用類型。有的則不需要】

四、


#include "stdafx.h"
#include<iostream>
#include<cassert>
#include<stdexcept>
#include<stdarg.h>  //處理可變參數(shù)的函數(shù)
using namespace std;


void Myerror(char const *fmt, ...){//參數(shù)至少一個
    assert(fmt);
    va_list p;   //創(chuàng)建一個char類型指針
    va_start(p, fmt); //讀取參數(shù)列表
    for (char const *s = fmt; *s; ++s){
        if (*s != '%')
            cerr.put(*s); //無緩沖輸出流
        else{
            switch (*(++s))
            {   //函數(shù)輸出類型選擇
            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() {
    //輸入四個參數(shù)
    Myerror("A string \"%s\", a single character \'%c\', an integer %d\n"
        "and a percent-symbol \'%%\'.\n",
        "Hello World", '$', 12345);
    return 0;
}

【相關(guān)文檔】
1.stdarg.h頭文件詳解
2.#include <stdarg.h>
3.C++中cout和cerr的區(qū)別?
4.深入C語言可變參數(shù)(va_arg,va_list,va_start,va_end,_INTSIZEOF)

五、

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,351評論 25 708
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,699評論 19 139
  • 簡介 C++98/03的設(shè)計(jì)目標(biāo):一、比C語言更適合系統(tǒng)編程(且與C語言兼容)。二、支持?jǐn)?shù)據(jù)抽象。三、支持面向?qū)ο?..
  • ———————————————回答好下面的足夠了---------------------------------...
    恒愛DE問候閱讀 1,846評論 0 4
  • 說起這組片子的制作團(tuán)隊(duì),可能是史上最清貧的劇組。從編劇、導(dǎo)演、燈光、后期、攝影、演員,全都是西山居世游的同事。投入...
    Mr少年客閱讀 679評論 9 50

友情鏈接更多精彩內(nèi)容