h6ex5: Basic object oriented programming in C
You cannot submit for this problem because the homework's deadline is due.
Description
A mathematical set is a collection of distinct objects, such as {1, 3, 9}, {\(r\), \(g\), \(b\)}, and {5, 11, 11} = {5, 11}. Based on the universal set header file below write in a corresponding ex5.c file the necessary functions to handle a set (creation, deletion as well as adding and removing elements). Assume a set can contain elements of type either char
, int
or double
.
Hint: to resize a memory block use the function realloc
.
Format
Files
You should submit a tar file containing a c source file ex5.c
. It should be like:
#include "universal_set.h"
// Add some code
void newSet(uset *set, int type) {
// Add some code
}
void deletSet(uset *set) {
// Add some code
}
void addElem(void *elem, uset *set) {
// Add some code
}
void remElem(void *elem, uset *set) {
// Add some code
}
Test Program
We will test your universal set with a testing program. We don't need to consider too much about it, only for a reference.
//
// Created by liu on 6/27/18.
//
#include "universal_set.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int cmpChar(const void *a, const void *b) {
return *(char *) a < *(char *) b ? -1 : 1;
}
int cmpInt(const void *a, const void *b) {
return *(int *) a < *(int *) b ? -1 : 1;
}
int cmpDouble(const void *a, const void *b) {
return *(double *) a < *(double *) b ? -1 : 1;
}
void printSet(uset *set) {
void *temp = malloc((size_t) set->card * set->type);
memcpy(temp, set->elem, (size_t) set->card * set->type);
switch (set->type) {
case CHAR:
qsort(temp, (size_t) set->card, (size_t) set->type, cmpChar);
for (int i = 0; i < set->card; i++) {
printf("%c ", *(char *) ((size_t) temp + i * set->type));
}
break;
case INT:
qsort(temp, (size_t) set->card, (size_t) set->type, cmpInt);
for (int i = 0; i < set->card; i++) {
printf("%d ", *(int *) ((size_t) temp + i * set->type));
}
break;
case DOUBLE:
qsort(temp, (size_t) set->card, (size_t) set->type, cmpDouble);
for (int i = 0; i < set->card; i++) {
printf("%.2lf ", *(double *) ((size_t) temp + i * set->type));
}
break;
default:
assert(0);
}
printf("\n");
free(temp);
}
int main(int argc, char *argv[]) {
char buffer[100];
scanf("%s", buffer);
int type = 0;
if (strcmp(buffer, "char") == 0) {
type = CHAR;
} else if (strcmp(buffer, "int") == 0) {
type = INT;
} else if (strcmp(buffer, "double") == 0) {
type = DOUBLE;
} else {
assert(0);
}
uset u1;
newSet(&u1, type);
int n; // The size of operations
scanf("%d", &n);
void *data = malloc((size_t) type);
for (int i = 0; i < n; i++) {
scanf("%s", buffer);
char temp[10];
switch (type) {
case CHAR:
scanf("%s", temp);
*(char *) data = temp[0];
break;
case INT:
scanf("%d", (int *) data);
break;
case DOUBLE:
scanf("%lf", (double *) data);
break;
default:
assert(0);
}
if (strcmp(buffer, "add") == 0) {
addElem(data, &u1);
} else if (strcmp(buffer, "rem") == 0) {
remElem(data, &u1);
} else {
assert(0);
}
printSet(&u1);
}
deletSet(&u1);
free(data);
return 0;
}
Input
elements type and operation number \(n\) on the first line.
an operation on the next \(n\) lines
Output
\(n\) lines of elements in the set (sorted by us) after each operation
Sample 1
Input:
char 4
add 1
add 2
add 3
add 4
Output:
1
1 2
1 2 3
1 2 3 4
Limitation
1s, 128MiB for each test case.
Assignment 6
- Status
- Finished
- Problems
- 6
- Open Since
- 2018-07-01 00:00
- DDL
- 2018-07-10 14:00
- Extension
- 240.0 hour(s)