明解C++(1-2章)

柴田望洋 著,孙巍 译。 京东: https://item.jd.com/10041710736716.html

第 1章 在画面上输出和从键盘输入

用include预处理语句引入头文件:相当于包含了头文件的内容。

using namespace xxx 语句是为人类创造的,为的是偷懒省略输入xxx::

cout: character out,cin:character input,<<>> 中尖尖的一端代表stream的流向。

常量关键字:const

#include <iostream>

using namespace std;

int main() {
    cout << "计算圆的周长。请输入半径r:\n";
    const double PI = 3.14159265;
    double r;
    cin >> r;
    cout << "\n周长是:" << 2 * PI * r << endl;

    return 0;
}

字符与字符串类型:

  • char
  • string(需要引入库string)
  • cin读取字符或字符串变量
    • >>会跳过空格换行等空白字符
    • getline(cin,变量名) 会读入回车之前的所有输入, 如果用getline,就全部用getline,否则>> 的回车输入会作为getline的输入。
    • 在getline之前使用 cin.ignore() 清空输入缓冲区,避免影响下一次输入。
#include <iostream>
#include <string>
using namespace std;

int main() {
    char c;
    string s;

    cout << "input a char:";
    cin >> c;
    cout << c << endl;

    cin.ignore();//清空输入缓冲区,避免影响下一次输入。
    
    cout << "input a string:";
    getline(cin, s);

    cout << s << endl;
    return 0;
}

第2章 程序流的分支

bool类型: true,false

  • 整数0为false,其他值为true
  • 逻辑运算,==,!,与 &&,或 ||, 条件运算符 x?y:z
  • 空语句 ;

求余数: 整数用 %,实数用fmod(a,b), fmod 需要cmath库

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

int main() {
    cout << 10 % 3 << endl; //1
    cout << fmod(10.5,3) << endl; //1.5
    cout << fmod(-10.5, 3) << endl; //-1.5
    return 0;
}

switch 开关语句:

  • 变量必须为整数或枚举类型,太沙雕

  • case 标签:表达式或语句块;break; 标签必须为常量

  • default: 没有匹配时的动作

  • 可以用if … else if … , 或循环替代

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

int main() {
    cout << "input a color name in lower case:" << endl;
    string color_name[3] = { {"red"},{"blue"},{"blue"}},input_color; // 字符串数组书写是真的丑
    getline(cin, input_color);
    int have_color = 0;

    for (int i = 0; i < 3; i++) {
    	if (color_name[i] == input_color) {
    		cout << "color "<<input_color<<" in the database\n";
    		have_color = 1;
    		break;
    	}
    }
    if (!have_color)cout << "color " << input_color << " not in the database\n";

    return 0;
}

c++关键字

部分关键字:

<p>明解C++(1-2章)</p>

数据类型,类型修饰符及布尔常量(15个)

数据类型: void,bool, int,char, char16_t , char32_t, wchar_t , float,double
类型修饰符:short, long,signed,unsigned
布尔常量: true, false

bool(布尔)类型,C++ 中的基本数据结构,其值可选为 true(真)或者 false(假)。C++ 中的 bool 类型可以和 int 混用,具体来说就是 0 代表 false,非 0 代表 true。bool 类型常用于条件判断和函数返回值。

字符类型参考:https://learn.microsoft.com/zh-cn/cpp/cpp/char-wchar-t-char16-t-char32-t?view=msvc-170

char(字符,character)类型,C++ 中的基本数据结构,其值一般为 0~255 的 int。这 256 个字符对应着 256 个 ASCII 码。char 类型的数据需要用单引号 括起来。

控制语句(12个)

循环控制:for,while,do
分支结构: if,else,switch,case,default
跳转控制:break,continue,return,goto

运算符(12个)

and, or, not, and_eq,not_eq,or_eq,xor,xor_eq ,compl, bitand,bitor, sizeof

类型定义(5个)

struct,union,enum,class,typedef

类访问修饰符

private,protected,public

类型限定符

const,volatile,restrict

访问限定符

this,friend,virtual,mutable,explicit,operator

命名空间

namespace,using

异常处理

throw,try,catch

模板

template,typename

内存管理

new, delete

存储说明符:

auto,register,static,extern,thread_local,mutable

部分关键字的替代表示

<p>明解C++(1-2章)</p>

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

int main() {
    //普通闰年:公历年份是4的倍数,且不是100的倍数的,为闰年(如2004年、2020年等就是闰年)。
    //世纪闰年:公历年份是整百数的,必须是400的倍数才是闰年(如1900年不是闰年,2000年是闰年)
    cout << "请输入一个年份:" << endl;
    int year;
    cin >> year;
    if ((year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)) cout << year << "是闰年";
    else cout << year << "不是闰年";

    return 0;
}

C++ 中的运算符

<p>明解C++(1-2章)</p>

<p>明解C++(1-2章)</p>

正文完
 
评论(没有评论)