前言
小白可以使用以下cmake代码,使得可以同时运行多个main函数。
# 遍历项目根目录下所有的 .cpp 文件
file (GLOB_RECURSE files *.cpp)
foreach (file ${files})
string(REGEX REPLACE ".+/(.+)\\..*" "\\1" exe ${file})
add_executable (${exe} ${file})
message (\ \ \ \ --\ src/${exe}.cpp\ will\ be\ compiled\ to\ bin/${exe})
endforeach ()
第一章
1.9
#include <iostream>
using namespace std;
int main()
{
int a = 50, sum = 0;
while(a<=100){
sum += a;
a++;
}
cout << "Sum of 50 to 100 is " << sum << " .";
return 0;
}
1.10
#include <iostream>
using namespace std;
int main()
{
int a = 10;
while(a >= 0){
cout << a <<endl;
--a;
}
return 0;
}
#include "iostream"
using namespace std;
int main()
{
int a = 0 , b = 0;
cout << "Please input 2 integer: " << endl;
cin >> a >> b;
if (a > b)
while (a >= b){
cout << b << endl;
b++;
}
else {
while (a <= b){
cout << a << endl;
a++;
}
}
return 0;
}
1.13
1.13(1.09)
#include "iostream"
using namespace std;
int main()
{
int sum = 0;
for (int i = 50; i <= 100; ++i) {
sum += i;
}
cout << sum << endl;
return 0;
}
1.13(1.10)
#include "iostream"
using namespace std;
int main()
{
for (int i = 10; i >= 0; --i) {
cout << i << endl;
}
return 0;
}
1.13(1.11)
#include "iostream"
using namespace std;
int main()
{
int v1, v2;
cout << "Please input 2 integer: ";
cin >> v1 >> v2;
cout <<"The integer between two numbers is (including two numbers): ";
if (v1 < v2) {
for (int j = v1; j <= v2; j++) {
cout << j << " ";
}
} else {
for (int j = v2; j <= v1; j++) {
cout << j << " ";
}
}
return 0;
}
1.16
#include "iostream"
using namespace std;
int main()
{
int num, sum;
cout << "Please input number you want to sum : " << endl;
while(cin>>num){
sum += num;
cout << "The sum of input values is : " << sum << endl;
}
return 0;
}