//--《C++捷径教程》读书笔记--Chapter 7--函数,第一部分:基础知识(第二部分)
//--Chapter 7--函数,第一部分:基础知识
//--11/14/2005 Mon.
//--Computer Lab
//--Liwei
//--程序#15 命令行参数
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
if(argc!=2){
cout<<"You forgot to type your name./n";
return 1;
}
cout<<"Hello "<<argv[1]<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程序#16 命令行参数
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int t,i;
for(t=0; t<argc; t++){
i=0;
cout<<"----";
while(argv[t][i]){
cout<<argv[t][i];
++i;
}
}
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程序#17 命令行参数
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
double a,b;
if(argc!=3){
cout<<"Usage: add num num/n";
return 1;
}
a=atof(argv[1]);
b=atof(argv[2]);
cout<<a+b;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程序#18 atoi()等使用
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int i;
long j;
double k;
i=atoi("100");
j=atol("100000");
k=atof("-0.123");
cout<<i<<' '<<j<<' '<<k;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程序#19 返回值
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int i;
i=abs(-10);
cout<<abs(-23);
abs(100);
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程序#20 return
#include <iostream>
using namespace std;
int find_substr(char *sub, char *str);
int main()
{
int index;
index=find_substr("three","one two three four.");
cout<<"Index of three is: "<<index<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
int find_substr(char *sub, char *str)
{
int t;
char *p,*p2;
for(t=0; str[t]; t++){
p=&str[t]; cout<<*p;
p2=sub;
while(*p2 && *p2==*p){
p++;
p2++;
}
if(!*p2) return t;
}
return -1;
}
//--程序#21 void
#include <iostream>
using namespace std;
void print_vertical(char *str);
int main(int argc, char *argv[])
{
if(argc==1)
print_vertical(argv[0]);
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
void print_vertical(char *str)
{
while(*str)
cout<<*str++<<endl;
}
//--程序#22 改写20
#include <iostream>
using namespace std;
char *find_substr(char *sub, char *str);
int main()
{
char *substr;
substr=find_substr("three","one two three four.");
cout<<"substring found: "<<substr<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
char *find_substr(char *sub, char *str)
{
int t;
char *p,*p2,*start;
for(t=0; str[t]; t++){
p=&str[t]; //cout<<*p;
start=p;
p2=sub;
while(*p2 && *p2==*p){
p++;
p2++;
}
if(!*p2) return start;
}
return 0;
}
//--程序#23 程序使用了函数原型进行强制类型检测
#include <iostream>
using namespace std;
void sqr_it(int *i);
int main()
{
int x;
x=10;
sqr_it(x);//error
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
void sqr_it(int *i)
{
*i=*i * *i;
}
//--程序#24 递归
#include <iostream>
using namespace std;
int factr(int n);
int fact(int n);
int count=0;
int main()
{
cout<<"4 factorial is "<<factr(4);
cout<<endl;
cout<<"4 factorial is "<<fact(4)<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
int factr(int n)
{
int answer;
count++;
cout<<"count: "<<count<<endl;
if(n==1) return 1;
answer=factr(n-1)*n;
cout<<"answer is: "<<answer<<endl;
return answer;
}
int fact(int n)
{
int t,answer;
answer=1;
for(t=1; t<=n; t++)
answer=answer*(t);
return answer;
}
//--程序#25 递归反向输出字符串
#include <iostream>
using namespace std;
void reverse(char *s);
int count;
int main()
{
char str[]="this";
reverse(str);
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
void reverse(char *s)
{
count++;
cout<<"count: "<<count<<endl;
if(*s)
reverse(s+1);
else
return;
cout<<*s<<"==";
}