读取以空格分隔的输入数字

问题描述 投票:17回答:3

这可能是一个初学者的问题,但我还没有找到适合我的答案。

目前,我正在编写一个程序,用于接收用户输入的类(可以是一个或多个用空格分隔的数字),然后确定该数字是素数,完美还是两者都不是。如果数字是完美的,那么它将显示除数。

到目前为止,我已经编写了关于素数,完美和列出除数的代码。我被困在程序的输入部分。我不知道如何将由空格分隔的输入一次一个地通过我的循环。

这是我目前的计划:

cout<<"Enter a number, or numbers separated by a space, between 1 and 1000."<<endl;
cin>>num;

while (divisor<=num)
    if(num%divisor==0)
    {
        cout<<divisor<<endl;
        total=total+divisor;
        divisor++;
    }
    else divisor++;
if(total==num*2)
    cout<<"The number you entered is perfect!"<<endl;
else cout<<"The number you entered is not perfect!"<<endl;


if(num==2||num==3||num==5||num==7)
    cout<<"The number you entered is prime!"<<endl;

else if(num%2==0||num%3==0||num%5==0||num%7==0)
    cout<<"The number you entered is not prime!"<<endl;
else cout<<"The number you entered is prime!"<<endl;

return 0;

它有效,但仅适用于单个数字。如果有人能帮助我让它能够读取由空格分隔的多个输入,那么我们将不胜感激。另外,只是旁注,我不知道会输入多少个数字,所以我不能只为每个数字创建一个变量。它将是一个随机数量的数字。

谢谢!

c++ input spaces
3个回答
19
投票

默认情况下,cin从输入中读取丢弃任何空格。因此,您所要做的就是使用do while循环多次读取输入:

do {
   cout<<"Enter a number, or numbers separated by a space, between 1 and 1000."<<endl;
   cin >> num;

   // reset your variables

   // your function stuff (calculations)
}
while (true); // or some condition

13
投票

我建议在行中读取一个字符串,然后根据空格拆分它。为此,您可以使用getline(...)函数。诀窍是拥有一个动态大小的数据结构,以便在分裂后保存字符串。可能最容易使用的是vector

#include <string>
#include <vector>
...
  string rawInput;
  vector<String> numbers;
  while( getline( cin, rawInput, ' ' ) )
  {
    numbers.push_back(rawInput);
  }

所以说输入看起来像这样:

Enter a number, or numbers separated by a space, between 1 and 1000.
10 5 20 1 200 7

您现在将有一个向量,数字,其中包含以下元素:{“10”,“5”,“20”,“1”,“200”,“7”}。

请注意,这些仍然是字符串,因此在算术中没用。要将它们转换为整数,我们使用STL函数,atoi(...)的组合,并且因为atoi需要c字符串而不是c ++样式字符串,所以我们使用string类'c_str()成员函数。

while(!numbers.empty())
{
  string temp = numbers.pop_back();//removes the last element from the string
  num = atoi( temp.c_str() ); //re-used your 'num' variable from your code

  ...//do stuff
}

现在这个代码存在一些问题。是的,它运行,但它有点笨重,它以相反的顺序排列数字。让我们重新编写它,使它更紧凑:

#include <string>
...
string rawInput;
cout << "Enter a number, or numbers separated by a space, between 1 and 1000." << endl;
while( getline( cin, rawInput, ' ') )
{
  num = atoi( rawInput.c_str() );
  ...//do your stuff
}

错误处理还有很大的改进空间(现在,如果你输入一个非数字,程序将会崩溃),并且有更多的方法来实际处理输入以使其成为可用的数字形式(编程的乐趣! ),但这应该给你一个全面的开始。 :)

注意:我将参考页面作为链接,但由于我的帖子少于15个,所以我发帖不能超过两个:/

编辑:我对atoi行为有点不对劲;我把它与Java的string-> Integer转换混淆了,当给定一个不是数字的字符串时会抛出一个Not-A-Number异常,如果没有处理异常则会崩溃程序。另一方面,atoi()返回0,这没有用,因为如果0是他们输入的数字怎么办?让我们使用isdigit(...)函数。这里要注意的一件重要事情是c ++样式字符串可以像数组一样访问,这意味着rawInput [0]是字符串中的第一个字符,一直到rawInput [length - 1]。

#include <string>
#include <ctype.h>
...
string rawInput;
cout << "Enter a number, or numbers separated by a space, between 1 and 1000." << endl;
while( getline( cin, rawInput, ' ') )
{
  bool isNum = true;
  for(int i = 0; i < rawInput.length() && isNum; ++i)
  {
    isNum = isdigit( rawInput[i]);
  }

  if(isNum)
  {
    num = atoi( rawInput.c_str() );
    ...//do your stuff
  }
  else
    cout << rawInput << " is not a number!" << endl;
}

布尔值(分别为true / false或1/0)用作for循环的标志,它遍历字符串中的每个字符并检查它是否为0-9位。如果字符串中的任何字符不是数字,则当它到达条件“&& isNum”时,循环将在下一次执行期间中断(假设您已经覆盖了循环)。然后在循环之后,isNum用于确定是否执行您的操作,或者打印错误消息。


5
投票

你想要:

  • 从控制台读取整行
  • 对行进行标记,沿空格分割。
  • 将这些拆分件放入数组或列表中
  • 逐步执行该数组/列表,执行prime / perfect / etc测试。

到目前为止,你的课程涵盖了这些方面的内容?

© www.soinside.com 2019 - 2024. All rights reserved.