69,381
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
[xxxx type] get_value(char * str, char * type)
{
if(strcmp(type, "int") == 0)
{
return atoi(str);
}else if(strcmp(type,"float") == 0)
{
return atof(str);
}
}
int main()
{
char *x = "33.01";
float t=0;
//t = atof(x);
get_value(x, "float");
printf("%f\n", t);
}
#include<stdio.h>
#include<stdlib.h>
void get_value(char * str, char * type, void* value)
{
if(strcmp(type, "int") == 0)
{
*(int*)value = atoi(str);
}else if(strcmp(type,"float") == 0)
{
*(float*)value = atof(str);
}
}
int main()
{
char *x = "33.01";
float t=0;
//t = atof(x);
get_value(x, "float", &t);
printf("%f\n", t);
}
#include <stdio.h>
#include <string.h>
void *get_value(char * str, char * type) {
static int i;
static float f;
static double d;
if (strcmp(type, "int") == 0) {
sscanf(str,"%d",&i);
return &i;
} else if (strcmp(type,"float") == 0) {
sscanf(str,"%f",&f);
return &f;
} else if (strcmp(type,"double") == 0) {
sscanf(str,"%lf",&d);
return &d;
}
return NULL;
}
int main() {
char *x = "33.0123456789";
void *pv;
pv=get_value(x,"int" ); if (pv) printf("%d\n" ,((int *)pv)[0]);
pv=get_value(x,"float" ); if (pv) printf("%.6g\n" ,((float *)pv)[0]);
pv=get_value(x,"double"); if (pv) printf("%.15lg\n",((double *)pv)[0]);
pv=get_value(x,"short" ); if (pv) printf("%hd\n" ,((short *)pv)[0]);
return 0;
}
//33
//33.0123
//33.0123456789
//