>第 7 章 结构体>数据抽象

冯海云 906702745@qq.com
2009-05-08 22:49:05

“这一层看到的是数据是结构体的两个成员x和y”这句话是不是应该把两个“是”分开写,这样意思更清晰些!


宋劲杉 songjinshan@akaedu.org
2009-05-10 21:13:50

多写了一个字。谢谢指出!我这几天不方便改,过几天一定改过来。


陈杨希 fusang12@126.com
2009-06-10 16:18:47

这一节看了很久也看不太懂。。是不是我太笨了?


Laciq 530107999@qq.com
2009-06-25 21:15:27

没学过高中数学,看起来真郁闷……


zhoudy dongyong800@163.com
2009-07-30 21:18:58

这个函数缺少返回表达式。 
struct complex_struct make_from_real_img(double x, double y)
{
	struct complex_struct z;
	z.A = atan2(y, x);
	z.r = sqrt(x * x + y * y);
    ++  return z;
}


宋劲杉 songjinshan@akaedu.org
2009-07-31 08:52:58

谢谢指出!


wy wy12218@hotmail.com
2010-01-18 14:18:52

这里介绍的是程序设计思想,或者叫数据结构的使用和算法设计,与语法规则本身没有关系,放在这章合适么?


宋劲杉 songjinshan@akaedu.org
2010-01-21 14:48:16

我有说过这一章只介绍语法规则而不讲其它么?


suyn suyun.cn@gmail.com http://su-yun.appspot.com/
2010-07-05 17:21:58

这一节写得太好了,尤其是分层和抽象的思想


LYLtim lylandtim@qq.com http://hi.baidu.com/lyltim
2011-08-05 07:41:21

atan2是什么?为什么?高中生表示已超出高中数学范畴,最好详细说明。


ivon yifeng.ivon.wang@hotmail.com
2011-11-22 04:31:23

/*
07.2.1
在本节的基础上实现一个打印复数的函数,打印的格式是x+yi,如果实部或虚部为0则省略,例如:1.0、-2.0i、-1.0+2.0i、1.0-2.0i。最后编写一个main函数测试本节的所有代码。
*/

#include <stdio.h>
#include <math.h>

struct complex_struct
{
	double x ;
	double y ;
} ;

double real_part(struct complex_struct z)
{
	return z.x;
}

double img_part(struct complex_struct z)
{
	return z.y;
}

double magnitude(struct complex_struct z)
{
	return sqrt(z.x * z.x + z.y * z.y);
}

double angle(struct complex_struct z)
{
	return atan2(z.y, z.x);
}

struct complex_struct make_from_real_img(double x, double y)
{
	struct complex_struct z;
	z.x = x;
	z.y = y;
	return z;
}

struct complex_struct make_from_mag_ang(double r, double A)
{
	struct complex_struct z;
	z.x = r * cos(A);
	z.y = r * sin(A);
	return z;
}

struct complex_struct add_complex(struct complex_struct z1, struct complex_struct z2)
{
	return make_from_real_img(real_part(z1) + real_part(z2), img_part(z1) + img_part(z2));
}

struct complex_struct sub_complex(struct complex_struct z1, struct complex_struct z2)
{
	return make_from_real_img(real_part(z1) - real_part(z2), img_part(z1) - img_part(z2));
}

struct complex_struct mul_complex(struct complex_struct z1, struct complex_struct z2)
{
	return make_from_mag_ang(magnitude(z1) * magnitude(z2), angle(z1) + angle(z2));
}

struct complex_struct div_complex(struct complex_struct z1, struct complex_struct z2)
{
	return make_from_mag_ang(magnitude(z1) / magnitude(z2), angle(z1) - angle(z2));
}

int print_complex(struct complex_struct z)
{
	if(real_part(z) != 0.0 && img_part(z) != 0.0)
	{
		if(img_part(z) > 0.0)
		{
			printf("%lf+%lfi\n", real_part(z), img_part(z)) ;
		}
		else
		{
			printf("%lf%lfi\n", real_part(z), img_part(z)) ;
		}
		
	}

	if(real_part(z) == 0.0)
	{
		if(img_part(z) != 0.0)
		{
			printf("%lfi\n", img_part(z)) ;
		}
		else
		{
			printf("0.0\n") ;
		}
	}

	if(img_part(z) == 0.0)
	{
		if(real_part(z) != 0.0)
		{
			printf("%lf\n", real_part(z)) ;
		}
		else
		{
			printf("0.0\n") ;
		}
	}

	return 0 ;
}

int main(void)
{
	struct complex_struct z1 ;
	struct complex_struct z2 ;
	
	z1 = make_from_real_img(1.0, 1.0) ;
 	z2 = make_from_mag_ang(1.0, 1.0) ;

	printf("real_part=%lf\n", real_part(z2)) ;
	printf("img_part=%lf\n", img_part(z2)) ;
	printf("magnitude=%lf\n", magnitude(z1)) ;
	printf("angle=%lf\n", angle(z1)) ;
	printf("\n") ;


	print_complex(add_complex(z1, z2)) ;
	print_complex(sub_complex(z1, z2)) ;
	print_complex(mul_complex(z1, z2)) ;
	print_complex(div_complex(z1, z2)) ;
	printf("\n") ;

	struct z3 = make_from_real_img(1.0, 0.0) ;
	struct z4 = make_from_real_img(0.0, -2.0) ;
	struct z5 = make_from_real_img(-1.0, 2.0) ;
	struct z6 = make_from_real_img(1.0, -2.0) ;

	print_complex(z3) ;
	print_complex(z4) ;
	print_complex(z5) ;
	print_complex(z6) ;
	printf("\n") ;

	return 0 ;
}


如果您有建设性意见,哪怕只是纠正一个错别字,也请不吝赐教,您留下的姓名和email将会出现在本书前言的致谢中。再次感谢您的宝贵意见!