格式化输入
#include <iostream>
#include<iomanip>
using namespace std;int main()
{double num=1090.123456;cout<<"num="<<num<<endl;cout<<oct<<"num="<<num<<endl;cout<<hex<<"num="<<num<<endl;cout<<dec<<"num="<<num<<endl;cout<<"******************************"<<endl;cout<<setbase(8)<<"num="<<num<<endl;cout<<setbase(16)<<"num="<<num<<endl;cout<<setbase(10)<<"num="<<num<<endl;cout<<"******************************"<<endl;cout<<setw(10)<<"num="<<num<<endl;cout<<setw(10)<<setfill('*')<<"num = "<<num<<endl;return 0;
}
c++斐波那契
#include <iostream>using namespace std;int main()
{int num1=1;int num2=1;int sum=0;cout<<num1<<endl;cout<<num2<<endl;for(int i=0;i<18;i++){sum=num1+num2;num1=num2;num2=sum;cout<<sum<<endl;}return 0;
}
大小写转换
#include <iostream>using namespace std;int main()
{char word;cout<<"输入一个字符"<<endl;cin>>word;if(word>=65&&word<=90){word=word+32;cout<<word<<endl;}else if(word>=97&&word<=122){word=word-32;cout<<word<<endl;}else{cout<<"*"<<endl;}return 0;
}
自定义命名空间
#include <iostream>using namespace std;namespace yh
{int num;double value;char key;void show(){cout<<"num="<<num<<"key="<<key<<"value="<<value<<endl;}
}
using yh::value;
int main()
{yh::num=666;value=3.14;using namespace yh;key='h';show();return 0;
}
判断字符串数字,字母,空格,符号的数量
#include <iostream>using namespace std;int main()
{string str1;cout<<"输入字符串"<<endl;getline(cin,str1);int len=0,num=0,word1=0,word2=0,sign1=0,space1=0;len=str1.size();for(int a=0;a<len;a++){if(str1[a]>='1'&&str1[a]<='9'){num++;}else if(str1[a]>='A'&&str1[a]<='Z'){word1++;}else if(str1[a]>='a'&&str1[a]<='z'){word2++;}else if(str1[a]==' '){space1++;}else{sign1++;}}cout<<"数字"<<num<<"大写字母"<<word1<<"小写字母"<<word2<<"空格"<<space1<<"符号"<<sign1<<endl;return 0;
}