C语言,希望运用模板
答案:1 悬赏:60 手机版
解决时间 2021-11-15 08:24
- 提问者网友:献世佛
- 2021-11-14 09:22
C语言,希望运用模板
最佳答案
- 五星知识达人网友:患得患失的劫
- 2021-11-14 09:55
没有,不过你可以使用void *作为通用参数,根据实际情况再转成想要得指针类型,这个就需要根据实际情况来设计了。不知道你想怎么用追问//数据结构P9的抽象类型三元组的定义#include"stdio.h"
struct Triplet
{
void *e1;
void *e2;
void *e3;
};
InitTriplet(Triplet &T,int v1,int v2,int v3)//这句老是说有问题
{
}
int main()
{
return 0;
}
不知道为什么一直运行不了,总是出错,能帮我写一个抽象类型三元组的定义的代码?谢谢!追答Triplet &T这种定义是引用,C++的东西,C语言没有的。
我简单写一个大概的逻辑,不知道是不是你想要得
#include#include
#define TYPE_INT (1)
#define TYPE_CHAr (2)
#define TYPE_SHORT (4)
struct Triplet
{
void *e1;
void *e2;
void *e3;
};
int initTriplet(struct Triplet **T,void* v1,void* v2,void* v3,int type ){
*T=(struct Triplet *)malloc( sizeof (struct Triplet));
if(*T ==NULL){
return -1;
}
if(type == TYPE_INT ){
(int*)((*T)->e1) =(int*)malloc( sizeof (int));
(int*)((*T)->e2) =(int*)malloc( sizeof (int));
(int*)((*T)->e3) =(int*)malloc( sizeof (int));
//申?失?的判断就不写了
*(int*)((*T)->e1) =*((int*) v1);
*(int*)((*T)->e2) =*((int*) v2);
*(int*)((*T)->e3) =*((int*) v3);
}else if(type == TYPE_CHAR ){
(char*)((*T)->e1) =(char*)malloc( sizeof (char));
(char*)((*T)->e2) =(char*)malloc( sizeof (char));
(char*)((*T)->e3) =(char*)malloc( sizeof (char));
//申?失?的判断就不写了
*(char*)((*T)->e1) =*((char*) v1);
*(char*)((*T)->e2) =*((char*) v2);
*(char*)((*T)->e3) =*((char*) v3);
}else if(type == TYPE_SHORT ){
(short*)((*T)->e1) =(short*)malloc( sizeof (short));
(short*)((*T)->e2) =(short*)malloc( sizeof (short));
(short*)((*T)->e3) =(short*)malloc( sizeof (short));
//申?失?的判断就不写了
*(short*)((*T)->e1) =*((short*) v1);
*(short*)((*T)->e2) =*((short*) v2);
*(short*)((*T)->e3) =*((short*) v3);
}
return 0;
}
void main(){
struct Triplet *t;
int a = 1,b=2,c=3;
initTriplet(&t,&a,&b,&c,TYPE_INT );
printf("%d,%d,%d",*(int*)(t->e1),*(int*)(t->e2),*(int*)(t->e3)); system("PAUSE");
}
struct Triplet
{
void *e1;
void *e2;
void *e3;
};
InitTriplet(Triplet &T,int v1,int v2,int v3)//这句老是说有问题
{
}
int main()
{
return 0;
}
不知道为什么一直运行不了,总是出错,能帮我写一个抽象类型三元组的定义的代码?谢谢!追答Triplet &T这种定义是引用,C++的东西,C语言没有的。
我简单写一个大概的逻辑,不知道是不是你想要得
#include
#define TYPE_INT (1)
#define TYPE_CHAr (2)
#define TYPE_SHORT (4)
struct Triplet
{
void *e1;
void *e2;
void *e3;
};
int initTriplet(struct Triplet **T,void* v1,void* v2,void* v3,int type ){
*T=(struct Triplet *)malloc( sizeof (struct Triplet));
if(*T ==NULL){
return -1;
}
if(type == TYPE_INT ){
(int*)((*T)->e1) =(int*)malloc( sizeof (int));
(int*)((*T)->e2) =(int*)malloc( sizeof (int));
(int*)((*T)->e3) =(int*)malloc( sizeof (int));
//申?失?的判断就不写了
*(int*)((*T)->e1) =*((int*) v1);
*(int*)((*T)->e2) =*((int*) v2);
*(int*)((*T)->e3) =*((int*) v3);
}else if(type == TYPE_CHAR ){
(char*)((*T)->e1) =(char*)malloc( sizeof (char));
(char*)((*T)->e2) =(char*)malloc( sizeof (char));
(char*)((*T)->e3) =(char*)malloc( sizeof (char));
//申?失?的判断就不写了
*(char*)((*T)->e1) =*((char*) v1);
*(char*)((*T)->e2) =*((char*) v2);
*(char*)((*T)->e3) =*((char*) v3);
}else if(type == TYPE_SHORT ){
(short*)((*T)->e1) =(short*)malloc( sizeof (short));
(short*)((*T)->e2) =(short*)malloc( sizeof (short));
(short*)((*T)->e3) =(short*)malloc( sizeof (short));
//申?失?的判断就不写了
*(short*)((*T)->e1) =*((short*) v1);
*(short*)((*T)->e2) =*((short*) v2);
*(short*)((*T)->e3) =*((short*) v3);
}
return 0;
}
void main(){
struct Triplet *t;
int a = 1,b=2,c=3;
initTriplet(&t,&a,&b,&c,TYPE_INT );
printf("%d,%d,%d",*(int*)(t->e1),*(int*)(t->e2),*(int*)(t->e3)); system("PAUSE");
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