C++ Primer 习题代码(个人手写)持续更新,学习C++中,有问题渴求指出

前言

小白可以使用以下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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值