从零开始的c++学习 3:[分支与循环]
Published:
记录一下c++学习的步骤 : base on Course ‘CS205 C/C++ Program Design’ in 2021 Fall at Southern University of Science and Technology. | 视频
week03 : Branching and Looping Statements
基本的条件和循环跟python差不多, 只记录一下第一次见的
// ? : 三元操作符, 跟单行的if else 类似 // `condition` ? `(if True)返回值` : `(if False)返回值` factor = isPositive ? 1 : -1; //跳转的效率低下,可以转换为计算的话就转 factor = (isPositive) * 2 - 1;
//goto.cpp 错误处理常用,其他场景少... float mysquare(float value) { float result = 0.0f; if(value >= 1.0f || value <= 0) { cerr << "The input is out of range." << endl; goto EXIT_ERROR; } result = value * value; return result; EXIT_ERROR: //do sth such as closing files here return 0.0f; }
//switch.cpp // 相当于goto, case 相当于一个label , Switch会跳转到label后按顺序往下执行(忽略case) // 所以要注意每个case 要break. switch (input_char) { case 'a': case 'A': cout << "Move left." << endl; break; case 'd': case 'D': cout << "Move right." << endl; break; default: cout << "Undefined key." << endl; break; }
Lab3 Exercises
1.Basic Linux command
# Demonstrate two Linux commends which are introduced previously and randomly
# selected by the SA for
#creating a directory
mkdir new_folder
#changing to another directory,
cd new_folder
#listing the subdirectories and files in a certain directory
ls folder
#or
ll folder
#removing files,
rm -r new_folder
#copying and moving files.
cp lab3/new_folder lab3.5/new_folder
#moving files
mv lab3/new_folder lab3.5/new_folder
2.Compile the 3 source files one by one using “g++ - c”, then link the generated object files together to generate an execute file. Demonstrate to a SA to pass the test.
//main.cpp
#include <iostream>
#include "functions.h"
using namespace std;
int main(){
print_hello();
cout << "this is main:" << endl;
cout << "this factorial of 5 is:" << factorial(5) << endl;
return 0;
}
//factorial.cpp
#include "functions.h"
int factorial(int n)
{
if (n==1)
return 1;
else
return n * factorial(n - 1);
}
//helloworld.cpp
#include <iostream>
#include "functions.h"
using namespace std;
void print_hello()
{
cout << "hello world!" << endl;
}
//functions.h
void print_hello();
int factorial(int n);
g++ -c main.cpp
g++ -c factorial.cpp
g++ -c printhello.cpp
g++ factorial.o main.o printhello.o -o a.out
./a.out
3.Run the following source code and explain the result. You need to explain the reason to a SA to pass the test.
#include <iostream>
using namespace std;
int main()
{
for(size_t n = 2; n >= 0; n--) //size_t 是个无符号整数 n==0 时, 输出0, 此时n--, 由于无符号,此时n将会变成范围内最大的那个数
cout << "n = " << n << " "; //故会一直循环打印整个size_t的范围
return 0;
}
4.Run the following source code and explain the result. You need to explain the reason to a SA to pass the test
#include <iostream>
using namespace std;
int main()
{
int n = 5;
int sum;
while(n >0){ //由于n初始化为5,while循环内没有改变n的值,所以这里会恒为True(死循环)
sum += n;
cout << "n = " << n << " ";
cout << "sum = " << sum << " "; // 会一直加五输出
}
return 0;
}
5. Run the following source code and explain the result. You need to explain the reason to a SA to pass the test.
#include <iostream>
using namespace std;
int main()
{
int n,fa;
// cout << "n = " << n << endl; --> 0
// cout << "fa = " << fa << endl; --> 0
do{
fa *= n;
n++;
}while(n <= 10);
cout << "fa = " << fa << endl; // --> 0
return 0;
}
//由于n,fa没有初始化, 我的机器初始化为0,
//fa = fa * n
//不论后面n怎么变, fa都是0,所以输出是0
//btw, n会从 0 ~ 11, 即先做do再判断while条件