美文网首页
第一章 c++过程化编程

第一章 c++过程化编程

作者: DeepWeaver | 来源:发表于2017-10-07 12:55 被阅读119次

学习内容:

  • 命名空间

  • 指针与动态分配内存

  • 引用与指针传参、返回指针、递归、函数默认值、重载的使用

  • 掌握C++11的基础语言扩展机制(auto类型推导、统一初始化、范围循环等)

  • 了解string和vector的基础应用

  • 实习任务一 :myNamespace
#include <iostream>
using namespace std;
namespace myNspace{
    void swap(int &a, int &b){
        int temp = a;
        a = b;
        b = temp;
    }
}
namespace myNspace2{
    void swap(int *a,int *b){
        int temp = *a;
        *a = *b;
        *b = temp;
    }
}
int main()
{
    int m = 1, n= 2;
    cout<<"m = "<<m<<endl;
    cout<<"n = "<<n<<endl<<endl;

    myNspace::swap(m,n);//这是用取地址的方法
    cout<<"m = "<<m<<endl;
    cout<<"n = "<<n<<endl<<endl;
    
    myNspace2::swap(&m,&n);//这是用指针的方法
    cout<<"m = "<<m<<endl;
    cout<<"n = "<<n<<endl;
    return 0;
}
运行结果:
m = 1
n = 2

m = 2
n = 1

m = 1
n = 2
[Finished in 0.3s]
  • 实习任务二 : getMax
#include <iostream>
using namespace std;
int *getMax(int *a, int size){
    int *max = new int(0);
    for(int i =0;i<size;i++){
        if(*max < *(a+i)){
            *max = *(a+i);
        }
    }
    cout<<max<<endl;
    return max;
}

int main()
{
    int a[8] = {1,2,43,3,5,6,7};
    int * temp = getMax(a,7);
    cout<<temp<<endl;
    cout<<*temp<<endl;  
    delete temp;//函数返回一个指针,函数中的指针在函数外销毁
    
    return 0;
}
运行结果:
0x7fcc8cc025c0
0x7fcc8cc025c0
43
[Finished in 0.3s]
  • 实习任务三 :getDiagonal
  1. 指针版
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
void print2Darray(int** array, int size){
    for(int i =0;i<size;i++){
        for(int j =0;j<size;j++){
            cout<<setw(4)<<array[i][j];
        }
        cout<<endl;
    }
    cout<<endl;
}
int * getDiagonal(int** matrix, int n){
    int *diag = new int[100];
    for(int i=0;i<n;i++){
        *(diag+i) = *(*(matrix+i)+i);
    }
    return diag;
}
int main(){

    int** matrix = new int*[10];
    for(int i=0;i<10;i++){
        *(matrix+i) = new int[10];
    }
    int x = 1;
    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            *(*(matrix+i)+j) = x;
            ++x;
        }
    }//初始化指针数组
    print2Darray(matrix,10);

    int* p = getDiagonal(matrix, 10);

    cout<<"Diag of the matrix:"<<endl;
    for(int i =0;i<10;i++){
        cout<<setw(4)<<*(p+i);
    }
    cout<<endl;
    for(int i =0;i<10;i++){
        delete [] *(matrix+i);
    }
    delete [] matrix;
    //这两步很关键,出来混迟早要还的 :)
    //要delete数组的每一行行地址还有数组指向这些行的首地址


    return 0;
}
运行结果:
   1   2   3   4   5   6   7   8   9  10
  11  12  13  14  15  16  17  18  19  20
  21  22  23  24  25  26  27  28  29  30
  31  32  33  34  35  36  37  38  39  40
  41  42  43  44  45  46  47  48  49  50
  51  52  53  54  55  56  57  58  59  60
  61  62  63  64  65  66  67  68  69  70
  71  72  73  74  75  76  77  78  79  80
  81  82  83  84  85  86  87  88  89  90
  91  92  93  94  95  96  97  98  99 100

Diag of the matrix:
   1  12  23  34  45  56  67  78  89 100
[Finished in 0.3s]
  1. vector版
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
vector<int> getDiagonal(const vector<vector<int> > & matrix){
    vector<int> a;//vector的好处:不用预先定义大小
    for(int i=0;i<matrix.size();i++){
        a.push_back(matrix[i][i]);
    }
    return a;
}
int main()
{
    vector< vector<int> > matrix(9, vector<int>(10));//定义一个二维向量(数组)
    int x = 1;
    for(int i=0;i<matrix.size();i++){
        for(int j=0;j<matrix[i].size();j++){
            matrix[i][j] = x;
            cout<<setw(3)<<x;
            x++;
        }
        cout<<endl;
    }
    cout<<endl;
    vector<int> m;

    m = getDiagonal(matrix);
    vector<int>::iterator it;
    for(vector<int>::iterator i=m.begin();i!=m.end();i++){
        cout<<setw(3)<<*i;
    }
      
    
    return 0;

}
运行结果:
  1  2  3  4  5  6  7  8  9 10
 11 12 13 14 15 16 17 18 19 20
 21 22 23 24 25 26 27 28 29 30
 31 32 33 34 35 36 37 38 39 40
 41 42 43 44 45 46 47 48 49 50
 51 52 53 54 55 56 57 58 59 60
 61 62 63 64 65 66 67 68 69 70
 71 72 73 74 75 76 77 78 79 80
 81 82 83 84 85 86 87 88 89 90

  1 12 23 34 45 56 67 78 89
