-
Notifications
You must be signed in to change notification settings - Fork 0
/
DEMO.CPP
97 lines (75 loc) · 2.05 KB
/
DEMO.CPP
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// "Natural selection" process with a single line of text
// By Simon Mitchell (96878291)
// 19/3/98
// This program will accept a small line of text, and apply the idea
// of "natural selection" to it, as described in Part A of the
// "BIOMORPHS" major project description.
// Just to get a "working" idea on screen in text mode, since I cannot
// get OWL to work on my current version of C++ (4.5 under Win95).
// FILES: demo.cpp - Source code
#include<iostream.h>
#include<stdlib.h>
#include<stddef.h>
// #define debugging // Debugging switch
int i=0, generations=1;
main (void)
{
char *name, *result;
int buf_size=100;
int count=0;
name = new char[buf_size];
result = new char[buf_size];
randomize();
cout << "Please enter some text!\n";
cin.getline(name,buf_size);
count=cin.gcount();
count--;
int finished=0;
int maxcount =0;
while (finished==0)
{
#ifdef debugging
cout << "*** At start of WHILE loop\n";
cout << "*** Finished = " << finished << "\n";
#endif
// MAXCOUNT here is used to determine how many characters were
// changed in the RESULT array.
// If MAXCOUNT == COUNT, then we changed them all, and we're not
// finished. If MAXCOUNT==0, nothing was changed and we can exit.
maxcount=0;
for (i=0;i!=count;i++)
{
if (result[i]!=name[i])
{
#ifdef debugging
cout << "*** Chars don't match.\n";
cout << "*** Result["<<i<<"] = " << result[i] << "\n";
cout << "*** Name["<<i<<"] = " << name[i] << "\n";
#endif
result[i]=random(96)+32;
maxcount++;
}
}
for (i=0;i!=count;i++)
cout << result[i];
cout << "\n";
#ifdef debugging
cout << "*** Maxcount = " << maxcount << "\n";
cout << "*** Count = " << count << "\n";
#endif
generations++;
if (maxcount==0)
{
finished=1;
cout << "\nFINISHED!\n";
cout << "This took " << generations << " generations to complete!\n";
}
#ifdef debugging
cout << "*** At end of WHILE loop.\n";
cout << "*** Finished = " << finished << "\n";
#endif
}
delete[] name;
delete[] result;
return 0;
}