第十五章的練習我這裏就直接跳過了,因爲一般情況下不太用得上。
因此就直接開始第十六章的練習了!
目錄
先看這裏:
博主的編譯環境:
VS 2017 Community
運行環境:Windows 10
因爲到了後期每個練習的代碼量是越來越大的,
所以如果大家複製不了或者想輕鬆一下的,可以直接從下面的網址下載源碼:
另外:如果網盤提示你下載客戶端,可以單個地下載,這樣不用下載客戶端
鏈接:https://pan.baidu.com/s/1YOAMrXZm5Jb3A-LgZBwLEA
提取碼:uh57
題目+源碼+運行效果:
P16-1:
開發一個包含你需要的預處理器定義的頭文件。
//useful functions are included here
//Func.h
#ifndef FUNC_H
#define FUNC_H#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define QUIT 5
#define SPACE ' '
#define SIZE 100#endif
P16-2:
兩數的調和平均數這樣計算:先得到兩數的倒數,然後計算兩個倒數的平均值,最後取計算結果的倒數。
使用 #define 指令定義一個宏 "函數" ,執行該運算。編寫一個簡單的程序測試該宏。
#include <stdio.h>
#define harmonic(numa, numb) (1 /((1 / (numa) + 1 / (numb)) / 2))
int main(void)
{
double numa = 3, numb = 3;printf("the harmonic mean of 3 and 3 is %.2f.", (double) harmonic(numa, numb));
return 0;
}
P16-3:
極座標使用向量的模和向量相對 X 軸逆時針旋轉的角度來描述該向量。直角座標用向量的
X 軸和 Y 軸來描述該向量。編寫一個程序,讀取向量的模和角度(單位:度)
然後顯示 X 軸和 Y 軸的座標。相關方程:X = r * cos A Y = r * sin A
需要一個函數來完成轉換,該函數接受一個包含極座標的結構,
並返回一個包含直角座標的結構(或指向該結構的指針)
/*
16-3
一弧度 = PI / 180
*/#include <stdio.h>
#include <math.h> //provide sin(), cos()#define RAD(X) ((X) * (3.1415926 / 180))
#define EATLINE while(getchar() != '\n') continuetypedef struct Polar_coor_sys
{
double r;
double angle;
}POLAR;typedef struct Rectan_coor
{
double x;
double y;
}RECTAN;void InitStruct(POLAR * polar, RECTAN * rectan);
void Get_P_coor(POLAR * polar);
//return the coor
RECTAN Change(POLAR * polar);int main(void)
{
POLAR polar;
RECTAN rectan;InitStruct(&polar, &rectan);
Get_P_coor(&polar);rectan = Change(&polar);
printf("That is (%.2f, %.2f).", rectan.x, rectan.y);
return 0;
}void InitStruct(POLAR * polar, RECTAN * rectan)
{
polar->r = 0;
polar->angle = 0;rectan->x = 0;
rectan->x = 0;
}void Get_P_coor(POLAR * polar)
{
double r = 0, ang = 0;printf("Enter the coor of a polar coor system: ");
while (scanf_s("%lf %lf", &r, &ang) != 2 || r <= 0 || ang == 0)
{
printf("Input error! Try again:");
EATLINE; //宏
continue;
}polar->r = r;
polar->angle = ang;
}//return the answer
RECTAN Change(POLAR * polar)
{
double rad = RAD(polar->angle);
RECTAN rectan;//work out the coor
rectan.x = polar->r * cos(rad);
rectan.y = polar->r * sin(rad);return rectan;
}
P16-4:
題目太長了.......
#include <stdio.h>
#include <time.h> //clock()
#include <windows.h> //sleep()#define ONE_SECOND 1555
int main(void)
{
clock_t time_begin = 0;
clock_t time_end = 0;
double sum = 0;time_begin = clock();
Sleep(ONE_SECOND);
time_end = clock();
//work out the answer.
sum = (double)(time_end - time_begin) / CLOCKS_PER_SEC;
printf("sleeping for %.2f seconds costs %.2f seconds.", (ONE_SECOND / (double)1000), sum);return 0;
}
P16-5:
title: 編寫一個函數接受這些參數:內含 int 類型元素的數組名,數組的大小和一個代表選取次數的值。
該函數從數組中隨機選擇指定數量的元素,並打印它們。每個元素只能使用一次(模擬抽獎數字或
挑選陪審團成員)。另外,如果你的實現有 time() 或類似的函數,可以再srand() 中
使用這個函數的輸出來初始化隨機數生成器 rand()。編寫一個簡單的程序測試該函數。
//title: 編寫一個函數接受這些參數:內含 int 類型元素的數組名,數組的大小和一個代表選取次數的值。
// 該函數從數組中隨機選擇指定數量的元素,並打印它們。每個元素只能使用一次(模擬抽獎數字或
// 挑選陪審團成員)。另外,如果你的實現有 time() 或類似的函數,可以再srand() 中
// 使用這個函數的輸出來初始化隨機數生成器 rand()。編寫一個簡單的程序測試該函數。
// A lottery game.#include <stdio.h>
#include <time.h> //provide time()
#include <stdlib.h> //provide srand(), rand()//There are 100 winners in max.
#define WINNER_NUM_MAX 100//Init array
void InitArray(int * arr, int min, int element_num, int mode);//Get the number of winners-----0~100
int GetNum(void);//Print out lucky numbers.
void Print_Winners(int lucky_num[], int element_num, int amount);int main(void)
{
int winners_num = 0; //Get the max amount of winnners
int Lucky_num[WINNER_NUM_MAX];srand((unsigned int)time(NULL)); //Set random number seed
InitArray(Lucky_num, 0, WINNER_NUM_MAX, 1);winners_num = GetNum();
Print_Winners(Lucky_num, WINNER_NUM_MAX, winners_num);return 0;
}//Init array-----0~element_num
void InitArray(int * arr, int min, int element_num, int mode)
{
switch (mode)
{
case 0: //set all number 0
for (int i = min; i < element_num; i++)
{
*arr++ = 0;
}
break;
case 1:
for (int i = min + 1; i <= element_num; i++)
{
*arr++ = i;
}
break;
}}
//Get the number of winners-----0~100
int GetNum(void)
{
int num = 0;printf("Give a number-----0~100 to get the program ready: ");
while (scanf_s("%d", &num) != 1 || num < 1 || num > 100)
{
printf("Your input is invalid. Give the correct one: ");
while (getchar() != '\n')
continue;
}
return num;
}//Print out lucky numbers.
void Print_Winners(int lucky_num[], int element_num, int amount)
{
int haveWon[WINNER_NUM_MAX];
InitArray(haveWon, 0, WINNER_NUM_MAX, 0);
int num = 0;printf("The winners: \n\n");
for (int i = 0; i < amount; i++)
{
num = rand() % element_num + 1;
if (haveWon[num - 1] == 0)
{
printf("%d\t", num);
haveWon[num - 1] = 1;
}
else
{
i--;
continue;
}if ((i + 1) % 6 == 0)
{
putchar('\n');
}
}
}