Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You basically want a constructor</p> <p>define your type,</p> <pre><code>typedef struct stats { //char playername[100]; //you probably want a place to record player names... int singles; int doubles; int triples; int home_runs; int walks; int be; //base on error int Ko; //strikeout int go; //groundout int fo; //flyout int sc; //sacrifice out int rbi; //runs batted in, reflects scoring, sacrifices, etc int at_bats; //calculated int hits; float BA; float OBP; float PWR; float HR_ratio; int TB; int SA; } StatObj; </code></pre> <p>Now you need something to create/initialize a StatObj. Some languages call this a constructor,</p> <pre><code>StatObj* StatNew() //maybe pass playername? { StatObj* so = malloc(sizeof(StatObj)); //recorded so-&gt;singles = 0; so-&gt;doubles = 0; so-&gt;triples = 0; so-&gt;homeruns = 0; so-&gt;walks = 0; so-&gt;at_bats = 0; so-&gt;be = 0; //base on error so-&gt;ko = 0; //strikeout so-&gt;go = 0; //groundout so-&gt;fo = 0; //flyout so-&gt;sc = 0; //sacrificeout so-&gt;rbi = 0; //runs batted in, reflects scoring, sacrifices, etc //calculated so-&gt;hits = 0; so-&gt;BA = 0.0; so-&gt;PWR = 0.0; so-&gt;OBP = 0.0; so-&gt;HR_ratio = 0.0; so-&gt;TB = 0; so-&gt;SA = 0; //what about walks, HBP (hit by pitch), BoE (base on error) OBP (on base pct)? return so; } </code></pre> <p>You will need a function to (re)calculate your stats.</p> <pre><code>void calculate(StatObj* sta) { int hits, onbase; float power; if(!sta) return; hits = sta-&gt;singles + sta-&gt;doubles + sta-&gt;triples + sta-&gt;homeruns; onbase = hits + sta-&gt;walks + sta-&gt;be; power = (sta-&gt;singles + 2*sta-&gt;doubles + 3*sta-&gt;triples + 4*sta-&gt;homeruns) //what about sacrifices? //calculate other stats as you define them sta-&gt;hits = hits; sta-&gt;BA = (hits*1.0)/(sta-&gt;at_bats*1.0); sta-&gt;OBP = (onbase*1.0)/(sta-&gt;at_bats*1.0); sta-&gt;PWR = (power*1.0)/(sta-&gt;at_bats*1.0); HR_ratio = (sta-&gt;homeruns*1.0)/(sta-&gt;at_bats*1.0); //sta-&gt;TB; //sta-&gt;SA; } int StatPrint(StatObj* sta) { if(!sta) return; /* printing the output using arrays */ printf("%6d",sta-&gt;hits); printf("BA\t\t\t\t%6.4f", sta-&gt;BA); printf("OBP\t\t\t\t%6.4f", sta-&gt;OBP); printf("TB\t\t\t%6f", sta-&gt;TB); printf("PWR\t\t\t\t%6.4f",sta-&gt;PWR); printf("HR%\t\t\t\t%6.4f",sta-&gt;HR_ratio); printf("SA\t\t\t\t%6.4f", sta-&gt;SA); printf("\n"); //et cetera } </code></pre> <p>You will want a function to record each atbat, and the result. You can pass the result of each atbat as a symbol/value, which then updates your raw statistics. Then you can have a function to (re)calculate your stats. Here is the record_atbat function,</p> <pre><code>// 1 single, 2 double, 3 triple, 4 homerun // x strikeout, k strikeout, e error, w walk // f - flyout, g - groundout, s - sacrifice, ... // do you want to record double play? triple play? void record_atbat( StatObj* sta, int ab, int result, int rbi ) { if(!sta) return; sta-&gt;at_bats++; sta-&gt;rbi += rbi; switch(result) { case 1: case '1': sta-&gt;singles++; break; case 2: case '2': sta-&gt;doubles++; break; case 3: case '3': sta-&gt;triples++; break; case 4: case '4': sta-&gt;homeruns++; break; case 'w': sta-&gt;walks++; break; case 'e': sta-&gt;be++; break; // k/x=strikeout, f=flyout, g=groundout, s=sacrifice, ... case 'k': case 'x': sta-&gt;ko++; break; case 'f': sta-&gt;fo++; break; case 'g': sta-&gt;go++; break; case 's': sta-&gt;sc++; break; //base on error, affects on-base percentage default: break; } calculate(sta); //keep stats current } </code></pre> <p>You will need to create at least one StatObj instance,</p> <pre><code>int main() { int i, hits; StatObj* sta = StatNew(); //you could create an array of these, one per player //StatObj* players[100]; //you probably want to record a bunch of players... record_atbat( sta, 1, 0 ); //single record_atbat( sta, 'k', 0 ); //strikeout record_atbat( sta, 2, 1 ); //double record_atbat( sta, 'w', 0 ); //walk record_atbat( sta, 'k', 0 ); //strikeout hits_number=calculate_hits_number(sta); /* calling function */ calculate(sta); StatPrint(sta); return (0); } </code></pre> <p>You will want functions to serialize (write to file), and deserialize (read from file).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload