0

该程序的目标是输入一个基数,后跟任意数量的空格,然后是一系列字符,只要它们比基数小 1 即可。我已经涵盖了错误,但我无法显示它。

如果我输入 2 1101,我的输出是对于给定的基数 2,“没有出现”。

The output should be the following: Test Case # 1
Input for Run 1:
2      1101
3    1212
5   66
2   1111
8   36
2 01

The output for Test Run 1: 
For the given base 2, the decimal value of the input string is 11.
For the given base 3, the decimal value of the input string is 70.
For the given base 5, the number is NOT valid!
For the given base 2, the decimal value of the input string is 15.
For the given base 8, the decimal value of the input string is 51.
For the given base 2, the decimal value of the input string is 2.

这是我遇到问题的程序这一部分的编码:

    #include <iostream>
#include <cmath>

using namespace std;

const int MAX_CHARS = 256;
const int MAX_BASE = 10;

int readUntiValidBaseRead();
int readNumbersReturningValue( int base );
int decimalValueOf( char chDigit );
bool isValid( char chDigit, int base );

//---------------------------------------------------------------------
// This function reads bases until a valid base is read or eof occurs.
// If an invalid base is read, an error message is displayed and the 
// rest of the line is ignored and another attempt to read a base value
// will be attempted.
// -1 is returned if eof occurs otherwise a valid base value is 
// returned.
//---------------------------------------------------------------------
int readUntilValidBaseRead()
{
   int readingBase;
   cin >> readingBase;
   while( !cin.eof() && (readingBase < 1 || readingBase > MAX_BASE))
   {
      cout << "Invalid base given, " << endl;
      cin.ignore(MAX_CHARS, '\n');
      cin >> readingBase;
   }
   if(readingBase > 1 && readingBase <= MAX_BASE)
      return readingBase;
   else
      return -1;
}

//---------------------------------------------------------------------
// This function reads in a sequence of characters that represent
// a number in the given base.  A valid sequence is given in a 
// "backwards" format such that the rightmost digit is given first,
// the second to the rightmost digit is next, etc. 
// This function returns the value of this sequence of characters if
// it is a valid sequence.  If it is not valid it returns -1. 
// params: base -> IN
//---------------------------------------------------------------------
int readNumbersReturningValue( int base )
{
   char readingNumber;
   int sum = 0;
   int theValue = 1;
   bool flaq = true;
   cin >> readingNumber;
   while(readingNumber != '\n' && flaq)
   {
      flaq = isValid(readingNumber, base);
      sum += (theValue* decimalValueOf(readingNumber));
      theValue *= base;
      cin >> readingNumber;
      flaq = isValid(readingNumber, base);
   }
   if(flaq == true)
      return sum;
   else
      return -1;
}


//---------------------------------------------------------------------
// This function returns the numeric value of the character digit that
// is stored in chDigit.
// params: chDigit -> IN
//---------------------------------------------------------------------
int decimalValueOf( char chDigit )
{
   int decimalNum;
   decimalNum = chDigit - '0';
   return decimalNum; //return integer value of 
}

//---------------------------------------------------------------------
// This function returns true if chDigit is a valid digit in the given
// base, it returns false otherwise.
// params: chDigit -> IN, base -> IN
//---------------------------------------------------------------------
bool isValid( char chDigit, int base )
{
   if(decimalValueOf(chDigit) >= 0 && decimalValueOf(chDigit) < base)
      return true;
   else
      return false;
}
//---------------------------------------------------------------------
//
//
//
int main()
{
   int totalSum = 0;
   int base;
   int singleSum;

   base = readUntilValidBaseRead();
   while(!cin.eof())
   {

      cout << "For the given base " << base << ", ";
      singleSum = readNumbersReturningValue(base);

      if(singleSum == -1)
      {
         cout << "Not valid. Throwing away rest of line. " << endl;
         cin.ignore(MAX_CHARS, '\n');
      }
      else
      {
         cout << "The decimal value of the input string is " << singleSum;
         totalSum += singleSum;
      }
      base = readUntilValidBaseRead();
   }
   cout << totalSum;
   return 0;
}
4

1 回答 1

0

这里:

cin >> readingNumber;
while(readingNumber != '\n' && flaq)
{
    flaq = isValid(readingNumber, base);
    sum += (theValue* decimalValueOf(readingNumber));
    theValue *= base;
    cin >> readingNumber;
    flaq = isValid(readingNumber, base);
}

sum无论读取的字符是否有效,您都会在第一次读取时递增。而且它永远不会有效,因为您的isValid()函数会返回false空格。您应该跳过任何空格,然后才开始检查数字。读入 a时无需担心空格int,但读入 a时需要担心空格char,因为空格字符是字符,cin不知道是否要跳过它们。

您可以通过以下方式跳过它们:

do {
    cin >> readingNumber;
} while ( isspace(readingNumber) );

你需要#include <cctype>isspace(). 请注意,这将跳过任何空白字符,包括制表符和换行符,因此\n如果您可能完全缺少输入,您可能需要单独检查。

这是一个工作示例,仅用于跳过空格并读取您的数字(即忽略读取基数):

#include <iostream>
#include <cctype>

int decimalValueOf(char chDigit) {
   return chDigit - '0';
}

bool isValid(char chDigit, int base) {
   return (decimalValueOf(chDigit) >= 0 && decimalValueOf(chDigit) < base );
}

int readNumbersReturningValue(int base) {
    char readingNumber;
    int sum = 0;
    int theValue = 1;
    bool flaq = true;

    do {
        std::cin >> readingNumber;
    } while ( std::isspace(readingNumber) );

    while ( readingNumber != '\n' && (flaq = isValid(readingNumber, base)) ) {
        sum += (theValue* decimalValueOf(readingNumber));
        theValue *= base;
        readingNumber = std::cin.get();
    }

    if ( flaq ) {
        return sum;
    } else {
        return -1;
    }
}

int main() {
    int sum = readNumbersReturningValue(2);
    std::cout << "Sum is: " << sum << std::endl;
    return 0;
}

输出:

paul@local:~/src/cpp/scratch$ ./readchars
             1101
Sum is: 11
paul@local:~/src/cpp/scratch$

请注意,这std::cin >> readingNumber;不适用于拾取换行符,因为std::cin它是行缓冲的,因此您必须std::cin.get()改用。

于 2013-10-17T01:21:51.367 回答