【问题标题】:How to print long int to the screen in c++?如何在 C++ 中将 long int 打印到屏幕上?
【发布时间】:2018-08-27 14:38:38
【问题描述】:

我试图从一个函数返回 long int 但它根本不起作用,然后我尝试在屏幕上打印一个 long int 数字但仍然不起作用。

#include<iostream>
using namespace std;

long int maximum_product(long int *input_array, int n){
    long int a,b;
    a = *input_array;
    b = *(input_array + 1);
    if (b > a){
        long int temp = a;
        a = b;
        b = temp;
    }

    for (int i = 2; i < n; i++){
        if (input_array[i] > b){
            if(input_array[i] > a){
                b = a;
                a = input_array[i];
            } else
                b = input_array[i];
        }
    }
    return a * b;
}

int main(){
    int n;
    cin >>n;
    long int *input_array = new long int[n];
    for (int i = 0; i < n; i++)
        cin >> input_array[i];
    cout << maximum_product(input_array, n);
    return 0;
}

这就是我所说的“不工作”的意思:

#include<iostream>
using namespace std;

int main(){
    long int y;
    cin >>y;
    cout<<y;
    return 0;
}

Result of the second program

【问题讨论】:

  • “不工作”是对您的问题的错误描述。请描述您的期望、观察到的以及它们有何不同。目前还不清楚是什么问题。
  • 欢迎来到 stackoverflow.com。请花一些时间阅读the help pagesread about how to ask good questions。最后请阅读this question checklist
  • 尝试不带逗号的输入。请尽量避免截图。文本可以复制和粘贴,但我的控制台不接受图像作为输入
  • 上一个示例中的问题是2,.... 被解析为2。使用std::cin时不能使用,作为分隔符
  • 要解决此类问题,首先要隔离问题。这里有两个步骤:读取一堆输入,然后计算结果。为了隔离问题,摆脱输入,并简单地将值分配给数组。看看这是否有效。如果不是,则问题出在计算中;如果它确实有效,则问题出在输入中。

标签: c++


【解决方案1】:

如果你想让std::cin阅读

2,147,483,646

作为long,你需要做一些额外的事情,因为没有你std::cin 只会读取第一个2 而忽略其余部分。

让我们从简单的开始......std::cin 有一个overload of its operator&gt;&gt; for long,但我们希望它做其他事情。我们如何让它选择不同的重载?我们传递一个不同的类型。然而,由于我们实际上只想要 long,所以我们使用了一个瘦包装器:

 struct read_long_with_comma_simple_ref {
     long& long;
     read_long_with_comma_simple_ref(long& value) : value(value) {}
 };

一旦我们提供了自己的operator&gt;&gt; 重载,我们就可以编写如下内容:

long x;
std::cin >> read_long_with_comma_simple_ref(x);

到目前为止很简单。我们如何实现operator&gt;&gt;?我能想到的最简单的方法就是忽略逗号:

std::istream& operator>>(std::istream& in,read_long_with_comma_simple_ref& ref) {
    std::string temp;
    // read till white space or new-line
    in >> temp;
    // remove all commas   
    temp.erase(std::remove(temp.begin(), temp.end(), ','), temp.end());
    // use a stringstream to convert to number
    std::stringstream ss(temp);
    ss >> rwcs.value;
    return in;
}

但是,这将接受诸如12,,,34 之类的无意义输入并将其解释为1234。我们当然希望避免这种情况并添加一些错误检查:

#include <iostream>
#include <limits>
#include <algorithm>
#include <sstream>
#include <ios>

 // same as before, just different name for a different type
 struct read_long_with_comma_ref {
     long& long;
     read_long_with_comma_ref(long& value) : value(value) {}
 };

 std::istream& operator>>(std::istream& in,read_long_with_comma_ref&& rwc){
     std::string temp;
     in >> temp;
     std::reverse(temp.begin(),temp.end());
     std::stringstream ss(temp);
     rwc.value = 0;
     long factor = 1;
     for (int i=0;i<temp.size();++i){
         char digit;
         ss >> digit;
         if (((i+1)%4)==0) {
             if (digit != ',') {
                 in.setstate(std::ios::failbit);
                 rwc.value = 0;
                 return in;
             }
         } else {
             int dig = digit - '0';
             if (dig < 0 || dig > 9) {
                 in.setstate(std::ios::failbit);
                 rwc.value = 0;
                 return in;
             }
             rwc.value += factor* (digit-'0');
             factor*=10;
         }
    }
    return in;
}

我们可以这样使用它:

long x;
std::cin >> read_long_with_comma_ref(x);
if (std::cin.fail()) std::cout << "reading number failed \n";
std::cout << x;

TL;DR 如果你想让std::cin 读取值2147483646 然后只需键入2147483646 并且不要使用, 作为分隔符,或者将其读取为字符串并删除在将逗号转换为数字之前。

【讨论】:

  • 我不确定 OP 是否打算识别分隔符来分隔数字或千位分隔符。 (可能是快照回答了这个问题,但由于公司的防火墙,我无法打开。到目前为止关于快照......)同时,我找到了SO: Print integer with thousands and millions separator。不过,这两个答案都再次基于std::imbue
  • @Scheff 哦,是的,快照回答了这个问题。似乎 OP 发现 2,147,483,646 是他们的最大长度并试图将其传递给cin
  • @Scheff hm 这看起来比我想出的要简单得多;)。我仍然认为我会将imbue 包装在我自己的operator&gt;&gt; 中,不知何故我不喜欢在全球范围内搞乱流的想法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多