cbrt c语音_多态性(C语言)

I'm trying to better understand the idea of polymorphism with examples from languages I know;

is there polymorphism in C?

解决方案

This is Nekuromento's second example, factored in the way I consider idiomatic for object-oriented C:

animal.h

#ifndef ANIMAL_H_

#define ANIMAL_H_

struct animal

{

// make vtable_ a pointer so they can be shared between instances

// use _ to mark private members

const struct animal_vtable_ *vtable_;

const char *name;

};

struct animal_vtable_

{

const char *(*sound)(void);

};

// wrapper function

static inline const char *animal_sound(struct animal *animal)

{

return animal->vtable_->sound();

}

// make the vtables arrays so they can be used as pointers

extern const struct animal_vtable_ CAT[], DOG[];

#endif

cat.c

#include "animal.h"

static const char *sound(void)

{

return "meow!";

}

const struct animal_vtable_ CAT[] = { { sound } };

dog.c

#include "animal.h"

static const char *sound(void)

{

return "arf!";

}

const struct animal_vtable_ DOG[] = { { sound } };

main.c

#include "animal.h"

#include

int main(void)

{

struct animal kitty = { CAT, "Kitty" };

struct animal lassie = { DOG, "Lassie" };

printf("%s says %s\n", kitty.name, animal_sound(&kitty));

printf("%s says %s\n", lassie.name, animal_sound(&lassie));

return 0;

}

This is an example of runtime polymorphism as that's when method resolution happens.

C1x added generic selections, which make compile-time polymorphism via macros possible. The following example is taken from the C1x April draft, section 6.5.1.1 §5:

#define cbrt(X) _Generic((X), \

long double: cbrtl, \

default: cbrt, \

float: cbrtf \

)(X)

Type-generic macros for math functions were already available in C99 via the header tgmath.h, but there was no way for users to define their own macros without using compiler extensions.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import numpy as np import pandas as pd from scipy.stats import kstest #from sklearn import preprocessing # get a column from dataframe def select_data(data, ny): yName = data.columns[ny] Y = data[yName] return Y # see which feature is normally distributed from dataframe def normal_test(df): for i in range(len(df.columns)): y = select_data(df,i) p = kstest(y,'norm') print("feature {}, p-value = {}".format(i,p[1])) # rescale feature i in dataframe def standard_rescale(df, i): y = select_data(df,i) m = np.mean(y) s = np.std(y) y = (y-m)/s return y # log-transform feature of dataframe def log_transform(df,i): y = select_data(df,i) y = np.log(y) return y # square root transform feature of dataframe def sqrt_transform(df,i): y = select_data(df,i) y = np.sqrt(y) return y # cube root transform feature of dataframe def cbrt_transform(df,i): y = select_data(df,i) y = np.cbrt(y) return y # transform dataframe into one of: standard, log, sqrt, cbrt def transform_dataframe(df, transformation): df_new = [] if transformation == "standard": for i in range(len(df.columns)-1): y = standard_rescale(df,i) df_new.append(y) df_new.append(df.iloc[:,no_feats]) elif transformation == "log": for i in range(len(df.columns)-1): y = log_transform(df,i) df_new.append(y) df_new.append(df.iloc[:,no_feats]) elif transformation == "sqrt": for i in range(len(df.columns)-1): y = sqrt_transform(df,i) df_new.append(y) df_new.append(df.iloc[:,no_feats]) elif transformation == "cbrt": for i in range(len(df.columns)-1): y = cbrt_transform(df,i) df_new.append(y) df_new.append(df.iloc[:,no_feats]) else: return "wrong arguments" df_new = pd.DataFrame(df_new) df_new = df_new.T return df_new df = pd.read_csv('iris.csv') no_feats = 4 df.columns =['0', '1', '2', '3', '4'] #normal_test(df) df_standard = transform_dataframe(df, "standard") #df_log = transform_dataframe(df, "log") #df_sqrt = transform_dataframe(df, "sqrt") #df_cbrt = transform_dataframe(df, "cbrt") #df_wrong = transform_dataframe(df, "lo") #print("standard-----------------------------------------") #normal_test(df_standard) #print("log-----------------------------------------") #normal_test(df_log) #print("square root-----------------------------------------") #normal_test(df_sqrt) #print("cube root-----------------------------------------") #normal_test(df_cbrt) result = df_standard # create new csv file with new dataframe result.to_csv(r'iris_std.csv', index = False, header=True)解释每一行代码
最新发布
06-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值