0

それで、私のプログラムの締め切りは過ぎ去ってしまい、多くの (re:all) 失敗したテストと、私がよく知らないその他のエラーが残っています。等級は特に気にしません。内容が理解できなければ、どうせ駄目です。時間があり、私と一緒にこのデバッグの一部を見てみたい場合は、私はすべて耳を傾けており、何が間違っているのかを本当に知りたいと思っています。すべてのコードとヘッダー ファイルを以下に掲載します。

#ifndef BIGNUM_H_INCLUDED
#define BIGNUM_H_INCLUDED

#include <string>
#include <iostream>
using namespace std;

class bignum
{
public:
// Constructors.
bignum();
bignum(int num_digits);
bignum(const string &digits);
bignum(const bignum &other);

// Destructors.
~bignum();

// Assignment operator.
bignum &operator=(const bignum &other);

// Accessors
int digits() const;
int as_int() const;
void bignum::setdigits(const int data[])const;
string as_string() const;
void print(ostream &out) const;
bignum add(const bignum &other) const;
bignum multiply(const bignum &other) const;
bool equals(const bignum &other) const;
int PublicNumberTest;

private:
// Pointer to a dynamically-allocated array of integers.  The last
// digit (ones place) is stored in digit[0], the next-to last (tens
// place) in digit[1], and so on.
int *digit;
// Number of digits in the array, not counting leading zeros.
// Should be less than or equal to the allocated size of digit.
int ndigits;
};
#endif




here is the cpp:


#include <iostream>
#include <string>
#include <math.h>
using namespace std;
#include "bignum.h"
void pause_215(bool have_newline);
bool test_bignum();

// Method implementations here.
bignum::bignum(){
digit[0];
}

bignum::~bignum(){
delete[] digit;
}

bignum::bignum(int num_digits)
{
digit = new int[];
for (int i = 0; i < num_digits; i++)
    digit[i] = 9;
}

bignum::bignum(const string &digits){
bool start = false;
int NumofDigits = 0;
int *digit = new int[digits.size()];
for (int i = 0; i < digits.size(); i++){ //will determine if the char at ith slot                                                can be converted
    if (!isdigit(digits[i])){
        break; //if you can't convert, break
    }
    else{
        int number = digits[i] - '0'; //converts char to #
        if ((number == 0) && (start = false)) //if the digit is a zero and the sequence has yet to start, do not enter the value
            continue;
        digit[NumofDigits] = number;
        start = true;
        NumofDigits++;
    }
}
if (NumofDigits == 0)
    digit[0] = 0;
}

bignum::bignum(const bignum &other){

ndigits = other.ndigits;
digit = new int[];
for (int i = 0; i < ndigits; i++)
    digit[i] = other.digit[i];
}

bignum& bignum::operator = (const bignum & other){

if (this == &other)
    return *this;
delete[] digit;
ndigits = other.ndigits;
digit = new int[];
for (int i = 0; i < ndigits; i++)
    digit[i] = other.digit[i];

return *this;
}

int bignum::digits() const {

return ndigits;
}

int bignum::as_int() const {  //I was using math.h and pow here, but it was giving a
                              //warning about conflicted data types so I did it the 
int as_int = 0;           //long way. I haven't tested this yet. Just wrote it. 
int pow_of_ten = 1;
for (int i = 0; i < ndigits; i++)
{   
    as_int = as_int + digit[i] * pow_of_ten;
    for (int k = 0; k == i; k++)
        pow_of_ten = 10 * pow_of_ten;
}
return as_int;
}
string bignum::as_string() const{


char *digit_char = new char[];
for (int i = 0; i < ndigits; i++){
    digit_char[i] = digit[i];
}
string str(digit_char);
return str;        //this isn't working because I just got this thing to compile at     11:25 and I've had 20 minutes to debug it. ndigits is not getting
}                       //initialized, which is why the first set         of tests is failing. 

