如何在 C++ 中实现 Java“扫描仪”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14821598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:29:38  来源:igfitidea点击:

How to implement Java "Scanner" in C++?

javac++visual-studio-2010

提问by Lemon Juice

Please have a look at the following java code

请看下面的java代码

import java.util.Scanner;

public class Main
{
    static int mul=1;
    static String  convert;
    static  char[] convertChar ;
    static StringBuffer buffer = new StringBuffer("");

    public static void main(String[]args)
    {

        Scanner scan = new Scanner(System.in);

        int number=0;
        int loopValue = scan.nextInt();
       //System.out.println("print: "+loopValue);

        for(int i=0;i<loopValue;i++)
        {
            number = scan.nextInt();
           // System.out.println("print: "+number);

            for(int a=1;a<=number/2;a++)
            {
                if(number%a==0)
                {
                    //System.out.println(a);
                    mul = mul*a;
                    //System.out.println(mul);
                }
            }

           convert  = String.valueOf(mul);
           convertChar = convert.toCharArray();

           if(convertChar.length>4)
            {
               /*System.out.print(convertChar[convertChar.length-4]);
               System.out.print(convertChar[convertChar.length-3]);
               System.out.print(convertChar[convertChar.length-2]);
               System.out.print(convertChar[convertChar.length-1]);
               System.out.println();*/

               buffer.append(convertChar[convertChar.length-4]);
               buffer.append(convertChar[convertChar.length-3]);
               buffer.append(convertChar[convertChar.length-2]);
               buffer.append(convertChar[convertChar.length-1]);

                System.out.println(buffer);

            }
            else
            {
                System.out.println(mul);
            }

            //System.out.println(mul);
            mul = 1;
        }

    }
}

This code is built to compute the product of positive divisors of a given number. I have used scanner here because I don't know how many inputs will be entered. That is why I can't go something like

此代码用于计算给定数字的正除数的乘积。我在这里使用了扫描仪,因为我不知道将输入多少个输入。这就是为什么我不能去像

int a, b;

cin >> a >> b

in C++. All the inputs will be inserted by a test engine, into one single like like following

在 C++ 中。所有输入都将由测试引擎插入,如下所示

6 2 4 7 8 90 3456

6 2 4 7 8 90 3456

How can I implement the Java "Scanner" using C++ ? Is there a header file for that? Please help!

如何使用 C++ 实现 Java“扫描仪”?有没有头文件?请帮忙!

回答by Rob?

You seem to be using Scannerto read one integer at a time from the standard input stream. This is easily accomplished with the extraction operator, operator>>.

您似乎正在使用Scanner从标准输入流中一次读取一个整数。这可以通过提取运算符轻松完成operator>>

Replace this code:

替换此代码:

    Scanner scan = new Scanner(System.in);

    int number=0;
    int loopValue = scan.nextInt();
   //System.out.println("print: "+loopValue);

    for(int i=0;i<loopValue;i++)
    {
        number = scan.nextInt();
       // System.out.println("print: "+number);

With this:

有了这个:

    int number=0;
    int loopvalue=0;
    std::cin >> loopvalue;

    for(int i = 0; i < loopValue; i++)
    {
        std::cin >> number;

You should check the value of std::cinafter the >>operations to ensure that they succeeded.

您应该检查std::cinafter>>操作的值以确保它们成功。

Refs:

参考:

回答by Simon

If you use std::cin >> value;to read the value then you can only process the entire line once a new-line has been detected.

如果您使用std::cin >> value;读取值,那么您只能在检测到换行后处理整行。

If you want to process each number as it is typed then you could use a function like:

如果您想按输入的方式处理每个数字,则可以使用如下函数:

int nextInt()
{
    std::stringstream s;
    while (true) 
    {
        int c = getch();
        if (c == EOF) break;
        putch(c); // remove if you don't want echo

        if ((c >= '0' && c <= '9') || (c == '-' && s.str().length() == 0)) 
            s << (char)c;
        else if (s.str().length() > 0)
            break;        
    }

    int value;
    s >> value;
    return value;
}

OK, there are probably more efficient ways to write that but it will read the input character by character until a number is encountered and will return whatever number is read when anything other than a number is encountered.

好吧,可能有更有效的方法来编写它,但它会逐个字符地读取输入,直到遇到一个数字,并在遇到除数字以外的任何数字时返回读取的任何数字。

E.g. 1 2 3 4 would return 1 on the first call, 2 on the second etc.

例如 1 2 3 4 将在第一次调用时返回 1,在第二次调用时返回 2,以此类推。