mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-19 10:12:38 +08:00
cpp lint fixes and instantiate template classes
This commit is contained in:
@@ -8,29 +8,26 @@
|
||||
* ./main student.txt
|
||||
************************************************************
|
||||
* */
|
||||
#include <assert.h>
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "stack.cpp"
|
||||
#include "stack.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
double GPA;
|
||||
double highestGPA;
|
||||
string name;
|
||||
std::string name;
|
||||
|
||||
assert(argc == 2);
|
||||
ifstream infile;
|
||||
stack<string> stk;
|
||||
std::ifstream infile;
|
||||
stack<std::string> stk;
|
||||
|
||||
infile.open(argv[1]);
|
||||
cout << fixed << showpoint;
|
||||
cout << setprecision(2);
|
||||
std::cout << std::fixed << std::showpoint;
|
||||
std::cout << std::setprecision(2);
|
||||
infile >> GPA >> name;
|
||||
highestGPA = GPA;
|
||||
|
||||
@@ -44,12 +41,12 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
infile >> GPA >> name;
|
||||
}
|
||||
cout << "Highest GPA: " << highestGPA << endl;
|
||||
cout << "Students the highest GPA are: " << endl;
|
||||
std::cout << "Highest GPA: " << highestGPA << std::endl;
|
||||
std::cout << "Students the highest GPA are: " << std::endl;
|
||||
while (!stk.isEmptyStack()) {
|
||||
cout << stk.top() << endl;
|
||||
std::cout << stk.top() << std::endl;
|
||||
stk.pop();
|
||||
}
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
CC= g++
|
||||
CFLAGS = -c -Wall
|
||||
|
||||
all: main test_stack
|
||||
stack.o: stack.cpp
|
||||
$(CC) $(CFLAGS) stack.cpp
|
||||
test_stack: stack.o
|
||||
$(CC) test_stack.cpp stack.o -o stk
|
||||
main: stack.o
|
||||
$(CC) main.cpp stack.o -o main
|
||||
|
||||
clean:
|
||||
rm *o stk main
|
||||
@@ -1,8 +1,7 @@
|
||||
#include "stack.h"
|
||||
#include <assert.h>
|
||||
#include <iostream>
|
||||
#include "./stack.h"
|
||||
|
||||
using namespace std;
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
/* Default constructor*/
|
||||
template <class Type>
|
||||
@@ -19,13 +18,13 @@ stack<Type>::~stack() {}
|
||||
template <class Type>
|
||||
void stack<Type>::display() {
|
||||
node<Type> *current = stackTop;
|
||||
cout << "Top --> ";
|
||||
std::cout << "Top --> ";
|
||||
while (current != NULL) {
|
||||
cout << current->data << " ";
|
||||
std::cout << current->data << " ";
|
||||
current = current->next;
|
||||
}
|
||||
cout << endl;
|
||||
cout << "Size of stack: " << size << endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "Size of stack: " << size << std::endl;
|
||||
}
|
||||
|
||||
/* Determine whether the stack is empty */
|
||||
@@ -68,20 +67,22 @@ void stack<Type>::pop() {
|
||||
delete temp;
|
||||
size--;
|
||||
} else {
|
||||
cout << "Stack is empty !" << endl;
|
||||
std::cout << "Stack is empty !" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
/* Operator "=" */
|
||||
template <class Type>
|
||||
stack<Type> stack<Type>::operator=(stack<Type> &otherStack) {
|
||||
stack<Type> &stack<Type>::operator=(const stack<Type> &otherStack) {
|
||||
node<Type> *newNode, *current, *last;
|
||||
|
||||
if (stackTop != NULL) /* If stack is no empty, make it empty */
|
||||
/* If stack is no empty, make it empty */
|
||||
if (stackTop != NULL) {
|
||||
stackTop = NULL;
|
||||
if (otherStack.stackTop == NULL)
|
||||
}
|
||||
if (otherStack.stackTop == NULL) {
|
||||
stackTop = NULL;
|
||||
else {
|
||||
} else {
|
||||
current = otherStack.stackTop;
|
||||
stackTop = new node<Type>;
|
||||
stackTop->data = current->data;
|
||||
@@ -101,3 +102,8 @@ stack<Type> stack<Type>::operator=(stack<Type> &otherStack) {
|
||||
size = otherStack.size;
|
||||
return *this;
|
||||
}
|
||||
|
||||
#include <string>
|
||||
/** force instantiate to export the type class */
|
||||
template class stack<std::string>;
|
||||
template class stack<int>;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* This class specifies the basic operation on a stack as a linked list */
|
||||
#ifndef STACK_H
|
||||
#define STACK_H
|
||||
#ifndef DATA_STRUCTURES_STK_STACK_H_
|
||||
#define DATA_STRUCTURES_STK_STACK_H_
|
||||
|
||||
/* Definition of the node */
|
||||
template <class Type>
|
||||
@@ -22,11 +22,11 @@ class stack {
|
||||
void pop(); /* Remove the top element of the stack */
|
||||
void clear();
|
||||
|
||||
stack<Type> operator=(stack<Type> &otherStack);
|
||||
stack<Type> &operator=(const stack<Type> &otherStack);
|
||||
// Overload "=" the assignment operator.
|
||||
private:
|
||||
node<Type> *stackTop; /* Pointer to the stack */
|
||||
int size;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif // DATA_STRUCTURES_STK_STACK_H_
|
||||
|
||||
@@ -1,56 +1,58 @@
|
||||
#include <iostream>
|
||||
#include "stack.cpp"
|
||||
#include "stack.h"
|
||||
|
||||
using namespace std;
|
||||
#include "./stack.h"
|
||||
|
||||
int main() {
|
||||
stack<int> stk;
|
||||
cout << "---------------------- Test construct ----------------------"
|
||||
<< endl;
|
||||
std::cout << "---------------------- Test construct ----------------------"
|
||||
<< std::endl;
|
||||
stk.display();
|
||||
cout << "---------------------- Test isEmptyStack ----------------------"
|
||||
<< endl;
|
||||
std::cout
|
||||
<< "---------------------- Test isEmptyStack ----------------------"
|
||||
<< std::endl;
|
||||
if (stk.isEmptyStack())
|
||||
cout << "PASS" << endl;
|
||||
std::cout << "PASS" << std::endl;
|
||||
else
|
||||
cout << "FAIL" << endl;
|
||||
cout << "---------------------- Test push ----------------------" << endl;
|
||||
cout << "After pushing 10 20 30 40 into stack: " << endl;
|
||||
std::cout << "FAIL" << std::endl;
|
||||
std::cout << "---------------------- Test push ----------------------"
|
||||
<< std::endl;
|
||||
std::cout << "After pushing 10 20 30 40 into stack: " << std::endl;
|
||||
stk.push(10);
|
||||
stk.push(20);
|
||||
stk.push(30);
|
||||
stk.push(40);
|
||||
stk.display();
|
||||
cout << "---------------------- Test top ----------------------" << endl;
|
||||
std::cout << "---------------------- Test top ----------------------"
|
||||
<< std::endl;
|
||||
int value = stk.top();
|
||||
if (value == 40)
|
||||
cout << "PASS" << endl;
|
||||
std::cout << "PASS" << std::endl;
|
||||
else
|
||||
cout << "FAIL" << endl;
|
||||
cout << "---------------------- Test pop ----------------------" << endl;
|
||||
std::cout << "FAIL" << std::endl;
|
||||
std::cout << "---------------------- Test pop ----------------------"
|
||||
<< std::endl;
|
||||
stk.display();
|
||||
stk.pop();
|
||||
stk.pop();
|
||||
cout << "After popping 2 times: " << endl;
|
||||
std::cout << "After popping 2 times: " << std::endl;
|
||||
stk.display();
|
||||
cout << "---------------------- Test overload = operator "
|
||||
"----------------------"
|
||||
<< endl;
|
||||
std::cout << "---------------------- Test overload = operator "
|
||||
"----------------------"
|
||||
<< std::endl;
|
||||
stack<int> stk1;
|
||||
cout << "stk current: " << endl;
|
||||
std::cout << "stk current: " << std::endl;
|
||||
stk.display();
|
||||
cout << endl << "Assign stk1 = stk " << endl;
|
||||
std::cout << std::endl << "Assign stk1 = stk " << std::endl;
|
||||
stk1 = stk;
|
||||
stk1.display();
|
||||
cout << endl << "After pushing 8 9 10 into stk1:" << endl;
|
||||
std::cout << std::endl << "After pushing 8 9 10 into stk1:" << std::endl;
|
||||
stk1.push(8);
|
||||
stk1.push(9);
|
||||
stk1.push(10);
|
||||
stk1.display();
|
||||
cout << endl << "stk current: " << endl;
|
||||
std::cout << std::endl << "stk current: " << std::endl;
|
||||
stk.display();
|
||||
cout << "Assign back stk = stk1:" << endl;
|
||||
std::cout << "Assign back stk = stk1:" << std::endl;
|
||||
stk = stk1;
|
||||
stk.display();
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user