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