//The code written by this blogger is only for readers' reference;
//If you have any shortcomings, please raise them, and the blogger will do their best to modify;
//Attach the programming practice questions after class;
//If it is useful to you, please like or share with others;
//16.18-2.c
#include <stdio.h>
#define HMEAN(X, Y) (2.0 * (X) * (Y) / ((X) + (Y)))
int main(void)
{
double x, y, ans;
printf("请输入2个数(按q退出本程序):");
while (scanf("%lf %lf", &x, &y) == 2)
{
ans = HMEAN(x, y);
printf("%g和%g的调和平均数是:%g\n", x, y, ans);
printf("您可以再次输入2个数(或按q退出):");
}
puts("本程序完成!");
return 0;
}
//-------------
//16.18 - 3.c
#include <stdio.h>
#include <math.h>
#define PI 3.1415926
typedef struct
{
double length;
double angle;
} polar;
typedef struct
{
double x;
double y;
} rect;
rect polar_to_rect(const polar *temp);
int main(void)
{
polar input;
rect answer;
printf("请输入一个向量长度和一个旋转角度(按q退出本程序): ");
while (scanf("%lf %lf", &input.length, &input.angle) == 2)
{
answer = polar_to_rect(&input);
printf("斜边:%g\n角度:%g°\n", input.length, input.angle);
printf("x轴坐标:%g\ny轴坐标:%g\n", answer.x, answer.y);
printf("您可以再次输入(或按q退出): ");
}
puts("本程序完成!");
return 0;
}
rect polar_to_rect(const polar *temp)
{
rect res;
static const double rad = PI / 180.0;
double ang = rad * temp->angle;
res.x = temp->length * sin(ang);
res.y = temp->length * cos(ang);
return res;
}
//-------------
//16.18 - 4.c
#include <stdio.h>
#include <time.h>
void delay(const double second);
int main(void)
{
double n;
printf("请您输入一个正整数(按q退出本程序):");
while (scanf("%lf", &n) == 1)
{
delay(n);
printf("您可以再次输入一个正整数(或按q退出):");
}
puts("本程序完成!");
return 0;
}
void delay(const double second)
{
clock_t start = clock();
clock_t end = clock();
while (((double)(end - start) / CLOCKS_PER_SEC) < second)
{
end = clock();
}
printf("时间延迟了%g秒\n", (double)(end - start) / CLOCKS_PER_SEC);
return;
}
//-------------
//16.18 - 5.c
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define LEN 30
void random_pick(int ar[], int length, int picks);
int main(void)
{
int i, pick;
int choices[LEN];
for (i = 0; i < LEN; i++)
{
choices[i] = i + 1;
}
printf("请您输入您想选择元素的个数(按q退出本程序): ");
while (scanf("%d", &pick) == 1)
{
if (pick > LEN || pick <= 0)
{
printf("数据有误! 请重新输入: ");
continue;
}
random_pick(choices, LEN, pick);
printf("您可以再次输入(或按q退出): ");
}
puts("本程序完成!");
return 0;
}
void random_pick(int ar[], int length, int picks)
{
int count = 0;
int i, br[length];
memcpy(br, ar, length * sizeof(int));
srand((unsigned int)time(0));
printf("您挑选的%d个数字是:\n", picks);
while (picks > 0)
{
i = rand() % length;
if (0 == br[i])
{
continue;
}
else
{
printf("%-8d", br[i]);
br[i] = 0;
--picks;
}
if (++count % 10 == 0)
{
putchar('\n');
}
}
putchar('\n');
return;
}
//-------------
//16.18 - 6.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 40
#define SLEN 5
struct names
{
char first[LEN];
char last[LEN];
};
int comp(const void *p1, const void *p2);
void show_names(const struct names *p, int n);
int main(void)
{
struct names staff[SLEN] =
{
{
"Francy, card"},
{
"Coffee, cancy"},
{
"Stephen, lory"},
{
"Jack, rosery"},
{
"Black, clover"}
};
puts("Random list:");
show_names(staff, SLEN);
qsort(staff, SLEN, sizeof(struct names), comp);
puts("\nSorted list:");
show_names(staff, SLEN);
return 0;
}
int comp(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);
}
}
void show_names(const struct names *p, int n)
{
int i = 0;
while (i < n)
{
i++;
printf("%s %s\n", p->first, p->last);
p++;
}
return;
}
//-------------
//16.18 - 7.c
#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()
{
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;
printf("%d个元素是:\n", n);
for (i = 0; i < n; i++)
{
printf("%-8g", ar[i]);
}
putchar('\n');
return;
}
double *new_d_array(int n, ...)
{
int i;
va_list ap;
double *pt;
va_start(ap, n);
pt = (double *)malloc(n * sizeof(double));
for (i = 0; i < n; i++)
{
pt[i] = va_arg(ap, double);
}
va_end(ap);
return pt;
}
//-------------
//----------------------------April 26, 2020-------------- -----------------