C Primer Plus(第五版)第16章 C预处理器和C库

16.1
#ifndef FUCK_H
#define FUCK_H
//头内容......
#endif

16.2
#include<stdio.h>
#define ave(x,y) 1/(((double)1/(x)+(double)1/(y))/2)
int main(void)
{
printf("%f",ave(1,3));
return 0;
}

16.3
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define pi (4*atan(1))
struct polar
{
double length;
double degree;
};
struct coordinate
{
double x;
double y;
};
struct coordinate reverse(struct polar);
int main(void)
{
struct polar p;
struct coordinate co;
puts("input the polar value,length");
scanf("%lf",&p.length);
while(getchar()!='\n')
;
puts("input the polar value,angle");
scanf("%lf",&p.degree);
while(getchar()!='\n')
;
co=reverse(p);
printf("the polar is %f length and %f degree\n",p.length,p.degree);
printf("the coordinate x is %f and y is %f\n",co.x,co.y);
exit(EXIT_SUCCESS);
}
struct coordinate reverse(struct polar p)
{
struct coordinate co;
double R=p.degree*pi/180;
co.x=p.length*cos(R);
co.y=p.length*sin(R);
return co;
}

16.4
#include<stdio.h>
#include<time.h>
int main(void)
{
double delay;
double a,b;
puts("input the number of delay in second.");
a=clock();
scanf("%lf",&delay);
while(delay>clock()/CLOCKS_PER_SEC)
;
b=clock();
printf("you desired delay is %f,actually delay is %f\n",delay,(b-a)/CLOCKS_PER_SEC);
return 0;
}

16.5
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void choose(int *,int,int);
int main(void)
{
int a[100];
int i;
for(i=0;i<100;i++)
a[i]=i+1;
choose(a,100,5);
return 0;
}
void choose(int * a,int n,int c)
{
srand((unsigned)time(0));
int s;
while(c>0)
{
s=rand()%n;
printf("%d\n",a[s]);
while(s<n)
{
a[s]=a[s+1];
s++;
}
n--;
c--;
}
}

16.6
#include <stdio.h>
#include<string.h>
#include <stdlib.h>
#define NUM 5
struct names
{
char first[40];
char last[40];
};
void fillarray(struct names ar[],int n);
void showarray(const struct names ar[],int n);
int mycomp(const void *p1, const void *p2);
int main(void)
{
struct names vals[NUM]=
{
{"first4","last4"},
{"first3","last3"},
{"first0","last0"},
{"first2","last2"},
{"first1","last1"},
};
puts("Random list:");
showarray(vals,NUM);
qsort(vals,NUM,sizeof(struct names),mycomp);
puts("\nSorted list:");
showarray(vals,NUM);
return 0;
}
void showarray(const struct names ar[],int n)
{
int index;
for( index=0;index<n;index++)
{
printf("%s %s\n", ar[index].first,ar[index].last);
if (index%6==5)
putchar('\n');
}
if (index%6!=0)
putchar('\n');
}
int mycomp(const void *p1, const void *p2)
{
const struct names*ps1=(const struct names*)p1;
const struct names*ps2=(const struct names*)p2;
int res;
res=strcmp(ps1->last,ps2->last);
if(res!=0)
return res;
else
return strcmp(ps1->first,ps2->first);
}

16.7
#include<stdio.h>
#include<stdlib.h>
#include<stdarg.h>
void show_array(const double ar[],int n);
double *new_d_array(int n,...);
int main(void)
{
double *p1;
double *p2;
p1=new_d_array(5,1.2,2.3,3.4,4.5,5.6);
p2=new_d_array(4,100.0,20.00,8.08,-1890.0);
show_array(p1,5);
show_array(p2,4);
free(p1);
free(p2);
return 0;
}
void show_array(const double ar[],int n)
{
int i;
for(i=0;i<n;i++)
{
if(i%5==0&&i!=0)
puts("");
printf("%g ",ar[i]);
}
puts("");
}
double *new_d_array(int n,...)
{
int i;
double *p;
p=(double *)malloc(n*sizeof(double));
va_list ap;
va_start(ap,n);
for(i=0;i<n;i++)
p[i]=va_arg(ap,double);
va_end(ap);
return p;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值