《C++捷徑教程》讀書筆記–Chapter 7–函數,第一部分:基礎知識(第一部分)

來源:互聯網
上載者:User

//--《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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.