繁体   English   中英

以连续顺序写入 3 个数字 C++

[英]Writing 3 numbers in consecutive order C++

我正在学习 Bjarne Stroustrups 的书。 我目前在第 3 章,我需要创建一个程序,用户输入 3 个数字,程序应按连续顺序输出它们。 我搜索了该站点,其他人也发布了相同的练习,但我尝试按照自己的方式进行操作,似乎存在逻辑问题,因为 IDE 没有发现我的源代码有任何问题。 不过我有我的怀疑。 这是源代码。

  #include "../../../std_lib_facilities.h"
int main()
{
    cout << "Enter three values\n";
    cout << "Enter the first value\n";
    int val1 = 0;
    int high = 00;
    int low = 000;
    cin >> val1;
    cout << "Enter the second value\n";
    int val2 = 0;
    cin >> val2;
    if (val1 >= val2)
        high = val1;
        low = val2;
    if (val1 <= val2)
        high = val2;
        low = val1;
    cout << endl;
    cout << "Enter the third value\n";
    int val3 = 0;
    if (val3 >= high)
        cout << low << " " << high << " " << val3;
    if (val3 <= low)
        cout << val3 << " " << low << " " << high;
    if (low < val3 < high)
        cout << low << " " << val3 << " " << high;
    cout << endl;
    return 0;

}

当我启动程序时一切正常,直到我输入第二个数字。 在那种情况下,程序输出 4 个值,然后继续执行第三个命令。 我不知道这很奇怪,但它必须与 int low、high、val1 和 val2 的分配有关。 这是示例输出:

Enter three values
Enter the first value
5
Enter the second value
4

Enter the third value
0 5 55 0 5

这里有什么问题?

您的代码有几个问题。

int high = 00;

这没有,但您无意中使用了您可能不理解的语言功能。 当您在数字前加上0 ,编译器会理解这意味着您的数字是一个八进制值。 00恰好与0相同,但一般来说最好写这样的行: int high = 0; , 或者甚至更好只是int high;


if (val1 >= val2)
    high = val1;
    low = val2;

high = val1; 在if语句中,但是low = val2; 不是。 你明明是故意的

if (val1 >= val2)
{
    high = val1;
    low = val2;
}

大多数代码审查指南建议对所有 if/while/for 语句(包括 1 行语句!)使用{} ,因为这是一个非常隐蔽的错误。


cout << "Enter the third value\n";
int val3 = 0;
if (val3 >= high)

您实际上从未读入第三个值。 这就是为什么程序在输入第二个值后继续完成的原因。 你大概是想...

cout << "Enter the third value\n";
int val3 = 0;
cin >> val3;
if (val3 >= high)

整体结构

编程中有一个原则,就是将输入读取代码与程序逻辑的其余部分分开。 它只是让事情更容易理解。 对于某些程序来说,这是很难做到的,但对于您来说则不然。 我建议像这样重写你的代码......

int main() 
{
  // First read in values from the user.
  int val1, val2, val3;
  cout << "Enter three values\n";
  cout << "Enter the first value\n";
  cin >> val1;
  cout << "Enter the second value\n";
  cin >> val2;
  cout << "Enter the third value\n";
  cin >> val3;
  // You could add a logging statement here to verify your input code is working.

  // Now print val1, val2, val3 in consecutive order
  // Rest of the code goes here.

像这样构造代码使 I/O 问题更容易找到。 缺少cin >> val3在上面的代码布局中很明显。

好吧,你搞砸了两件事,一是算法,二是语言的使用。 算法是你需要做的方式,而语言的使用是你如何使用语言中的工具来表达自己。 现在你想要做的很清楚,但你首先搞砸的是使用if 您已经使用了所有if s,这是在您的条件相互独立时完成的,就像您想检查两个条件是否为真一样,您打印了两次“YES”。 例子:

if(/*condition 1*/)
    cout<<"YES\n";
if(/*condition 2*/)
    cout<<"YES\n";  

在您的问题中,数字可以更大、更小或更少。 以我将要编写的方式实现您的代码对您来说很容易阅读和编写。

 #include "../../../std_lib_facilities.h"

int maximum(int a, int b){
    if(a>b)
        return a;
    return b;
}

int minimum(int a, int b){
    if(a<b)
        return a;
    return b;
}

int main()
{
    int val1, val2, val3;
    cout << "Enter three values you bastard1\n";
    cout << "Enter the first value\n";
    cin >> val1;
    cout << "Enter the second value\n";
    cin >> val2;
    cout << "Enter the third value\n";
    cin>> val3;
    cout<<"The numbers in order are:\n";
    int a,b,c;
    a = minimum(val1 , minimum(val2,val3));
    c = maximum(val1 , maximum(val2,val3));
    b = val1 + val2 + val3 - a - c;
    cout<< a << " " << b << " " <<c<<"\n";
    return 0;

}

谢谢大家的回答,到此结束,运行正常。 它唯一的问题似乎是在对数字进行排序后重复最后一个 IF 语句,除非仅在该语句成真的情况下。 (当 val3 大于最低值但小于最高值时。

#include "../../../std_lib_facilities.h"
int main()
{
    cout << "Enter three values\n";
    cout << "Enter the first value\n";
    int val1 = 0;
    int high = 0;
    int low = 0;
    cin >> val1;
    cout << "Enter the second value\n";
    int val2 = 0;
    cin >> val2;
    high = (val1 >= val2) ? val1 : val2;
    low = (val1 <= val2) ? val1 : val2;
    cout << "Enter the third value\n";
    int val3 = 0;
    cin >> val3;
    if (val3 >= high)
    {
        cout << low << " " << high << " " << val3;
    }
    if (val3 <= low)
    {
        cout << val3 << " " << low << " " << high;
    }
    if (low<val3<high)
    {
        cout << low <<" "<< val3<<" " << high;
    }

    cout << endl;
    return 0;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM