d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Make Me A Better Programmer - From Step 1
Prev1111213141556Next
Add Reply New Topic New Poll
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 16 2012 10:55pm
i always thought the .length() method returned integers anyways. that would be a useless cast if i was correct.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 17 2012 12:40am
Quote (AbDuCt @ Sep 16 2012 11:55pm)
i always thought the .length() method returned integers anyways. that would be a useless cast if i was correct.


it returns size_t
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 17 2012 07:11am
Quote (Eep @ Sep 17 2012 02:40am)
it returns size_t


size_t is just an unsigned integer and when you cast it to int() you are just casting it to a signed integer. no difference really unless you specifically need one of the other.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 17 2012 09:35am
I didn't know what size t was so I casted them
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 3 2012 01:25am
2nd project complete (reverse polish calculator)

Code
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

struct stack
{
   int value;
   stack *next;
};

stack *calcStack = NULL;

void push(int num);
int pop();
void operation(char op);
void operation2(char op);
bool opCheck(char op);
int main()
{
   char temp[20];
   while(cin >> temp)
   {
       if (isdigit(temp[0]))
       {
           push(atoi(temp));
       }
       else if (opCheck(temp[0]))
       {
           operation(temp[0]);
       }
       else
       {
           operation2(temp[0]);
       }
   }
return 0;
}

void push(int num)
{
   stack *newItem = new(stack);
   newItem->value = num;
   newItem->next = calcStack;
   calcStack = newItem;
}

int pop()
{
   int item = calcStack->value;
   stack *tempPtr = calcStack;
   calcStack = calcStack->next;
   delete tempPtr;
   return item;
}

void operation(char op)
{
   int num1, num2;
   num1 = pop();
   num2 = pop();

   switch(op)
   {
       case '+':
           push(num2+num1);
           break;
       case '-':
           push(num2-num1);
           break;
       case '*':
           push(num2*num1);
           break;
       case '/':
           push(num2/num1);
           break;
       default:
           break;
   }
}

void operation2(char op)
{
   int num;
   switch(op)
   {
       case 'u':
           num = pop();
           push(-num);
           break;
       case 'p':
           num = pop();
           cout << num << endl;
           break;
       case 'e':
           cout << "Thanks for using the reverse polish calculator." << endl;
           exit(0);
       default:
           break;
   }
}

bool opCheck(char op)
{
   if (op == '+' || op == '-' || op == '*' || op == '/')
   {
       return true;
   }
   return false;
}
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 25 2012 06:24pm
My 3rd project worked the first time!! Made a few simple mistakes (missing a semicolon here and there) and had some issues with pointers which I will discuss in a sec

project requirements:

Quote
For your third project, you are to write a program to create a frequency list
of words in a passage.

A frequency list is a list of the distinct words in the
passage together with a cout of the number of times that word occurs.

The input will be read from stdin [i.e., cin]. The output is to be sent to
stdout [i.e., cout], sorted alphabetically on the words.

I want you, for each word that is read, to convert all the characters to lower
case [so that The and the are counted together] and if the word ends in a
punctuation symbol, to remove it [you may assume that there is at most one
punctuation mark].

You can use a binary tree, sorted on the words, to hold the words and their
counts. If a word is seen again, increment the count.


here is the code:

MAIN:

Code
#include <iostream>
#include "tree.h"
#include <string>
#include <ctype.h>

using namespace std;

int main()
{
   string word;
   string::iterator it;
   int strlen;
   tNode *root = NULL;

   while (cin >> word)
   {
       it = word.end()-1;
       strlen = word.length();
       if (ispunct(word[strlen-1]))
       {
           word.erase(it);
       }
       for (int i = 0; i < strlen; i++)
       {
           word[i] = tolower(word[i]);
       }
       tAdd(word, root);
   }
   inOrder(root);

return 0;
}


TREE.CPP

Code
#include "tree.h"
#include <string>
#include <iostream>

using namespace std;

void tAdd(string input, tree & root)
{
   if (root == NULL)
   {
       tNode *node = new(tNode);
       node->word = input;
       node->counter = 1;
       node->left = node->right = NULL;
       root = node;
       return;
   }
   if (input == root->word)
   {
       root->counter++;
       return;
   }
   else if (input < root->word)
   {
       tAdd(input, root->left);
   }
   else tAdd(input, root->right);
}

void inOrder(tree root)
{
   if (root == NULL)
   {
       return;
   }
   inOrder(root->left);
   cout << root->word << " has occurred " << root->counter << " time(s)." << endl;
   inOrder(root->right);
}


TREE.H

Code
#include <iostream>
#include <string>
using namespace std;

struct tNode
{
   string word;
   int counter;
   tNode *left, *right;
};

typedef tNode *tree;

void tAdd(string word, tree & root);
void inOrder(tree root);


proj. said his main was like 67 lines. I wonder why his was so much longer.


Anyways, a funky issue I was having:

for some reason, if I didn't typedef tNode as *tree, and just passed functions something like

Code
void tAdd(string word, tNode *& root);


it was giving me grief. I saw in the professors examples he did the typedef, and it worked again for some reason. What was I doing wrong exactly? The error I was getting was something along the lines of root not being a member class of tNode*& or something.

This post was edited by Eep on Oct 25 2012 06:30pm
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Oct 26 2012 06:51am
This makes me want to pick up C++ again. It's been a really long time since I've written anything meaningful in it and I'm starting to forget everything about it.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 26 2012 02:06pm
I am happy that I got my code to be so small this time around. I probably could have gotten it a bit smaller if I knew a bit more
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 15 2012 11:49am
Soooo I got a new project and I could use some ideas. I posted in the C/C++ forum but I think even general advice could help

here is the thread:

http://forums.d2jsp.org/topic.php?t=65257988&f=122

if anyone could offer some suggestions let me know.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 18 2012 04:48am
I am rather confuzzled still. Could someone (proficient in c++) at least tell me if I am even getting WARMER? Here is my latest pseudo-but-not code:

IDEA: I want a function to parse lines of input. The inputs are coming from a file which is being sent to the program right after compiling eg:
Code
g++ proj4.cpp < some_input_file
I need the function to sort thru lines of inputs like:

Code
Steve1 12Nov1931 M Mary1 17Dec1833 Steve2 Harry1 Sue1 3Jan1959 X
Steve2 31Dec1957 S Xavier1 X


(THESE ARE 2 POSSIBLE LINES OF INPUT I SHOULD EXPECT IN MY PROGRAM)

I need to fill the class's data fields. More explanation is done in that thread I posted above!!

anyway, here is 30 mins worth of me thrusting my head against the keyboard has produced:

Code
void someFunction (string temp, Person & head)
{

int delim;
string data;
head = new(Person);

while (!temp.empty())
{

delim = temp.find_first_of(" ");

data = temp.substr(0,delim-1);

temp.erase(0,delim-1);

if (head.name.empty())
head.name = data;
else if (head.dob.empty())
head.dob = data;
else if (head.marital.empty())
{
head.marital = data;
if (head.marital == S)
continue;
else
{
head->spouse = new(Person);
head->children = new(kids);
continue;
}
}
else if (head->spouse.name.empty())
head->spouse.name = data;


This post was edited by Eep on Nov 18 2012 04:48am
Go Back To Programming & Development Topic List
Prev1111213141556Next
Add Reply New Topic New Poll