写写C++换换脑子:
变量与常量:
#include <iostream>
using namespace std;
#define Day 7
int main() {
//变量类型 变量名=变量初始值
//数据类型:整型 int short long longlong (所占字节不同)
int a = 10;
cout << "a =" << a << endl;
/*常量:不可修改
* 宏常量:#define
* 修饰: const 修饰的变量
*/
cout << "一周" <<Day << "天" << endl;
const int year = 12;
cout << "一年" << year << "月" << endl;
//sizeof关键字,统计数据类型(或变量)所占内存空间
cout << "a所占内存空间:" << sizeof(a) << endl;
/*浮点型:
单精度float f
双精度double 默认双精度
*/
float f = 3.14f;
cout << "f=" << f << endl;
double f1 = 3.14;
cout << "f1=" << f1 << endl;
//科学计数法
float f2 = 3e2;
cout << "f2=" << f2 << endl;
/*
字符型:char ch = 'a'
单引号,其中只能有一个字符
*/
char ch = 'a';
cout << ch << endl;
//对应的ASCII编码
cout << (int)ch << endl; // a-97 A -65
/*
转义字符: \n \t \v \\
*/
cout << "hello world " << endl;
cout << " hello world \n";
cout << "\\" << endl;
//制表符 \t
cout << "aaa\thello world" << endl;
cout << "aaaaa\thello world" << endl;
cout << "aa\thello world" << endl;
/*
字符串:char 变量名[] = "字符串值"
*/
char str1[] = "hello world";
cout << str1 << endl;
string str2 = "hello ";
cout << str2 << endl;
//bool类型 true false 非0 值都代表真
bool flag = true; //1
cout << flag << endl;
flag = false; //0
cout << flag << endl;
//数据输入: cin >> 变量
cout << "输入变量:" ;
cin >> a;
cout << a+10 << endl;
system("pause");
return 0;
}
运算符:
#include<iostream>
using namespace std;
int main() {
int a;
int b;
cout << "输入变量"<<endl;
cin >> a;
cin >> b;
cout << "a+b=" << a + b << endl;
cout << "a*b=" << a * b << endl;
cout << "a-b=" << a - b << endl;
cout << "a/b=" << a / b << endl;
cout << "取模(取余)a % b = " << a % b << endl; //两个小数不能做取模运算
a = 10;
b = ++a; //前置递增
cout << a << endl;
cout << b << endl;
a = 10;
b = a++; //后置递增
cout << a << endl;
cout << b << endl;
a = 10;
b = --a; //前置递减
cout << a << endl;
cout << b << endl;
a = 10;
b = a--; //后置递减
cout << a << endl;
cout << b << endl;
//赋值运算符
a = 10;
a += 2;
cout << a << endl;
a = 10;
a -= 2;
cout << a << endl;
a = 10;
a *= 2;
cout << a << endl;
a = 10;
a /= 2;
cout << a << endl;
a = 10;
a %= 2;
cout << a << endl;
/*
比较运算符:
== != < > <= >=
满足表达式输出1(True)
不满足输出0(False)
逻辑运算符:
! 非 a为真,!a为假
&& 与 a和b同时为真则为真
|| 或 a和b有一个为真则为真,都为假时结果为假
*/
system("pause");
return 0;
}
条件选择语句:
#include<iostream>
using namespace std;
/*
选择结构:
if语句:
单行
if(条件){满足条件执行的代码}
多行
if(条件){满足条件执行的代码}
else{不满足条件执行的代码}
多条件
if(条件1){满足条件1执行的代码}if(条件2){满足条件2执行的代码}....else{不满足以上条件执行的代码}
嵌套if语句
三目运算符
switch语句
*/
int main() {
//单行if语句
int score;
cout << "输入分数:";
cin >> score;
if (score >= 600) {
cout << "成绩为:" << score << endl;
}
//多行if语句
cout << "输入分数:";
cin >> score;
if (score >= 600)
{
cout << "成绩为:" << score << endl;
}
else
{
cout << "成绩不足600" << endl;
}
//多条件if语句
cout << "输入分数:";
cin >> score;
if (score >= 600)
{
cout << "成绩大于600" << endl;
}
else if (400 <= score < 600)
{
if (score >= 500)
{
cout << "成绩大于500" << endl;
}
else if (400 <= score)
{
cout << "成绩大于400 小于500" << endl;
}
}
else
{
cout << "成绩小于400" << endl;
}
//三目运算符,返回的是变量可以继续赋值
int a = 10;
int b = 20;
int c;
c = (a > b ? a : b);
cout << "c=" << c << endl;
(a > b ? a : b) = 100;
cout << "a= "<< a << endl;
cout << "b=" << b << endl;
//switch语句 判断条件只能为整形和字符型
cout << "输入分数:";
cin >> score;
switch (score)
{
case 600:
cout << "满分" << endl;
break;
default:
cout << "成绩合格" << endl;
break;
}
system("pause");
return 0;
}
循环结构语句:
#include<iostream>
using namespace std;
#include<ctime>
int main()
{
//while循环语句
//猜数字
srand((unsigned int)time(NULL)); //随机数种子
int num = rand() % 100 + 1; //1-100随机数
int val;
cout << "输入你猜的数:";
int a = 0;
while (a<5)
{
cin >> val;
if (val < num)
{
cout << "过小" << endl;
}
else if (val > num)
{
cout << "过大" << endl;
}
else if (val == num)
{
cout << "猜对了" << endl;
break;
}
a ++;
if (a == 5)
{
cout << "游戏结束" << endl;
}
}
//do...while 循环语句 do{循环语句} while(循环条件)
int num = 100;
do
{
int a; int b; int c;
a = num / 100;
b = num / 10 % 10;
c = num % 10;
if ( a * a* a + b * b * b + c * c * c == num)
{
cout <<"水仙花数:" << num << endl;
}
num++;
} while (num < 1000);
//for 循环语句 for(起始表达式;条件表达式;末尾循环体){循环语句;}
//敲桌子游戏
for (int i = 1; i <= 100; i++)
{
if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
{
cout << "敲桌子" << endl;
}
else
{
cout << i << endl;
}
}
//乘法口诀表
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
{
cout << j << "*" << i << "=" << i * j<<" ";
}
cout << endl;
}
/*跳转语句:
break
continue
goto:
goto 标记;
标记:
*/
system("pause");
return 0;
}