[Finished in 0.4s]
  • 实习任务四:四则运算
#include <iostream> 
#include <ctime> 
using namespace std; 
int add(int a,int b){
    return a+b; 
}
int sub(int a,int b){ 
    return a-b;
}
int multiply(int a,int b){
    return a*b; 
}
int devide(int a, int b){
    return a/b;
}
char menu(){ 
    char choice;
    cout<<"1) add two numbers\n"; 
    cout<<"2) sub two numbers\n"; 
    cout<<"3) multiply two numbers\n"; 
    cout<<"4) devede two numbers\n";
    cout<<"0) quit\n";
    cout<<"Enter your choice:\n"; 
    cin>>choice;
    return choice;
}
bool answerQuestion(int num1,int num2,int (*f)(int ,int ), int answer ){
    return f(num1,num2)==answer; 
}

int main()
{ 
    int x_c = 0, x_w = 0;//answer correct & wrong counter
    while(true) {
        char choice=menu(); 
        if(choice=='0')
            break;
        int num1, num2;
        num1=rand()%90+10; 
        num2=rand()%90+10; 
        decltype(add) *pf; //add函数的类型
        char op; 
        switch(choice){
    //得到 10~99 的整数
            case '1':pf=add; //add函数的类型给了pf
                     op='+'; 
                     break; 
            case '2':pf=sub; 
                     op='-'; 
                     break;
            case '3':pf=multiply; 
                     op='*'; 
                     break;
            case '4':pf=devide;
                     op='/';
                     break; 
            default :continue;
        }
        if(op=='/'){
            while(num1<=num2||num1%num2!=0){
                num1=rand()%90+10; 
                num2=rand()%90+10; 
            }
        }
        int answer;
        cout<<num1<<op<<num2<<"=?";
        cin>>answer;
        if(answerQuestion(num1, num2,pf,answer)==true){
            x_c++;
            cout<<"Correct!"<<endl; 
        }
        else{
            if(x_w==3)
                break;
            x_w++;
            cout<<"Wrong!"<<endl; 
        }
    }
    cout<<"The number of your correct answers is "<<x_c<<endl;
    cout<<"The number of your wrong answers is "<<x_w<<endl;
return 0; 
}
  • 课后练习
  1. 默认参数
#include <iostream>
using namespace std;
int sum( int a, int b=1, int c=3 ){
    return a+b+c; 
}
int main()
{
    //try comment the code below and see what happens
int sum(int a, int b=3, int c=4); 
cout<<sum (2)<<endl; 
cout<<sum (2,5)<<endl; 
cout<<sum (2,3,6)<<endl; 
return 0;
}
  1. 函数引用?
#include <iostream>
using namespace std;
char & elem(char *s, int n){
    return s[n]; //传出来的是char&型
}
int * add(int a, int b){
    int *c = new int(a+b);
    return c;//传出来的是int*型
}
int main(){
    char str[]="HelloWorld"; 
    cout<<elem(str,1)<<endl;
    elem(str,1)= 'A'; //&str[1] = 'A'?
    cout<<elem(str, 1)<<endl;
    cout<<str<<endl;
    int * k;
    k = add(1,2);
    cout<<*k<<endl;
    delete k;
return 0; 
}
  1. 作用域
#include <iostream>
using namespace std;
int x = 10;
int main()
{
    int x = 15;
    cout<<x<<endl;
    cout<<::x<<endl;
    return 0;
}
  1. exchanging numbers not working
#include <iostream> 
using namespace std; 
void xhg(int *a,int *b){
    int *tmp;
    tmp=b; 
    b=a; 
    a=tmp; //仅仅交换了a和b代表的地址,地址指向的东西没有变
    cout<<*a<<' '<<*b<<endl;
}
void xhg2(int *a, int*b){
    int tmp;
    tmp = *b;
    *b = *a;
    *a = tmp;
    cout<<*a<<' '<<*b<<endl;
}
int main()
{
    int x(5),y(4); 
    xhg(&x,&y); 
    cout<<x<<' '<<y<<endl; 
    xhg2(&x,&y); 
    cout<<x<<' '<<y<<endl; 
    return 0;
}
  1. 递归求和
