PDA

Click to See Complete Forum and Search --> : Newbie in need of sample project to aid in learning process.


HoSs
May 30th, 2000, 11:42 PM
When I learned vb, my dad's friend taught me how to do some things and gave me some sample projects and basically let me learn by trial and error and When i came to this site, was helped greatly, and now I'm moving on to c++ and I have no idea what im doing and if somebody could just give me a sample project of a calculator(that was the first thing i worked on in vb) or something like that, I would greatly appreciate it. thanx in advance! )

May 31st, 2000, 04:26 AM
Me too, im having trouble moving to C++ and a sample project would be great!

May 31st, 2000, 05:46 AM
Well, the calculator based on a GUI maybe a little too much.
I would start with a simple console application.
The following is a code sample for a calculator,
at the command line type the expression you want to calculate, you can use parenthesis +,-,* and /.
As I'm sure you'll notice it is not that simple,
I think in case of c/c++ you should buy a book and take a class.
c/c++ takes time and dedication to learn, unlike VB,
where you pretty much can figure things out as you go.

For this sample you should know what a stack and a queue is.
This sample will actually implement them using what's called a singly linked list (you should find out what that means also if you don't already), but you could use a standard library for these.

Linked list(singly)-imagine a linked list to be a string of marbles.
You can add things to the front of it and to the back of it.
If you need to put one in the middle you don't need to restring the whole thing, all you need to do is break the string between two marbles and put it there(I'm getting an image of an array here, they are similar in structure but in a linked list you don't have to move everything over when you need to insert something in the middle or the front.There are advantages and disadvantages to using a linked list or an array to implement stacks or queues, you should definitely read a book if you want to know more about this).
And you can only go in one direction(single/singly),from the first marble to the last marble.
Stack-is a linked list (in this example)where you insert marbles in front of the first one, and if you remove a marble you can only remove the first one until you have an empty list.(first in, first out or FIFO)
Queue-is a linked list (in this example)where you always insert marbles at the end of the list and remove always the front one.(first in, last out or FILO)

Think of this -> as the . in Text1 . Text
Try to figure out what is going on, on the stack and in the queue.

int intStack::isEmpty() is saying that the function isEmpty
belongs to the class intSatck and returns an integer.

I know this is a lot of reading but if you really want to learn c/c++ this is nothing.

#include<iostream.h>
//additional libraries for string manipulation
#include<ctype.h> //for isdigit()
#include<stdlib.h> //for atoi()
#include<string.h> //for strlen()
#include<string> //not used

#define OPEN 1 //parenthesis state **open**
#define CLOSED 0 //parenthesis state **closed**

using namespace std;//not used

struct NodeType
{
int data; //integer value of node
char type; //node type
char oper; //operator
NodeType *link; //pointer to the next structure
};
typedef NodeType * NodePtr;

//---------------BEGIN STACK IMPLEMENTATION---------------//
class intStack
{
public:
void Push(int newItem,char o,char t);
void Pop();
int TopInt();
int IsEmpty();
char TopOpe();
char TopTyp();
intStack();
~intStack();
private:
NodeType* top;
};

intStack::intStack() //constructor
{
top=NULL;
}

intStack::~intStack() //destructor
{
NodePtr tempPtr;
while(top!=NULL)
{
tempPtr=top;
top=top->link;
delete tempPtr;
}
}

int intStack::IsEmpty()
{
if(top==NULL)
return 1;
else
return 0;
}
int intStack::TopInt() //returns int value on top
{
return top->data;
}

char intStack::TopOpe() //returns top operator
{
return top->oper;
}

char intStack::TopTyp() //returns type of the top node
{
return top->type;
}


void intStack::Pop() //pops one from the stack
{
NodePtr tempPtr=top;
top=top->link;
delete tempPtr;
}

void intStack::Push(int newItem,char o,char t) //pushes one on the stack
{
NodePtr newPtr=new NodeType;
newPtr->data=newItem;
newPtr->oper=o;
newPtr->type=t;
newPtr->link=top;
top=newPtr;
}

//---------------END STACK IMPLEMENTATION---------------//

//---------------BEGIN QUE IMPLEMENTATION---------------//
class intQue
{
public:
void enQue(int newItem, char o, char t);
void buildInfQue(char ex[256]);
void deQue();
void drawQue();
int parCount();
int frontInt();
int termCount() const;
int enQued();
int IsEmpty();
char frontOpe();
char frontTyp();
intQue();
~intQue();
private:
NodeType *front;
NodeType *rear;
int pCount,tCount,eQued;

};

intQue::intQue() //constructor
{
front=rear=NULL;
pCount=tCount=eQued=0;
}

