如何在C++中实现Java "Scanner"?

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

本文关键字:Java Scanner 实现 C++      更新时间:2023-10-16

请查看以下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;
        }
    }
}

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

int a, b;
cin >> a >> b

在C++中。所有的输入都将由一个测试引擎插入到一个类似的中

6 2 4 7 8 90 3456

如何使用C++实现Java"Scanner"?有头文件吗?请帮忙!

您似乎在使用Scanner从标准输入流中一次读取一个整数。使用提取运算符operator>>可以很容易地实现这一点。

替换此代码:

    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);

有了这个:

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

您应该在>>操作之后检查std::cin的值,以确保它们成功。

参考:

  • http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html
  • http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt

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

如果你想在键入每个数字时对其进行处理,那么你可以使用以下函数:

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;
}

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

例如,1 2 3 4在第一次呼叫时会返回1,在第二次呼叫时返回2。