《C++捷径教程》读书笔记--Chapter 7--函数,第一部分:基础知识(第一部分)

该博客是《C++捷径教程》第七章函数基础知识的读书笔记,包含多个C++程序示例,涉及作用域、变量声明位置、函数调用(传递指针、数组、字符串等)等内容,通过代码展示了C++函数使用中的各种情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//--《C++捷径教程》读书笔记--Chapter 7--函数,第一部分:基础知识(第一部分)
//--Chapter 7--函数,第一部分:基础知识
//--11/14/2005 Mon.
//--Computer Lab
//--Liwei


//--程序#1  作用域
#include <iostream>
using namespace std;

void f1();

int main()
{
 char str[]="this is str in main().";   
 cout<<str<<endl;
 f1();
 cout<<str<<endl;

    cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

void f1()
{
 char str[80];
 cout<<"Enter something: ";
 cin>>str;
 cout<<str<<endl;
}


//--程序#2  本程序将解释变量式如何被限制在代码块中的
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
 int choice;

 cout<<"1、add numbers or: /n";
 cout<<"2、concatenate strings?: /n";

 cin>>choice;
 if(choice==1){
  int a,b;
  cout<<"Enter two numbers: ";
  cin>>a>>b;
  cout<<"Sum is "<<a+b<<endl;
 }
 else{
  char s1[80],s2[80];
  cout<<"Enter two strings: ";
  cin>>s1>>s2;
  //cin>>s2;
  strcat(s1,s2);
  cout<<"Concatenation is "<<s1<<endl;
 }//end if-else

    cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

//--程序#3  不正确的程序
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
 int choice;

 cout<<"1、add numbers or: /n";
 cout<<"2、concatenate strings?: /n";

 cin>>choice;
 if(choice==1){
  int a,b;
  cout<<"Enter two numbers: ";
  cin>>a>>b;
  cout<<"Sum is "<<a+b<<endl;
 }
 else{
  char s1[80],s2[80];
  cout<<"Enter two strings: ";
  cin>>s1>>s2;
  //cin>>s2;
  strcat(s1,s2);
  cout<<"Concatenation is "<<s1<<endl;
 }//end if-else

 a=10;//error
 cout<<s1;//error

    cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}


//--程序#4  内部变量覆盖外部变量
#include <iostream>
//#include <cstring>
using namespace std;

int main()
{
 int i,j;
 i=10;
 j=100;

 if(j>0){
  int i;
  i=j/2;
  cout<<"inner i: "<<i<<endl;
 }//end if

 cout<<"outer i: "<<i<<endl;

    cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

//--程序#5  变量声明的位置
#include <iostream>
//#include <cstring>
using namespace std;

int main()
{
 cout<<"Enter a number: ";
 int a;
 cin>>a;

 cout<<"Enter a second number: ";
 int b;
 cin>>b;

 cout<<"Product: "<<a*b<<endl;

    cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

//--程序#6  变量声明的位置
#include <iostream>
//#include <cstring>
using namespace std;

int main()
{
 for(int i=0; i<10; i++){
  cout<<i<<" ";
  cout<<"squared is "<<i*i<<endl;
 }


 i=100;// error 标准C++不这样
 cout<<i<<endl;

    cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

//--程序#7  一个简单的加法练习程序
#include <iostream>
#include <cstdlib>
using namespace std;

void drill();

int count;
int num_right;

int main()
{
    cout<<"How many practice problems: ";
 cin>>count;

 num_right=0;

 do{
  drill();
  count--;
 }while(count);

 cout<<"You got "<<num_right<<" right. ";
 
    cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

void drill()
{
 int count;
 int a,b,ans;
 a=rand()%100;
 b=rand()%100;

 for(count=0; count<3; count++){
  cout<<"Your have 3 times chance to answer."<<" now is: "<<count+1<<endl;
  cout<<"what is "<<a<<" + "<<b<<" ? ";
  cin>>ans;
  if(ans==a+b){
   cout<<"Right./n";
   num_right++;
   return;
  }//if
 }//for

 cout<<"You have used up all your tries./n";
 cout<<"The answer is "<<a+b<<endl;
}


//--程序#8  传递一个指针给函数7
#include <iostream>
using namespace std;

void f(int *j);

int main()
{
 int i;
 int *p;
 
 p=&i;

 f(p);

 cout<<i<<endl;
 
    cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

void f(int *j)
{
 *j=100;
}


//--程序#9  传递一个指针给函数7
#include <iostream>
using namespace std;

void f(int *j);

int main()
{
 int i;
 
 f(&i);

 cout<<i<<endl;
 
    cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

void f(int *j)
{
 *j=100;
}

//--程序#10  用数组调用函数
#include <iostream>
using namespace std;

//void display(int num[10]);
void display(int num[]);
//void display(int *num);

int main()
{
 int t[10],i;

 for(i=0; i<10; ++i)
  t[i]=i;

 display(t);

    cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

void display(int *num)
{
 int i;
 for(i=0; i<10; i++)
  cout<<num[i]<<' ';
}

//--程序#11  用数组元素调用函数
#include <iostream>
using namespace std;

void display(int num);

int main()
{
 int t[10],i;

 for(i=0; i<10; ++i)
  t[i]=i;
 for(i=0; i<10; i++)
  display(t[i]);

    cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

void display(int num)
{
 cout<<num<<"    ";
}

//--程序#12  用数组调用函数
#include <iostream>
using namespace std;

void cube(int *n,int num);

int main()
{
 int i,nums[10];

 for(i=0; i<10; i++)
  nums[i]=i+1;

 cout<<"Original contents: ";
 for(i=0; i<10; i++)
  cout<<nums[i]<<' ';

 cout<<endl;

 cube(nums,10);

 cout<<"Altered contents: ";
 for(i=0; i<10; i++)
  cout<<nums[i]<<' ';

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

void cube(int *n, int num)
{
 //while(num)
 while(*n){
  *n=*n * *n * *n;
  //num--;
  n++;
 }
}


//--程序#13  传递一个字符串给函数
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

void stringupper(char *str);

int main()
{
 char str[80];
 strcpy(str,"this is a test.ttt");

 stringupper(str);

 cout<<str<<endl;

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

void stringupper(char *str)
{
 while(*str){
  *str=toupper(*str);
  str++;
 }

}

//--程序#14  函数strlen()的另一个版本
#include <iostream>
using namespace std;

int mystrlen(char *str);

int main()
{
 cout<<"Length of hello there is: ";
 cout<<mystrlen("hello there.");

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

int mystrlen(char *str)
{
 int i;
 for(i=0; str[i]; i++);

 return i;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值