TASK1
#include <stdio.h>
#include <string.h>
#define N 3
typedef struct student {
int id;
char name[20];
char subject[20];
double perf;
double mid;
double final;
double total;
char level[10];
} STU;
void input(STU [], int);
void output(STU [], int);
void calc(STU [], int);
int fail(STU [], STU [], int);
void sort(STU [], int);
int main() {
STU st[N], fst[N];
int k;
printf("錄入學生成績資訊:\n");
input(st, N);
printf("\n成績處理...\n");
calc(st, N);
k = fail(st, fst, N);
sort(st, N);
printf("\n學生成績排名情況:\n");
output(st, N);
printf("\n不及格學生資訊:\n");
output(fst, k);
return 0;
}
void input(STU s[], int n) {
int i;
for(i = 0; i < n; i++)
scanf("%d %s %s %lf %lf %lf", &s[i].id, s[i].name, s[i].subject,
&s[i].perf, &s[i].mid, &s[i].final);
}
void output(STU s[], int n) {
int i;
printf("-----------------\n");
printf("學號 姓名 科目 平時 期中 期末 總評 等級\n");
for(i = 0; i<n; i++)
printf("%d %-6s %-4s %-4.0f %-4.0f %-4.0f %-4.1f %s\n",s[i].id,s[i].name,s[i].subject,s[i].perf,s[i].mid,s[i].final,s[i].total,s[i].level);
}
void calc(STU s[],int n) {
int i;
for(i = 0; i < n; i++) {
s[i].total = s[i].perf * 0.2 +
s[i].mid * 0.2 +
s[i].final * 0.6;
if(s[i].total >= 90)
strcpy(s[i].level, "優");
else if(s[i].total >= 80 && s[i].total < 90)
strcpy(s[i].level, "良");
else if(s[i].total >= 70 && s[i].total < 80)
strcpy(s[i].level, "中");
else if(s[i].total >= 60 && s[i].total < 70)
strcpy(s[i].level, "及格");
else
strcpy(s[i].level, "不及格");
}
}
int fail(STU s[], STU t[], int n) {
int i, cnt = 0;
for(i = 0; i < n; i++)
if(s[i].total < 60)
t[cnt++] = s[i];
return cnt;
}
void sort(STU s[], int n) {
int i, j;
STU t;
for(i = 0; i < n-1; i++)
for(j = 0; j < n-1-i; j++)
if(s[j].total < s[j+1].total) {
t = s[j];
s[j] = s[j+1];
s[j+1] = t;
}
}
TASK2
#include <stdio.h>
#include <string.h>
#define N 10
#define M 80
typedef struct {
char name[M];
char author[M];
} Book;
int main() {
Book x[N] = { {"《一九八四》", "喬治.奧威爾"},
{"《美麗新世界》", "赫胥黎"},
{"《昨日的世界》", "斯蒂芬.茨威格"},
{"《萬曆十五年》", "黃仁宇"},
{"《一隻特立獨行的豬》", "王小波"},
{"《百年孤獨》", "馬爾克斯"},
{"《查令十字街84號》", "海蓮.漢芙"},
{"《只是孩子》", "帕蒂.史密斯"},
{"《刀鋒》", "毛姆"},
{"《沉默的大多數》", "王小波"} };
Book *ptr;
int i;
char author[M];
printf("所有圖書資訊: \n");
for(ptr = x; ptr < x + N; ++ptr)
printf("%-30s%-30s\n", ptr->name, ptr->author);
printf("\n輸入作者名: ");
gets(author);
for(ptr = x; ptr < x + N; ++ptr)
if(strcmp(ptr->author, author) == 0) {
printf("%-30s%-30s\n", ptr->name, ptr->author);
}
return 0;
}
TASK3.1
#include <stdio.h>
#include <stdlib.h>
#define N 80
typedef struct FilmInfo {
char name[N];
char director[N];
char region[N];
int year;
struct FilmInfo *next;
} Film;
void output(Film *head);
Film *insert(Film *head, int n);
int main() {
int n;
Film *head;
head = NULL;
printf("輸入影片數目: ");
scanf("%d", &n);
head = insert(head, n);
printf("\n所有影片資訊如下: \n");
output(head);
return 0;
}
Film *insert(Film *head, int n) {
int i;
Film *p;
for(i = 1; i <= n; ++i) {
p = (Film *)malloc(sizeof(Film));
printf("請輸入第%d部影片資訊: ", i);
scanf("%s %s %s %d", p->name, p->director, p->region, &p->year);
p->next = head;
head = p;
}
return head;
}
void output(Film *head) {
Film *p;
p = head;
while(p != NULL) {
printf("%-20s %-20s %-20s %d\n", p->name, p->director, p->region, p->year);
p = p -> next;
}
}
TASK3.2
#include <stdio.h>
#include <stdlib.h>
#define N 80
typedef struct FilmInfo {
char name[N];
char director[N];
char region[N];
int year;
struct FilmInfo *next;
} Film;
void output(Film *head); // 遍歷輸出連結串列資訊
Film *insert(Film *head, int n); // 向連結串列中插入n個結點,返回頭指標
int main() {
int n; // 結點數
Film *head; // 頭指標變數,存放連結串列中第一個節點的地址
Film *p; // 存放新申請的Film節點記憶體空間地址
// 建立頭結點
p = (Film *)malloc(sizeof(Film));
p->next = NULL;
head = p; // 頭指標變數存放頭節點的地址
printf("輸入影片數目: ");
scanf("%d", &n);
// 向連結串列中插入n部影片資訊
head = insert(head, n);
// 遍歷輸出連結串列中所有影片資訊
printf("\n所有影片資訊如下: \n");
output(head);
return 0;
}
// 向連結串列中插入n個結點,從表頭插入,返回頭指標變數
Film *insert(Film *head, int n) {
int i;
Film *p;
for(i = 1; i <= n; ++i) {
p = (Film *)malloc(sizeof(Film));
printf("請輸入第%d部影片資訊: ", i);
scanf("%s %s %s %d", p->name, p->director, p->region, &p->year);
// 把結點從表頭插入到連結串列中
p->next = head->next;
head->next = p;
}
return head;
}
// 遍歷輸出連結串列資訊
void output(Film *head) {
Film *p;
p = head->next;
while(p != NULL) {
printf("%-20s %-20s %-20s %d\n", p->name, p->director, p->region, p->year);
p = p -> next;
}
}
TASK4
#include <stdio.h>
#define N 10
typedef struct {
char isbn[20]; // isbn號
char name[80]; // 書名
char author[80]; // 作者
double sales_price; // 售價
int sales_count; // 銷售冊數
} Book;
void output(Book x[], int n);
void sort(Book x[], int n);
double sales_amount(Book x[], int n);
int main() {
Book x[N] = {{"978-7-229-14156-1", "源泉", "安.蘭德", 84, 59},
{"978-7-5133-5261-1", "李白來到舊金山", "譚夏陽", 48, 16},
{"978-7-5617-4347-8", "陌生人日記", "周怡芳", 72.6, 27},
{"978-7-5722-5475-8", "晶片簡史", "汪波", 74.9, 49},
{"978-7-5046-9568-0", "資料化決策", "道格拉斯·W·哈伯德", 49, 42},
{"978-7-5133-4388-6", "美好時代的背後", "凱瑟琳.布", 34.5, 39},
{"978-7-1155-0509-5", "無窮的開始:世界進步的本源", "戴維·多伊奇", 37.5, 55},
{"978-7-5321-5691-7", "何為良好生活", "陳嘉映", 29.5 , 31},
{"978-7-5133-5109-6", "你好外星人", "英國未來出版集團", 118, 42},
{"978-7-2011-4617-1", "世界盡頭的咖啡館", "約翰·史崔勒基", 22.5, 44}};
printf("圖書銷量排名: \n");
sort(x, N);
output(x, N);
printf("\n圖書銷售總額: %.2f\n", sales_amount(x, N));
return 0;
}
// 待補足:函式output()實現
void output(Book x[], int n){
int i;
for(i=0;i<n;i++){
printf("%s %s %s %lf %d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count);
}
}
// 待補足:函式sort()實現
void sort(Book x[], int n){
int i,j;
Book t;
for(i=0;i<n-1;i++){
for(j=0;j<i-1-n;j++)
if(x[j].sales_count < x[j+1].sales_count){
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
}
}
// 待補足:函式sales_count()實現
double sales_amount(Book x[], int n){
double s,sum=0;
int i;
for(i=0;i<n;i++){
s=x[i].sales_price * x[i].sales_count;
sum+=s;
}
return sum;
}
TASK5
#include <stdio.h>
typedef struct {
int year;
int month;
int day;
} Date;
// 函式宣告
void input(Date *pd);
int day_of_year(Date d);
int compare_dates(Date d1, Date d2);
void test1();
void test2();
int main() {
printf("測試1: 輸入日期, 列印輸出這是一年中第多少天\n");
test1();
printf("\n測試2: 兩個人年齡大小關係\n");
test2();
}
void test1() {
Date d;
int i;
printf("輸入日期:(以形如2024-06-01這樣的形式輸入)\n");
for(i = 0; i < 3; ++i) {
input(&d);
printf("%04d-%02d-%02d是這一年中第%d天\n\n", d.year, d.month, d.day, day_of_year(d));
}
}
void test2() {
Date Alice_birth, Bob_birth;
int i;
int ans;
printf("輸入Alice和Bob出生日期:(以形如2005-08-11這樣的形式輸入)\n");
for(i = 0; i < 3; ++i) {
input(&Alice_birth);
input(&Bob_birth);
ans = compare_dates(Alice_birth, Bob_birth);
if(ans == 0)
printf("Alice和Bob一樣大\n\n");
else if(ans == -1)
printf("Alice比Bob大\n\n");
else
printf("Alice比Bob小\n\n");
}
}
void input(Date *pd) {
scanf("%04d-%02d-%02d",&(pd->year),&(pd->month),&(pd->day));
}
int day_of_year(Date d) {
int s,n=0,i;
int map[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if((d.year % 4==0 && d.year % 100 != 0)||d.year%400==0)
map[2]=29;
if(d.month==1)
n=d.day;
if(d.month != 1){
for(i=1;i<=d.month-1;i++){
s=map[i];
n+=s;
}
n=n+d.day;
}
return n;
}
int compare_dates(Date d1, Date d2) {
int a,b;
a=day_of_year(d1);
b=day_of_year(d2);
if(d1.year<d2.year)
return -1;
if(d1.year>d2.year)
return 1;
else{
if(a<b)
return -1;
if(a==b)
return 0;
if(a>b)
return 1;
}
}
TASK6
#include <stdio.h>
#include <string.h>
enum Role {admin, student, teacher};
typedef struct {
char username[20]; // 使用者名稱
char password[20]; // 密碼
enum Role type; // 賬戶型別
} Account;
// 函式宣告
void output(Account x[], int n); // 輸出賬戶陣列x中n個賬戶資訊,其中,密碼用*替代顯示
int main() {
Account x[] = {{"A1001", "123456", student},
{"A1002", "123abcdef", student},
{"A1009", "xyz12121", student},
{"X1009", "9213071x", admin},
{"C11553", "129dfg32k", teacher},
{"X3005", "921kfmg917", student}};
int n;
n = sizeof(x)/sizeof(Account);
output(x, n);
return 0;
}
// 待補足的函式output()實現
// 功能:遍歷輸出賬戶陣列x中n個賬戶資訊
// 顯示時,密碼欄位以與原密碼相同欄位長度的*替代顯示
void output(Account x[], int n) {
// 待補足
// ×××
int i, j;
char s[][10]={"admin","student","teacher"};
for(i = 0; i < n; i++) {
int len = strlen(x[i].password);
printf("%s\t\t", x[i].username);
for(j = 0; j < len; j++) {
printf("*");
}
if(len < 8)
printf("\t\t");
else
printf("\t");
switch(x[i].type) {
case 0:
printf("%s\n",s[0]);
break;
case 1:
printf("%s\n",s[1]);
break;
default:
printf("%s\n",s[2]);
}
}
}