Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Clark Lin
StudyCpp
Commits
a48a3dd7
Commit
a48a3dd7
authored
Nov 08, 2022
by
Administrator
Browse files
add 3 more scripts in Basic folder
parent
992778e3
Changes
3
Hide whitespace changes
Inline
Side-by-side
01.Basic/Operator.cpp
0 → 100644
View file @
a48a3dd7
#include <iostream>
#include <Windows.h>
using
namespace
std
;
int
main
()
{
// 解决控制台中文输出乱码
SetConsoleOutputCP
(
CP_UTF8
);
/****************************************
* 算术运算符
* **************************************/
int
a
=
21
;
int
b
=
10
;
int
c
;
c
=
a
+
b
;
cout
<<
"Line 1 - c 的值是 "
<<
c
<<
endl
;
c
=
a
-
b
;
cout
<<
"Line 2 - c 的值是 "
<<
c
<<
endl
;
c
=
a
*
b
;
cout
<<
"Line 3 - c 的值是 "
<<
c
<<
endl
;
c
=
a
/
b
;
cout
<<
"Line 4 - c 的值是 "
<<
c
<<
endl
;
c
=
a
%
b
;
cout
<<
"Line 5 - c 的值是 "
<<
c
<<
endl
;
c
=
a
++
;
cout
<<
"Line 6 - c 的值是 "
<<
c
<<
endl
;
c
=
a
--
;
cout
<<
"Line 7 - c 的值是 "
<<
c
<<
endl
;
/****************************************
* 关系运算符
* **************************************/
if
(
a
==
b
)
{
cout
<<
"Line 1 - a 等于 b"
<<
endl
;
}
else
{
cout
<<
"Line 1 - a 不等于 b"
<<
endl
;
}
if
(
a
<
b
)
{
cout
<<
"Line 2 - a 小于 b"
<<
endl
;
}
else
{
cout
<<
"Line 2 - a 不小于 b"
<<
endl
;
}
if
(
a
>
b
)
{
cout
<<
"Line 3 - a 大于 b"
<<
endl
;
}
else
{
cout
<<
"Line 3 - a 不大于 b"
<<
endl
;
}
/* 改变 a 和 b 的值 */
a
=
5
;
b
=
20
;
if
(
a
<=
b
)
{
cout
<<
"Line 4 - a 小于或等于 b"
<<
endl
;
}
if
(
b
>=
a
)
{
cout
<<
"Line 5 - b 大于或等于 a"
<<
endl
;
}
/****************************************
* 逻辑运算符
* **************************************/
a
=
5
;
b
=
20
;
if
(
a
&&
b
)
{
cout
<<
"Line 1 - 条件为真"
<<
endl
;
}
if
(
a
||
b
)
{
cout
<<
"Line 2 - 条件为真"
<<
endl
;
}
/* 改变 a 和 b 的值 */
a
=
0
;
b
=
10
;
if
(
a
&&
b
)
{
cout
<<
"Line 3 - 条件为真"
<<
endl
;
}
else
{
cout
<<
"Line 4 - 条件不为真"
<<
endl
;
}
if
(
!
(
a
&&
b
)
)
{
cout
<<
"Line 5 - 条件为真"
<<
endl
;
}
/****************************************
* 位运算符
* **************************************/
a
=
60
;
// 60 = 0011 1100
b
=
13
;
// 13 = 0000 1101
c
=
0
;
c
=
a
&
b
;
// 12 = 0000 1100
cout
<<
"Line 1 - c 的值是 "
<<
c
<<
endl
;
c
=
a
|
b
;
// 61 = 0011 1101
cout
<<
"Line 2 - c 的值是 "
<<
c
<<
endl
;
c
=
a
^
b
;
// 49 = 0011 0001
cout
<<
"Line 3 - c 的值是 "
<<
c
<<
endl
;
c
=
~
a
;
// -61 = 1100 0011
cout
<<
"Line 4 - c 的值是 "
<<
c
<<
endl
;
c
=
a
<<
2
;
// 240 = 1111 0000
cout
<<
"Line 5 - c 的值是 "
<<
c
<<
endl
;
c
=
a
>>
2
;
// 15 = 0000 1111
cout
<<
"Line 6 - c 的值是 "
<<
c
<<
endl
;
/****************************************
* 赋值运算符
* **************************************/
a
=
21
;
c
=
a
;
cout
<<
"Line 1 - = 运算符实例,c 的值 = : "
<<
c
<<
endl
;
c
+=
a
;
cout
<<
"Line 2 - += 运算符实例,c 的值 = : "
<<
c
<<
endl
;
c
-=
a
;
cout
<<
"Line 3 - -= 运算符实例,c 的值 = : "
<<
c
<<
endl
;
c
*=
a
;
cout
<<
"Line 4 - *= 运算符实例,c 的值 = : "
<<
c
<<
endl
;
c
/=
a
;
cout
<<
"Line 5 - /= 运算符实例,c 的值 = : "
<<
c
<<
endl
;
c
=
200
;
c
%=
a
;
cout
<<
"Line 6 - %= 运算符实例,c 的值 = : "
<<
c
<<
endl
;
// a = 11 = 0000 1011
c
<<=
2
;
// a = 44 = 0010 1100
cout
<<
"Line 7 - <<= 运算符实例,c 的值 = : "
<<
c
<<
endl
;
c
>>=
2
;
cout
<<
"Line 8 - >>= 运算符实例,c 的值 = : "
<<
c
<<
endl
;
c
&=
2
;
cout
<<
"Line 9 - &= 运算符实例,c 的值 = : "
<<
c
<<
endl
;
c
^=
2
;
cout
<<
"Line 10 - ^= 运算符实例,c 的值 = : "
<<
c
<<
endl
;
c
|=
2
;
cout
<<
"Line 11 - |= 运算符实例,c 的值 = : "
<<
c
<<
endl
;
return
0
;
}
\ No newline at end of file
01.Basic/VariableStorageDesc.cpp
0 → 100644
View file @
a48a3dd7
#include <iostream>
#include <Windows.h>
void
func
(
void
);
static
int
count
=
10
;
// 全局变量
int
count_in_main
;
// 定义一个变量,这个变量将被外部cpp程序引用
extern
void
write_extern
();
// 引用外部cpp程序的函数
int
main
()
{
// 解决控制台中文输出乱码
SetConsoleOutputCP
(
CP_UTF8
);
// register 存储类用于定义存储在寄存器中而不是 RAM 中的局部变量。这意味着变量的最大尺寸等于寄存器的大小(通常是一个词),且不能对它应用一元的 '&' 运算符(因为它没有内存位置)。
// 寄存器只用于需要快速访问的变量,比如计数器。
// 还应注意的是,定义 'register' 并不意味着变量将被存储在寄存器中,它意味着变量可能存储在寄存器中,这取决于硬件和实现的限制。
register
int
miles
;
// 在多次调用func的过程中,func中的局部变量i在程序的生命周期内保存,不需要在每次它进入和离开作用域时进行创建和销毁。
// 因此,使用 static 修饰局部变量可以在函数调用之间保持局部变量的值。
while
(
count
--
)
{
func
();
}
// !!! 由于使用到extern函数,编译的时候需要将这个cpp文件和VariableStorageDescSupport.cpp一起编译
// 编译命令 g++.exe .\VariableStorageDesc.cpp .\VariableStorageDescSupport.cpp -o .\VariableStorageDesc
count_in_main
=
5
;
write_extern
();
return
0
;
}
void
func
(
void
)
{
static
int
i
=
5
;
// 局部静态变量,不会在每次被调用时进行创建和销毁。
i
++
;
std
::
cout
<<
"变量 i 为 "
<<
i
;
std
::
cout
<<
" , 变量 count 为 "
<<
count
<<
std
::
endl
;
}
\ No newline at end of file
01.Basic/VariableStorageDescSupport.cpp
0 → 100644
View file @
a48a3dd7
#include <iostream>
extern
int
count_in_main
;
// 引用外部程序VariableStorageDesc的全局变量count_in_main
void
write_extern
(
void
)
{
std
::
cout
<<
"count_in_main is "
<<
count_in_main
<<
". 此处是调用外部cpp文件VariableStorageDescSupport.cpp输出的结果。"
<<
std
::
endl
;
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment