
C++
今天写点啥
趁现在……
展开
-
C++:for语句总结
#include <iostream>using namespace std;int main(){ /* for循环语句:满足条件,执行循环 语法:for(起始表达式;条件表达式;末尾循环体){循环语句} */ int k = 0; for (; k < 20; k++) { cout<<"k= " << k << endl; } /* 注;for循环中的表达式,要用分号进行分隔 总结:while、do...while、f原创 2020-12-27 11:24:44 · 409 阅读 · 0 评论 -
C++:switch语句总结
#include <iostream>using namespace std;int main(){ /* switch语句:执行多条件分支语句 语法: switch(表达式) { case(结果1):执行语句;break; case(结果2):执行语句;break; case(结果3):执行语句;break; case(结果3):执行语句;break; } */ int score = 0; //打分 cout << "您想打的分数是:"原创 2020-12-27 11:23:00 · 707 阅读 · 0 评论 -
C++:比较运算符总结
#include <iostream>using namespace std;int main(){ int a = 20; int b = 10; cout << (a == b) << endl;//结果为假 cout << (a <= b) << endl;//结果为假 cout << (a >= b) << endl;//结果为真 cout << (a != b) <原创 2020-12-27 11:20:16 · 450 阅读 · 0 评论 -
C++:变量总结
#include <iostream>using namespace std;int main(){//变量存在的意义:方便进行内存的管理//变量创建的语法:数据类型 变量名 = 变量初始值; int a = 10; cout << "a = " << a << endl; system("pause"); return 0;}运行结果:...原创 2020-12-27 11:18:47 · 172 阅读 · 0 评论 -
C++:布尔类型总结
#include <iostream>using namespace std;int main(){ //创建bool数据类型 bool flag = true; cout << flag << endl; //布尔类型所占的内存空间大小 cout << "布尔类型所占的内存空间大小: "<<sizeof(flag) << endl; system("pause"); return 0;}运行结果:...原创 2020-12-27 11:17:24 · 168 阅读 · 0 评论 -
C++:常量总结
#include <iostream>//常量:用于记录程序中不可修改的数据//定义的方式://#define 宏常量:#define 常量名 常量值//通常定义在文件的上方,表示一个常量//const修饰的变量:const 数据类型 常量名 = 常量值//通常在变量前加关键字const,修饰该变量为常量,不可修改#define day 7 //宏常量using namespace std;int main(){ cout << "一周有:" <<原创 2020-12-27 11:16:00 · 133 阅读 · 0 评论 -
C++:程序流程结构
#include <iostream>using namespace std;int main(){ /* 顺序结构:程序按顺序执行,不发生跳转 选择结构:依据条件是否满足,有选择的执行相应的功能 循环结构:依据条件是否满足,循环多次执行某段代码 */ ////选择语句 单行if语句 ////用户输入分数,如果分数大于600分,视为考上一本大学,在屏幕上显示 ////用户输入分数 //int score = 0; //cout << "请输入你高考的分数原创 2020-12-27 11:14:34 · 150 阅读 · 0 评论 -
C++:赋值运算符总结
#include <iostream>using namespace std;int main(){ //先运算,再赋值 int a1 = 10; a1 += 2; cout << "a1=" << a1 << endl; //结果为12 int a2 = 10; a2 -= 2; cout << "a2=" << a2 << endl; //结果为8 int a3 = 10; a3 *=原创 2020-12-27 11:12:07 · 220 阅读 · 0 评论 -
C++:逻辑运算符总结
#include <iostream>using namespace std;int main(){ /* 逻辑运算符:用于根据表达式返回真值或假值 ! 非 如果a为假,则!a为真 && 与 如果a b都为真,则a&&b都为真,否则为假 || 或 如果a和b有一个为真,则结果为真,二者为假时,结果为假 */ //! 非 如果a为假,则!a为真 int a = 10; cout << !a <<原创 2020-12-26 14:10:58 · 688 阅读 · 0 评论 -
C++:嵌套语句总结
#include <iostream>using namespace std;int main(){ //请输入你的考试分数: int score = 0; cout << "请输入你的考试分数" << endl; cin >> score; //您输入的考试分数是: cout << "您输入的考试分数是:" << score << endl; //判断 if (score > 600) {原创 2020-12-26 14:09:27 · 604 阅读 · 0 评论 -
C++:三目运算总结
#include <iostream>using namespace std;int main(){ /* 三目运算:实现简单的判断 语法:表达式1?表达式2:表达式3 解释:如果表达式1的结果为真,则执行表达式2,并返回;否则执行表达式3,并返回 注:三目运算的返回值是一个变量,可以进行赋值操作 */ int a = 10; int b = 20; int c = 0; c = a > b ? a : b; cout << "c= " <&l原创 2020-12-26 14:07:46 · 479 阅读 · 0 评论 -
C++:数据的输入
#include <iostream>#include <string>using namespace std;int main(){ /* 数据的输入 作用:用于从键盘获取数据 关键字:cin 语法:cin >> 变量 */ int a = 0; cout << "请输入整形变量:" << endl; cin >> a; cout <<"您输入的a的值是: "<< a <<原创 2020-12-26 14:05:38 · 353 阅读 · 0 评论 -
C++:数据类型总结
#include<iostream>using namespace std;/*数据类型(不同类型占据的内存空间不同) int 占4个字节 short 占2个字节 long 占4个字节 long long 占8个字节*/int main(){ short num1 = 10; //短整型 int num2 = 10; //整型 long num3 = 10; //长整型 long long num4 =原创 2020-12-26 14:03:09 · 215 阅读 · 0 评论 -
C++:算术运算符总结
#include <iostream>using namespace std;int main(){ //加减乘除运算 int a1 = 10; int b1 = 20; cout << a1 + b1 << endl; cout << a1 - b1 << endl; cout << a1 * b1 << endl; cout << a1 / b1 << endl;//两个整数相原创 2020-12-26 14:00:49 · 481 阅读 · 0 评论 -
C++:循环语句总结
#include <iostream>using namespace std;int main(){ /* while语句:满足循环条件,执行循环语句 语法:while(循环条件){循环语句} 解释:只要循环条件为真,就会执行循环语句 */ int num = 1; while (num<10) { cout << "num= " << num << endl; num++; } system("pause"); re原创 2020-12-26 13:58:58 · 248 阅读 · 0 评论 -
C++:转义字符总结
#include <iostream>using namespace std;int main(){ //转义字符 //换行符 \n cout << "hello world\n"; //反斜杠 \\ cout << "\\"<<endl; //水平制表符 \t cout << "aaa\tbbb" << endl; system("pause"); return 0;}运行截图:...原创 2020-12-26 13:57:02 · 219 阅读 · 0 评论 -
C++:字符总结
#include <iostream>using namespace std;int main(){ char ch = 'a'; cout << ch << endl; cout << sizeof(ch) << endl; cout << int(ch) << endl; system("pause"); return 0;}/* 字符型变量用于显示单个字符 语法:char ch='a';原创 2020-12-26 13:54:22 · 175 阅读 · 0 评论 -
C++:字符串总结
#include <iostream>#include <string> //使用字符串时需要添加的头文件using namespace std;int main(){ /* 用于表示一串字符 C语言风格:char 变量名[]="字符串值" C++风格:string 变量名="字符串值" */ string str = "hello world"; cout << str << endl; system("pause"); retu原创 2020-12-26 13:51:21 · 145 阅读 · 0 评论 -
C++:基础知识总结
#include <iostream>using namespace std;void simon(int);int main(){ simon(3); cout << "pick an integer:"; int count; cin >> count; simon(count); cout << "done" << endl; return 0;}void simon(int n){ cout <<原创 2020-11-30 10:59:49 · 399 阅读 · 0 评论