c++的问题求大神帮助啊!!!!!!
给了下面的set.h,希望能写出一个set.c来,功能是创造一个组(可以创造,消除这个组,和添加或除掉组里的数)例子:{1,3,9}{r,g,b}{5,11,11}={5...
给了下面的set.h,希望能写出一个set.c来,功能是创造一个组(可以创造,消除这个组,和添加或除掉组里的数)
例子:{1,3,9}
{r,g,b}
{5,11,11} = {5,11}
下面是set.h
/* initial memory allocated for the set */
#define INITSETSIZE 64
/* size of the different types */
#define CHAR 1
#define INT sizeof(int)
#define DOUBLE sizeof(double)
typedef struct universalSet {
void *elem;//pointer to elements of the set
int card;//cardinal of the set
int type;//type of elements in the set (CHAR INT or DOUBLE)
} uset;
/* initialize an empty set of type type (one of: CHAR INT DOUBLE)
* allocate memory for the set: INITSETSIZE * type
*/
void newSet(uset *set, int type);
/* free the memory allocated by newSet */
void deletSet(uset *set);
/* add the element pointed by elem to the end of set
* check whether the element is already in the set
* resize memory if card > allocated memory
* new allocated memory = allocated memory+64
* e.g. before: mem=128, card=128, after: men=192, card=129
*/
void addElem(void *elem, uset *set);
/* remove the element pointed by elem from the set
* do nothing if the set does not contain this element
* resize memory if "too much memory" is used
* new allocated memory = allocated memory-64
* e.g. before: mem=192, card=129, after: card=128, mem=128
*/
void remElem(void *elem, uset *set);
求大神帮助。。。。。 展开
例子:{1,3,9}
{r,g,b}
{5,11,11} = {5,11}
下面是set.h
/* initial memory allocated for the set */
#define INITSETSIZE 64
/* size of the different types */
#define CHAR 1
#define INT sizeof(int)
#define DOUBLE sizeof(double)
typedef struct universalSet {
void *elem;//pointer to elements of the set
int card;//cardinal of the set
int type;//type of elements in the set (CHAR INT or DOUBLE)
} uset;
/* initialize an empty set of type type (one of: CHAR INT DOUBLE)
* allocate memory for the set: INITSETSIZE * type
*/
void newSet(uset *set, int type);
/* free the memory allocated by newSet */
void deletSet(uset *set);
/* add the element pointed by elem to the end of set
* check whether the element is already in the set
* resize memory if card > allocated memory
* new allocated memory = allocated memory+64
* e.g. before: mem=128, card=128, after: men=192, card=129
*/
void addElem(void *elem, uset *set);
/* remove the element pointed by elem from the set
* do nothing if the set does not contain this element
* resize memory if "too much memory" is used
* new allocated memory = allocated memory-64
* e.g. before: mem=192, card=129, after: card=128, mem=128
*/
void remElem(void *elem, uset *set);
求大神帮助。。。。。 展开
2个回答
展开全部
#include "set.h"
#include <stdlib.h>
void newSet(uset* set, int type) {
set->elem = malloc(INITSETSIZE * type);
set->card = 0;
set->type = type;
}
void deletSet(uset *set) {
free(set->elem);
set->elem = NULL;
set->card = 0;
}
void addElem(void *elem, uset* set) {
int i;
if (set->type == CHAR) {
for (i = 0; i < set->card; i++) {
if (((char*)set->elem)[i] == *(char*)elem)
return;
}
if (set->card % INITSETSIZE == 0) {
set->elem = realloc(set->elem, (set->card + INITSETSIZE) * CHAR);
}
((char*)set->elem)[set->card++] = *(char*)elem;
}
else if (set->type == INT) {
for (i = 0; i < set->card; i++) {
if (((int*)set->elem)[i] == *(int*)elem)
return;
}
if (set->card % INITSETSIZE == 0) {
set->elem = realloc(set->elem, (set->card + INITSETSIZE) * INT);
}
((int*)set->elem)[set->card++] = *(int*)elem;
}
else if(set->type == DOUBLE) {
for (i = 0; i < set->card; i++) {
if (((double*)set->elem)[i] == *(double*)elem)
return;
}
if (set->card % INITSETSIZE == 0) {
set->elem = realloc(set->elem, (set->card + INITSETSIZE) * DOUBLE);
}
((double*)set->elem)[set->card++] = *(double*)elem;
}
}
void remElem(void *elem, uset* set) {
int i;
if (set->type == CHAR) {
for (i = 0; i < set->card; i++) {
if (((char*)set->elem)[i] == *(char*)elem) {
((char*)set->elem)[i] = ((char*)set->elem)[set->card - 1];
set->card--;
if (set->card % INITSETSIZE == 0 && set->card != 0) {
set->elem = realloc(set->elem, set->card * CHAR);
}
}
}
}
else if (set->type == INT) {
for (i = 0; i < set->card; i++) {
if (((int*)set->elem)[i] == *(int*)elem) {
((int*)set->elem)[i] = ((int*)set->elem)[set->card - 1];
set->card--;
if (set->card % INITSETSIZE == 0 && set->card != 0) {
set->elem = realloc(set->elem, set->card * INT);
}
}
}
}
else if (set->type == DOUBLE) {
for (i = 0; i < set->card; i++) {
if (((double*)set->elem)[i] == *(double*)elem) {
((double*)set->elem)[i] = ((double*)set->elem)[set->card - 1];
set->card--;
if (set->card % INITSETSIZE == 0 && set->card != 0) {
set->elem = realloc(set->elem, set->card * DOUBLE);
}
}
}
}
}
展开全部
思路:if -999 break 或者用!=-999循环
子函数是一个带递归的n=0 f(n)=n+f(n-1),或者使用循环。
请自己思考,代码就很简单了,组装而已。http://wenku.baidu.com/link?url=1hOLMMCqek6CbxYoxa63wF6OoLD9rvR3cxpP3mqKW-ZKfv1ma84cHLcN0NOn-S_cxMXsXDWHGTjhmZlr1ueuWUOP2zeHnhU8-B-HId_GI6y可借鉴一下。
子函数是一个带递归的n=0 f(n)=n+f(n-1),或者使用循环。
请自己思考,代码就很简单了,组装而已。http://wenku.baidu.com/link?url=1hOLMMCqek6CbxYoxa63wF6OoLD9rvR3cxpP3mqKW-ZKfv1ma84cHLcN0NOn-S_cxMXsXDWHGTjhmZlr1ueuWUOP2zeHnhU8-B-HId_GI6y可借鉴一下。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询