2016-04-30 61 views
-2

當我嘗試將十六進制數字添加到當前的十六進制數字時,我總是收到分段錯誤。這裏就是我得到我的分段錯誤行(我認爲):分割錯誤C++十六進制計算器

newHex->insertTail(sum); 

這裏是整個程序:

#include <iostream> 
#include <vector> 
#include <stdlib.h> 
#include <string> 
using namespace std; 
#undef NULL 
const int NULL = 0; 
const char SENTINEL = '#'; 
typedef int element; 

class listnode { 

    public: 
     element data; 
     listnode * next; 
}; 

class LList { 

    private: 
     listnode * head; 
     listnode * tail; 
     listnode * view; 

    public: 
     LList(); 
     ~LList(); 
     void read(); 
     listnode* next(); 
     void print(); 
     void insertTail(element val); 
     void clean(); 
     //void add(LList operand); 
     //void multiply(LList operand); 
     element deleteHead(); 
}; 

class Calculator { 

    public: 
     Calculator(); 
     LList* add(LList& left, LList& right); 
     //LList* multiply(LList& left, LList& right); 
}; 

Calculator::Calculator() { 

}; 

LList* Calculator::add(LList& left, LList& right) { 
    int sum, carry = 0, lval = 0, rval = 0; 
    bool calculating = true; 
    listnode *leftNode; 
    listnode *rightNode; 
    LList* newHex; 
    while(calculating) { 

     leftNode = left.next(); 
     rightNode = right.next(); 

     if(leftNode == NULL) { 
      lval = 0; 
     } 
     else 
      lval = leftNode->data; 

     if(rightNode == NULL) { 
      rval = 0; 
     } 
     else 
      rval = rightNode->data; 


     if(leftNode == NULL && rightNode == NULL) { 
      calculating = false; 
      if(carry != 0) { 
       newHex->insertTail(carry); 
      } 
      break; 
     } 

     sum = lval + rval + carry; 
     carry = 0; 
     if(sum >= 16) { 
      carry = 1; 
      sum -= 16; 
     } 
     cout << "Segmentation Fault Below :)" << endl; 
     newHex->insertTail(sum); 

    } 

    return newHex; 
}; 

listnode* LList::next() { 
    listnode* temp = view; 
    if(temp != NULL) 
     view = view->next; 

    if(view == NULL) { 
    } 
    return temp; 
}; 


LList::LList(){ 
    head = NULL; 
    view = NULL; 
}; 


void LList::print() { 
    listnode * temp; 
    int i = 0; 
    string printValues; 
    temp = head; 
    while(temp != NULL) { 
     int var = temp -> data; 
     char character = ' '; 
     if(i % 3 == 0 && i != 0) 
      printValues += ','; 
     i++;  
     if(var > 9 && var < 16) 
     { 
      character = static_cast <char>(var + 65 - 10); 

     }else if (var <= 9 && var >= 0) 
      character = static_cast <char>(var + 48); 
     printValues += character; 
     temp = temp -> next; 

    } 
    string tempValues; 
    for(int i = printValues.length() - 1; i >= 0; i--) 
     tempValues += printValues[i]; 
    cout << tempValues; 
    cout << endl; 
}; 

void LList::read() { 
    string userval; 
    int i; 
    bool parsing = true; 
    char curval; 
    vector <int> values; 
    clean(); 
    while(parsing) { 
     cin >> userval; 
     for(unsigned int i = 0; i < userval.length(); i++) { 
      curval = userval[i]; //this is your character 
      if(curval >= 48 && curval <= 57) 
       values.push_back(static_cast <int>(curval - 
      48)); 

      if(curval >= 65 && curval <= 70) 
       values.push_back(static_cast <int>(curval - 
      65 + 10)); 

      if(curval == ' ') 
       break; 

      if(curval == SENTINEL) { 
       parsing = false; 
       break; 
      } 
     } 
    } 
    for(int i = values.size() -1; i >= 0; i--) { 
     insertTail(values[i]); 
    } 
}; 

