Created
December 11, 2019 03:00
-
-
Save uhmseohun/d5b9e4f64755b0ae67e5524a4b259f28 to your computer and use it in GitHub Desktop.
수학 집합 단원
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <time.h> | |
#include <algorithm> | |
using namespace std; | |
const int maxSize = 150; // The maximum number of elements | |
const int boundSize = 50; | |
struct Comparator{ | |
bool operator () (const int a, const int b){ | |
return a > b; | |
} | |
}; | |
struct Set { | |
char name; | |
int elements[maxSize], count; | |
void initialize (char NAME) { | |
name = NAME, count = 0; | |
} | |
char* repr () { | |
char* stringValue = (char *) malloc(sizeof(char) * maxSize * 5); | |
sprintf(stringValue, "{ "); | |
for (int i=0; i<count; ++i) { | |
sprintf(stringValue + strlen(stringValue), "%d", elements[i]); | |
if (i < count - 1) sprintf(stringValue + strlen(stringValue), ", "); | |
} | |
sprintf(stringValue + strlen(stringValue), " }\n"); | |
return stringValue; | |
} | |
void output () { | |
printf("%c { ", name); | |
for (int i=0; i<count; ++i) { | |
printf("%d", elements[i]); | |
if (i < count - 1) printf(", "); | |
} | |
printf(" }\n"); | |
} | |
int insertValue (int value) { | |
if (indexOf(value) >= 0) return -1; | |
elements[count++] = value; | |
return value; | |
} | |
int removeValue (int value) { | |
int index = indexOf(value); | |
if (index < 0) return -1; | |
for (int i=index; i<count-1; ++i) { | |
elements[i] = elements[i+1]; | |
} | |
count--; | |
return value; | |
} | |
void subtract (Set s) { | |
for (int i=0; i<s.count; ++i) { | |
removeValue(s.elements[i]); | |
} | |
} | |
void unionWith (Set s) { | |
for (int i=0; i<s.count; ++i) { | |
insertValue(s.elements[i]); | |
} | |
} | |
void intersectWith (Set s) { | |
for (int i=0; i<count; ++i) { | |
if (s.indexOf(elements[i]) < 0) removeValue(elements[i]); | |
} | |
} | |
int indexOf (int target) { | |
for (int i=0; i<count; ++i) { | |
if (elements[i] == target) return i; | |
} | |
return -1; | |
} | |
}; | |
void randomizeSet (Set *s, int count) { | |
for (int i=0; i<count; ++i) { | |
s -> insertValue(rand() % 15); | |
} | |
} | |
void printBound () { | |
printf("\n"); | |
for(int i=0; i<boundSize; ++i) { | |
printf("-"); | |
} | |
printf("\n\n"); | |
} | |
int main () { | |
srand(time(NULL)); | |
Set a; | |
a.initialize('A'); | |
randomizeSet(&a, 5); | |
a.output(); | |
Set b; | |
b.initialize('B'); | |
randomizeSet(&b, 7); | |
b.output(); | |
a.unionWith(b); | |
printf("A U B = %s\n", a.repr()); | |
printBound(); | |
Set c; | |
c.initialize('C'); | |
randomizeSet(&c, 5); | |
c.output(); | |
Set d; | |
d.initialize('D'); | |
randomizeSet(&d, 7); | |
d.output(); | |
c.subtract(d); | |
printf("C - D = %s\n", c.repr()); | |
printBound(); | |
Set e; | |
e.initialize('E'); | |
randomizeSet(&e, 5); | |
e.output(); | |
Set f; | |
f.initialize('F'); | |
randomizeSet(&f, 7); | |
f.output(); | |
c.intersectWith(d); | |
printf("E ∩ F = %s\n", e.repr()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment