博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++ 4-随机数字、goto语句、选择、循环结构、三目运算符 code sample
阅读量:3576 次
发布时间:2019-05-20

本文共 3831 字,大约阅读时间需要 12 分钟。

#include 
#include
#include
#include
#include
#define random(x) rand()%xusing namespace std;int main(int argc, char const *argv[]){//顺序结构;程序按顺序执行,不发生跳转 int A = 40; A++; A *= A; cout << A << endl;//选择结构;依据条件是否满足,有选择的执行相应功能 int score = 0; cout << "please input score:" << endl; cin >> score; cout << "the score is: " << score << endl; //if语句 //注意事项,在if判断语句后面,不要加分号 if (score > 600) { cout << "you are very perfect" << endl; if (score > 700 && 1==1) //与操作 { cout << "on my god" << endl; } } else if (score > 500) { cout << "you are very good" << endl; } else if (score > 400) { cout << "you are only qualified" << endl; } else { cout << "you need to more time to study" << endl; } //三目运算符 /* 语法:表达式1 ? 表达式2 :表达式3 解释: 如果表达式1的值为真,执行表达式2,并返回表达式2的结果; 如果表达式1的值为假,执行表达式3,并返回表达式3的结果。 */ int a = 10; int b = 20; int c = 0; c = a > b ? a : b; cout << "c = " << c << endl; //C++中三目运算符返回的是变量,可以继续赋值 (a > b ? a : b) = 100; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl; //switch语句 /* 注意1:switch语句中表达式类型只能是整型或者字符型 注意2:case里如果没有break,那么程序会一直向下执行 总结:与if语句比,对于多条件判断时,switch的结构清晰,执行效率高,缺点是switch不可以判断区间 */ //请给电影评分 //10 ~ 9 经典 // 8 ~ 7 非常好 // 6 ~ 5 一般 // 5分以下 烂片 int film_score = 0; cout << "please input score of the film " << endl; cin >> film_score; switch (film_score) { case 10: case 9: cout << "classic" << endl; break; case 8: cout << "very nice" << endl; break; case 7: case 6: cout << "commonly" << endl; break; default: cout << "Rotten film" << endl; break; }//循环结构;依据条件是否满足,循环多次执行某段代码 /* 语法:while(循环条件){ 循环语句 } 解释:只要循环条件的结果为真,就执行循环语句 */ // c++ 产生随机数 // C++中没有自带的random函数,要实现随机数的生成就需要使用rand()和srand()。 // rand()会返回一随机数值, 范围在0至RAND_MAX 间。RAND_MAX定义在stdlib.h, 其值为2147483647。 // 不过,由于rand()的内部实现是用线性同余法做的,所以生成的并不是真正的随机数,而是在一定范围内可看为随机的伪随机数。 /* srand()可用来设置rand()产生随机数时的随机数种子。通过设置不同的种子,我们可以获取不同的随机数序列。 可以利用srand((int)(time(NULL))的方法,利用系统时钟,产生不同的随机数种子。不过要调用time(),需要加入头文件< ctime >。 */ cout << rand()%100 << "\tthe one " << endl; // 0-100内随机数,但是下一次执行还是该结果; cout << rand()%100 << "\tthe two " << endl; srand((int)time(0)); // 产生随机种子 把0换成NULL也行 for (int i = 0; i < 10; i++) { cout << random(100) << " "; } cout << rand()%100 << endl; /* 产生一定范围随机数的通用表示公式是: 要取得[0,n) 就是rand()%n 表示 从0到n-1的数 要取得[a,b)的随机整数,使用(rand() % (b-a))+ a; 要取得[a,b]的随机整数,使用(rand() % (b-a+1))+ a; 要取得(a,b]的随机整数,使用(rand() % (b-a))+ a + 1; 通用公式:a + rand() % n;其中的a是起始值,n是整数的范围。 要取得a到b之间的随机整数,另一种表示:a + (int)b * rand() / (RAND_MAX + 1)。 要取得0~1之间的浮点数,可以使用rand() / double(RAND_MAX)。 */ cout << "play a game,hahaha" << endl; while (1==1){ int random_shuzi = random(100); int input_shuzi = -1; cout << "Please guess the number: "; cin >> input_shuzi; if (input_shuzi==random_shuzi){ cout << "bingo" << endl; break; } else if (input_shuzi==-1){ cout << "you will quit"; break; } else{ continue; } } int while_num = 0; while (while_num < 10){ cout << while_num << endl; while_num ++; } /* do…while循环语句 作用: 满足循环条件,执行循环语句 语法: do{ 循环语句 } while(循环条件); **注意:**与while的区别在于do…while会先执行一次循环语句,再判断循环条件 */ int num = 0; do { cout << num << endl; num++; } while (num < 10); //嵌套for循环 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (j == 5) { break; } cout << "*" << " "; } cout << endl; } //goto语句,如果标记的名称存在,执行到goto语句时,会跳转到标记的位置 cout << "1" << endl; goto FLAG; cout << "2" << endl; cout << "3" << endl; cout << "4" << endl; FLAG: cout << "5" << endl; system("pause"); return 0;}

转载地址:http://cuagj.baihongyu.com/

你可能感兴趣的文章
N10-sql注入(information_schema注入)
查看>>
N1-Kali虚拟机中SQLmap
查看>>
N11-sql注入(http头注入)
查看>>
N2-sqlmap初使用
查看>>
N12-sql盲注原理以及boolean盲注案例实现
查看>>
N13-sqli盲注 基于时间型
查看>>
N1 技术心得 2019-6-26
查看>>
N1-环境配置
查看>>
N2-审计方法与步骤
查看>>
N3-常见的INI配置
查看>>
代码审计 N4 常见危险函数和特殊函数(一)
查看>>
MySQL笔记
查看>>
计算机运算方法之(原码 补码 反码 移码)
查看>>
计算机组成原理之(二进制与十进制互相转换,数的定点表示与浮点数表示)例题:设浮点数字长16位,其中阶码5位(含有1位阶符),尾数11位(含有1位数符)
查看>>
选择排序(java代码实现)
查看>>
插入排序
查看>>
哈夫曼树java代码实现
查看>>
快速排序
查看>>
vue路由高亮的两种方式
查看>>
vue router 报错: Uncaught (in promise) NavigationDuplicated {_name:""NavigationDuplicated"... 的解决方法
查看>>