|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
C++ date help please
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||
|
WaterCooler
|
Part of my HW is to create a display of transactions with debits/credits/etc. I want them to be listed reverse chronologically but am having trouble getting the mind set on how to do this. The information is in an array of classes and the date is split into the day, month, and year as separate variables inside the class. I need to separate the customer's transactions via account number from the array then put those transactions in reverse chronological order.
Any help would be greatly appreciated . . . .it's been a long project and i think my brain is just fried. ![]() >> oh i must also use an overloaded insertion operator to display those accounts. . .i can handle that (i think) but i didnt know if it would make a difference.
__________________
XPS M1330: T7500, 4GB, 13.3" LED WXGA, 128 8400 GS, 120GB 5400RPM, 802.11 AGN, 9 cell, Fingerprint, Ultimate x64, 4 Year Accidental Damage/lojack/warranty. __________________
|
||||||||||||
|
|
|
|
#2 (permalink) | |||||||||||||
|
4.0ghz
|
Use a for loop backwards. That is, start with the upper end of the array size and end at zero, with cntr-- as the operator (cntr is the counter).
__________________
|
|||||||||||||
|
|
|
|
#3 (permalink) | |||||||||||||
|
110100001101001111000
|
Quote:
FOR latest_transaction to first_transaction IF transaction_acct_number == user_acct_number PRINT current_transaction That way you'll get the reverse chronological and it'll be filtered by acct. number. Not sure if that's what you need, but that's what I gathered. EDIT: If you know how to use stacks, this will GREATLY simplify things. Create a stack called trans_history or something for each account number. After each transcation, check the account number and push the transaction onto that users stack. All you have to do for the transaction history is POP all the transactions from the user's trans_history and you're done. That's actually how I'd do this problem, now that I think of it. If you need help implementing a stack using normal arrays, feel free to ask.
__________________
Last edited by C-bro : 03-18-07 at 01:40 PM. |
|||||||||||||
|
|
|
|
#4 (permalink) | ||||||||||||
|
WaterCooler
|
ok that clarifies some things. . . but how would i go about joining the three parts for the date? . . . I feel like im missing something very simple . . UGG . . . thanks for the help you two . . reps to both
![]()
__________________
XPS M1330: T7500, 4GB, 13.3" LED WXGA, 128 8400 GS, 120GB 5400RPM, 802.11 AGN, 9 cell, Fingerprint, Ultimate x64, 4 Year Accidental Damage/lojack/warranty. __________________
|
||||||||||||
|
|
|
|
#5 (permalink) | ||||||||||||
|
WaterCooler
|
oooh. . . so i should do this:
PHP Code:
__________________
XPS M1330: T7500, 4GB, 13.3" LED WXGA, 128 8400 GS, 120GB 5400RPM, 802.11 AGN, 9 cell, Fingerprint, Ultimate x64, 4 Year Accidental Damage/lojack/warranty. __________________
|
||||||||||||
|
|
|
|
#6 (permalink) | |||||||||||||
|
4.0ghz
|
Assuming it is organized chronologically, that should work barring any minor syntax errors. I am not as sharp as I used to be with C++, so I can't guarantee anything. If you need to organize them by account number, then you will need some while loop to determine if it is an existing account or a new one and proceed from there as to how you will store the transaction information. The reverse for loop suggestion was me thinking you had everything listed in chronological order and you had to reverse it. You can also do two separate data sorts. That is, you can organize them by reverse chronological order and then a separate function can organize them by account number if it gets too confusing to do it in one step.
__________________
|
|||||||||||||
|
|
|
|
#7 (permalink) | ||||||||||||
|
110100001101001111000
|
That for-loop will most likely work, but I would really suggest implementing my earlier stack idea. I'll do a quick demo of how it would work with this list of transactions: Say we have acct numbers 01 and 02 and the following transactions:
date - account # - type - amt. 3/15/2007 - 02 - deposit - 2000 3/16/2007 - 01 - deposit - 5000 3/17/2007 - 02 - withdraw - 500 3/18/2007 - 01 - withdraw - 200 Those were the 4 transactions that happened. When they happen you'll put them onto a stack for each account. So every acct will have a stack of transactions. If you looks at this this independently, you'd see: Account 01 stack: 3/18/2007 - 01 - withdraw - 200 3/16/2007 - 01 - deposit - 5000 Account 02 stack: 3/17/2007 - 02 - withdraw - 500 3/15/2007 - 02 - deposit - 2000 Since you push the items onto each respective stack as they happen, they're always sitting there in reverse chronological order. To view all the transactions for each customer you'd just go. AccountXX.pop() and that would return the most recent transaction for the customer. That way you could specifiy only the 5 or 10 most recent transactions, or give the entire history of the client. In other words, if you were to pop off the entire account 01 stack, you'd see a list like this, since stacks always push and pop from the top of the list. 3/18/2007 - 01 - withdraw - 200 3/16/2007 - 01 - deposit - 5000 This gives as many transactions you want, for a given account number, and it does so in reverse chronological order.
__________________
|
||||||||||||
|
|
|
|
#8 (permalink) | |||||||||||||
|
4.0ghz
|
I want account 01!
__________________
|
|||||||||||||
|
|
|
|
#9 (permalink) | ||||||||||||
|
WaterCooler
|
Thanks guys. . .
>> C-Bro I would do stack but a. I dont know how to and i would learn but b. my professor is one of those that would likely get mad at me applying things we havent learned in class yet I ended up saying F' it and went oldest to newest using a complicated series of for loops. . . ill look into stacks for the future and inquire about them next class. I realized that part of the requirement was to calculate the balance at the time of display. . .which ment i had to use the beginning balance (which is often the earliest) first. . . . im sick of this class and am just taking the past of least resistance now But thanks for all your help. . . its good to know i have a place where i can get help on the subject. there are only 4 people in my class and two are failing. . . and my instructor is an ass so he's little help.
__________________
XPS M1330: T7500, 4GB, 13.3" LED WXGA, 128 8400 GS, 120GB 5400RPM, 802.11 AGN, 9 cell, Fingerprint, Ultimate x64, 4 Year Accidental Damage/lojack/warranty. __________________
Last edited by DanNEBTD : 03-19-07 at 12:17 AM. |
||||||||||||
|
|
|
|
#10 (permalink) | |||||||||||||
|
110100001101001111000
|
Quote:
Good luck with it. Stacks would be the way to go, but like you said, if you haven't learned it yet, you most likely can't use it. Not sure if you can read this, but this was a very simple (and messy) implementation of a stack that I had to write. It's in Java but you should be able to see what they're all about. They're very handy. Code:
public class MyStack <E>{
protected int capacity; //Max number of elements
protected E S[]; //Array of tyep E
protected int top = -1; //Index of the top of the stack
//Constructors
public MyStack(){
capacity = 1000;
S =(E[]) new Object[capacity];
}
public MyStack (int i){
capacity = i;
S =(E[]) new Object[capacity];
}
//Returns size of Stack
public int getSize(){
return (top+1);
}
//Checks if Stack is empty
public boolean isEmpty(){
return (top<0);
}
//Pushes element onto stack
public void push (E e){
S[++top] = e;
}
//Pops element off stack
public E pop() throws MyEmptyStackException{
if (top < 0){
throw new MyEmptyStackException ("Stack Is Empty!");
}
else{
E e = S[top];
S[top--] = null;
return (e);
}
}
//Returns the top element
public E peek() throws MyEmptyStackException{
if (top < 0){
throw new MyEmptyStackException ("Stack Is Empty!");
}
else
return (S[top]);
}
}
__________________
|
|||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|