Thursday, October 30, 2008

Pointer Pointer

Well i like to share some code that i worked on, for a school. It goal was to show the power of pointers.
 
  1: #include <string.h>
  2: #include <stdio.h>
  3: #include <stdlib.h>
  4: #include <ctype.h>
  5: #include <windows.h>
  6: #include <conio.h>
  7: #include <iostream>
  8: #define LOCATE(r, c) GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfo);csbiInfo.dwCursorPosition.X = c;csbiInfo.dwCursorPosition.Y = r; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), csbiInfo.dwCursorPosition);
  9: 
 10: //Used to chnage console collors in windows
 11: CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
 12: HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 
 13: HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
 14: 
 15: using namespace std;
 16: 
 17: void paintScreen(int years), moveData(int *loca, char *buffer, float **table), moveLoc(int *loc, int year, char dir), viewTable(float **table, int *year);
 18: double findTotal(float **, int);
 19: void printCollumTotal(float **table, int *year);
 20: double findCollumTotal(float *table, int *year);
 21: float **dArray( int nRows, int nCols);
 22: 
 23: 
 24: int main()
 25: {
 26:     int loc[] = {0,0}, year = 0, i=0; //loc is the x,y loc on table, year numver of rows, i index in buffer
 27:     bool breakVar = 0; //used to brak to the outer while loop, on halt
 28:     unsigned char c;//input char 0-256
 29:     static char charBuffer[11];//buffer for imput string
 30: 
 31:     system("CLS");
 32:     puts("How many years do you want to enter? ");
 33:     scanf("%i",&year);
 34:     float **ppTable = dArray(year, 5); //makes the array/table
 35:     
 36:     paintScreen(year);
 37:     
 38:     while(!breakVar)
 39:     {
 40:         i=0;
 41:         charBuffer[0] = '\0';
 42:         while(!((c=getch()) == 9 || c == 13 || c == 224 || c == 'Q' || c == 'P' || c == 'V'))
 43:         {
 44:             if(*(loc + 1) == 0)
 45:             {
 46:                 if(c>='0' && c <= '9')
 47:                 {
 48:                     charBuffer[i++]=c;
 49:                     putch(c);
 50:                     if(i == 4)
 51:                     {
 52:                         charBuffer[i]='\0';
 53:                         moveData(loc,charBuffer,ppTable);
 54:                         moveLoc(loc,year,'R');
 55:                         i=0;
 56:                         break;
 57:                     }
 58:                 }
 59:             }
 60:             else
 61:             {
 62:                 if(c>='0' && c <= '9' || c == '.')
 63:                 {
 64:                     charBuffer[i++]=c;
 65:                     putch(c);
 66:                     if(i == 10)
 67:                     {
 68:                         charBuffer[i]='\0';
 69:                         moveData(loc,charBuffer,ppTable);
 70:                         printCollumTotal(ppTable,&year);
 71:                         moveLoc(loc,year,'R');
 72:                         i = 0;
 73:                         break;
 74:                     }
 75:                 }    
 76:             }
 77: 
 78:             if(c==8)
 79:             {
 80:                 putchar(8);
 81:                 i--;
 82:             }
 83:         }
 84:         if(c == 9)
 85:         {
 86:             charBuffer[i]='\0';
 87:             moveData(loc,charBuffer,ppTable);
 88:             printCollumTotal(ppTable,&year);
 89:             moveLoc(loc,year,'R');
 90:         }
 91:         else if(c == 13)
 92:         {
 93:             charBuffer[i]='\0';
 94:             moveData(loc,charBuffer,ppTable);
 95:             printCollumTotal(ppTable,&year);
 96:             moveLoc(loc,year,'D');
 97:         }
 98:         else if(c == 224)
 99:         {
100:             c = getch();
101:             if(c == 72)//up arrow
102:             {
103:                 charBuffer[i]='\0';
104:                 moveData(loc,charBuffer,ppTable);
105:                 printCollumTotal(ppTable,&year);
106:                 moveLoc(loc,year,'U');
107:             }
108:             else if(c == 75)//left
109:             {
110:                 charBuffer[i]='\0';
111:                 moveData(loc,charBuffer,ppTable);
112:                 printCollumTotal(ppTable,&year);
113:                 moveLoc(loc,year,'L');
114:             }
115:             else if(c == 80)//down
116:             {
117:                 charBuffer[i]='\0';
118:                 moveData(loc,charBuffer,ppTable);
119:                 printCollumTotal(ppTable,&year);
120:                 moveLoc(loc,year,'D');
121:             }
122:             else if(c == 77)//right
123:             {
124:                 charBuffer[i]='\0';
125:                 moveData(loc,charBuffer,ppTable);
126:                 printCollumTotal(ppTable,&year);
127:                 moveLoc(loc,year,'R');
128:             }
129:         }
130:         else if(c == 'Q')
131:         {    
132:             breakVar = 1;
133:             continue;
134:         }
135:         else if(c == 'V')
136:         {
137:             viewTable(ppTable,&year);
138:             breakVar = 1;
139:             continue;
140:         }
141:         else if(c == 'P')
142:         {
143:             breakVar = 1;
144:             continue;
145:         }
146: 
147:     }
148:         
149:     delete [] *ppTable;
150:     delete [] ppTable;
151:     return 1;
152: }
153: 
154: float **dArray( int nRows, int nCols)
155: {
156:     float **ppi;
157:     float *pool;
158:     float *curPtr;
159:     //(step 1) allocate memory for array of elements of column
160: 
161:     ppi = new float*[nRows];
162: 
163:     //(step 2) allocate memory for array of elements of each row
164:     pool = new float [nRows * nCols];
165: 
166:     // Now point the pointers in the right place
167:     curPtr = pool;
168:     for( int i = 0; i < nRows; i++)
169:     {
170:         *(ppi + i) = curPtr;
171:          curPtr += nCols;
172:     }
173:     for( int i = 0; i < nRows; i++)
174:     {
175:         for(int j=0;j <nCols; j++)
176:         {
177:             *(*(ppi + i)+j) = 0;
178:         }
179:     }
180:     
181:     return ppi;
182: }
183: 
184: void paintScreen(int years)
185: {
186:     system("CLS");
187:     LOCATE(2,28);
188:     puts("Data Entry Forum");
189:     LOCATE(5,0);
190:     puts("YEAR");
191:     LOCATE(5,13);
192:     puts("QUARTER 1");
193:     LOCATE(5,32);
194:     puts("QUARTER 2");
195:     LOCATE(5,51);
196:     puts("QUARTER 3");
197:     LOCATE(5,70);
198:     puts("QUARTER 4");
199:     for(int i = 6; i < years + 8; i++)
200:     {
201:         for(int j = 12; j<70; j+=19)
202:         {
203:             LOCATE(i,j);
204:             if(i == years + 7)
205:             {
206:                 puts("|");
207:             }
208:             else
209:             {
210:                 puts("$");
211:             }
212:         }
213:     }
214:     LOCATE(6 + years,0);
215:     for(int i = 0; i < 80; i++)
216:     {
217:         putch('-');
218:     }
219:     LOCATE(7 + years,0);
220:     puts("TOTAL:");
221:     LOCATE(9 + years,0);
222:     puts("Press Q at any time to Quit, V to view data reports, P to print");
223:     LOCATE(6,0);
224: }
225: void moveData(int *loc, char *buffer, float **table)
226: {
227:     if(strlen(buffer)!=0) //checks if it is already empty
228:     {
229:         *(*(table + *loc) + *(loc + 1)) = atof(buffer); //wont wipe out old data, if empty
230:         strcpy(buffer,"");
231:     }
232: }
233: void moveLoc(int *loc, int year, char dir)
234: {
235:     if(dir == 'U')
236:     {
237:         if(*loc > 0)
238:         {
239:             *loc = *loc - 1;
240:         }
241: 
242:     }
243:     else if(dir == 'R')
244:     {
245:         if(*(loc + 1) > 4)
246:         {
247:             *(loc + 1) = 0;
248:             *loc = *loc + 1;
249:         }
250:         else
251:         {
252:             *(loc + 1) = *(loc + 1) + 1;
253:         }
254:     }
255:     else if(dir == 'D')
256:     {
257:         if(*loc < year - 1)
258:         {
259:             *loc = *loc + 1;
260:         }
261:     }
262:     else if(dir == 'L')
263:     {
264:         if(*(loc + 1) > 0)
265:         {
266:             *(loc + 1) = *(loc + 1) - 1;
267:         }
268:     }
269:     
270:     if(*(loc + 1)== 1)
271:     {
272:         LOCATE(6 + *loc,13);
273:     }
274:     else if(*(loc + 1)== 0)
275:     {
276:         LOCATE(6 + *loc,0);
277:     }
278:     else
279:     {
280:         LOCATE(6 + *loc,*(loc + 1)*19 - 6);
281:     }
282: }
283: double findTotal(float *aArray, int aNum)
284: {
285:     double total = 0;
286:     for(int i=1; i <= aNum; i++)
287:     {
288:         total += *(aArray + i);
289:     }
290:     return total;
291: }
292: double findCollumTotal(float *table, int *year)
293: {
294:     double total = 0;
295:     for(int i = 0; i < *year; i++)
296:     {
297:         total += *(table + 5 * i);
298:     }
299:     return total;
300: }
301: void viewTable(float **table, int *year)
302: {
303:     double grandTotal = 0;
304:     system("CLS");
305:     puts("YEAR\tQ1\t\tQ2\t\tQ3\t\tQ4\t\tTOTAL");
306:     for(int i = 0; i < *year; i++)
307:     {
308:         printf("%-4.0f\t",*(*(table + i)));
309:         for(int j = 1; j < 6; j++)
310:         {
311:             if(j==5)
312:             {
313:                 grandTotal+=findTotal(*(table + i),4);
314:                 printf("%-10.2f\t",findTotal(*(table + i),4));
315:             }
316:             else
317:             {
318:                 printf("%-10.2f\t",*(*(table + i) + j));
319:             }
320:         }
321:         printf("\n\r");
322:     }
323:     printf("\n\rTOTAL\t");
324:     for(int j = 1; j<=5; j++)
325:     {
326:         {
327:             if(j==5)
328:             {
329:                 printf("%-10.2f",grandTotal);
330:             }
331:             else
332:             {
333:                 printf("%-10.2f\t",findCollumTotal(*table + j,year));
334:             }
335:         }
336:     }
337:     printf("\n\rAVERAGE\t");
338:     for(int j = 1; j<5; j++)
339:     {
340:         {
341:             printf("%-10.2f\t",(findCollumTotal(*table + j,year)/(*year)));
342:         }
343:     }
344:     printf("\n\r");
345:     printf("\n\rYearly AVERAGE: %-10.2f\n\r", grandTotal/(*year));
346:     system("Pause");
347: }
348: void printCollumTotal(float **table, int *year)
349: {
350:     LOCATE(*year +7,13)
351:     for(int i = 0; i <67; i++)
352:     {
353:         printf(" ");
354:     }
355:     for(int j = 0; j<4; j++)
356:     {
357:         LOCATE(*year + 7,j*19+12);
358:         {
359:             printf("|%-10.2f",findCollumTotal(*table + (j+1),year));
360:         }
361:     }
362: }

2 comments:

ctskier said...

Now that is some excellent light reading! Once you get past the first line its hard to stop...really grabs your attention.

this is my favorite part.

Void...()*&^%$# L **9&^////void

love it!

haha jk

High Power Rocketry said...

Good code, I have to try it out.