第一题
将后面的头文件全部放入第一题中
#pragma once
#ifdef SIXTH_2
#include <stdio.h>
#define TIAOHE(X,Y) (1/((1/(X)+1/(Y))/2))
#endif
#ifdef SIXTH_3
#include <stdio.h>
#include <math.h>
#define PI 3.14159
#define Radian(A) ((A)*PI/180)
#define CoordinateX(R,Y) ((R)*cos(Radian(Y)))
#define CoordinateY(R,Y) ((R)*sin(Radian(Y)))
typedef struct{
double novecter;
double angle;
}PolarCoor;
typedef struct{
double x;
double y;
}Coordinate;
extern Coordinate Angle_Coor(PolarCoor therr);
#endif
#ifdef SIXTH_4
#include <stdio.h>
#include <time.h>
extern void time_yc(const double tim);
#endif
#ifdef SIXTH_5
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <string.h>
#define SHILI 30
#define RAND(X,Y) (rand()%((Y)-(X)+1)+(X))
extern void rand_num_one(const int* name_name, int name_time, int name_num);
#endif
#ifdef SIXTH_6
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM 40
#define SLEN 5
typedef struct {
char first[NUM];
char last[NUM];
}names;
void showarray(const names ar[], int n);
int mycomp(const void* p1, const void* p2);
#endif
#ifdef SIXTH_7
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
void show_array(const double ar[], int n);
double* new_d_array(int n, ...);
#endif
#ifdef SIXTH_2
#define TIAOHE(X,Y) (1/((1/(X)+1/(Y))/2))
#endif
第二题
#define SIXTH_2
#include "sixth_one.h"
int main(void)
{
double x, y;
double sum;
printf("请输入两个数:");
scanf_s("%lf%lf",&x,&y);
sum=TIAOHE(x, y);
printf("\nx为:%.6lf,y为:%.6lf,调和平均数为:%.6lf\n", x, y, sum);
return 0;
}
第三题
#define SIXTH_3
#include "sixth_one.h"
int main(void)
{
PolarCoor example_input;
Coordinate example_output;
printf("Hello,please enter norm of vector(请输入向量的模):");
scanf_s("%lf", &example_input.novecter);
printf("Please enter angle(请输入角度):");
scanf_s("%lf", &example_input.angle);
puts("您已经输入极坐标信息,请稍等:");
example_output=Angle_Coor(example_input);
printf("您输入的向量长度为%.2lf,您输入的角度为%.2lf\n", example_input.novecter, example_input.angle);
printf("对应的x坐标为%.2lf,对应的y坐标为%.2lf.\n", example_output.x, example_output.y);
printf("完整表示为(%.2lf,%.2lf)", example_output.x, example_output.y);
return 0;
}
Coordinate Angle_Coor(PolarCoor three)
{
Coordinate good;
good.x = CoordinateX(three.novecter, three.angle);
good.y = CoordinateY(three.novecter, three.angle);
return good;
}
第四题
#define SIXTH_4
#include "sixth_one.h"
int main(void)
{
double ceshi;
printf("请输入一个数(小于0或者非数字结束):");
while (scanf_s("%lf", &ceshi) == 1)
{
time_yc(ceshi);
printf("请输入一个数,<0或者非数字结束:");
}
puts("Done!");
return 0;
}
void time_yc(const double tim)
{
clock_t start = clock();
clock_t end = clock();
while (((double)((double)end - (double)start) / CLOCKS_PER_SEC) < tim)
{
end = clock();
}
printf("时间延迟数为%g\n,时间开始数为%ld,时间结束数为%ld\n", ((double)((double)end - (double)start) / CLOCKS_PER_SEC),start,end);
}
第五题
#define SIXTH_5
#include "sixth_one.h"
int main(void)
{
int a[SHILI];
int i;
int nno;
srand((unsigned)time(0));
for (i = 0; i < SHILI; i++)
{
a[i] = rand();
}
printf("请输入要抽几次奖:");
while (scanf_s("%d", &nno) == 1)
{
printf("输出抽奖的值:\n");
rand_num_one(a, SHILI, nno);
printf("\n请输入要抽几次奖(输入q结束 ):");
}
scanf_s("%d", &nno);
return 0;
}
void rand_num_one(const int* name_name, int name_time, int name_num)
{
int i;
int copy;
int* r_bool;
r_bool = (int *)malloc(sizeof(int) * name_time);
memset(r_bool, 0, sizeof(int) * name_time);
srand((unsigned)time(0));
for (i = 0; i < name_num; i++)
{
copy=RAND(0, name_time);
printf("copy:%d,", copy);
if (r_bool[copy] == 0)
{
r_bool[copy] = 1;
printf("%-8d\n", name_name[copy]);
}
}
free(r_bool);
}
第六题
#define SIXTH_6
#include "sixth_one.h"
int main(void)
{
names vals[SLEN] = {
{"MARK","ONE"},
{"HARK","TWO"},
{"CHAK","THREE"},
{"GHAK","FOUR"},
{"TACK","FIVE"}
};
puts("打印排序前数据:");
showarray(vals, SLEN);
qsort(vals, SLEN, sizeof(names), mycomp);
puts("\n打印排序后数据:");
showarray(vals, SLEN);
return 0;
}
void showarray(const names ar[],int n)
{
int index;
for (index = 0; index < n; index++)
printf("%40s,%40s\n", ar[index].first, ar[index].last);
}
int mycomp(const void* p1, const void* p2)
{
const names* a1 = (const names*)p1;
const names* a2 = (const names*)p2;
if ((strcmp(a1->first, a2->first)) < 0)
return -1;
else if (strcmp(a1->first, a2->first) == 0)
{
if (strcmp(a1->last, a2->last) < 0)
return -1;
else if (strcmp(a1->last, a2->last) == 0)
return 0;
else
return 1;
}
else
return 1;
}
第七题
#define SIXTH_7
#include "sixth_one.h"
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;
}
double* new_d_array(int n, ...)
{
int x;
va_list ap;
double* pe = NULL;
va_start(ap, n);
pe = (double*)malloc(n * sizeof(double));
if (NULL == pe)
exit(EXIT_FAILURE);
for (x = 0; x < n; x++)
{
pe[x] = (double)va_arg(ap, double);
}
va_end(ap);
return pe;
}
void show_array(const double ar[], int n)
{
int x;
printf("共%d个数字", n);
for (x = 0; x < n; x++)
{
printf("%6.2lf ", ar[x]);
if (x % 5 == 4)
putchar('\n');
}
putchar('\n');
}
|