C++day08光速入门指南--数组和函数的结合

数组作为函数的参数

  • 数组中元素中作为函数的参数
    案例: 统计数组对应元素中大于/小于/等于的个数
#include <iostream>
using namespace std;
int large(int x, int y){
    if (x > y)
        return 1;
    else if (x < y)
        return -1;
    else
        return 0;
}
int main(){
    int a[4] = {2223, 166 , 33 , 55};
    int b[4] = {323, 166 , 3 , 5335};
    int g = 0,  e = 0, l = 0;
    for (int i = 0; i < 4; ++i) {
        int flag = large(a[i], b[i]);
        if (flag == 1)
            g++;
        else if (flag == 0)
            e++;
        else
            l++;
    }
    cout<<" a[i] > b[i] counts "<<g<<endl;
    cout<<" a[i] = b[i] counts "<<e<<endl;
    cout<<" a[i] < b[i] counts "<<l<<endl;

}


案例

  • 编写一个可以控制范围和个数的生成数组函数
  • 编写打印数组的函数
  • 对生成数组传入冒泡排序函数进行排序
  • 对生成数组传入选择排序函数进行排序
#include <iostream>
#include <ctime>
using namespace std;
//- 编写一个可以控制范围和个数的生成数组函数
//- 对生成数组传入冒泡排序函数进行排序
//- 对生成数组传入选择排序函数进行排序
/*
 * arr[] 返回数组
 * a 生成随机数的下限
 * b 生成随机数的上限
 * n 是生成个数
 */
