当前位置: 首页 > news >正文

C++(8.21)c++初步

1.斐波那契:

#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;}elsea[i]=a[i-1]+a[i-2];cout <<setw(4) << left << a[i];}cout <<endl;return 0;
}

2.判断输入的符号

#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;elsecout << "符号" << endl;}return 0;
}

3.命名空间:

#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命名空间中的变量acout << "Hello World!" << endl;return 0;
}

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

#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.判断输入字符串有几个空格,几个字母,几个数字

#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.字符串比较

#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;elsecout<<"str1小于str"<<endl;return 0;
}

7.字符串逆置

#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;
}

 

 

http://www.lryc.cn/news/138672.html

相关文章:

  • 【【Verilog典型电路设计之log函数的Verilog HDL设计】】
  • 数字放大(C++)
  • FOC控制框架图
  • Spring工具类(获取bean,发布事件)
  • 腾讯云和阿里云服务器折扣对比_看看哪家划算?
  • GO语言中的Defer与Error异常报错详细教程
  • AP6315 DC单节锂电池充电IC 同步2A锂电芯片
  • PDF校对工具正式上线,为用户提供卓越的文档校对解决方案
  • WSL 配置 Oracle 19c 客户端
  • ChatGPT⼊门到精通(1):ChatGPT 是什么
  • idea启动正常,打成jar包时,启动报错
  • 软考高级系统架构设计师系列论文八十九:论软件需求分析方法和工具的选用
  • java八股文面试[JVM]——类加载器
  • CSS中如何实现元素之间的间距(Margin)合并效果?
  • 【实操干货】如何开始用Qt Widgets编程?(三)
  • 基于深度学习的图像风格迁移发展总结
  • 小程序页面间有哪些传递数据的方法?
  • bh002- Blazor hybrid / Maui 保存设置快速教程
  • 同源政策与CORS
  • 科技资讯|三星再申请智能戒指商标,智能穿戴进入更小型化发展
  • HarmonyOS开发第一步,熟知开发工具DevEco Studio
  • 【应急响应】Linux常用基础命令
  • 什么是Pytorch?
  • Baidu World 2023,定了!
  • ProxySQL+MGR高可用搭建
  • 【Unity小技巧】在Unity中实现类似书的功能(附git源码)
  • STM32设置为I2C从机模式(HAL库版本)
  • 牛客网Verilog刷题 | 入门特别版本
  • ROS通信机制之话题(Topics)的发布与订阅以及自定义消息的实现
  • 容灾设备系统组成,容灾备份系统组成包括哪些