#include <iostream> 
using namespace std; 
int ff(int *a,int size){
    if(size==1) 
        return a[0];
    return a[size-1]+ff(a,size-1); 
}
int main()
{
    int a[5]={1,2,3,4,5}; 
    cout<<"result: "<<ff(a,5)<<endl; 
    return 0;
}
  1. 递归逆序输出字符串
#include <iostream>
using namespace std;
void f(const string& s,int n){
    cout<<s[n-1]; 
    if(n>1)
        f(s,n-1); 
}
int main(){
    f("animal",6);
    cout<<endl; 
    f("hello",3); 
    return 0;
}
  1. 寻找数组最大值和最小值之间的差
#include <iostream>
using namespace std;
int func(int data[],int size){
    int a=data[0];
    int b=data[0];
    for(int i=1;i<size;++i) {
        if(data[i]>a) 
            a=data[i];
        if(data[i]<b) 
            b=data[i]; 
    }
    return a-b;

}
int main()
{
    int a[] = {9,3,2,-1,8,0,4}; 
    cout<<func(a,7)<<endl; 
    cout<<func(a+2,4)<<endl; 
    return 0;
}
  1. sum from 1 to 100 at the interval of x
#include <iostream> 
using namespace std; 
int fun(int interval=1){
    int sum=0, i=0;
    for(i=0; i<100; i+=interval)
        sum+=i; 
    return sum;
}
int main()
{
    cout<<"Result1: "<<fun(2)<<endl; 
    cout<<"Result2: "<<fun()<<endl; 
    return 0;
}

9.calculate the average number of the fist n numbers of the array

#include <iostream>
using namespace std;
double func( double pData[], int size); 
int main(){
    double array[] = {2.2, 3.8, 6, 5.4}; 
    cout<<"Result: "<<func(array, 4)<<endl; 
    cout<<"Result: "<<func(array, 3)<<endl; 
    return 0;
}
double func( double pData[], int size){
    double result=0;
    int i;
    for(i=0; i<size; ++i) {
        result+=pData[i]; 
    }
    result /= size;
    return result; 
}
  1. vector iteration
#include <iostream> 
#include <vector> 
using namespace std; 
int main(){
    vector<int> vec{2,4,5,6,10,15,3,21,36,72,9,13};
    for(int i=0;i<vec.size();++i)
        cout<<vec[i]<<" "; 
    cout<<endl;
    for(auto it=vec.begin ();it!=vec.end ();++it) 
        cout<<*it<<" ";
    cout<<endl; 
    for(auto e : vec)
        cout<<e<<" ";
        cout<<endl;
    return 0; 
}

相关文章

  • effective C++ 笔记

    第一章 从 C 到 C++ 条款1 C++是语言联邦这条意思是C++支持面向过程、面向对象、泛型编程、函数编程、元...

  • C++ 简介

    C++ 简介 C++ 是一种静态类型的、编译式的、通用的、大小写敏感的、不规则的编程语言,支持过程化编程、面向对象...

  • C++是什么?怎么学?

    C++简介 C++ 是一种静态类型的、编译式的、通用的、大小写敏感的、不规则的编程语言,支持过程化编程、面向对象编...

  • 第一章 c++过程化编程

    学习内容: 命名空间 指针与动态分配内存 引用与指针传参、返回指针、递归、函数默认值、重载的使用 掌握C++11的...

  • 极客班第一周学习笔记

    初识C++ C++是在C之上基于对象,面向对象的编程语言。c++相比c在编程上更加模块化,具象化。 C++代码规范...

  • 第八章:模板元编程

    模板是在编译时实例化的。C++ 的某些功能可以与实例化过程结合,成为一种原始的递归“编程语言”。C++ 有多种功能...

  • 1.1.01_C++ 简介

    C++ 是一种静态类型的、编译式的、通用的、大小写敏感的、不规则的编程语言,支持过程化编程、面向对象编程和泛型编程...

  • C++

    C++ 是一种静态类型的、编译式的、通用的、大小写敏感的、不规则的编程语言,支持过程化编程、面向对象编程和泛型编程...

  • C++ Primer+Plus(第6版)中文版.epub

    【下载地址】 C++是在C语言基础上开发的一种集面向对象编程、通用编程和传统的过程化编程于一体的编程语言,是C语言...

  • 2018年6月26日【python学习笔记】

    类 1.编程思想: 面向过程编程:C语言(只有逻辑和过程) 面向对象编程:Java、c++、oc、python等(...

网友评论

      本文标题:第一章 c++过程化编程

      本文链接:https://www.haomeiwen.com/subject/zrjdyxtx.html