C++(8.21)c++初步

1.斐波那契:

cpp 复制代码
#include <iostream>
#include<iomanip>

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    int a[10];
    for(int i=0;i<10;i++)
    {
        if(0==i||1==i)
        {
            a[i]=1;
        }
        else
        a[i]=a[i-1]+a[i-2];
        cout <<setw(4) << left << a[i];
    }
    cout <<endl;
    return 0;
}

2.判断输入的符号

cpp 复制代码
#include <iostream>
#include<iomanip>
using namespace std;

int main()
{
    char a;
    while(1)
    {
        cout << "请输入一个字符(#退出)" << endl;
        cin >> a;
        if('#'==a)
        {
            cout << "符号" << endl;
            break;
        }
        if(a>='A'&&a<='Z')
        cout << "大写字母" <<endl;
        else if(a>='a'&&a<='z')
        cout << "小写字母" <<endl;
        else if(a>='0'&&a<='9')
        cout << "数字" <<endl;
        else
        cout << "符号" << endl;
    }
    return 0;
}

3.命名空间:

cpp 复制代码
#include <iostream>
using namespace std;
namespace my_std {
int a=90;
char b='a';
struct c;
void hw();
namespace my_sonstd {
void ha();
}
}
void my_std::my_sonstd::ha()//定义自定义命名空间my_std中的自定义命名空间my_sonstd中的函数
{
    cout << "Hello World3!" << endl;
}
void my_std::hw()//定义自定义my_std命名空间中的函数
{
    cout << "Hello World2!" << endl;
}
void hh()//定义全局变量的函数
{
    cout << "Hello World1!" << endl;
}
//using namespace my_std;//在主函数上引用自定义的命名空间,主函数中的a,b均为自定义中的变量
int main()
{
    my_std::my_sonstd::ha();
    ::hh();//使用全局变量函数
    my_std::hw();//使用自定义命名空间中的函数
    cout << my_std::b <<endl;
    cout << my_std::a << endl;//应用自己定义的my_std命名空间中的变量a
    cout << "Hello World!" << endl;
    return 0;
}

4.字符串c语言风格转c++风格

cpp 复制代码
#include <iostream>
#include <cstring>
using namespace std;
using INT =int;// 相当于typedef
int main()
{
    INT a=100;
    cout << a << endl;
    cout <<sizeof(INT) << endl;
    cout << "Hello World!" << endl;
    char str[]="helllo";
    string str1=str;
    string str2="word";
    string str3(5,'a');
    string str4("niaho");
    cout << str << endl;
    cout << str1 << endl;
    cout << str2 << endl;
    cout << str3 << endl;
    cout << str4 << endl;
    return 0;
}

5.判断输入字符串有几个空格,几个字母,几个数字

cpp 复制代码
#include <iostream>

using namespace std;

int main()
{
    string str;
    getline(cin,str);
    int count_number=0;
    int count_char=0;
    int count=0;
    for(int i=0;i<(int)str.size();i++)
    {
       if(' '==str[i])
       {
           count++;
       }
       else if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z'))
       {
           count_char++;
       }
       else if(str[i]>='0'&&str[i]<='9')
           count_number++;
    }
    cout << "空格个数为: ";
    cout << count << endl;
    cout << "数字个数为: ";
    cout << count_number << endl;
    cout << "字母个数为: ";
    cout << count_char << endl;
    return 0;
}

6.字符串比较

cpp 复制代码
#include <iostream>
#include<cstring>
using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    string str="word";
    string str1="hi";
    cout <<sizeof(str)<<endl;
    cout << strlen(str.c_str())<<endl;//将c++风格的字符串str转换成c风格的字符数组
    cout <<sizeof(str.c_str())<<endl;
    if(str1.compare(str)>0)
        cout<< "str1大于str"<<endl;
    else if(0==str1.compare(str))
        cout<<"str1等于str"<<endl;
    else
        cout<<"str1小于str"<<endl;

    return 0;
}

7.字符串逆置

cpp 复制代码
#include <iostream>

using namespace std;

namespace Myspace {
string strrev(string str);
}
string Myspace::strrev(string str)
{
    int i=0;
    int len=str.length()-1;
    while(i<len)
    {
        char t=str[i];
        str[i]=str[len];
        str[len]=t;
        i++;len--;
    }
    return str;
}
int main()
{
    string str;
    getline(cin,str);
    cout << Myspace::strrev(str) << endl;
    return 0;
}
相关推荐
Json____17 分钟前
使用python的 FastApi框架开发图书管理系统-前后端分离项目分享
开发语言·python·fastapi·图书管理系统·图书·项目练习
运器12336 分钟前
【一起来学AI大模型】支持向量机(SVM):核心算法深度解析
大数据·人工智能·算法·机器学习·支持向量机·ai·ai编程
人生在勤,不索何获-白大侠39 分钟前
day16——Java集合进阶(Collection、List、Set)
java·开发语言
Zedthm1 小时前
LeetCode1004. 最大连续1的个数 III
java·算法·leetcode
LIN-JUN-WEI1 小时前
[ESP32]VSCODE+ESP-IDF环境搭建及blink例程尝试(win10 win11均配置成功)
c语言·开发语言·ide·vscode·单片机·学习·编辑器
神的孩子都在歌唱1 小时前
3423. 循环数组中相邻元素的最大差值 — day97
java·数据结构·算法
YuTaoShao1 小时前
【LeetCode 热题 100】73. 矩阵置零——(解法一)空间复杂度 O(M + N)
算法·leetcode·矩阵
望获linux2 小时前
【Linux基础知识系列】第四十三篇 - 基础正则表达式与 grep/sed
linux·运维·服务器·开发语言·前端·操作系统·嵌入式软件
眠りたいです2 小时前
Mysql常用内置函数,复合查询及内外连接
linux·数据库·c++·mysql
dying_man2 小时前
LeetCode--42.接雨水
算法·leetcode