无法理解为什么功能不起作用

时间:2013-10-17 01:00:11

标签: c++ function

这个程序的目标是输入一个基数,后跟任意数量的空格,然后是一系列字符,任何数字,只要它们比基数少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;
}

1 个答案:

答案 0 :(得分: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。您应该跳过任何空格,然后才开始检查数字。您在阅读int时无需担心空格,但在阅读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()