Commit b4797ad0 authored by Clark Lin's avatar Clark Lin
Browse files

add more basic scripts

parent a48a3dd7
# Exclude all files
*
# Include all folders
!**/
# Include all cpp file under all folder
!**/*.cpp
# Include .gitignore itself
!.gitignore
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
// 申明
cout << "Declaration" << endl;
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0] << endl;
// 修改
cout << "Modificaiton" << endl;
cars[0] = "Opel";
cout << cars[0] << endl;
// 循环
cout << "for loop" << endl;
string vehicles[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
for (int i = 0; i < 5; i++) {
cout << vehicles[i] << endl;
}
// 循环2
cout << "for each loop" << endl;
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout << i << endl;
}
// 省略大小
cout << "Omit size during declaration" << endl;
string pc[3] = {"Lenovo", "HP", "Dell"}; // Also three arrays
for (int i = 0; i < sizeof(pc) / sizeof(string); i++) {
cout << pc[i] << endl;
}
// 获取大小
cout << "Get size" << endl;
cout << sizeof(pc) / sizeof(string) << endl;
// 多维数组
cout << "Loop multi-dimension" << endl;
string letters[2][4] = {
{ "A", "B", "C", "D" },
{ "E", "F", "G", "H" }
};
cout << letters[0][2] << endl;
// 循环多维数组
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << letters[i][j] << " ";
}
cout << endl;
}
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
// 输入数字
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum << endl;
// 输入字符串
string firstName, lastName;
cout << "Type your first name: ";
cin >> firstName; // get user input from the keyboard
cout << "Type your last name: ";
cin >> lastName; // get user input from the keyboard
cout << "Your name is: " << firstName << " " << lastName << endl;
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
// We put "1" to indicate there is a ship.
bool ships[4][4] = {
{ 0, 1, 1, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 }
};
// Keep track of how many hits the player has and how many turns they have played in these variables
int hits = 0;
int numberOfTurns = 0;
// Allow the player to keep going until they have hit all four ships
while (hits < 4) {
int row, column;
cout << "Selecting coordinates\n";
// Ask the player for a row
cout << "Choose a row number between 0 and 3: ";
cin >> row;
// Ask the player for a column
cout << "Choose a column number between 0 and 3: ";
cin >> column;
// Check if a ship exists in those coordinates
if (ships[row][column]) {
// If the player hit a ship, remove it by setting the value to zero.
ships[row][column] = 0;
// Increase the hit counter
hits++;
// Tell the player that they have hit a ship and how many ships are left
cout << "Hit! " << (4-hits) << " left.\n\n";
} else {
// Tell the player that they missed
cout << "Miss\n\n";
}
// Count how many turns the player has taken
numberOfTurns++;
}
cout << "Victory!\n";
cout << "You won in " << numberOfTurns << " turns";
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
// while loop
cout << "while loop:" << endl;
int i = 0;
while (i < 5) {
cout << i << endl;
i++;
}
// do while loop
i = 0;
cout << "do while loop:" << endl;
do {
cout << i << endl;
i++;
}
while (i < 5);
// for loop
cout << "for loop:" << endl;
for (int i = 0; i < 5; i++) {
cout << i << endl;
}
// for each loop
cout << "for each loop:" << endl;
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout << i << endl;
}
// break
cout << "break:" << endl;
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
cout << i << endl;
}
// continue
cout << "continue:" << endl;
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
cout << i << endl;
}
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
string food = "Pizza"; // A food variable of type string
cout << "Output value and address by variable and reference" << endl;
cout << food << endl; // Outputs the value of food (Pizza)
cout << &food << endl; // Outputs the memory address of food (0x6dfed4)
// 定义指针
cout << "Output value and address by pointer" << endl;
string* ptr = &food;
cout << *ptr << endl;
cout << ptr << endl;
// 修改指针指向内存的值
cout << "Modify value by pointer" << endl;
*ptr = "Hamburger";
// 指针指向变量的值也被改变了
cout << food << endl;
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
string food = "Pizza";
string &meal = food;
cout << food << endl; // Outputs Pizza
cout << meal << endl; // Outputs Pizza
// Get memory address
cout << &food << endl; // Outputs like 0x6dfed4
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
string greeting = "Hello";
cout << greeting << endl;
string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
cout << fullName << endl;
fullName = firstName.append(" ").append(lastName);
cout << fullName << endl;
cout << "The length of the txt string is: " << fullName.length() << endl;
// 竖着打印
for (int i = 0; i < fullName.length(); i++) {
cout << fullName[i] << endl;
}
// 转义字符
cout << "We are the so-called \"Vikings\" from the north." << endl;
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
// Create a structure variable called myStructure
struct {
int myNum;
string myString;
} myStructure;
// Assign values to members of myStructure
myStructure.myNum = 1;
myStructure.myString = "Hello World!";
// Print members of myStructure
cout << myStructure.myNum << endl;
cout << myStructure.myString << endl;
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
char mode;
cout << "Please choose mode (A/B/C): ";
cin >> mode;
switch(mode) {
case 'A':
cout << "Mode: " << mode << endl;
break;
case 'B':
cout << "Mode: " << mode << endl;
break;
case 'C':
cout << "Mode: " << mode << endl;
break;
default:
cout << "Invalid mode!" << endl;
}
}
\ No newline at end of file
......@@ -42,7 +42,7 @@ int main(){
* char : -128 到 127 或者 0 到 255
* unsigned char : 0 到 255
*******************************************/
char char_min, char_max, char_A, char_overflow;
char unsigned char_min, char_max, char_A, char_overflow;
char_min = int(0);
char_max = int(255);
char_A = int(65);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment