我编译我的代码没有任何错误。但是,当我尝试运行该程序时,会弹出一个包含这些内容的窗口:
Program: C:\Windows\SYSTEM32\MSVCP120D.dll
File: c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring
Line: 1168
Expression: invalid null pointer
我不确定问题是什么或它可能是什么。我绝对肯定地知道标题非常好,因为它是由教授提供的,我们不会触及它。
INTENT
代码的意图是从用户获取二进制输入并显示十进制等效值。然后取十进制输入并将其打印为二进制数。
CODE
主要
#include "stdafx.h"
#include "LinkedStack.h"
#include <string>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
string binaryNum = "0"; // Holds the entered binary value
string decResult = "0"; // Holds the decimal result
int binResult = 0, pow = 1; // Holds the binary result and the power of 1 for conversion
int decimal = 0; // Holds the entered decimal value
int decimalRootVal; // Holds the decimal value for description later on
// Initialize stack
linkedStackType<string> binaryStack;
cout << "Please enter a binary number starting from the left." << endl; // Takes user input to convert binary into decimal
cout << "Binary Value: ";
cin >> binaryNum; // Takes input and assigns it to an int
binaryStack.push(binaryNum); // Pushes binaryNum into the stack
for (int i = binaryNum.length() - 1; i >= 0; pow <<= 1, --i){ // Binary to decimal conversion
binResult += (binaryNum[i] - '0') * pow;
}
cout << "Decimal equivalent of " << binaryNum << ": " << binResult << endl; // Prints the decimal equivalent of binaryNum
binaryStack.pop(binaryNum); // Empties the stack
cout << "What is the decimal you wish to be turned into binary? "; // Asks for user input of a decimal
cin >> decimal;
decimalRootVal = decimal; // Shown to the user later on for description
while (decimal > 0){ // Decimal to binary conversion
decResult += (decimal & 1) ? "1" : "0";
decimal >>= 1;
binaryStack.push(decResult); // Pushes the binary value for each placement into the stack
}
cout << "Binary equivalent of " << decimalRootVal << ": " << decResult << endl;
binaryStack.destroyStack();// Destroys the stack
}
标题
#ifndef H_StackType
#define H_StackType
#include <iostream>
#include "stdafx.h"
//Definition of the node
template <class Type>
struct nodeType{
Type info;
nodeType<Type> *link;
};
template<class Type>
class linkedStackType{
public:
const linkedStackType<Type>& operator = (const linkedStackType<Type>&);// Overload the assignment operator
void initializeStack(); // Initialize the stack to an empty state.
// Post: Stack elements are removed; top = NULL
bool isEmptyStack();// Function returns true if the stack is empty;
// Otherwise, it returns false
bool isFullStack(); // Function returns true if the stack is full;
// Otherwise, it returns false
void push(const Type& newItem); // Add the newItem to the stack.
// Pre condition: stack exists and is not full
// Post condition: stack is changed and the newItem is added to the top of stack.
// top points to the updated stack
void pop(Type& poppedElement); // Remove the top element of the stack.
// Pre condition: Stack exists and is not empty.
// Post condition: stack is changed and the top element is removed from the stack.
// The top element of the stack is saved in poppedElement
void destroyStack();// Remove all elements of the stack, leaving the stack in an empty state.
// Post condition: top = NULL
linkedStackType();// Default constructor; Post condition: top = NULL
linkedStackType(const linkedStackType<Type>& otherStack);// Copy constructor
~linkedStackType();// Destructor; All elements of the stack are removed from the stack
private:
nodeType<Type> *top;// Pointer to the stack
};
template<class Type>// Default constructor
linkedStackType<Type>::linkedStackType(){
top = NULL;
}
template<class Type>
void linkedStackType<Type>::destroyStack(){
nodeType<Type> *temp; // Pointer to delete the node
while (top != NULL){ // While there are elements in the stack
temp = top; // Set temp to point to the current node
top = top->link; // Advance top to the next node
delete temp; // Deallocate memory occupied by temp
}
}
template<class Type>
void linkedStackType<Type>::initializeStack(){
destroyStack();
}
template<class Type>
bool linkedStackType<Type>::isEmptyStack(){
return(top == NULL);
}
template<class Type>
bool linkedStackType<Type>::isFullStack(){
return 0;
}
template<class Type>
void linkedStackType<Type>::push(const Type& newElement){
nodeType<Type> *newNode; // Pointer to create the new node
newNode = new nodeType<Type>; // Create the node
newNode->info = newElement; // Store newElement in the node
newNode->link = top; // Insert newNode before top
top = newNode; // Set top to point to the top node
}
template<class Type>
void linkedStackType<Type>::pop(Type& poppedElement){
nodeType<Type> *temp; // Pointer to deallocate memory
poppedElement = top->info; // Copy the top element into poppedElement
temp = top; // Set temp to point to the top node
top = top->link; // Advance top to the next node
delete temp; // Delete the top node
}
template<class Type>// Copy constructor
linkedStackType<Type>::linkedStackType(const linkedStackType<Type>& otherStack){
nodeType<Type> *newNode, *current, *last;
if (otherStack.top == NULL)
top = NULL;
else{
current = otherStack.top; // Set current to point to the stack to be copied
// Copy the top element of the stack
top = new nodeType<Type>; // Create the node
top->info = current->info; // Copy the info
top->link = NULL; // Set the link field of the node to null
last = top; // Set last to point to the node
current = current->link; // Set current to point to the next node
// Copy the remaining stack
while (current != NULL){
newNode = new nodeType<Type>;
newNode->info = current->info;
newNode->link = NULL;
last->link = newNode;
last = newNode;
current = current->link;
}
}
}
template<class Type>// Destructor
linkedStackType<Type>::~linkedStackType(){
nodeType<Type> *temp;
while (top != NULL){ // While there are elements in the stack
temp = top; // Set temp to point to the current node
top = top->link; // Advance first to the next node
delete temp; // Deallocate the memory occupied by temp
}
}
template<class Type>// Overloading the assignment operator
const linkedStackType<Type>& linkedStackType<Type>::operator = (const linkedStackType<Type>& otherStack){
nodeType<Type> *newNode, *current, *last;
if (this != &otherStack){ // Avoid self-copy
if (top != NULL) // If the stack is not empty, destroy it
destroyStack();
if (otherStack.top == NULL)
top = NULL;
else{
current = otherStack.top; // Set current to point to the stack to be copied
// Copy the top element of otherStack
top = new nodeType<Type>; // Create the node
top->info = current->info; // Copy the info
top->link = NULL; // Set the link field of the node to null
last = top; // Make last point to the node
current = current->link; // Make current point to the next node
// Copy the remaining elements of the stack
while (current != NULL){
newNode = new nodeType<Type>;
newNode->info = current->info;
newNode->link = NULL;
last->link = newNode;
last = newNode;
current = current->link;
}
}
}
return *this;
}
#endif
答案 0 :(得分:5)
您的代码中存在许多问题。
首先,NULL指针错误来自
string binaryNum = 0;
没有string
的构造函数接受整数。如果您希望字符串为"0"
,请使用string x("0");
第二次,您的代码从二进制转换为十进制是错误的。
for (int i = binaryNum.length() - 1; i >= 0; pow <<= 1){
binResult += (binaryNum[i] - '0');
}
pow
在哪里使用? i
在哪里递减?如果binaryNum
的长度大于1,则会出现无限循环。将其更改为
for (int i = binaryNum.length() - 1; i >= 0; pow <<= 1, --i){
binResult += (binaryNum[i] - '0')*pow;
}
第三,您的代码从十进制转换为二进制是错误的。进行以下更改。
添加#include <algorithm>
//decResult = to_string(decimal); comment this line!
binaryStack.push(decResult);
while(decimal>0){
decResult += (decimal & 1)?"1":"0";
decimal>>=1;
}
reverse(decResult.begin(), decResult.end());
答案 1 :(得分:1)
这不是初始化字符串的有效方法:
string binaryNum = 0;
如果你想要一个空字符串:
string binaryNum;
如果你想要一个包含零的字符串:
string binaryNum = "0";