啃掉C++:概念性基本代码:变练边学:输入输出、循环、条件语句、函数、类

输入输出

//这是一个关于输入输出的程序
//首先头文件要放在前面
#include<iostream>

int main(){
    std:: cout<<"Enter two numbers"<<std::endl;
    //输出运算符 <<
    //输出运算符左边的变量必须是一个ostream对象
    //输出运算符右边的就是要打印的值,这个运算符将要打印的值写入到ostream对象当中
    //语句中输出了两个运算符,第二个运算符返回了其左侧的对象
    /**std:: cout<<"Enter two numbers"<<std::endl;
     * 等价于:(std:: cout<<"Enter two numbers")<<std::endl;
     * std::cout<<"Enter two numbers";
     * std::cout<<std::endl;
     **/
    //第二个运算符打印endl,这是一个被称为操纵符的特殊值。写入endl的效果是结束当前行,并且将设备关联的缓冲区中的内容刷到设备中
    //前缀std::指出名字cout和endl是定义在名为std的命名空间namespace中的
    //命名空间可以帮助我们避免不经意的名字定义冲突
    //标准库定义的所有名字都在命名空间std中。


    int v1=0,v2=0;
    std::cin>>v1>>v2;
    /** 输入运算符 >>
     *  接受一个istream作为左侧运算对象,接受一个对象作为右侧运算对象。
     * 它从给定的istream读入数据,并且存入给定对象当中
     * 等价于(std::cin>>v1)>>v2;
     * std::cin>>v1;
     * std::cin>>v2;
     **/
    std::cout<<"The sum of "<<v1<<" and "<<v2<<" is "<<v1+v2<<std::endl;
    //上面都解释完了,就没有什么可以说的了
    
    system("pause");
    return 0;
}

随机数:

#include<iostream>
using namespace std;
#include<string>

int main(){
    int random=rand()%100+1000;
    cout<<random<<endl;

    system("pause");
    return 0;
}

while循环

//while 语句的用法
#include<iostream>
int main(){
    int sum=0,val=0;
    //括号里面是条件
    while(val<=10){
        //开始循环过程
        sum+=val;
        ++val;//前缀递增运算符 相当于val=val+1;
    }
    std::cout<<"sum of 1 to 10 inclusive is"<<sum<<std::endl;
    system("pause");
    return 0;
}

for循环

#include<iostream>
int main(){
    int sum=0;
    for (int val=0;val<=10;++val){
        sum+=val;
        std::cout<<"sum of 1 to 10 inclusive is"<<sum<<std::endl;
        
    }
    system("pause");
        return 0;
}

if 条件选择

//在一个不交叉的序列比如2 2 2 3 3 3这种
//此代码可以计算出这种序列中各个元素出现的次数

#include<iostream>
int main(){
    int currval=0,val=0;
    //currval是代表目前的数,val表示输入的数
    //先输入一个数表示有数据可以处理
    if (std:: cin>> currval){
        int cnt=1; //把这个输入的数记录为1
        while(std:: cin>>val){
            //在while循环中继续输入数值
            if (val==currval){
                ++cnt;
                //如果输入的数值和前面输入的数值一样,那么计数器加一
            }
            else{
                //如果不一样
                std::cout<<currval<<
                "  occurs "<<cnt<<" times"<<std::endl;
                //将会输入计数的值
                currval=val;
                //并且将当前的数开始计算个数并且计数器重置为1
                cnt=1;
            }

        }
        std::cout<<currval<<" occurs "<<cnt<<" times"<<std::endl;

    }
    return 0;
}

其他

#include<iostream>
int main(){
    int sum=0,value=0;
    //循环条件的求值就是执行这个表达式
    //如果有一个无效的输入,比如读入的值不是一个整数,那么istream对象的状态就会变味无效
    while(std::cin>>value){
        sum+=value;
        std::cout<<"The sum is"<<sum<<std::endl;
    }
    system("pause");
    return 0;
}

函数体系

最简单的加法函数

#include<iostream>
#include<string>
using namespace std;

int add(int a,int b){
    int sum;
    sum=a+b;
    return sum;
}
int main(){
    int a;
    int b;
    cin>>a>>b;
    cout<<add(a,b)<<endl;

    system("pause");
    return 0;
}

函数形参的改变不会影响实参

#include<iostream>
#include<string>
using namespace std;

void swap(int a,int b){
    int temp;
    temp=a;
    a=b;
    b=temp;
    cout<<a<<" "<<b<<endl;
}
int main(){
    int a;
    int b;
    cin>>a>>b;
    cout<<a<<" "<<b<<endl;
    swap(a,b);
    cout<<a<<" "<<b<<endl;

    system("pause");
    return 0;
}

函数声明:如果函数体写在main函数的后面,使用函数声明就可以调用该函数

#include<iostream>
#include<string>
using namespace std;
int swap(int a,int b);
//函数声明可以多次声明,定义只能有一次
int main(){
    int a;
    int b;
    cin>>a>>b;
    cout<<max(a,b)<<endl;


    system("pause");
    return 0;
}
int swap(int a,int b){
    return a>b? a:b;
}

函数的分文件编写

  • 创建后缀名为.h的头文件
  • 创建后缀名为.cpp的源文件
  • 在头文件里面写函数声明
  • 在源文件里面写函数的具体内容

头文件

#include<iostream>
using namespace std;


void swap(int a,int b);

源文件

#include"swap.h"
void swap(int a,int b){
    int temp;
    temp=a;
    a=b;
    b=temp;
    cout<<"a= "<<a<<"  b=  "<<b<<endl;
}

比如一个图书售卖系统

可以定义一个Sales_item类

使用 Sales_item item;

是想表达item是一个Sales_items类型的对象

#include<iostream>
#include "Sales_item.h"
int main(){
    Sales_item book;
    std:: cin >> book;
    std::cout<<book<<std::endl;
    return 0;
}

标准库的头文件使用尖括号<>表明
不标准的头文件使用双引号标明

成员函数

#include<iostream>
#include "Sales_item.h"
int main(){
    Sales_item item1,item2;
    std::cin>>item1>>item2;
    //检查item1和item2是不是表示相同的书
    if (item1.isbn()==item2.isbn()){
        //如果是相同的书就让他们相加
        std::cout<<item1+item2<<std::endl;
        return 0;//表示成功
    }
    else{
        std::cerr<<"Data must refer to same ISBN"<<std::endl;
        //cerr是输出错误的标准流信息,不需要经过缓冲,更加快速。
        
        return -1;//表示失败

    }
    return 0;
}

item1.isbn()==item2.isbn()

调用名字为isbn的成员函数。成员函数是定义为函数类的一部分
函数,有时也被叫做方法

调用的时候使用点 . 运算符

实例:书店程序

假如我有一个书店的库

#include<iostream>
#include "Sales_item.h"
//调用这个库
int main(){
    //调用创建一个total对象,确保有数据可以处理
    Sales_item total;
    if (std::cin>>total){
        //创建一个trans对象
        Sales_item trans;
        while (std::cin>>trans){
            if (total.isbn()==trans.isbn()){
                //如果书的号码相同,那么加上销量
                total+=trans;
            }
            else{
                //如果书的号码不同,输出上一个书的销量,
                //然后将销量的初始值变成当前另一本书的销量值
                std::cout<<total<<std::endl;
                total=trans;
            }
        }
        std::cout<<total<<std::endl;

    }
    else{
        //没有数据,那就拜拜
        std::cerr<<"No data?!"<<std::endl;
        return -1;
    }
    
    return 0;
}
posted @ 2021-04-22 19:43  Zeker62  阅读(44)  评论(0编辑  收藏  举报