void genrateArray(int arr[], int a, int b, int n){
    srand((unsigned int )time(NULL));
    for (int i = 0; i < n; ++i) {
        //  生成a~b 之间的一个随机整数
        arr[i] = rand() % (b-a+1)+ a;
    }
}
void printArray(int arr[], int len){
    cout << "[";
    for (int i = 0; i < len; ++i) {
       if (i==len-1)
           cout << arr[i] << "]";
       else
           cout << arr[i] << ", ";
    }
    cout <<  endl;
}
void bubbleSort(int arr[], int len){
    for (int i = 0; i < len -1; ++i) {
        for (int j = len-1; j > i; --j) {
            if (arr[j-1] > arr[j]){
                int temp = arr[j-1];
                arr[j-1] = arr[j];
                arr[j] = temp;
            }
        }
    }
}
void selectSort(int arr[], int len){

    for (int i = 0; i < len - 1; ++i) {
        int minIndex = i;
        for (int j = i; j < len; ++j) {
            if (arr[j] < arr[minIndex]){
                minIndex = j;
            }
        }
        // 优化
        if (minIndex!=i){
            int temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }
}
int main(){
    const int N = 10;
    int arr[N];
    genrateArray(arr, 1, 100, N);
    cout<<"selectSort Before"<< endl;
    printArray(arr, N);
    selectSort(arr, N);
    cout<<"selectSort After"<< endl;
    printArray(arr, N);
    cout<<"bubbleSort Before"<< endl;
    genrateArray(arr, 1, 1000, N);
    printArray(arr, N);
    cout<<"bubbleSort After"<< endl;
    bubbleSort(arr, N);
    printArray(arr, N);
}



反转数组

#include <iostream>
using namespace std;
void inverse(int a[], int len){
    for (int i = 0; i < len/2; ++i) {
        int temp = a[i];
        a[i] = a[len -1 -i];
        a[len -1 -i] = temp;
    }
}
int main()
{
    int a[9] = {6, 7, 2, 5, 4, 3, 6, 1, 9};
    // 反转数组
    inverse(a, 9);
    // 输出数组
    for (int i = 0; i < 9; ++i) {
        cout << a[i]<<"\t";
    }

}
作用域

块作用域 {} 隔离代码的
在一个函数内部定义的变量或者在一个块中定义的变量称为局部变量, 声明周期在块中有效

#include <iostream>
using namespace std;
int main()
{
    int a=1, b=2;

    ++a;
    ++b;
    {
        int b=4, c;

        c=a+b;       //c只能在该复合语句内使用
        cout<<"a="<<a<<", "<<"b="<<b<<", "<<"c="<<c<<endl;
    }
    cout<<"a="<<a<<", "<<"b="<<b<<endl;

}

24623
文件作用域
在函数外面定义的变量或者用extern声明的变量
, 文件是c++的编译单位, 在所有函数外定义的标识符具有文件作用域, 作用域从定义点开始一直到本文件结束

#include <iostream>

int i = 10; // 文件作用域
int main()
{
    // 在内部操作全局变量 :: 符合
    int i, j = 5;
    i=20;             //访问局部变量i
    ::i=::i+4;          //访问全局变量i
    j=::i+i;           //访问全局变量i和局部变量i、j
    std::cout<<"::i="<<::i<<std::endl; // 14
    std::cout<<"i="<<i<<std::endl; // 20
    std::cout<<"j="<<j<<std::endl; //  34

}


存储类别

局部变量的存储类别
c++中 局部变量的存储为

  • auto 默认的不用写, 若没有明确的赋值, 初始值是不确定的
  • static 静态局部变量被分配到静态存储中(具有记忆功能)
#include <iostream>
using namespace std;

int fun(int x) {
    static int a = 3;
    a += x;
    return a;
}

int main() {
    int k = 2, m = 1, n;
    n = fun(k);
    cout << "first: n=" << n << endl;  // 5
    n = fun(m);
    cout << "second: n=" << n << endl; // 6
}


static 版本的阶乘

#include <iostream>

using namespace std;

int fac(int n)
{
    static  int f=1;
    f=f*n;
    return  f;
}

int main() {
    for (int i=1; i<=5; i++)
        cout<<i<<"!="<<fac(i)<<endl;
}


register
适用于变量使用频繁的情况

int fac(register int n)
{
    static int f=1;
    f=f*n;
    return(f);
}

全局变量

定义在所有函数外的变量, 均为静态存储, 作用域是定义处到源文件结束处, 可以使用 extern 和 static 对其作用域进行扩展和限制

  • extern 是扩展
#include <iostream>
using namespace std;
int min(int a, int b)            //定义min函数
{
    int c;

    c = a < b ? a : b;
    return (c);
}
extern int a, b;

int main()
{

    cout<<min(a, b)<<endl;
}

int a = 4, b = 7;


  • static 是限制
    有时候可能需要限制某些全局变量,不让别的文件使用, 可以用static做限制

字符数组

字符数组与字符串区别

  • 字符串一定是一个char的数组,但char的数组未必是字符串;
  • 数字0(和字符‘\0’等价)结尾的char数组就是一个字符串,但如果char数组没有以数字0结尾, 那么就不是一个字符串,只是普通字符数组,所以字符串是一种特殊的char的数组
#include <iostream>
#include <string>
using namespace std;
int main (){
    // 字符数组与字符串区别
    // 字符串一定是一个char的数组,但char的数组未必是字符串;
    // 数字0(和字符‘\0’等价)结尾的char数组就是一个字符串,但如果char数组没有以数字0结尾
    // 那么就不是一个字符串,只是普通字符数组,所以字符串是一种特殊的char的数组
    char c1[] = {'c', 'p', 'b'};
    char a = 'b';
    char c[] = "a";

    int arr[] = {1, 2, 5};
    cout<< "c1 = "<< c1 <<endl;
    cout<< "arr = "<< arr <<endl;

    char c2[] = {'c', 'p', 'b', 0};
    cout<< "c2 = "<< c2 <<endl;

    char c3[] = {'c', 'p', 'b', '\0'};
    cout<< "c3 = "<< c3 <<endl;

    char c4[] = {'c', 'p', 'b', 'a', 'q', 0};
    cout<< "c4 = "<< c4 <<endl;

    char c5[] = "abcdef";
    cout<< "c5 = "<< c5 <<endl;

    char c6[100] = {0};
    cout<< "c6 = "<< c6 <<endl;


//    char c7[2] = {'a', 'b', 0}; 越界
//    cout<< "c7 = "<< c7 <<endl;
    char c7[50] = {'1', 'a', 'b', '0', '7', 0};
    cout<< "c7 = "<< c7 <<endl;
    char c8[50] = {'1', 'a', 'b', 0, '7'};
    cout<< "c8 = "<< c8 <<endl;
    char c9[50] = {'1', 'a', 'b', '\0', '7'};
    cout<< "c9 = "<< c9 <<endl;


    string s = "aaaaa";
    cout<< s <<endl;

    return 0;

}

#include <iostream>
using namespace std;
int main()
{   char a = 'a';
    char c[15] = { 'I', ' ', 'a', 'm', ' ', 'a', ' ', 's', 't', 'u', 'd', 'e', 'n', 't', '.'};
    for (int i = 0; i < 15 ; ++i) {
        cout<< (int)c[i]<< "  ";
    }
    cout<<endl;
    cout<<"a = "<< (int)a << endl;  // 97 65
}


C++规定以字符’\0’作为字符串结束标志, ‘\0’的ASCII 值是0, 称为空字符, 但是字符数组并不要求必须以’\0’作为结束标志, 可以没有, 下面都是合法的.

char c1[5]  = {'G', 'o', 'o', 'd', '!'};
char c2[] = {"Good!"};
char c3[] = "Good!";
char c4[]  = {'G', 'o', 'o', 'd', '!', '\0'};

编译预处理

// 求球体的表面积及体积
// 带参数的宏定义示例
#include <iostream>
using namespace std;
#define PI 3.1415926

int main( )
{
    double r, s, v;

    cout<<"input radius :";
    cin>>r;     //输入半径
    s=4*PI*r*r;
    v=4.0/3*PI*r*r*r;
    cout<<"\A = "<<s<<"\n  V  = "<<v<<'\n';
}

宏定义里面可以层层运算

#include <iostream>
using namespace std;

#define R 5.0
#define PI 3.1415926
#define L 2*PI*R
#define S PI*R*R

int main( )
{
   cout<<"L="<<L<<"\nS="<<S<<endl;
}

条件编译

LETTER 0 执行下面的, 其他执行上面的(true or false)
使之能将字母全改成大写输出或全改成小写输出。*/

#include <iostream>
using namespace std;
#define LETTER 6
int main( )
{
   char c;
   do
   {
       cin.get(c);// 输入一个字符然后给c
#if LETTER
       if (c>='a'&& c<='z')
           c=c-32;
#else
       if (c>='A'&&c<='Z')
           c=c+32;
#endif
       cout<<c;

   }
   while (c!='\n');
}

枚举(enum)

#include <iostream>
#include <iomanip>
// 枚举
using namespace std;
// enum 枚举名字  枚举具体的值
enum color {red, yellow, blue, white, black, purple};

// 枚举类型作为函数的参数
void print(enum color c){
   switch (c) {
       case  red:    cout<<setw(10)<<"red";     break;
       case  yellow: cout<<setw(10)<<"yellow";  break;
       case  blue:   cout<<setw(10)<<"blue";    break;
       case  white:  cout<<setw(10)<<"white";   break;
       case  black:  cout<<setw(10)<<"black";   break;
       case  purple: cout<<setw(10)<<"purple";  break;
       default: break;
   }
}

// i, j , k 是不同颜色的球  查看共有多少种组合  i != j != k
int main()
{
   int count = 0;
   color i, j , k; // i, j , k是枚举类型
   // red purple 分别是头和尾部
   for(i = red; i<=purple; i = color(int(i) + 1)){
       for(j = red; j<=purple; j = color(int(j) + 1)){
           if(i!=j){
               for(k = red; k<=purple; k = color(int(k) + 1)){
                   if ((k!=i)&&(k!=j)){
                       cout<< ++count<<"\t";
                       print(i);
                       print(j);
                       print(k);
                       cout<<endl;

                   }
               }
           }
       }

   }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值