c++day10

作者: __method__ | 来源:发表于2021-02-20 23:26 被阅读0次

编写一个函数my_strcpy,完成与系统标准库函数strcpy( )相同的功能

#include <iostream>
// 编写一个函数my_strcpy,完成与系统标准库函数strcpy( )相同的功能
using namespace std;
void  my_strcpy(char s1[], char s2[]){
    int i = 0;
    while(s2[i] != '\0'){
        s1[i] = s2[i++];
    }
    // 给s1也添加一个结束标志
    s1[i]='\0';
}
int main()
{
    char s1[80], s2[80];
    cout<< "please input a string";
    cin.getline(s2, 80);
    my_strcpy(s1, s2);
    cout<<s1<<endl;
    cout<<s2<<endl;

}

英文单词个数统计

#include <iostream>
#include <string.h>
using namespace std;
int  wordCount(char string[]){
    int len, num = 0;
    len = strlen(string);
    for (int i = 0; i < len;) {
        while (string[i] == ' ')i++; // 过滤掉连续的空格
        if (i < len) num++;   // 单词加1
        while (string[i]!= ' '&& i < len) i++;
    }
    return num;
}
int  wordCount2(char string[]){
    int i, num;
    char c = ' ';
    for ( i = num= 0; string[i] != '\0'; i++) {
        if (c== ' '&& string[i] != ' '){
            num++;
        }
        c = string[i];
    }
    return num;
}
int main()
{
//    char string[80];
//    cout<< "please input a seq";
    char string[] = "please input a seq";
//    cin.getline(string, 80);
    int num = wordCount(string);
    int num2 = wordCount2(string);
    cout<<"cin = "<<string<<endl;
    cout<<"wordcount = "<<num<<endl;
    cout<<"wordcount2 = "<<num2<<endl;

}
// "I    am a Student"

比较大小

#include <iostream>
#include <string.h>
// 有三个字符串,要求找出其中最小者
using namespace std;

int main()
{
    char target[80]; // 存最小值的
    char str1[80] = "American";
    char str2[80] = "China";
    char str3[80] = "Amarican";
    if (strcmp(str1, str2)<0){
        // str1 < str2
        strcpy(target, str1);
    } else{
        // str1 > str2
        strcpy(target, str2);
    }
    if (strcmp(str3 ,target)<0){
        strcpy(target, str3);
    }
    cout<< "min string = "<<target<<endl;


编写一个程序,计算一个字符串中子串出现的次数

#include <iostream>
#include <string.h>
//  编写一个程序,计算一个字符串中子串出现的次数
using namespace std;
int countSub(char mystring[], char sstring[]){
    int i, j,k, num = 0;
    for (i = 0;  mystring[i] !='\0'; ++i) {// 从主字符串位置开始扫描
        // 比较与子串是否相同
        for ( j = i,k=0; mystring[j]!='\0'&& sstring[k] ==mystring[j] ; j++, k++ );
        // 在 子串结束后计数一次
        if (sstring[k] =='\0'){
            num++;
        }

    }
    return num;
}
int main()
{
    char mystring[80]  = "i am a student you are a student student";
    char sstring[80]  = "student";
    int num = countSub(mystring, sstring);
    if (num>0){
        cout<<"subcount = "<< num<<endl;
    } else{
        cout<<"not in seq = "<<endl;

    }
}

编译预处理

  • 宏定义
//例6.1 求球体的表面积及体积
// Li0601.cpp不带参数的宏定义示例
#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<<"\n面积为:"<<s<<"\n体积为:"<<v<<'\n';
}

宏定义里面可以层层运算

//例 6.2 求圆的周长和面积
#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; 
}

带参数的宏定义

//例6.3 带参数的宏定义
#include <iostream>
using namespace std;
#define MUL(a, b) a*b

int main( )
{
        int x(5), y(8), t;  //x初值为5,y初值为8

        t=MUL(x+1, y-2); //A
        // t = x+1*y-2 = 11 宏体被替换
        cout<<"t="<<t<<endl;
}

条件编译

LETTER 0 执行下面的, 其他执行上面的

/*例6.4 输入一行字母字符,根据需要设置条件编译,
使之能将字母全改成大写输出或全改成小写输出。*/
#include <iostream>
using namespace std;
#define LETTER 1

int main( )
{
    char c;

    do
    {   
        cin.get(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');
}

相关文章

  • c++day10

    编写一个函数my_strcpy,完成与系统标准库函数strcpy( )相同的功能 英文单词个数统计 比较大小 编写...

网友评论

      本文标题:c++day10

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