void LList::insertTail(element val) { 
    listnode * temp; 
    temp = new listnode; 
    temp -> data = val; 
    temp -> next = NULL; 

    if(head == NULL) { 
     head = temp; 
     view = head; 
    } 
    else 
     tail -> next = temp; 
    tail = temp; 
}; 

void LList::clean() { 
    while(head != NULL) 
     deleteHead(); 
}; 

void validCommands() { 
    cout << "Valid commands are:" << endl; 
    cout << " e enter enter the current "; 
    cout << "hexadecimal "; 
    cout << "number from the keyboard" << endl; 
    cout << " a add  add a new hexadecimal "; 
    cout << "number to the current hex. number" << endl; 
    cout << " m multiply "; 
    cout << "multiply a new hexadecimal number "; 
    cout << "by the current hex. number" << endl; 
    cout << " h help show this help menu" << endl; 
    cout << " q quit quit the program" << endl << endl; 
}; 

/* 
void LList::multiply(LList operand) { 
    int left, right, product, carry; 
    listnode* operandNext = operand.next(); 
    listnode* currentNode = this->head; 
    while(operandNext != NULL) { 
     left = operandNext->data; 
     right = currentNode->data; 
     product = left * right; 
     carry = 0; 
     if(product >= 16) { 
      product -= 16; 
      carry = 1; 
     } 
     if(currentNode == NULL) { 
      this->insertTail(left); 
     } 
     currentNode->data = product; 
     currentNode = currentNode->next; 
     operandNext = operandNext->next; 
    } 
}; 
*/ 

element LList::deleteHead() { 
    listnode * temp; 
    temp = head; 
    head = head -> next; 
    delete temp; 
    return temp -> data; 
}; 

LList::~LList(){ 
    delete head; 
}; 

int main() { 
    LList L, add,multiply; 
    Calculator calc; 
    L.insertTail(0); 
    char option; 
    bool run; 
    cout << "Hexadecimal Calculator, Ver 1.0.0 \n"; 
    cout << endl; 



    do { 
    cout << "Current Hexadecimal number is: "; 
    L.print(); 
    cout << endl; 

    cout << "Command (h for help): "; 
    cin >> option; 
    cout << endl << endl; 
     switch(option) { 
      case 'e' : 
       cout << "Enter a hexadecimal number "; 
       cout << "followed by #: "; 
       L.read(); 
       cout << endl << "Entering completed."; 
       cout << endl << endl; 
      break; 
      case 'a' : 
       cout << "Adding a new hexadecimal number "; 
       cout << "to the current hex. number" << endl; 
       cout << endl; 
       cout << "Enter a hexadecimal "; 
       cout << "number, follow by #: "; 
       add.read(); 
       cout << endl << "Addition completed."; 
       cout << endl; 
       L = *calc.add(L, add); 
       cout << endl; 
       break; 
      case 'm' : 
       cout << "Multiplying a new hexadecimal "; 
       cout << "number "; 
       cout << "to the current hex. number" << endl; 
       cout << endl; 
       cout << "Enter a hexadecimal "; 
       cout << "number, follow by #: "; 
       //multiply.read(); 
       cout << endl << "Multiplication completed."; 
       cout << endl; 
       //L.multiply(multiply); 
       cout << endl; 
       break; 
      case 'h' : validCommands(); 
      break; 
      case 'q' : run = false; 
      break; 
     }; 
    } while (run); 
     exit(0); 

} 
+2

當您使用調試器逐步執行代碼時,您觀察到了什麼? –

+1

'這裏是我遇到分段錯誤的地方(我認爲)'你會成爲程序員還是算命師?不需要猜測,真的。有些工具會顯示確切的行,所有變量的值,堆棧跟蹤等。使用調試器! – Drop

+0

'const int NULL = 0;' - 這不是個好主意 - 讓事情變得困惑 –

回答

1

如果我沒有錯,問題是newHex,在Calculator::add()

你定義一個指針LList

LList* newHex; 

,你用它

newHex->insertTail(carry); 
[...] 
newHex->insertTail(sum); 

沒有分配它。