void bignum::print(ostream &out) const{
    for (int i = 0; i < ndigits; i++)
    {
        if (i > 0)
        out << digit[i];
    }
    out << endl;
}
bignum bignum::add(const bignum& other) const{

int carry = 0;
int larger = 0;
int numofdigits = 0;
int currentDigit = 0;
int size_other = other.ndigits;
int *b = new int[];
int *total = new int[];
int temp_sum = 0;
if (size_other > ndigits)
    larger = size_other;
else
    larger = ndigits;
for (int i = 0; i < larger + 1; i++){
    int aa = 0;
    int bb = 0;
    if (i < ndigits)
        aa = digit[i];
    if (i < other.ndigits)
        bb = b[i];
    temp_sum = aa + bb + carry;
    currentDigit = temp_sum % 10;
    total[numofdigits] = currentDigit;
    numofdigits++;
    carry = temp_sum / 10;
}
bignum sum(numofdigits);
for (int j = 0; j < numofdigits; j++)
    sum.digit[j] = total[j];
return sum;
}

//void bignum::setdigits(const int *temp_row) const{
//  digit = temp_row; 
//}

bignum bignum::multiply(const bignum& other) const{
bignum product;
int carry = 0;
int sum = 0;
int j = 0;
int *temp_row = new int[];
int count = 0;
for (int i = 0; i < ndigits-1; i++){
    carry = 0;
    temp_row[i] = 0;
    for (j; j < other.ndigits - 1; j++){
        sum = digit[i] * other.digit[j] + carry;
        temp_row[i + j] = sum % 10;
        carry = sum / 10;
        count++;
    }
    if (carry>0){
        temp_row[i + j] = carry;
        count++;
    }
    bignum row(count);
    for (int k = 0; k < count; k++){
        row.digit[i] = temp_row[i];
    }

        product = product.add(row);
} return product;
}

bool bignum::equals(const bignum& other) const{
bool equal = true;
if (ndigits != other.ndigits)
    return false;
for (int i = 0; i < ndigits; i++){
    if (digit[i] != other.digit[i])
        return false;
    else;
}
}

// Ordinary (nonmember) functions here.
int main()
{
bool success = test_bignum();
if (success) {
    cout << "All tests succeeded, good job!" << endl;
} else {
    cout << "Some tests failed, keep trying!" << endl;
}

pause_215(false);
return 0;
}


