题解 | #KiKi设计类继承#

KiKi设计类继承

https://www.nowcoder.com/practice/699ba050e2704591ae3e62401a856b0e

#include<stdio.h>

const float pi=3.14;
struct shapes;

typedef void(*Init)(struct shapes *x); //初始化 
typedef void(*Scanf)(struct shapes *x);//输入值 
typedef void(*Getarea)(struct shapes *x);//获取面积 
typedef void(*Printf)(struct shapes *x);

struct shapes{				//结构体定义 
	int x;
	int y;
	int circle_x;
	int Rec_x;
	double xy_area;
	double circle_area;
	double Rec_area;
	Scanf scanf; 	
	Getarea getarea;  
	Printf print; 
	Init init;
};
void Init_func(struct shapes *x); 
void scanf_func(struct shapes *x);
void getarea_func(struct shapes *x);
void printf_func(struct shapes *x);


void Init_func(struct shapes *x){   //初始化 
	x->circle_x=0;
	x->Rec_x=0;
	x->x=0;
	x->y=0;
	x->xy_area=0;
	x->circle_area=0;
	x->Rec_area=0;
}

void scanf_func(struct shapes *x){		
	int a,b,c,d;
	scanf("%d %d",&a,&b);
	scanf("%d",&c);
	scanf("%d",&d);
	x->x=a;
	x->y=b;
	x->circle_x=c;
	x->Rec_x=d;
}

void getarea_func(struct shapes *x){   //面积计算   xy_area 矩形面积 circle_area 圆面积  Rec_area 正方形面积 
		x->xy_area=x->x*x->y;
		x->circle_area=x->circle_x*x->circle_x*pi;
		x->Rec_area=x->Rec_x*x->Rec_x;
	
}


void printf_func(struct shapes *x){		//	
	if(x->x!=0){
		printf("%g\n",x->xy_area);
	}else if(x->circle_x!=0){
		printf("%g\n",x->circle_area);
	}else if(x->Rec_x!=0){
		printf("%g\n",x->Rec_area);
	}
}




int main(){
	
	//定义 
	struct shapes my_func;
	my_func.init=Init_func;
	my_func.init(&my_func);
	my_func.getarea=getarea_func;
	my_func.scanf=scanf_func;
	my_func.print=printf_func;
	
	//输入 
	my_func.scanf(&my_func);
	my_func.getarea(&my_func);
	
	//结构体 Rectangle1 
	struct shapes  Rectangle1;
	my_func.init(&Rectangle1);
	Rectangle1.x=my_func.x;
	Rectangle1.y=my_func.y;
	Rectangle1.xy_area=my_func.xy_area;
	
	//结构体 Circle
	struct shapes Circle;
	my_func.init(&Circle);
	Circle.circle_x=my_func.circle_x;
	Circle.circle_area=my_func.circle_area;
	
	//Rectangle2
	struct shapes Rectangle2;
	my_func.init(&Rectangle2);	
	Rectangle2.Rec_x=my_func.Rec_x;
	Rectangle2.Rec_area=my_func.Rec_area;
	
	my_func.print(&Rectangle1); 
	my_func.print(&Circle); 
	my_func.print(&Rectangle2); 
	return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
Hakasee:我的简历和你的基本一样,上周去了上海,boss投了三百家, 三家线下面试 第一家没有做题,全是八股和项目,因为第一次面试不怎么熟练,挂了 第二家,给你几个题目(①css垂直居中文字,字体每两秒闪烁一下以及点击弹窗,②给你一个链接,实现可视化地图,③然后是八股,图片性能优化,以及对图片app有什么想法),45分钟内做完,然后老板面试) 第三家特别偏僻,有点阴森,到了之后让了一个工位给我,有四个题目,①格式化时间 年月日当前时间星期几② 正则表达式提取新闻文字,③在文本域输入文字生成选择题以及选项④生成商品排版还是什么来着 三家都是不超过50人的小公司 两家线上牛客笔试(卡伦特,七牛云,但是笔试不仅要考前端,还要考后端,算法,甚至数学题 我的建议是如果只做了这两个vue项目且不怎么熟练的情况下,先沉淀沉淀,把react学了,上海好的公司基本都是react查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务