I refer to Nak Gui Choi's solution.I change Copy Constructor and Reverse () function that use depth() function.
<Copy Constructor>Stack::Stack(const Stack& S){
Node* temp = S._top;
unsigned int d= depth();
for(;d>0&&temp != 0;d--){
push(temp->_data);
temp = temp->_next;
}
reverse();
}
< Reverse () >void Stack::reverse(){
Node* temp = _top;
_top = (Node*)0;
unsigned int d= depth();
for(;d>0&&temp!=0;d--){
push(temp->_data);
temp = temp->_next;
}
< Depth() >unsigned int Stack::depth(){
unsigned int dept = 0;
Node* temp = this->_top;
for(;temp !=0;dept++){
temp = temp->_next;
} return dept;
}
OOP344-SENECA
Friday, 11 October 2013
Tuesday, 24 September 2013
Exercise #3 for OOP344 _Yunki Lee
/* ******************************
* Program: Basic Math
* Author : Yunki Lee
* Student ID : 022 - 155 - 121
* File: main.cpp
********************************/
#include "basicmath.h"
int main(int argc, char* argv[])
{
bMath::Math bm(argc, argv);
return 0;
}
/* ******************************
* Program: Basic Math
* Author : Yunki Lee
* Student ID : 022 - 155 - 121
* File: basicmath.h
********************************/
#ifndef __BASICMATH_H__
#define __BASICMATH_H__
namespace bMath{
class Math {
bool isValid (int argc, char* argv[]);
public :
Math (int argc, char* argv[]);
void prn (int argc, char* argv[]);
void prnNum (int argc, char* argv[]);
};
}
#endif
/* ******************************
* Program: Basic Math
* Author : Yunki Lee
* Student ID : 022 - 155 - 121
* File: basicmath.cpp
********************************/
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdarg>
#include "basicmath.h"
using namespace std;
namespace bMath {
Math::Math (int argc, char* argv[]) //constructor which requires two arguments
{
prnNum(argc, argv); //call calculating function
}
bool Math::isValid (int argc, char* argv[])
{
bool check = false ;
int i;
/
if(argc!=4|| strlen(argv[2]) !=1 || !argv)
{
check =false;
}
if(*argv[2] == '+' || '-' || 'X'||'x' || '/')
{
for (i = 1; i<argc ; i+=2) {
if((*argv[i] >= 48 && *argv[i] <= 57) || *argv[i] == '.')
{
check = true;
}
}
}
return check;
}
void Math::prn (int argc, char* argv[]){
cout<< "<number> <+-x/> <number><ENTER>" <<endl;
}
}
void Math::prnNum (int argc, char* argv[]){
double b =0.0;
if(isValid(argc, argv)){
switch(*argv[2])
{ case '+' :
b = atof(argv[1])+ atof(argv[3]);
break;
case '-' :
b = atof(argv[1]) - atof(argv[3]);
break;
case '/' :
b = atof(argv[1]) / atof(argv[3]);
break;
case 'X' :
case 'x' :
b = atof(argv[1]) * atof(argv[3]);
break;
}
cout << argv[1] << argv[2] << argv[3] << "=" << b << endl;
}
else{
prn(argc, argv);
}
}
}
<Output>
* Program: Basic Math
* Author : Yunki Lee
* Student ID : 022 - 155 - 121
* File: main.cpp
********************************/
#include "basicmath.h"
int main(int argc, char* argv[])
{
bMath::Math bm(argc, argv);
return 0;
}
/* ******************************
* Program: Basic Math
* Author : Yunki Lee
* Student ID : 022 - 155 - 121
* File: basicmath.h
********************************/
#ifndef __BASICMATH_H__
#define __BASICMATH_H__
namespace bMath{
class Math {
bool isValid (int argc, char* argv[]);
public :
Math (int argc, char* argv[]);
void prn (int argc, char* argv[]);
void prnNum (int argc, char* argv[]);
};
}
#endif
/* ******************************
* Program: Basic Math
* Author : Yunki Lee
* Student ID : 022 - 155 - 121
* File: basicmath.cpp
********************************/
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdarg>
#include "basicmath.h"
using namespace std;
namespace bMath {
Math::Math (int argc, char* argv[]) //constructor which requires two arguments
{
prnNum(argc, argv); //call calculating function
}
bool Math::isValid (int argc, char* argv[])
{
bool check = false ;
int i;
/
if(argc!=4|| strlen(argv[2]) !=1 || !argv)
{
check =false;
}
if(*argv[2] == '+' || '-' || 'X'||'x' || '/')
{
for (i = 1; i<argc ; i+=2) {
if((*argv[i] >= 48 && *argv[i] <= 57) || *argv[i] == '.')
{
check = true;
}
}
}
return check;
}
void Math::prn (int argc, char* argv[]){
cout<< "<number> <+-x/> <number><ENTER>" <<endl;
}
}
void Math::prnNum (int argc, char* argv[]){
double b =0.0;
if(isValid(argc, argv)){
switch(*argv[2])
{ case '+' :
b = atof(argv[1])+ atof(argv[3]);
break;
case '-' :
b = atof(argv[1]) - atof(argv[3]);
break;
case '/' :
b = atof(argv[1]) / atof(argv[3]);
break;
case 'X' :
case 'x' :
b = atof(argv[1]) * atof(argv[3]);
break;
}
cout << argv[1] << argv[2] << argv[3] << "=" << b << endl;
}
else{
prn(argc, argv);
}
}
}
<Output>
Subscribe to:
Posts (Atom)