c++day09

2021-02-19  本文已影响0人  __method__

插入排序基础版(后插1)

#include <iostream>
# define C 6
using namespace std;
// 插入排序
void insert_sort(int a[], int n){
        // 后插算法
    // 从 1 开始 是因为 我们要和 索引 0 的位置比较
    int j, p;  //  p是 临时变量用于记住插入 当前看的那个值,也就是以后要插入某个位置的值
    for (int i = 1; i < n ; i++) {
        p = a[i];
        for(j = i-1; j>=0; j--){
            if (p < a[j]){
                a[j+1] = a[j];
            }
        }
        a[j+1] = p;
    }
}
int main()
{
    int a[C] = {8, 4, 9, 6, 5, 2};
    insert_sort(a, C);
    cout<< "insert_sort finised"<< endl;
    for (int i = 0; i < C ; ++i) {
        cout<< a[i]<< "\t";
    }
}


插入排序基础版(后插2)

改进

#include <iostream>
# define C 6
using namespace std;
// 插入排序
void insert_sort(int a[], int n){
        // 后插算法
    // 从 1 开始 是因为 我们要和 索引 0 的位置比较
    int j, p;  //  p是 临时变量用于记住插入
    for (int i = 1; i < n ; i++) {
        p = a[i];
        for(j = i-1; j>=0&&p < a[j]; j--){
                a[j+1] = a[j];
        }
        a[j+1] = p;
    }
}
int main()
{
    int a[C] = {8, 4, 9, 6, 5, 2};
    insert_sort(a, C);
    cout<< "insert_sort finised"<< endl;
    for (int i = 0; i < C ; ++i) {
        cout<< a[i]<< "\t";
    }
}

插入排序基础版(前插)

#include <iostream>
# define C 6
using namespace std;
// 插入排序
void insert_sort(int a[], int n){
        // 前插算法
    // 从 1 开始 是因为 我们要和 索引 0 的位置比较
    int j, p;  //  p是 临时变量用于记住插入
    for (int i = 1; i < n ; i++) {
        p = a[i];
        for(j = 0; j<i&&p>=a[j]; j++); // 找到插入的位置 j
        for (int k = i; k>j ; k--) {
            a[k] = a[k-1]; // 右移
        }
        a[j] = p;
    }
}
int main()
{
    int a[C] = {8, 4, 9, 6, 5, 2};
    insert_sort(a, C);
    cout<< "insert_sort finised"<< endl;
    for (int i = 0; i < C ; ++i) {
        cout<< a[i]<< "\t";
    }
}

字符数组

ASCII 的 A = 65 a = 97

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

打印三角形

#include <iostream>
using namespace std;
int main()
{
    char star[ ][5]={{' ', ' ', '*'},{' ', '*', ' ', '*'},{'*', ' ', '*', ' ', '*'}};
    for (int i = 0; i < 3 ; ++i) {
        for (int j = 0; j < i + 3 ; ++j) {
            cout<< star[i][j];
        }
        cout<<endl;
    }

}

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;
int main()
{
   char str1[20],  str2[20], str3[20];
   cout<<"please input  3strings "<< endl;
   cin>>str1;
   cin>>str2;
   cin>>str3;
   cout<<"str1 = "<< str1<< endl;
   cout<<"str2 = "<< str2<< endl;
   cout<<"str3 = "<< str3<< endl;

}

使用 cin.getline

#include <iostream>
using namespace std;
int main()
{
    char str1[80];
    char str2[4]={'G', 'o', 'o', 'd'};
    cout<<"please input  a strings "<< endl;
    cin.getline(str1, 80); // 表示最多输入79个字符,系统自动在最后加一个'\0'
    cout<<"str1="<<str1<<endl;
//    cout<<"str2="<<str2<<endl;
    for (int i = 0; i < 4 ; ++i) {
        cout<< str2[i];
    }
}

字符串处理函数

使用字符串处理函数需要导入 string.h文件

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    char s1[ ]="How do you do!";
    char s2[ ]="Hello!";
    char s3[80];
    cin>>s3;
    cout << "s1 length = " << strlen(s1) << endl;
    cout << "s2 length = " << strlen(s2) << endl;
    cout << "s3 length = " << strlen(s3) << endl;

}
  1. 求字符串的长度strlen()(字符数组)
  2. 字符串复制strcpy(str1, str2)str2复制到str1中
  3. 字符串连接strcat(str1, str2)str2连接到str1后面
  4. 字符串比较strcmp(str1, str2) 下面是比较示例
    strcmp("student", "student") // 相等返回 0
    strcmp("student", "Student") // 大于返回 1
    strcmp("Student", "student") // 小于返回 -1
  5. 大写变小写strlwr( )
  6. 小写变大写strupr( )
  7. strncpy(str1, str2, n)将str2的前n个字符复制到str1中
  8. strncmp(str1, str2, n)比较前n个字符
上一篇 下一篇

猜你喜欢

热点阅读