A set is any well-defined collection of objects called the elements or members of the set.
Required Tasks:
Finish Set.cpp. Its header file is Set.hpp
.
(1) Defines two constructors. One without parameters, Another has two parameter, mean initial set with a array and its size.
(2) Defines five function. All description is in Set.hpp
.
Hint
集合中成员不重复。不需要考虑集合中成员的顺序。
From 李天培
Provided Codes
main.cpp
//
// main.cpp
// C++
//
// Created by 李天培 on 16/2/25.
// Copyright © 2016年 lee. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include "set.hpp"
void display(int* members, int size) {
std::sort(members, members + size);
std::cout << "{";
for (int i = 0; i < size; i++) {
if (i < size - 1) std::cout << members[i] << ", ";
else std::cout << members[i];
}
std::cout << "}" << std::endl;
}
int main(int argc, const char * argv[]) {
int test[5];
std::cin >> test[0]
>> test[1]
>> test[2]
>> test[3]
>> test[4];
// Constructor 1
Set s1 = Set();
display(s1.getMembers(), s1.getSize());
std::cout << "is empty set: " << s1.isEmptySet() << std::endl;
// append func
std::cout << "append: " << s1.append(test[0]) << std::endl;
std::cout << "append: " << s1.append(test[4]) << std::endl;
display(s1.getMembers(), s1.getSize());
// repeat append
std::cout << "append: " << s1.append(test[0]) << std::endl;
display(s1.getMembers(), s1.getSize());
std::cout << "is empty set: " << s1.isEmptySet() << std::endl;
// Constructor 2
Set s2 = Set(test, 5);
// remove func
std::cout << "remove: " << s2.remove(test[0]) << std::endl;
display(s2.getMembers(), s2.getSize());
// repeat append
std::cout << "remove: " << s2.remove(test[0]) << std::endl;
display(s2.getMembers(), s2.getSize());
return 0;
}
set.hpp
//
// Set.hpp
// C++
//
// Created by 李天培 on 16/2/25.
// Copyright © 2016年 lee. All rights reserved.
//
#ifndef Set_hpp
#define Set_hpp
#include <stdio.h>
#define MAX_MEMBERS 100
class Set {
private:
int members[MAX_MEMBERS];
int size;
public:
// Create an empty set.
Set();
// Create an set with some element.
Set(int* m, int s);
// append a element to set.
// If element in the set, return false.
// Or insert in set and return true.
bool append(int e);
// remove a element by its value from set.
// If element in the set, then remove it and return true.
// Or return false.
bool remove(int e);
// return true if the set is empty, or return false.
bool isEmptySet();
// return set.
int* getMembers();
// return size of set.
int getSize();
// return false if element not in the set, or return true.
bool isInSet(int e);
};
#endif /* Set_hpp */
Submission
set.cpp
#include "set.hpp"
Set::Set() {
size = 0;
}
Set::Set(int* m, int s) {
int temp[100];
int tsize = 0;
for (int i = 0; i<s - 1; i++) {
int flag = 1;
for (int j = i + 1; j<s; j++) {
if (m[i] == m[j]) {
flag = 0;
break;
}
}
if (flag)
temp[tsize++] = m[i];
}
temp[tsize++] = m[s - 1];
size = tsize;
for (int i = 0; i<tsize; i++)
members[i] = temp[i];
}
bool Set::append(int e) {
if (isInSet(e))
return false;
members[size] = e;
size++;
return true;
}
bool Set::remove(int e) {
int i;
for (i = 0; i<size; i++)
if (members[i] == e)
break;
if (i == size)
return false;
for (int j = i; j<size - 1; j++)
members[j] = members[++i];
size--;
return true;
}
bool Set::isEmptySet() {
if (size)
return false;
return true;
}
int* Set::getMembers() {
return members;
}
int Set::getSize() {
return size;
}
bool Set::isInSet(int e) {
for (int i = 0; i<size; i++)
if (members[i] == e)
return true;
return false;
}
Standard Answer
set.cpp
//
// Set.cpp
// C++
//
// Created by 李天培 on 16/2/25.
// Copyright © 2016年 lee. All rights reserved.
//
#include "set.hpp"
Set::Set() {
size = 0;
}
Set::Set(int* m, int s) {
size = 0;
for (int i = 0; i < s ; i++) {
if (!isInSet(m[i])) {
members[size++] = m[i];
}
if (size == MAX_MEMBERS) break;
}
}
bool Set::append(int element) {
if (size < MAX_MEMBERS && !isInSet(element)) {
members[size] = element;
size++;
return true;
} else {
return false;
}
}
bool Set::remove(int element) {
for (int i = 0; i < size; i++) {
if (members[i] == element) {
members[i] = members[--size];
return true;
}
}
return false;
}
bool Set::isEmptySet() {
return (size == 0) ? true : false;
}
int* Set::getMembers() {
return members;
}
int Set::getSize() {
return size;
}
bool Set::isInSet(int element) {
for (int i = 0; i < size; i++) {
if (members[i] == element) {
return true;
}
}
return false;
}