Hi leute,
da ich noch nich so Mega viel Erfahrung mit C++ und Zeigern habe, will ich euch einfach mal meine Lösung der oben gennanten Aufgabe zeigen. Funktionieren tut das Programm aufjedenfall, aber geht das noch besser? Wäre schön, wenn ihr da mal euren Senf dazu gebt.
Jonas
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#include <TriBase.h>const int iMaxZahlen = 1000;tbResult tausche(int* piSpeicher1, int* piSpeicher2)
{
int* piPuffer = (int*) tbMemAlloc(iMaxZahlen*4);for (int i = 0; i < iMaxZahlen; i++)
{
piPuffer[i] = piSpeicher2[i];
piSpeicher2[i] = piSpeicher1[i];
piSpeicher1[i] = piPuffer[i];
}
TB_SAFE_MEMFREE(piPuffer);return TB_OK;
}void gebe_aus(int* piSpeicher1, int* piSpeicher2)
{tbWriteToLog("Speicher1:");for(int i=0; i < iMaxZahlen; i++)
{tbWriteToLog("%d: %d", i+1, *piSpeicher1);
}
tbWriteToLog("Speicher2:");for(int i=0; i < iMaxZahlen; i++)
{tbWriteToLog("%d: %d", i+1, *piSpeicher2);
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char* pcCommandLine, int iShowCommand)
{
tbInit();
int* piZahl1 = (int*) tbMemAlloc(iMaxZahlen*4);
int* piZahl2 = (int*) tbMemAlloc(iMaxZahlen*4);for (int i=0; i < iMaxZahlen;i++)
{
piZahl1[i] = tbIntRandom(1,1000);
piZahl2[i] = tbIntRandom(1,1000);
}
// Zahlen werden vor dem Tausch ins Logbuch geschrieben
gebe_aus(piZahl1, piZahl2);
//Tausch findet stattif (tausche(piZahl1,piZahl2) == 0)
{tbWriteToLog("Tausch erfolgreich!");
}
// Zahlen werden zur Kontroller nach dem Tausch erneut isn Logbuch geschrieben
gebe_aus(piZahl1, piZahl2);
TB_SAFE_MEMFREE(piZahl1);
TB_SAFE_MEMFREE(piZahl2);
tbExit();return 0;
}
|