Global var in Balance calculation | | |
Hello everyone...
Anybody can help me on this question, please?
i´ve this code in my application:
/*for Card creation*/
typedef struct card
{
etc,etc
} Card;
typedef Card *PtrCrt;
/*For trans creation*/
typedef struct trans
{
etc,etc
} Trans;
typedef Trans *PtrTrans;
I need to calculate the Balance of every card and how put this verification
in one function?! | | | | re: Global var in Balance calculation
"Maria Mela" <h4fun@netvisao.ptwrote in message
news:newscache$e1d8cj$nfj$1@newsfront4.netvisao.pt ... Quote:
Hello everyone...
Anybody can help me on this question, please?
i´ve this code in my application:
>
/*for Card creation*/
typedef struct card
{
etc,etc
} Card;
typedef Card *PtrCrt;
>
/*For trans creation*/
typedef struct trans
{
etc,etc
} Trans;
typedef Trans *PtrTrans;
>
I need to calculate the Balance of every card and how put this
verification in one function?!
int balance(const Card *c)
{
int result;
/* calculate */
return result;
}
int main(void)
{
Card c = { /* etc. */ };
printf("balance = %d\n", balance(&c));
return 0;
}
For more specific advice, ask more specific questions.
-Mike | | | | re: Global var in Balance calculation
Maria Mela wrote: Quote:
Hello everyone...
Anybody can help me on this question, please?
i´ve this code in my application:
>
/*for Card creation*/
typedef struct card
{
etc,etc
} Card;
typedef Card *PtrCrt;
>
/*For trans creation*/
typedef struct trans
{
etc,etc
} Trans;
typedef Trans *PtrTrans;
>
I need to calculate the Balance of every card and how put this verification
in one function?!
>
>
Please be more specific. What is the balance of a card? What do you
need to verify? How are you representing the aggregation of cards?
As an array? A singly-linked list? Many singly-linked lists?
P.S. http://www.nls.net/mp/413/sea.ZIP
Self-expanding arrays. (This is the code your college professor
warned you about.) :) :) :) | | | | re: Global var in Balance calculation
Thks for your help... But to be more specific here´s one part of my code...
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <alloc.h>
#include <string.h>
typedef enum observations
{
Item1=1,Item2,Item3,Item4,Item5
}Observations;
typedef enum trans_type
{
CRG=1, DBT,TRF
}Trans_type;
typedef enum utente
{
Student=1, Functionary ,Professor
}Utente;
typedef struct data
{
int day, month, year;
}Data;
typedef struct card
{
int num_card;
char name[255];
Utente utente;
Data data;
float balance;
char morada[60];
struct card *next;
} Card;
typedef Card *PtrCrt;
typedef struct transaccions
{
int codigo;
char nome[60];
Data data;
Trans_tipo trans_tipo;
float montante,crg;
Observacoes observacoes;
char local_treinos[60];
char nome_treinador[60];
struct trans *next;
PtrCrt p_cartao;
} Trans;
typedef Trans *PtrTrans;
/*------------------------Functions------------------------*/
void Insert_Card (PtrCrt *lista);
void Change_Card (PtrCrt *lista);
void Remove_Card (PtrCrt *lista);
void View_Cards (PtrCrt *lista);
void Delet_Cards (PtrCrt *lista);
int View_if_Cards (PtrCrt *lista, int cod, int ver);
void Insert_Trans (PtrTrans *lista);
void Remove_Trans (PtrTrans *lista);
void View_Trans (PtrTrans *lista);
void Delet_Trans (PtrTrans *lista);
int View_if_Trans (PtrTrans *lista, int cod, int ver);
void Error (char *msg);
PtrCrt Adress_Card (PtrCrt *lista, int cod);
PtrTrans Adress_Trans (PtrTrans *lista, int cod);
void View_Cards_Assoc (PtrCrt *lista);
void View_Card_Trans (PtrTrans *lista);
void Menu_Cards (void);
void Menu_Transaccions (void);
void Menu_Listall (void);
I need to calculate the Balance of every card in all new transaccions and
how put this verification
in one function?!
It´s better create one more strcut for movements (transaccions) cards?
I´m confused...
"Jeff Mullen" <413@nls.netescreveu na mensagem
news:a3da$45b46c27$49d77f6$12003@ALLTEL.NET... Quote:
Maria Mela wrote: Quote:
>Hello everyone...
>Anybody can help me on this question, please?
>i´ve this code in my application:
>>
>/*for Card creation*/
>typedef struct card
>{
>etc,etc
>} Card;
>typedef Card *PtrCrt;
>>
>/*For trans creation*/
>typedef struct trans
>{
>etc,etc
>} Trans;
>typedef Trans *PtrTrans;
>>
>I need to calculate the Balance of every card and how put this
>verification in one function?!
>>
>>
>
Please be more specific. What is the balance of a card? What do you
need to verify? How are you representing the aggregation of cards?
As an array? A singly-linked list? Many singly-linked lists?
>
P.S.
> http://www.nls.net/mp/413/sea.ZIP
>
Self-expanding arrays. (This is the code your college professor
warned you about.) :) :) :)
| | | | re: Global var in Balance calculation
Maria Mela wrote: Quote:
>
Thks for your help... But to be more specific here´s one part of my code...
>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <alloc.h>
#include <string.h>
Please don't top-post. Your answer belongs after (or intermixed
with) the material to which you reply, after snipping. See the
references in my sig. below.
The above code is off-topic here, since conio.h and alloc.h are
non-standard.
--
Some informative links:
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/ (taming google)
<http://members.fortunecity.com/nnqweb/ (newusers) | | | | re: Global var in Balance calculation
Jeff Mullen wrote, On 22/01/07 07:47:
<snip> Extracts from the code in the archive above...
from sea.h:
| #ifndef _SEA_H_
|
| #define _SEA_H_
Names starting with an underscore followed by an upper case letter are
reserved for use by the implementation. Don't do that.
from sea.c:
| else if ((ST_sea = malloc(sizeof(SEA))) == NULL)
Better to use:
else if ((ST_sea = malloc(sizeof *ST_sea))) == NULL)
Then you do not have to worry about getting the type correct. Very
useful if the type ever gets changed.
| {
| ST_sea = NULL;
| } /* if */
Isn't assigning NULL to ST_sea a bit pointless when you have just found
that it is equal to NULL? This smells like it has not been properly
thought out to me.
| if ((ST_sea->V_items = realloc(ST_sea->V_items, ST_sea->i_num_alloc
| * ST_sea->i_item_size)) == NULL)
If realloc fails here you have just lost your pointer to the old, still
valid memory. You should always assign the result of realloc to a
temporary variable for testing and then, only if realloc succeeded
overwrite your old pointer.
Also you use something called WITHIN the definition of which is not
provided. Possibly it is defined in friendly.h, but you don't provide
that in the ZIP you provide that in the ZIP you pointed people at.
I've not looked any further because it currently is not in a compilable
state.
--
Flash Gordon | | | | re: Global var in Balance calculation
CBFalconer said: Quote:
Maria Mela wrote: Quote:
>>
>Thks for your help... But to be more specific here´s one part of my
>code...
>>
>#include <stdio.h>
>#include <stdlib.h>
>#include <conio.h>
>#include <alloc.h>
>#include <string.h>
<snip> Quote:
The above code is off-topic here, since conio.h and alloc.h are
non-standard.
I don't entirely agree, or at least I think your explanation is incomplete.
It is entirely possible that conio.h and alloc.h are user-defined headers
whose source the OP unaccountably forgot to include, which might well be
written entirely in ISO C.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www. | | | | re: Global var in Balance calculation
Maria Mela wrote: Quote:
Thks for your help... But to be more specific here´s one part of my code...
>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <alloc.h>
#include <string.h>
>
typedef enum observations
{
Item1=1,Item2,Item3,Item4,Item5
>
}Observations;
>
>
typedef enum trans_type
{
CRG=1, DBT,TRF
>
}Trans_type;
>
typedef enum utente
{
Student=1, Functionary ,Professor
>
}Utente;
>
typedef struct data
{
int day, month, year;
>
}Data;
>
>
typedef struct card
{
int num_card;
char name[255];
Utente utente;
Data data;
float balance;
char morada[60];
struct card *next;
>
} Card;
>
typedef Card *PtrCrt;
>
typedef struct transaccions
{
int codigo;
char nome[60];
Data data;
Trans_tipo trans_tipo;
float montante,crg;
Observacoes observacoes;
char local_treinos[60];
char nome_treinador[60];
struct trans *next;
PtrCrt p_cartao;
>
} Trans;
>
typedef Trans *PtrTrans;
/*------------------------Functions------------------------*/
>
void Insert_Card (PtrCrt *lista);
void Change_Card (PtrCrt *lista);
void Remove_Card (PtrCrt *lista);
void View_Cards (PtrCrt *lista);
void Delet_Cards (PtrCrt *lista);
int View_if_Cards (PtrCrt *lista, int cod, int ver);
>
void Insert_Trans (PtrTrans *lista);
void Remove_Trans (PtrTrans *lista);
void View_Trans (PtrTrans *lista);
void Delet_Trans (PtrTrans *lista);
int View_if_Trans (PtrTrans *lista, int cod, int ver);
>
>
void Error (char *msg);
PtrCrt Adress_Card (PtrCrt *lista, int cod);
PtrTrans Adress_Trans (PtrTrans *lista, int cod);
>
void View_Cards_Assoc (PtrCrt *lista);
void View_Card_Trans (PtrTrans *lista);
void Menu_Cards (void);
void Menu_Transaccions (void);
>
void Menu_Listall (void);
I need to calculate the Balance of every card in all new transaccions and
how put this verification
in one function?!
It´s better create one more strcut for movements (transaccions) cards?
I´m confused...
>
>
"Jeff Mullen" <413@nls.netescreveu na mensagem
news:a3da$45b46c27$49d77f6$12003@ALLTEL.NET... Quote:
>Maria Mela wrote: Quote:
>>Hello everyone...
>>Anybody can help me on this question, please?
>>i´ve this code in my application:
>>>
>>/*for Card creation*/
>>typedef struct card
>>{
>>etc,etc
>>} Card;
>>typedef Card *PtrCrt;
>>>
>>/*For trans creation*/
>>typedef struct trans
>>{
>>etc,etc
>>} Trans;
>>typedef Trans *PtrTrans;
>>>
>>I need to calculate the Balance of every card and how put this
>>verification in one function?!
>>>
>>>
>Please be more specific. What is the balance of a card? What do you
>need to verify? How are you representing the aggregation of cards?
>As an array? A singly-linked list? Many singly-linked lists?
>>
>P.S.
>>
> http://www.nls.net/mp/413/sea.ZIP
>>
>Self-expanding arrays. (This is the code your college professor
>warned you about.) :) :) :)
>
>
Thank you for the additional information, but you still didn't answer
the first question I asked: what is the "balance" of a card?
ASSUMING that you are storing your cards in a singly-linked list,
and that you have a header pointer around somewhere, you can do
your computations using a FOR loop. Assuming that the header
pointer, called "hand," is in your code somewhere, and that you
have a function called computer_balance that takes a pointer to
card as its argument and computes the balance for that card:
Card *scrutiny;
int balance = 0;
for (scrutiny = hand; scrutiny != NULL; scrutiny = scrutiny->next)
{
balance += compute_balance(scrutiny);
} /* for */
should be close to what you're asking for, as I understand it
based on the fragmentary information that you have given. | | | | re: Global var in Balance calculation
Flash Gordon wrote: Quote:
Jeff Mullen wrote, On 22/01/07 07:47:
>
<snip>
> >
Extracts from the code in the archive above...
>
from sea.h:
>
| #ifndef _SEA_H_
|
| #define _SEA_H_
>
Names starting with an underscore followed by an upper case letter are
reserved for use by the implementation. Don't do that.
>
from sea.c:
| else if ((ST_sea = malloc(sizeof(SEA))) == NULL)
>
Better to use:
else if ((ST_sea = malloc(sizeof *ST_sea))) == NULL)
Then you do not have to worry about getting the type correct. Very
useful if the type ever gets changed.
>
| {
| ST_sea = NULL;
| } /* if */
>
Isn't assigning NULL to ST_sea a bit pointless when you have just found
that it is equal to NULL? This smells like it has not been properly
thought out to me.
>
| if ((ST_sea->V_items = realloc(ST_sea->V_items,
ST_sea->i_num_alloc
| * ST_sea->i_item_size)) == NULL)
>
If realloc fails here you have just lost your pointer to the old, still
valid memory. You should always assign the result of realloc to a
temporary variable for testing and then, only if realloc succeeded
overwrite your old pointer.
>
Also you use something called WITHIN the definition of which is not
provided. Possibly it is defined in friendly.h, but you don't provide
that in the ZIP you provide that in the ZIP you pointed people at.
>
I've not looked any further because it currently is not in a compilable
state.
Thank you for looking my code over. I hope that all the people
whom you provide with unsolicited reviews will be as forthright,
and in as much a position to make changes regarding your constructive
criticism, as I am.
As a matter of internet etiquette, you should have sent me a private
email about this--not because the message might air some "dirty
laundry" of mine (I don't care about that if it improves the code),
but because, as I am rather busy, the message on a usenet list might
expire before I can get back to it. Thankfully, that was not the
case this time. You should also consider asking people whether
they would like their code reviewed when they do not explicitly
request this service. As I pointed out, I appreciate your efforts,
but you should be more sensitive to the possibility of other points
of view.
Jeff | | | | re: Global var in Balance calculation
Jeff Mullen <413@nls.netwrites: Quote:
As a matter of internet etiquette, you should have sent me a private
email about this--not because the message might air some "dirty
laundry" of mine (I don't care about that if it improves the code),
but because, as I am rather busy, the message on a usenet list might
expire before I can get back to it.
It's not normal practice to send email in response to a Usenet
article, unless the email is not in line with the topic of the
group. Generally it's people who view Usenet as "write-only" who
ask for email replies. Quote:
You should also consider asking people whether they would like
their code reviewed when they do not explicitly request this
service.
It's also not normal practice to ask about this in comp.lang.c.
Generally, we assume that if you're posting code here, or posting
about code here, you'd like us to look it over.
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield | | | | re: Global var in Balance calculation
Jeff Mullen <413@nls.netwrites: Quote:
Flash Gordon wrote: Quote:
Jeff Mullen wrote, On 22/01/07 07:47:
<snip> Extracts from the code in the archive above...
from sea.h:
[snip] Quote:
>
Thank you for looking my code over. I hope that all the people
whom you provide with unsolicited reviews will be as forthright,
and in as much a position to make changes regarding your constructive
criticism, as I am.
>
As a matter of internet etiquette, you should have sent me a private
email about this--not because the message might air some "dirty
laundry" of mine (I don't care about that if it improves the code),
but because, as I am rather busy, the message on a usenet list might
expire before I can get back to it. Thankfully, that was not the
case this time. You should also consider asking people whether
they would like their code reviewed when they do not explicitly
request this service. As I pointed out, I appreciate your efforts,
but you should be more sensitive to the possibility of other points
of view.
If you don't want your code publicly reviewed, I suggest not making it
publicly available. If you don't want it reviewed in this newsgroup,
don't post a link to it in this newgroup. The review was as much for
the benefit of other readers who saw the link as it was for you.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. | | | | re: Global var in Balance calculation
Jeff Mullen said:
<snip> Quote:
Thank you for looking my code over. I hope that all the people
whom you provide with unsolicited reviews will be as forthright,
and in as much a position to make changes regarding your constructive
criticism, as I am.
>
As a matter of internet etiquette, you should have sent me a private
email about this
Wrong. Post here, read here.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www. | | | | re: Global var in Balance calculation
On Thu, 01 Feb 2007 19:43:37 -0500, in comp.lang.c , Jeff Mullen
<413@nls.netwrote: Quote:
>As a matter of internet etiquette, you should have sent me a private
>email about this--
This is usenet: write here, read here. Quote:
>because, as I am rather busy, the message on a usenet list might
>expire before I can get back to it.
So you mean that because /you're/ busy, the dozens of people reading
your posting have to send you individual messages. Don't you think
thats a bit absurd? Quote:
>case this time. You should also consider asking people whether
>they would like their code reviewed when they do not explicitly
>request this service.
One of the things that happens to code here in CLC is that it gets
reviewed. If you don't want your code reviewed, the fix is - don't
post it or link to it.
Of course, if you don't post it, then its awfully hard for people to
help you with any problems with it. You're likely to get lots of
replys along hte lines of "my crystal ball is broken, but I think its
line 42".
--
Mark McIntyre
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan | | | | re: Global var in Balance calculation
Keith Thompson wrote, On 02/02/07 02:13: Quote:
Jeff Mullen <413@nls.netwrites: Quote:
>Flash Gordon wrote: Quote:
>>Jeff Mullen wrote, On 22/01/07 07:47:
<snip> Quote: Quote: Quote:
>>Extracts from the code in the archive above...
>>from sea.h:
[snip] Quote:
>Thank you for looking my code over. I hope that all the people
>whom you provide with unsolicited reviews will be as forthright,
<snip> Quote:
If you don't want your code publicly reviewed, I suggest not making it
publicly available. If you don't want it reviewed in this newsgroup,
don't post a link to it in this newgroup. The review was as much for
the benefit of other readers who saw the link as it was for you.
For the record, I consider any posting of code here (or links to such
code) to be, amongst other things, a request for it to be reviewed so no
review can be considered unsolicited. This includes any code I post, of
course, and whilst I don't like having my errors pointed out I like even
less having the errors left uncorrected.
I agree with the rest of what Ben, Keith and Richard posted as well.
--
Flash Gordon |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|