online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
#include <stdio.h> #include <iostream> #include <vector> #include "raster.h" //#include "triplet.h" #include "myStruct.h" #include "myTemplate.h" using std::endl; using std::cout; //=================================================== //=================================================== int main (int argc, char** argv) { charRaster* crPtr; charRaster* head; charRaster* tail; charRaster myRaster(5,5,'.'); myRaster.setName("myRaster"); charRaster rasterCopy(myRaster); charRaster copyCopy; copyCopy = rasterCopy; copyCopy.setName("copyCopy"); rasterCopy.setName("rasterCopy"); myRaster.putVal(0,0,'0'); rasterCopy.putVal(1,1,'1'); copyCopy.putVal(2,2,'2'); cout<<myRaster; cout<<rasterCopy; cout<<copyCopy; cout<<"BEGIN pointer operations:"<<endl; head = &myRaster; myRaster.InsertAfter(&copyCopy); myRaster.InsertAfter(&rasterCopy); crPtr = myRaster.GetNext(); cout<<*crPtr; crPtr = crPtr->GetNext(); cout<<*crPtr; crPtr = crPtr->GetPrev(); cout<<*crPtr; crPtr = crPtr->GetPrev(); cout<<*crPtr; tail = crPtr->Tail(); cout<<"tail:"<<*tail; head = copyCopy.Head(); cout<<"head:"<<*head; tail = myRaster.Tail(); cout<<"tail:"<<*tail; cout<<"max: "<<*GetMax(&myRaster, &rasterCopy)<<endl; return(0); fPoint fOrigin = {0.0,0.0}; iPoint iOrigin = {0,0}; fPoint fPolyline[1000]; iPoint iPolyline[1000]; fPoint* fPntPtr; iPoint* iPntPtr; char* cPtr; int index; cout<<"sizeof fPoint: "<<sizeof(fPoint)<<endl; cout<<"sizeof iPoint: "<<sizeof(iPoint)<<endl; cout<<"sizeof fPolyline: "<<sizeof(fPolyline)<<endl; cout<<"sizeof iPolyline: "<<sizeof(iPolyline)<<endl; cout<<endl; for (int i=0; i<1000; i++) { fPolyline[i].x = (float)i; fPolyline[i].y = 1000-fPolyline[i].x; } index = 41; cout<<"fPolyline[index]: "<<fPolyline[index].x<<","<<fPolyline[index].y<<endl; fPntPtr = &fPolyline[index]; cout<<"fPntPtr: "<<fPntPtr<<endl; fOutStr(fPntPtr); cout<<endl; // increment the pointer. what is the result of that? fPntPtr++; cout<<"fPntPtr: "<<fPntPtr<<endl; fOutStr(fPntPtr); //===================================================================== //===================================================================== char thisChar = 'A'; for (int i=0; i<1000; i++) { iPolyline[i].id = thisChar; iPolyline[i].x = i; iPolyline[i].y = iPolyline[i].x*i; if (thisChar == 'Z') thisChar = 'A'; else thisChar++; } index = 80; iPntPtr = &iPolyline[index]; cPtr = (char*)iPntPtr; cout<<"cPtr: "<<cPtr<<endl; cout<<"char at index "<<index<<": "<<*cPtr<<endl; cPtr++; cout<<"cPtr: "<<cPtr<<endl; cout<<"next char: "<<*cPtr<<endl<<endl; cout<<"iPntPtr: "<<iPntPtr<<endl; iOutStr(iPntPtr); cout<<endl; cout<<"iPolyline[index]: "<<iPolyline[index].x<<","<<fPolyline[index].y<<endl; cout<<endl; index++; cout<<"iPolyline[index]: "<<&iPolyline[index]<<endl; // let's call iOutStr with an alternate syntax iOutStr(&iPolyline[index]); cout<<endl; // what if we do this? cout<<"wait, what?"<<endl; iOutStr(&iPntPtr[10]); // *(iPntPtr+10) // OR what about THIS? iPntPtr+=10; iOutStr(iPntPtr); return(0); }
//============================================================ // class: charRaster // creates a data structure suitable for gridded data // accepts the number of rows and columns as input // accepts number of rows and columns with a default fill value //============================================================ #pragma once #include <string> #include <iostream> using std::string; #define A_CONST 1 #define DEEP_COPY class charRaster { private: int rowCount; int columnCount; int size; string name; charRaster* next; charRaster* prev; char* rasterData; char* returnString; //================================================================ // helper function //================================================================ int calcOffset(int row, int column); int calcOffset2(int row, int column); int initialize(int rows, int columns, char userVal); public: //================================================================ // constructor with fill value of SPACE //================================================================ charRaster(); charRaster(string name); charRaster(int rows, int columns); charRaster(const charRaster &cRast); //================================================================ // constructor with an initial fill value supplied at instantiation //================================================================ charRaster(int rows, int columns, char initVal); //================================================================ // destructor //================================================================ ~charRaster(); //================================================================ //================================================================ bool operator<(const charRaster& r); #ifdef A_CONST charRaster& operator=(const charRaster r); #endif //================================================================ // return the column count //================================================================ int getColumnCount() const; //================================================================ // returns the row count //================================================================ int getRowCount() const; int getSize() const; void setName(string name); void SetNext(charRaster* ptr); void SetPrev(charRaster* ptr); charRaster* GetNext() const; charRaster* GetPrev() const; void InsertAfter(charRaster* thisRast); void Insert(int position, charRaster* thisRast); charRaster* Head(); charRaster* Tail() const; //================================================================ // place char at row, column //================================================================ int putVal(int row, int column, char userVal); //================================================================ // place char at row, column //================================================================ int fillRow(int row, int column, char userVal, int count); //================================================================ // return an entire row of data //================================================================ string getRow(int row); string getName() const; //================================================================ // return an entire column of data //================================================================ std::string getColumn(int column) const; //================================================================ //================================================================ void showAllRows(); friend std::ostream& operator << (std::ostream& os, charRaster& mgt); };
#include <iostream> #include "raster.h" using std::cout; using std::endl; using std::string; using std::ostream; //================================================================ // helper for constructor //================================================================ int charRaster::initialize(int rows, int columns, char initVal) { rowCount = rows; columnCount = columns; size = rows*columns; rasterData = new char[size]; returnString = new char[size]; next = NULL; prev = NULL; name = "UNNAMED"; for (int i=0; i<size; i++) { rasterData[i] = initVal; } for (int i=0; i<size; i++) { returnString[i] = initVal; } } //================================================================ // constructor with fill value of SPACE //================================================================ charRaster::charRaster(int rows, int columns) { initialize(rows, columns, ' '); } //================================================================ // constructor with fill value of SPACE //================================================================ charRaster::charRaster() { initialize(0,0, ' '); name = "EMPTY"; } //================================================================ // constructor with fill value of SPACE //================================================================ charRaster::charRaster(string name) { initialize(0,0, ' '); name = name; } //================================================================ // constructor with an initial fill value supplied at instantiation //================================================================ charRaster::charRaster(int rows, int columns, char initVal) { initialize(rows, columns, initVal); } //================================================================ // destructor //================================================================ charRaster::~charRaster() { cout<<"destructor: "<<name<<endl; delete[] rasterData; delete[] returnString; } //================================================================ // naive copy constructor //================================================================ charRaster::charRaster(const charRaster &cRast) { cout<<"copy constructor: "<<cRast.name<<endl; rowCount = cRast.rowCount; columnCount = cRast.columnCount; size = cRast.size; name = cRast.name; rasterData = new char[size]; returnString = new char[size]; for (int i=0; i<size; i++) { rasterData[i] = cRast.rasterData[i]; } for (int i=0; i<size; i++) { returnString[i] = cRast.returnString[i]; } } //================================================================ //================================================================ bool charRaster::operator < (const charRaster& r) { if (size<r.size) return (true); } #ifdef A_CONST //================================================================ // copy assignment constructor //================================================================ charRaster& charRaster::operator = (const charRaster cRast) { cout<<"copy constructor (=): "<<cRast.name<<endl; rowCount = cRast.rowCount; columnCount = cRast.columnCount; size = cRast.size; name = cRast.name; #ifndef DEEP_COPY // this would be the result of a "shallow copy" // here, the "copy" of the original would be sharing the same data space // this is NOT what we want and is WHY we create a assignment constructor rasterData = cRast.rasterData; returnString= cRast.returnString; #endif #ifdef DEEP_COPY rasterData = new char[size]; returnString = new char[size]; for (int i=0; i<size; i++) { rasterData[i] = cRast.rasterData[i]; } for (int i=0; i<size; i++) { returnString[i] = cRast.returnString[i]; } #endif } #endif //================================================================ // NAIVE VERSION //================================================================ int charRaster::calcOffset2(int row, int column) { return(row*columnCount+column); } //================================================================ // SMART VERSION //================================================================ int charRaster::calcOffset(int row, int column) { if (row < 0) return(-1); if (row >rowCount) return(-2); if (column < 0) return(-3); if (column > columnCount) return(-4); return(row*columnCount+column); } //================================================================ //================================================================ int charRaster::getColumnCount() const { return(columnCount); } //================================================================ //================================================================ int charRaster::getSize() const { return(size); } //================================================================ //================================================================ int charRaster::getRowCount() const { return(rowCount); } //================================================================ //================================================================ void charRaster::setName(string userName) { name = string(userName); } //================================================================ //================================================================ void charRaster::SetNext(charRaster* ptr) { next = ptr; } //================================================================ //================================================================ void charRaster::SetPrev(charRaster* ptr) { prev = ptr; } //================================================================ //================================================================ charRaster* charRaster::GetNext() const{ return(next); } //================================================================ //================================================================ charRaster* charRaster::GetPrev() const{ return(prev); } //================================================================ //================================================================ void charRaster::InsertAfter(charRaster* newRast) { newRast->next = next; newRast->prev = this; if (next!=NULL) next->prev = newRast; next = newRast; } //================================================================ //================================================================ void charRaster::Insert(int position, charRaster* newRast) { charRaster* thisRast = this->Head(); int i = 0; while (i<position) { thisRast = this->GetNext(); i++; } cout<<"i: "<<i<<endl; cout<<*thisRast<<endl; } //================================================================ //================================================================ charRaster* charRaster::Tail() const{ charRaster* cPtr = next; while (cPtr->next!=NULL) cPtr = cPtr->next; return(cPtr); } //================================================================ //================================================================ charRaster* charRaster::Head() { charRaster* cPtr = this; while (cPtr->prev!=NULL) cPtr = cPtr->prev; return(cPtr); } //================================================================ //================================================================ string charRaster::getName() const { return name; } //================================================================ // place char at row, column //================================================================ int charRaster::putVal(int row, int column, char userVal) { int offset = calcOffset(row, column); if (offset < 0) return(-1); rasterData[offset] = userVal; return(0); } //================================================================ // place char at row, column // DANGEROUS // VULNERABLE // also, doesn't make use of existing functionality (putVal) //================================================================ int charRaster::fillRow(int row, int column, char userVal, int count) { int offset = calcOffset(row, column); if (offset < 0) return(-1); for (int i = 0; i<count; i++) { rasterData[offset] = userVal; offset++; } return(0); } //====: ============================================================ // return an entire row of data //================================================================ string charRaster::getRow(int row) { int offset = calcOffset(row,0); if (offset<0) return(""); for (int i=0; i<columnCount; i++) returnString[i] = rasterData[offset+i]; returnString[offset+columnCount] = 0; return("Row "+std::to_string(row)+": "+ returnString); } //================================================================ // return an entire column of data //================================================================ string charRaster::getColumn(int column) const { int offset = column; for (int i=0; i<rowCount; i++) { returnString[i] = rasterData[offset]; offset += columnCount; } returnString[offset+rowCount] = 0; return(returnString); } //============================== // overload "<<" //============================== ostream& operator << (ostream& os, charRaster& mgt) { os<<endl<<"charRaster Data Area: "<<mgt.name<<endl; for (int j=0; j<mgt.getRowCount(); j++){ os<<mgt.getRow(j)<<" "<<endl; } os<<endl; return os; }
//=================================================== //=================================================== template <class T> class myTriplet { T a; T b; T c; public: myTriplet (T first, T second, T third) {a=first; b=second; c=third;} T getmax (); T getmin (); }; template <class T> T myTriplet<T>::getmax () { T retval; retval = a>b? a : b; retval = c>retval? c : retval; return retval; } template <class T> T myTriplet<T>::getmin () { T retval; retval = a<b? a : b; retval = c<retval? c : retval; return retval; }
//================================================== // pair of integers representing a point in 2-space //================================================== #pragma pack(push, 1) struct iPoint { char id; int x, y; }; //================================================== //================================================== struct fPoint { float x, y; }; #pragma pack(pop) //================================================== //================================================== void fOutStr(fPoint* fp); void iOutStr(iPoint* fp);
#include <iostream> #include "myStruct.h" using namespace std; void fOutStr(fPoint* fp) { cout<<"fPntPtr->x,y: "<<fp->x<<","<<fp->y<<endl; } void iOutStr(iPoint* fp) { cout<<"iPntPtr->x,y: "<<fp->id<<","<<fp->x<<","<<fp->y<<endl; }
you can delete this if you want
template <class myType> myType GetMax (myType a, myType b) { return (a>b?a:b); }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue