C++ primer复习(一、输入与输出)

1.1、hello world程序

#include <iostream>
#include <windows.h>
int main()
{
    
    
    std::cout<<"hello world!";
    system("pause");
    return 0;
}

1.2、初识输入输出

iostream库包含两个基础类型,istream(输入流)、ostrean(输出流)。
istream:cin
ostream:cout、cerr(输出错误和警告信息)、clog(输出程序运行的一般性信息)。
注意我只是写了给例子,但是cerr和clog不是用来打印一般信息的。

#include <iostream>
#include <windows.h>
int main()
{
    
    
    int a, b;
    std::cout<<"请输入a,b其中a需要比b大";
    std::cin>>a>>b;
    if(a <= b) std::cerr<<"a小于等于b";
    else std::cout<<"a大于b";
    std::clog<<"程序运行完成";
    system("pause");
    return 0;
}

操作符的特殊值endl:打印endl给人看起来的感觉是换行,其实是结束当前行,把与设备关联的缓冲区(数据临时存储区)中的内容刷到设备中。(endl也是在std命名空间中)
大家可以看到,我上面的输入输出流操作都用了一个std::,其可以帮我们说明我们用的cin和cout是定义在std命名空间中,可以用using namespace std;表示我们用的名都来自std命名空间。

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    
    
    cout<<"今天是周日"<<endl;
    cout<<"明天要上健美操"<<endl;
    system("pause");
    return 0;
}

1.3、注释简介

注释一行://内容
注释多行:/* 内容*/

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    
    
    cout<<"今天是周日"<<endl;//可以多睡一会
    cout<<"明天要上健美操"<<endl;
    /*
    太难了
    */
    system("pause");
    return 0;
}

1.4、控制流

while的使用

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    
    
    int a = 0;
    string answer1,answer2;
    while(a < 10){
    
    
        cout<<"今天还喜欢她吗"<<endl;
        cin>>answer1;
        if(answer1 == "不") break;//结束了
        cout<<"是否过了一天"<<endl;
        cin>>answer1;
        if(answer1 == "不") continue;//还继续爱她但是今天没过
        a++;
        cout <<"这是爱你的第"<<a<<"天"<<endl;
    }
    system("pause");
    return 0;
}

for循环的运用

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    
    
    string answer1,answer2;
    for(int i = 0;i < 10;){
    
     //这里可以写成i = 0;i < 10;i++但是如果逻辑不适合在这里写,空出来就行。
        cout<<"今天还喜欢她吗"<<endl;
        cin>>answer1;
        if(answer1 == "不") break;//结束了
        cout<<"是否过了一天"<<endl;
        cin>>answer1;
        if(answer1 == "不") continue;//还继续爱她但是今天没过
        i++;
        cout <<"这是爱你的第"<<i<<"天"<<endl;
    }
    system("pause");
    return 0;
}

读取不定数量的输入数据(可以用ctrl+z结束)

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    
    
    int a;
    while(cin>>a){
    
    
        cout<<"输入的是"<<a<<endl;
    }
    system("pause");
    return 0;
}

if语句

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    
    
    int a;
    cin>>a;
    if(a > 0) cout<<"a大于0";
    else cout<<"a不大于0";
    system("pause");
    return 0;
}

1.5、类简介

比如,人是一个类,人有几个属性身高,体重等,人可以吃饭,学习等。行为可以作为函数存入类,属性可以作为变量存入类。
C++ primer的例子我很不喜欢,当时大二第一次看我也没看懂。我自己写了一个苹果的例子。

#include <iostream>
#include <windows.h>
using namespace std;
class apple{
    
    
public:
int wight;
string name;
apple(int w,string n){
    
    
    wight = w;
    name = n;
}
void can(){
    
    
    cout<<"eat"<<"重量"<<wight<<"品种"<<name<<endl;
}
};
int main()
{
    
       apple a(1,"红富士");
    a.can();
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45743162/article/details/115048609