一鼠冕、寫出一個類似“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,因為這個程序拼接起來它的輸入去產(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)
三侵浸、
#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é)點單詞的大小
if (order == 0){ //如果相等,則本節(jié)點次數(shù)加一
++(node->count);
break;
}
else{
Tnode * &next = (order < 0) ? node->left : node->right;
if (next == 0){//不存在子節(jié)點則添加
next = new_Tnode(word);
break;
}
else{
node = next;
}
}
}
}
else{//創(chuàng)建第一個節(jié)點
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)