C++primer习题(if)

 

Exercises Section 1.4.3

// TEST.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "iostream"


int main()
{
    std::cout << "please enter two numbers";
    int v1, v2;
    std::cin >> v1 >> v2; 
    //std::cout << std::endl;
    int lower, higher;
    if (v1 <= v2)
    {
        lower = v1;
        higher = v2;
    }
    else
    {
        lower = v2;
        higher = v1;
    }
    int sum=0;
    for (int sal = lower; sal<= higher; ++sal)
    {
        sum += sal;
    }
    std::cout << "the sum of" << v1 << "and" << v2 << "is" << sum;
    return 0;
}

 

 

Exercise 1.14:

What happens in the program presented in this section if the input values are equal?

如果输入值相等,本节展示的程序将产生什么问题?

 

输出的还是原来的数。

Exercise 1.15:

Compile and run the program from this section with two equal values as input. Compare the output to what you predicted in the previous exercise. Explain any discrepancy between what happened and what you predicted.

用两个相等的值作为输入编译并运行本节中的程序。将实际输出与你在上一习题中所做的预测相比较,解释实际结果和你预计的结果间的不相符之处。

 

Exercise 1.16:

Write a program to print the larger of two inputs supplied by the user.

编写程序,输出用户输入的两个数中的较大者。

int main()
{
    std::cout << "please enter two numbers";
    std::cout << std::endl;
    int v1, v2;
    std::cin >> v1 >> v2; 
    
    
    if (v1 <= v2)
    {
        std::cout << v2;
    }
    else
    {
        std::cout << v1;
    }
    
    return 0;
}

Exercise 1.17:

Write a program to ask the user to enter a series of numbers. Print a message saying how many of the numbers are negative numbers.

编写程序,要求用户输入一组数。输出信息说明其中有多少个负数。

// TEST.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "iostream"


int main()
{
    std::cout << "请输入要输入的数的个数";
    //std::cout << std::endl;
    int x;
    std::cin >> x;
    std::cout << "请输入" << x << "个数";
    int i, num, cout = 0, art = 0;;
    for (i = 0; i !=x; ++i)
    {
        std::cin >> num;
        if (num < 0)
        {
            cout++;
        }
        else
            art++;
    }
    std::cout <<"这组数共有" <<cout<<"个负数";
    
    return 0;

    
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值