intQue::~intQue() //destructor
{
NodePtr tempPtr;
while(front!=NULL)
{
tempPtr=front;
front=tempPtr->link;
delete tempPtr;
}
}

int intQue::IsEmpty()
{
if(front==NULL)
return 1;
else
return 0;
}
void intQue::drawQue() //display contents of the q
{
NodePtr temp=front;
while(temp)
{
if(temp->type=='O')
cout << " " << temp->oper;
else
cout << " " << temp->data;
temp=temp->link;
}
}

int intQue::enQued() //returns number of items in the q
{
return eQued;
}

int intQue::frontInt() //returns int value of front
{
return front->data;
}

int intQue::termCount() const //returns original number of terms in q
{
return tCount;
}

char intQue::frontOpe() //returns operator of front
{
return front->oper;
}

char intQue::frontTyp() //returns type of front
{
return front->type;
}

int intQue::parCount() //returns number of parenthesis
{
return pCount;
}

void intQue::enQue(int newItem,char o,char t) //enters item in q
{
NodePtr newPtr=new NodeType;
newPtr->data=newItem;
newPtr->oper=o;
newPtr->type=t;
newPtr->link=NULL;
if(front==NULL)
front=rear=newPtr;
else
{
rear->link=newPtr;
rear=newPtr;
}
eQued++;
}

void intQue::deQue() //removes item from q
{
NodePtr tempPtr=front;
front=tempPtr->link;
delete tempPtr;
if(front==NULL)
rear=NULL;
eQued--;
}

void intQue::buildInfQue(char ex[256]) //builds q from line
{
int exLen=0;
exLen=strlen(ex);
for(int i=0;i<exLen;i++)
{
switch(ex[i])
{
case '+':
case '-':
case '*':
case '/':
enQue(0,ex[i],'O');
tCount++;
break;
case '(':
case ')':
enQue(0,ex[i],'P');
pCount++;tCount++;
break;
case ' ':
break;
default:
int j=i,k=0,num;char tcs[10]={0};
do
{
tcs[k]=ex[j];
j++;k++;
}while(isdigit(ex[j]));
num=atoi(tcs);
i=j-1;
enQue(num,'N','N');
tCount++;
}
}
}
//---------------END QUE IMPLEMENTATION---------------//

void main()
{
int Evaluate(int t1,int t2, char op);
intStack opStack,numStack;
intQue infQue,posQue;

int parIs,push=0,term1,term2;
char oper,ex[256]={0};
cout << "Infix: ";
cin.getline(ex,256,'\n');

infQue.buildInfQue(ex);

for(int i=0;i<infQue.termCount();i++)
{
if(infQue.frontTyp()=='P')
{
if(infQue.frontOpe()=='(')
parIs=OPEN;
if(infQue.frontOpe()==')')
parIs=CLOSED;
}
if(infQue.frontTyp()=='N')
posQue.enQue(infQue.frontInt(),infQue.frontOpe(),infQue.frontTyp());
if(infQue.frontTyp()=='O' && parIs)
{
opStack.Push(0,infQue.frontOpe(),infQue.frontTyp());
push++;
}
if(!parIs)
{
posQue.enQue(0,opStack.TopOpe(),opStack.TopTyp());
opStack.Pop();
push--;
parIs=OPEN;
}
infQue.deQue();
}
for(int j=0;j<push;j++)
{
posQue.enQue(0,opStack.TopOpe(),opStack.TopTyp());
opStack.Pop();
}
cout << "Postfix: ";
posQue.drawQue();
cout << endl << "Value: ";
while(!posQue.IsEmpty())
{
if(posQue.frontTyp()=='N')
{
numStack.Push(posQue.frontInt(),posQue.frontOpe(),posQue.frontTyp());
posQue.deQue();
}
else
{
oper=posQue.frontOpe();
posQue.deQue();
term1=numStack.TopInt();
numStack.Pop();
term2=numStack.TopInt();
numStack.Pop();
numStack.Push(Evaluate(term1,term2,oper),'N','N');
}
}
cout << numStack.TopInt() << endl;
}

int Evaluate(int term1,int term2,char op)
{
switch(op)
{
case '+':
return(term1 + term2);
case '-':
return(term2-term1);
case '*':
return(term1 * term2);
case '/':
return(term2/term1);
default:
cout << "\n\tInvalid operator.";
return 0;
}
}

[Edited by Faust on 05-31-2000 at 06:55 PM]

HoSs
May 31st, 2000, 06:20 AM
thank you, Faust, but when I try to execute it it says 20 errors.

May 31st, 2000, 12:35 PM
If you are using a compiler other than MSVC++ you should probably take out everything that pertains to the string object, the #include<string> and using namespace std,
otherwise everything should be fine.
Let me know if that works or better yet tell me what compiler you are using and paste the errors that come up.