《明解C語言》示例程式碼和練習程式碼[第12章]
阿新 • • 發佈:2019-02-09
第12章 結構體
示例程式碼:
示例程式碼12-1
/* 將5名學生的“身高”按升序排列 */ #include <stdio.h> #define NUMBER 5 // 學生人數 // 交換x和y指向的整數值 void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } // 將陣列data的前n個元素按升序排列 void sort(int data[], int n) { int k = n - 1; while (k >= 0) { int i, j; for (i = 1, j = -1; i <= k; i++) if (data[i - 1] > data[i]) { j = i - 1; swap(&data[i], &data[j]); } k = j; } } int main(void) { int i; int height[] = {178, 175, 173, 165, 179}; sort(height, NUMBER); for (i = 0; i < NUMBER; i++) printf("%2d : %4d\n", i + 1, height[i]); return (0); }
示例程式碼12-2
/* 對5名學生的“姓名和身高”按身高進行升序排序 */ #include <stdio.h> #include <string.h> #define NUMBER 5 // 學生人數 // 交換x和y指向的整數值 void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } // 將sx和sy指向的字串進行交換 void swaps(char sx[], char sy[]) { char temp[256]; strcpy(temp, sx); strcpy(sx, sy); strcpy(sy, temp); } // 對陣列data和name的前n個元素進行升序排序 void sort(int data[], char name[][20], int n) { int k = n - 1; while (k >= 0) { int i, j; for (i = 1, j = -1; i <= k; i++) if (data[i - 1] > data[i]) { j = i - 1; swap(&data[i], &data[j]); swaps(name[i], name[j]); } k = j; } } int main(void) { int i; int height[] = {178, 175, 173, 165, 179}; char name[][20] = {"Sato", "Sanka", "Takao", "Mike", "Masaki"}; sort(height, name, NUMBER); for (i = 0; i < NUMBER; i++) printf("%2d : %-8s%4d\n", i + 1, name[i], height[i]); return (0); }
示例程式碼12-3
/* 用表示學生的結構體來顯示佐中的資訊 */ #include <stdio.h> #include <string.h> struct gstudent { char name[20]; // 姓名 int height; // 身高 float weight; // 體重 long school; // 獎學金 }; int main(void) { struct gstudent sanaka; strcpy(sanaka.name, "Sanaka"); // 姓名 sanaka.height = 175; // 身高 sanaka.weight = 60.5; // 體重 sanaka.school = 70000; // 獎學金 printf("姓 名 = %s\n", sanaka.name); printf("身 高 = %d\n", sanaka.height); printf("體 重 = %f\n", sanaka.weight); printf("獎學金 = %ld\n", sanaka.school); return (0); }
示例程式碼12-4
/*
對錶示學生的結構體成員佐中進行初始化
*/
#include <stdio.h>
struct gstudent {
char name[20]; // 姓名
int height; // 身高
float weight; // 體重
long school; // 獎學金
};
int main(void)
{
struct gstudent sanaka = {"Sanaka", 175, 60.5, 70000};
printf("姓 名 = %s\n", sanaka.name);
printf("身 高 = %d\n", sanaka.height);
printf("體 重 = %f\n", sanaka.weight);
printf("獎學金 = %ld\n", sanaka.school);
return (0);
}
示例程式碼12-5
/*
擁有超能力的洋子
*/
#include <stdio.h>
struct gstudent {
char name[20]; // 姓名
int height; // 身高
float weight; // 體重
long school; // 獎學金
};
void hiroko(struct gstudent *std)
{
if ((*std).height < 180) (*std).height = 180;
if ((*std).weight > 80) (*std).weight = 80;
}
int main(void)
{
struct gstudent sanaka = {"Sanaka", 175, 60.5, 70000};
hiroko(&sanaka);
printf("姓 名 = %s\n", sanaka.name);
printf("身 高 = %d\n", sanaka.height);
printf("體 重 = %f\n", sanaka.weight);
printf("獎學金 = %ld\n", sanaka.school);
return (0);
}
示例程式碼12-6
/*
擁有超能力的洋子 (其二)
*/
#include <stdio.h>
// 表示學生的結構體
typedef struct {
char name[20]; // 姓名
int height; // 身高
float weight; // 體重
long school; // 獎學金
} student;
void hiroko(student *std)
{
if (std->height < 180) std->height = 180;
if (std->weight > 80) std->weight = 80;
}
int main(void)
{
student sanaka = {"Sanaka", 175, 60.5, 70000};
hiroko(&sanaka);
printf("姓 名 = %s\n", sanaka.name);
printf("身 高 = %d\n", sanaka.height);
printf("體 重 = %f\n", sanaka.weight);
printf("獎學金 = %ld\n", sanaka.school);
return (0);
}
示例程式碼12-7
/*
返回結構體的函式
*/
#include <stdio.h>
struct xyz {
int mx;
long my;
double mz;
};
// 返回結構體xyz
struct xyz set_xyz(int x, long y, double z)
{
struct xyz temp;
temp.mx = x;
temp.my = y;
temp.mz = z;
return (temp);
}
int main(void)
{
struct xyz xyz = {0, 0, 0};
xyz = set_xyz(10, 320, 35.6);
printf("xyz.mx = %d\n", xyz.mx);
printf("xyz.my = %ld\n", xyz.my);
printf("xyz.mz = %f\n", xyz.mz);
return (0);
}
示例程式碼12-8
/*
將5名學生按身高進行升序排序
*/
#include <stdio.h>
#define NUMBER 5 // 學生人數
typedef struct {
char name[20]; // 姓名
int height; // 身高
float weight; // 體重
long school; // 獎學金
} student;
// 對x和y指向的學生進行交換
void swap(student *x, student *y)
{
student temp = *x;
*x = *y;
*y = temp;
}
// 對陣列data的前n個元素按身高進行升序排列
void sort(student data[], int n)
{
int k = n - 1;
while (k >= 0) {
int i, j;
for (i = 1, j = -1; i <= k; i++)
if (data[i - 1].height > data[i].height) {
j = i - 1;
swap(&data[i], &data[j]);
}
k = j;
}
}
int main(void)
{
int i;
student std[] = {
{"Sato", 178, 61.0, 80000},
{"Sanaka", 175, 60.5, 70000},
{"Takao", 173, 80.0, 0},
{"Mike", 165, 72.0, 70000},
{"Masaki", 179, 77.5, 70000},
};
sort(std, NUMBER);
puts("-------------------------");
for (i = 0; i < NUMBER; i++)
printf("%-8s %6d%6.1f%7ld\n",
std[i].name, std[i].height, std[i].weight, std[i].school);
puts("-------------------------");
return (0);
}
示例程式碼12-9
/*
顯示當天日期
*/
#include <time.h>
#include <stdio.h>
void put_date(void)
{
time_t current;
struct tm *local;
char wday_name[][7] = {"日", "一", "二", "三", "四", "五", "六"};
time(¤t); // 取得當前時間
local = localtime(¤t); // 轉換為本地時間的結構體
printf("%4d 年 %02d 月 %02d 日 (%s)", local->tm_year + 1900 // 年
, local->tm_mon + 1 // 月
, local->tm_mday // 日
, wday_name[local->tm_wday]);// 星期
}
int main(void)
{
printf("今天是");
put_date();
printf("。\n");
return (0);
}
示例程式碼12-10
/*
計算兩點之間的距離
*/
#include <math.h>
#include <stdio.h>
#define sqr(n) ((n)*(n)) // 計算乘方
typedef struct { // 點
int x; // x座標
int y; // y座標
} point;
// 返回點pa和點pb之間的距離
double distanceof(point pa, point pb)
{
return (sqrt(sqr(pa.x - pb.x) + sqr(pa.y - pb.y)));
}
int main(void)
{
point p1, p2;
printf("點1的x座標:"); scanf("%d", &p1.x);
printf(" y座標:"); scanf("%d", &p1.y);
printf("點2的x座標:"); scanf("%d", &p2.x);
printf(" y座標:"); scanf("%d", &p2.y);
printf("兩點之間的距離為%.2f。\n", distanceof(p1, p2));
return (0);
}
示例程式碼12-11
/*
汽車行駛與燃料消耗
*/
#include <math.h>
#include <stdio.h>
#define sqr(n) ((n)*(n)) // 計算乘方
typedef struct { // 點
int x; // x座標
int y; // y座標
} point;
typedef struct { // 汽車
double fuel; // 剩餘燃料
point pt; // 當前位置
} car;
// 顯示當前位置和剩餘燃料
void put_info(car c)
{
printf("當前位置:(%d, %d)\n", c.pt.x, c.pt.y);
printf("剩餘燃料:%.2f升\n", c.fuel);
}
// 向x和y方向行駛(dx,dy)距離
int move(car *c, int dx, int dy)
{
double dist = sqrt(sqr(dx) + sqr(dy));
if (dist > c->fuel)
return (0);
c->pt.x += dx; c->pt.y += dy; c->fuel -= dist;
return (1);
}
int main(void)
{
car mycar = {90.0, {0, 0}};
while (1) {
int slct;
int dx, dy; // 行駛距離
put_info(mycar);
printf("開動汽車嗎[Yes...1 / No...0]:");
scanf("%d", &slct);
if (slct != 1) break;
printf("X方向的行駛距離:"); scanf("%d", &dx);
printf("Y方向的行駛距離:"); scanf("%d", &dy);
if (!move(&mycar, dx, dy))
puts("\a燃料不足無法行駛。");
}
return (0);
}
練習程式碼:
練習程式碼e12-1
/*
編寫如下函式,顯示當前時間
*/
#include <time.h>
#include <stdio.h>
void put_time(void)
{
time_t current;
struct tm *now;
time(¤t); // 取得當前時間
now = localtime(¤t); // 轉換為本地時間的結構體
printf("%02d:%02d:%02d", now->tm_hour // 時
, now->tm_min // 分
, now->tm_sec); // 秒
}
int main(void)
{
printf(" 現在是");
put_time();
printf("。\n");
return (0);
}