zoukankan      html  css  js  c++  java
  • C++ 预备知识#关于C++

    1.杂

    • 计算机语言要处理概念——数据和算法。数据是程序使用和处理的信息,而算法是程序使用的方法。
    • 过程性编程是试图使问题满足语言的过程性方法,强调的是算法方面,而OOP试图让语言来满足问题的需求,其理念是设计与问题的本质特性相对应的数据格式。
    • 在C++中,类是一种规范,它描述了新型的数据格式,对象是根据这种规范构造的特定数据结构。
    • 可移植相关:硬件和语言上的差异。

    2.一个示例程序

    //displays a mesage
    #include<iostream>
    int main()
    {
        using namespace std;
        cout<<"Come up and C++ me some time.";
        cout<<endl;
        cout<<"You won't regret it!"<<endl;
        return 0;
    }
    View Code

    3.进入C++

    (1)int main(void) 描述的是main()和操作系统之间的接口。关键字void指出函数不接受任何参数,在C++中让括号空着与使用void完全等效,在C中,让括号空着意味着对是否接收参数表示沉默。

    (2)iostream中的io指的是输入(cin)和输出(cout).

    (3)关于using namespace std;//命名空间。类、函数和变量是C++编译器的标准组件,他们都被放在名称空间std中。

    (4)cout将一个字符串插入到输出流中。endl;//表示重起一行。

    (5)C++风格:

    • 每行一条语句。
    • 每个函数都有一个开始花括号和一个结束花括号,这两个花括号各占一行。
    • 函数中的语句都相对于花括号进行缩进。
    • 与函数名称相关的圆括号周围没有空白。

    4.C++语句

    C++语句
    //提供两种语句,声明语句创建变量、赋值语句给变量赋值
    //重新认识cout的智能
    #include<iostream>
    int main()
    {
        using namespace std;
    
        int carrots;
        carrots=25;
    
        cout<<"I have";
        cout<<carrots;//打印carrots的当前值25.
        cout<<"carrots.";
        cout<<endl;
        carrots=carrots-1;
        cout<<"Crunch,crunch,Now I have "<<carrots<<" corrots."<<endl;
        return 0;
    }
    C++语句

    (1)在C++中,所有变量都必须声明,定义声明导致编译器为变量分配内存空间,相应的还有引用声明。
    (2)cout和cin的智能化,请注意cout<<"n";和cout<<n;的区别。cin使用>>操作符从收入流中抽取字符。cout<<"n"<<"b";(cout可以使用<<进行拼接).

    (3)类描述了一种数据类型的全部属性,对象是根据这些描述创建的实体。

    //引入cin
    #include<iostream>
    int main()
    {
        using namespace std;
        int carrots;
        cout<<"How many carrots do you have ?";
        cin>>carrots;       //c++input
        cout<<"Here are two more.";
        carrots=carrots+2;
        cout<<"Now you have"<<carrots<<"carrots."<<endl;//使用<<作为连接连续输出
        return 0;
    }
    View Code

    5.函数简介

    使用sqrt()函数
    //使用sqrt()函数
    #include<iostream>
    #include<cmath>//提供sqrt()函数原型
    int main()
    {
        using namespace std;
        double area;
        cout<<"Enter the floor area,in square feet,of your home:";
        cin>>area;
        double side;//允许在任何地方声明新变量
        side=sqrt(area);
        cout<<"That's the equivalent of a square 39.1918"
            <<"feet to side."<<endl;
        cout<<"How fascinating!"<<endl;
        cout<<side<<endl;//输出结果39.1918
        //cout<<side<<endl;??
        return 0;
    }
    使用sqrt()函数

    (1)有返回值的函数(使用return关键字来提供返回值)和没有返回值的函数。不返回值的函数,不能将该函数的调用放在赋值语句或其他表达式中。

    (2)对于有返回值的函数,可以认为函数执行完毕后,语句中的函数调用部分将被替换为返回值。

    (3)对于C++库中的每个函数,都在一个或多个头文件中提供了其原型。原型只描述函数接口,库文件中包含了函数的编译代码,而头文件则包含函数原型。

    (4)C+=允许在程序的任何地方声明新的变量。

    (5)C++不允许将函数定义嵌套在另一个函数定义中。每个函数都是独立的,所有函数的创建都是平等的。

    (6)main()函数的返回值并不是返回给程序的其它部分,而是被返回给操作系统。

    (7)函数原型描述了函数接口(函数如何与程序的其它部分交互),参数列表指出了何种信息将被传递给参数,函数类型指出了返回值的类型。

    调用函数
    //调用函数
    #include<iostream>
    void simon(int);//声明函数
    
    int main()
    {
        using namespace std;
        simon(3);//调用函数
        cout<<"Pick an integer:";
        int count;
        cin>>count;
        simon(count);
        cout<<"Done!"<<endl;
        return 0;
    }
    
    void simon(int n)//函数定义部分
    {
        using namespace std;
        cout<<"Simon says touch your toes "<< n <<" times."<<endl;
    }//函数不需要返回语句
    调用函数

    (8)一般地,只让需要访问名称空间的函数访问它是更好的选择。

    //用户定义的有返回值的函数
    #include<iostream>
    int stonetolb(int);//函数声明
    
    int main()
    {
        using namespace std;
        int stone;
        cout<<"Enter the weight in stone:";
        cin>>stone;
        int pounds=stonetolb(stone);//把stone作为函数的参数
        cout<<stone<<" stone =";
        cout<<pounds<<" pounds."<<endl;
        return 0;
    }
    
    int stonetolb(int n)
    {
        return 14*n;//函数的返回值
    }
    用户定义的有返回值的函数
  • 相关阅读:
    LeetCode153 Find Minimum in Rotated Sorted Array. LeetCode162 Find Peak Element
    LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word
    LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number
    LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four
    LeetCode225 Implement Stack using Queues
    LeetCode150 Evaluate Reverse Polish Notation
    LeetCode125 Valid Palindrome
    LeetCode128 Longest Consecutive Sequence
    LeetCode124 Binary Tree Maximum Path Sum
    LeetCode123 Best Time to Buy and Sell Stock III
  • 原文地址:https://www.cnblogs.com/dondre/p/4089759.html
Copyright © 2011-2022 走看看