好的,所以我一直在寻找谷歌和论坛几个小时,似乎无法理解如何解决这个问题。
我需要编写一个程序,首先确定用户输入的数字是否为基数为5(换句话说,数字中只有0s,1s,2s,3s和4s)。然后,我必须计算数字中有多少0,1s,2s等,并将其显示给用户。
我看到有人说我应该将int
转换为string
,然后使用cin.get()
。
我注意到我无法在cin.get()
上使用string
,它必须是char
。
我只能使用while
循环进行此分配,没有while... do
循环。
任何帮助表示赞赏!!
这是我到目前为止的所有内容,显然我的错误就在其中:
//----------------------------------------------
// Assignment 3
// Question 1
// File name: q1.cpp
// Written by: Shawn Rousseau (ID: 7518455)
// For COMP 218 Section EC / Winter 2015
// Concordia University, Montreal, QC
//-----------------------------------------------
// The purpose of this program is to check if the
// number entered by the user is a base of 5
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
// Declaring variables
int number;
int zeros;
int ones;
int twos;
int threes;
int fours;
bool base5;
// Get data and calculate
cin >> number;
string numberString = to_string(number);
// Determine if the number is a base 5 number
while (cin.get(numberString) == 0 || cin.get(numberString) == 1 ||
cin.get(numberString) == 2 || cin.get(numberString) == 3 ||
cin.get(numberString) == 4)
base5 = true;
// Determine the number of each digits
zeros = 0;
ones = 0;
twos = 0;
threes = 0;
fours = 0;
return 0;
}
答案 0 :(得分:0)
您需要注意的几件事情:
从std::string
获取特定字符的一种方法是[]
。 e.g。
std::string myString{"abcdefg"};
char myChar = myString[4]; // myChar == 'e'
cin.get(aString)
并未尝试从aString
获取数据。它继续从stdin获取数据并存储在aString中。一旦获得数据并放入字符串,您就可以简单地操作字符串本身。
一小段代码,用于计算字符串中元音的数量。如果你能理解它,那么你的工作应该没有问题。 (没有编译,可能是一些拼写错误)
std::string inputString;
std::cin >> inputString;
// you said you need a while loop.
// although it is easier to with for loop and iterator...
int i = 0;
int noOfVowels = 0;
while (i < inputString.length()) {
if (inputString[i] == 'a'
|| inputString[i] == 'e'
|| inputString[i] == 'i'
|| inputString[i] == 'o'
|| inputString[i] == 'u' ) {
++noOfVowels;
}
++i;
}
std::cout << "number of vowels : " << noOfVowels << std::endl;
答案 1 :(得分:0)
你可以尝试这种方法。这将解决您的需求。
#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int number=0;
int zeros=0;
int ones=0;
int twos=0;
int threes=0;
int fours=0;
bool valid=true;;
int counter = 0;
cout<<"Enter the number: ";
cin >> number;
stringstream out;
out << number; //int to string
string numberString = out.str();
cout<<"\n\nNumber after string conversion : "<<numberString;
cout<<"\nPrinting this just to show that the conversion was successful\n\n\n";
while (counter < numberString.length())
{
if (numberString[counter] == '0')
zeros++;
else if(numberString[counter] == '1')
ones++;
else if(numberString[counter] == '2')
twos++;
else if(numberString[counter] == '3')
threes++;
else if(numberString[counter] == '4')
fours++;
else
valid=false;
counter++;
}
if(valid==true)
{
cout<<"\nZeros : "<<zeros;
cout<<"\nOnes : "<<ones;
cout<<"\nTwos : "<<twos;
cout<<"\nThrees : "<<threes;
cout<<"\nFours : "<<fours;
}
else
cout<<"\n\nInvalid data...base of 5 rule violated";
_getch();
return 0;
}
答案 2 :(得分:0)
如果你不想使用std :: string然后使用字符,首先循环来自用户的输入,直到按下ENTER。
char ch = 0;
while ((ch = cin.get()) != '\n')
{
...
}
对于每个读取的字符,检查它是否是一个数字(std :: isdigit),如果它在0..4范围内,如果没有退出并给出一些不是基数5的消息
有一组int来跟踪数字的频率
int freq[5] = {0,0,0,0,0};
检查字符有效后从数字中减去ascii值并将其用作数组中的索引,增加:
freq[ch - '0']++;
e.g。
char ch;
int freq[5] = {0};
while ((ch = cin.get()) != '\n')
{
cout << ch;
freq[ch-'0']++;
}
for (int i = 0; i < sizeof(freq)/sizeof(freq[0]); ++i)
{
cout << static_cast<char>(48+i) << ":" << freq[i] << endl;
}
答案 3 :(得分:0)
这是一个计算数字的有用函数:
// D returns the number of times 'd' appears as a digit of n.
// May not work for n = INT_MIN.
int D(int n, int d) {
// Special case if we're counting zeros of 0.
if (n == 0 && d == 0) return 1;
if (n < 0) n = -n;
int r = 0;
while (n) {
if (n % 10 == d) r++;
n /= 10;
}
return r;
}
在您的代码中,您可以天真地使用它来解决问题而无需任何进一步的循环。
if (D(n, 5) + D(n, 6) + D(n, 7) + D(n, 8) + D(n, 9) != 0) {
cout << "the number doesn't consist only of the digits 0..4."
}
cout << "0s: " << D(n, 0) << "\n";
cout << "1s: " << D(n, 1) << "\n";
cout << "2s: " << D(n, 2) << "\n";
cout << "3s: " << D(n, 3) << "\n";
cout << "4s: " << D(n, 4) << "\n";
你也可以(或者也应该)在这里使用循环来减少冗余。