1.开发一个包含你需要的预处理器定义的头文件。
// 头文件
#include <stdio.h>
void print_c(char); // 打印字符
void print_s(char *); // 打印字符串
void print_l(long); // 打印整数
void print_d(double); // 打印浮点数
void print_c(char ch)
{
putchar(ch);
putchar('\n');
}
void print_s(char * str)
{
printf("%s",str);
putchar('\n');
}
void print_l(long a)
{
printf("%ld",a);
putchar('\n');
}
void print_d(double i)
{
printf("%lf",i);
putchar('\n');
}
// 包含头文件的测试代码
#include "print.h"
int main(void)
{
char ch='a';
char * str="This is a string.";
long a=1066123;
double i=15.1234;
print_c(ch);
print_s(str);
print_l(a);
print_d(i);
return 0;
}
2.两数的调和平均数这样计算:先得到两数的倒数,然后计算两个倒数的平均值,最后取计算结果的倒数。使用#define指令定义一个宏“函数”,执行该运算。编写一个简单的程序测试该宏。
#define T_a(X,Y) (2/(1/(X)+1/(Y))) //定义带参数的宏
//测试函数
#include <stdio.h>
int main(void)
{
double a,b;
puts("Enter two double numeric: ");
while(scanf("%lf %lf",&a,&b)==2)
{
printf("Result: %.3lf\n",T_a(a,b));
puts("Enter two double numeric: ");
}
puts("Done.");
return 0;
}
3.极坐标用向量的模(即向量的长度)和向量相对x轴逆时针旋转的角度来描述该向量。直角坐标 用向量的x轴和y轴的坐标来描述该向量(见图16.3)。编写一个程序,读取向量的模和角度(单位:度),然后显示x轴和y轴的坐标。相关方程如下:
x = r* cos A y = r* sin A
需要一个函数来完成转换,该函数接受一个包含极坐标的结构,并返回一个包含直角坐标的结构( 或返回指向该结构的指针)。