C语言【微项目05】—模拟弱类型变量与模拟万能打印输出函数print(采用结构体模拟数据类型参数化变量)
【TDTX】
AnalogWeakTypeParameter.c
#include <stdio.h>
#include <stdbool.h>
typedef struct Object
{
int sym[13];
char *chp;
char ch;
short st;
int a;
bool bol;
long b;
long long lg;
float c;
double d;
char s[1000];
enum em
{
sun
};
union un
{
char chh;
short stt;
int aa;
long bb;
long long lgg;
float cc;
double dd;
};
struct objecT
{
};
}Object;
void print(Object object,bool hh)
{
if(object.sym[0] == 1)
{
if(hh == 0)
{
printf("%c",object.ch);
}
else
{
printf("%c\n",object.ch);
}
return;
}
if(object.sym[1] == 1)
{
if(hh == 0)
{
printf("%d",object.st);
}
else
{
printf("%d\n",object.st);
}
return;
}
if(object.sym[2] == 1)
{
if(hh == 0)
{
printf("%d",object.a);
}
else
{
printf("%d\n",object.a);
}
return;
}
if(object.sym[3] == 1)
{
if(hh == 0)
{
if(object.bol == 1)
{
printf("true");
}
else
{
printf("false");
}
}
else
{
if(object.bol == 1)
{
printf("true\n");
}
else
{
printf("false\n");
}
}
return;
}
if(object.sym[4] == 1)
{
if(hh == 0)
{
printf("%ld",object.b);
}
else
{
printf("%ld\n",object.b);
}
return;
}
if(object.sym[5] == 1)
{
if(hh == 0)
{
printf("%lld",object.lg);
}
else
{
printf("%lld\n",object.lg);
}
return;
}
if(object.sym[6] == 1)
{
if(hh == 0)
{
printf("%f",object.c);
}
else
{
printf("%f\n",object.c);
}
return;
}
if(object.sym[7] == 1)
{
if(hh == 0)
{
printf("%lf",object.d);
}
else
{
printf("%lf\n",object.d);
}
return;
}
if(object.sym[8] == 1)
{
if(hh == 0)
{
printf("%s",object.s);
}
else
{
printf("%s\n",object.s);
}
return;
}
if(object.sym[12] == 1)
{
if(hh == 0)
{
printf("%s",object.chp);
}
else
{
printf("%s\n",object.chp);
}
return;
}
return;
}
void setzero(int a[])
{
for(int i=0;i<13;i++)
{
a[i]=0;
}
return;
}
void printArrays(int a[])
{
printf("Arrays:");
for(int i=0;i<13;i++)
{
printf("%d",a[i]);
}
puts("");
return;
}
int main()
{
Object ob;
{
ob.a=0;
setzero(ob.sym);
ob.sym[2]=1;
print(ob,1);
}
{
ob.ch='P';
setzero(ob.sym);
ob.sym[0]=1;
print(ob,0);
}
{
strcpy(ob.s,"hello world");
setzero(ob.sym);
ob.sym[8]=1;
print(ob,0);
}
{
ob.c=66666.6f;
setzero(ob.sym);
ob.sym[6]=1;
print(ob,1);
}
{
ob.d=956.25;
setzero(ob.sym);
ob.sym[7]=1;
print(ob,1);
}
{
ob.chp="myString";
setzero(ob.sym);
ob.sym[12]=1;
print(ob,1);
}
{
ob.bol=0;
setzero(ob.sym);
ob.sym[3]=1;
print(ob,0);
ob.bol=1;
setzero(ob.sym);
ob.sym[3]=1;
print(ob,0);
}
return 0;
}
运行结果示例
------------------------------------------------------第五次发项目类文章有点激动啊!----------------------------------------------------- -----------------------------------------------------【C语言—微项目—自编练习】------------------------------------------------------ ----------------------------------------------------------------【TDTX】-----------------------------------------------------------------
|