bool test_bignum()
{
// Current test number.  Be sure to increment this before each test!
int curr_test = 0;
// Number of failed tests.
int failed = 0;

// Test 1: do the default constructor and as_string work?
curr_test++;
bignum simple;
if (simple.as_string() != "0") {
    cerr << "Test " << curr_test << " failed." << endl;
    failed++;
}

// Test 2: does the integer constructor work?
curr_test++;
bignum nines(9);
if (nines.as_string() != "999999999") {
    cerr << "Test " << curr_test << " failed." << endl;
    failed++;
}

// Test 3: does the string constructor work (correct input)?
curr_test++;
bignum hundred("100");
if (hundred.as_string() != "100") {
    cerr << "Test " << curr_test << " failed." << endl;
    failed++;
}

// Test 4: does the string constructor work (bad input)?
curr_test++;
bignum sixtyfive("65abcd");
if (sixtyfive.as_string() != "65") {
    cerr << "Test " << curr_test << " failed." << endl;
    failed++;
}

// Test 5: does the string constructor work (all bad input)?
curr_test++;
bignum zero("not a number");
if (zero.as_string() != "0") {
    cerr << "Test " << curr_test << " failed." << endl;
    failed++;
}

// Test 6: does the string constructor work (long input)?
curr_test++;
bignum huge("123456789123456789");
if (huge.as_string() != "123456789123456789") {
    cerr << "Test " << curr_test << " failed." << endl;
    failed++;
}

// Test 7: does the copy constructor work?
curr_test++;
bignum copy(sixtyfive);
if (copy.as_string() != "65") {
    cerr << "Test " << curr_test << " failed." << endl;
    failed++;
}

// Test 8: does the assignment operator work?
curr_test++;
simple = hundred;
if (simple.as_string() != "100") {
    cerr << "Test " << curr_test << " failed." << endl;
    failed++;
}

// Test 9: does the destructor work, and do the copy constructor
// and assignment operator make deep copies?  A failure is likely
// to crash the program.
curr_test++;
{
    bignum secondcopy(copy);
    bignum thirdcopy(secondcopy);
    bignum newcopy("175");
    thirdcopy = hundred;
    copy = newcopy;
    newcopy = bignum("42");
}
if (copy.as_string() != "175" || hundred.as_string() != "100") {
    cerr << "Test " << curr_test << " failed." << endl;
    failed++;
}

// Test 10: does digits() work?
curr_test++;
if (hundred.digits() != 3) {
    cerr << "Test " << curr_test << " failed." << endl;
    failed++;
}

// Test 11: does digits() work (zero)?
curr_test++;
if (zero.digits() != 1) {
    cerr << "Test " << curr_test << " failed." << endl;
    failed++;
}

// Test 12: does multiply() work (no carry)?
curr_test++;
bignum multiplicand("123");
bignum multiplier("12");
bignum product = multiplicand.multiply(multiplier);

if (product.as_string() != "1476") {
    cerr << "Test " << curr_test << " failed." << endl;
    failed++;
}

// Test 13: does multiply() work (with carry)?
curr_test++;
bignum mc1("963");
bignum mc2("82");
bignum mcp = mc1.multiply(mc2);

if (mcp.as_string() != "78966") {
    cerr << "Test " << curr_test << " failed." << endl;
    failed++;
}

// Test 14: does multiply() work (large numbers)?
curr_test++;
bignum mh1("12345678900");
bignum mh2("98765432100");
bignum mhp = mh1.multiply(mh2);

if (mhp.as_string() != "1219326311126352690000") {
    cerr << "Test " << curr_test << " failed." << endl;
    failed++;
}

// TODO: add two more tests for multiply()
// TODO: add four tests each for add(), as_int(), and equals()

return (failed == 0);
}


{
if (have_newline) {
    // Ignore the newline after the user's previous input.
    cin.ignore(200, '\n');
}

// Prompt for the user to press ENTER, then wait for a newline.
cout << endl << "Press ENTER to continue." << endl;
cin.ignore(200, '\n');
}

(!! [コピー コンストラクター (ディープ コピー)]の数字に [] を削除しようとすると、エラーが発生します。これは間違っています-コピーconstrではなく、代入演算子にあります.!!)そして、これがリークによるものなのか(私が持つべき場所にデストラクタを実装していない)、それとも単なる構文なのかはわかりません。なんだけど、それは本当に私を悩ませています。それは私が最初に見たいものです。ただし、パズルを組み立てるためにテスト ケースを 1 つずつフィルタリングするのが最善であるため、このアプローチが実用的でない可能性があることは理解しています。これは私が興味を持っているもう 1 つのことです。このような問題が発生した場合の最適なデバッグ方法は何ですか? 修正を提供する場合、私はあなたが何をしたかよりも、あなたがしたことをどのように、なぜしたのかに本当に興味があります. 時間があれば、その理由を教えてください。

最後に、このフォーラムから直接、あちこちで少し助けてもらいましたが、構文などを探すことで間接的に多くの助けを得ることができました。必ずしも地球上で最も使いやすいとは限らない言語を人々が学習できるように、時間と労力を割いてくれてありがとう。C++ について最初にRUNするためだけに学ぶことがたくさんあるようで、膨大な時間がかかりますが、実行する価値はあります。もう一度、すべてに感謝します。

ブラッド

4

1 に答える 1

0

コードがたくさんありますが、デフォルトのコンストラクターは問題ではありませんか?

bignum::bignum(){
digit[0];
}

new で作成されていないものを削除している可能性があります。

別のヒント:

digit = new int[];

サイズを宣言する必要があります。

于 2013-10-19T21:19:55.880 に答える