這個程序的目標是輸入一個基數,然後輸入任意數量的空格,然後輸入任意數字的序列,只要它們比基數小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;
}
你錯過了你的一半功能。如果您需要特定功能的幫助,請構建一個僅使用該功能的完整且可編譯的示例。如果你這樣做,很可能你會自己發現你的問題,但如果沒有,發佈一些我們可以編譯和研究的東西。 –
你有沒有反對'std:string'? – Beta
打印出'base'的值。你確定'readUntilValidBaseRead'可以嗎? – Cramer