概述
1,变量类型(常用)
C++中有
a,字符型char占1字节。
b,整型 short(2),int(4),long(linux系统中为4,windows中为8),long long(8);
c,浮点型 float(4),double(8)。且他们在输出时默认为6为有效数字
d,布尔型 bool(1),只有两个值true ,false;
#include<iostream>
using namespace std;
int main()
{
bool flag;
flag = true;
cout<<"flag="<<flag<<endl;
flag = false;
cout<<"flag="<<flag<<endl;
return 0;
}
e,字符串型 string(需要包含#include头文件);char str[] = “”;注意在C语言中可以这样写char *str = “hello c”;
在C++中不能这样使用会报警告
#include<iostream>
#include<string>
using namespace std;
int main()
{
char str[] = "hello C++";
cout<<str<<endl;
string str1 = "hello C++";
cout<<str1<<endl;
char *str2 = "hello C++";//C++ forbid to doing this opeartion;
cout<<str2<<endl;
return 0;
}
2,C++中输出和输入与c语言最大的区别就是不需要占位符
cout<<" "<<str<<endl;
" "里面的东西原样输出,str为变量;
#include<iostream>
#include<string>
using namespace std;
int main()
{
int zx=0;
char zf = 'a';
float fd = 3.1415;
bool flag = false;
string str = "hello";
cout<<"please input zx"<<endl;
cin>>zx;
cout<<"zx="<<zx<<endl;
cout<<"please input zf"<<endl;
cin>>zf;
cout<<"zf="<<zf<<endl;
cout<<"please input fd"<<endl;
cin>>fd;
cout<<"fd="<<fd<<endl;
cout<<"please input flag"<<endl;
cin>>flag;
cout<<"flag="<<flag<<endl;
cout<<"please input str"<<endl;
cin>>str;
cout<<"str="<<str<<endl;
return 0;
}
3,运算符
在C++中的运算符都和C中一样。注意取模运算不能让两个小数取模。
4,条件结构,循环结构,顺序结构
条件结构:if()else()
#include<iostream>
using namespace std;
int main()
{
while(1){
int score=0;
cout<<"please input score"<<endl;
cin>>score;
if(score>=600){
if(score>=650){
cout<<"perfect"<<endl;
}
else{
cout<<"nice"<<endl;
}
}
else if(score>=500){
cout<<"good"<<endl;
}
else{
cout<<"bad"<<endl;
}
}
return 0;
}
循环结构do{}while();for(){}
顺序结构switch() {
case 0:
break;
default:
break
}
注意:switch条件只能是字符和数字
5,三目运算符
#include<iostream>
using namespace std;
int main()
{
int a=0;
int b = 20;
int c = 30;
cout<<"c="<<c<<endl;
a>b?b:c = 100;
cout<<"c = "<<c<<endl;
a=a>b?b:c;
cout<<"a = "<<a<<endl;
return 0;
}
阶段小游戏猜数字
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
int vrand;
int getkey;
int count = 10;
srand((unsigned int)time(NULL));
vrand = rand()%100+1;
while(count){
cout<<"please input your num"<<endl;
cin>>getkey;
if(getkey > vrand){
cout<<"max"<<endl;
}
else if(getkey < vrand){
cout<<"min"<<endl;
}
else if(getkey == vrand){
cout<<"congradulation"<<endl;
break;
}
count--;
}
return 0;
}
6,break;continue;goto
break:直接退出此次循环;
continue:不在执行continue这个循环以后的程序,直接进行下次循环;
goto:直接跳到标志位
7,冒泡排序
#include<iostream>
using namespace std;
int main()
{
int array[]={5,8,9,4,6,3,7,1};
int i = 0;
int cnt = sizeof(array)/sizeof(int);
int first=0;
int count=cnt;
cout<<"correct"<<endl;
for(int n=0;n<cnt;n++)
{
cout<<"array:"<<array[n]<<" ";
}
for(i=0;i<cnt;i++)
{
for(int j=0;j<cnt-i-1;j++)
{
if(array[j]>array[j+1])
{
int temp =array[j];
array[j] = array[j+1];
array[j+1]=temp;
}
}
}
cout<<"correct"<<endl;
cnt = sizeof(array)/sizeof(int);
for(int n=0;n<cnt;n++)
{
cout<<"array:"<<array[n]<<" ";
}
return 0;
}``
8,二维数组
#include<iostream>
#include <stdio.h>
using namespace std;
int main()
{
int array[3][3]={1,2,3,4,5,6,7,8,9};
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<array[i][j];
}
putchar('n');
}
cout<<"array size= "<<sizeof(array)<<endl;
cout<<"array one hang= "<<sizeof(array[0])<<endl;
cout<<"array elemental size= "<<sizeof(array[0][0])<<endl;
cout<<"what array lie= "<<sizeof(array[0])/sizeof(array[0][0])<<endl;
cout<<"what array hang= "<<sizeof(array)/sizeof(array[0])<<endl;
cout<<"array address= "<<array<<endl;
cout<<"array[0] address= "<<array[0]<<endl;
cout<<"array[1] address= "<<array[1]<<endl;
cout<<"array[2] address= "<<array[2]<<endl;
cout<<"array[0][0] address= "<<&array[0][0]<<endl;
cout<<"array[0][1] address= "<<&array[0][1]<<endl;
return 0;
}
最后
以上就是平常早晨为你收集整理的C++基础语法学习1的全部内容,希望文章能够帮你解决C++基础语法学习1所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复