修改文件结构。
This commit is contained in:
70
POP/13/Exercise01.c
Normal file
70
POP/13/Exercise01.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#define MALE false
|
||||
#define FEMALE true
|
||||
|
||||
typedef struct {
|
||||
int year;
|
||||
int month;
|
||||
int date;
|
||||
} DATE;
|
||||
|
||||
typedef struct {
|
||||
int ID;
|
||||
char name[21];
|
||||
bool sex;
|
||||
DATE birthday;
|
||||
double score;
|
||||
} STUDENT;
|
||||
|
||||
double getAvg(STUDENT students[], int size) {
|
||||
double sum = 0;
|
||||
int i = 0;
|
||||
for (i = 0; i < size; i++) {
|
||||
sum += students[i].score;
|
||||
}
|
||||
sum /= size;
|
||||
return sum;
|
||||
}
|
||||
|
||||
void sortStu(STUDENT students[], int size) {
|
||||
bool moved = false;
|
||||
int i = 0;
|
||||
STUDENT tmp;
|
||||
do {
|
||||
moved = false;
|
||||
for (i = 0; i < size - 1; i++) {
|
||||
if (students[i].score < students[i + 1].score) {
|
||||
tmp = students[i];
|
||||
students[i] = students[i+1];
|
||||
students[i+1] = tmp;
|
||||
moved = true;
|
||||
}
|
||||
}
|
||||
} while (moved);
|
||||
for (i = 0; i < size; i++) {
|
||||
printf("%s: %lf\n", students[i].name, students[i].score);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
STUDENT students[10];
|
||||
int i = 0;
|
||||
// freopen("fakedata","r",stdin);
|
||||
for (i = 0; i < 10; i++) {
|
||||
printf("ID: ");
|
||||
scanf("%d", &students[i].ID);
|
||||
printf("Name(max 20 char): ");
|
||||
scanf("%20s", students[i].name);
|
||||
printf("Sex: (MALE-0/FEMLE-1)");
|
||||
scanf("%d", &students[i].sex);
|
||||
printf("Birthday (Year/Month/Date):");
|
||||
scanf("%d/%d/%d", &students[i].birthday.year, &students[i].birthday.month, &students[i].birthday.date);
|
||||
printf("Scores:\n");
|
||||
scanf("%lf", &students[i].score);
|
||||
}
|
||||
printf("The Average is: %lf\n", getAvg(students, 10));
|
||||
printf("Sort result:\n");
|
||||
sortStu(students, 10);
|
||||
return 0;
|
||||
}
|
||||
38
POP/13/Exercise02.c
Normal file
38
POP/13/Exercise02.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
enum weekday {sun, mon, tue, wed, thu, fri, sat};
|
||||
|
||||
int main() {
|
||||
int input = 0;
|
||||
char output[10];
|
||||
scanf("%d", &input);
|
||||
switch (input % 7)
|
||||
{
|
||||
case sun:
|
||||
strcpy(output, "Sunday");
|
||||
break;
|
||||
case mon:
|
||||
strcpy(output, "Monday");
|
||||
break;
|
||||
case tue:
|
||||
strcpy(output, "Tuesday");
|
||||
break;
|
||||
case wed:
|
||||
strcpy(output, "Wednesday");
|
||||
break;
|
||||
case thu:
|
||||
strcpy(output, "Thursday");
|
||||
break;
|
||||
case fri:
|
||||
strcpy(output, "Friday");
|
||||
break;
|
||||
case sat:
|
||||
strcpy(output, "Saturday");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
printf("%s", output);
|
||||
return 0;
|
||||
}
|
||||
79
POP/13/Exercise03.c
Normal file
79
POP/13/Exercise03.c
Normal file
@@ -0,0 +1,79 @@
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
long long int numerator;
|
||||
unsigned long long int denominator;
|
||||
} FRACTION;
|
||||
|
||||
FRACTION Fra_Add(FRACTION f1, FRACTION f2);
|
||||
FRACTION Fra_Sub(FRACTION f1, FRACTION f2);
|
||||
long long findGcd(long long a, long long b);
|
||||
FRACTION reduceFrac(FRACTION origin);
|
||||
|
||||
int main() {
|
||||
FRACTION result, toBeAdd;
|
||||
int i = 0;
|
||||
|
||||
result.numerator = 0;
|
||||
result.denominator = 1;
|
||||
toBeAdd.numerator = 1;
|
||||
i = 1;
|
||||
while (i < 11) {
|
||||
toBeAdd.denominator = 2 * i - 1;
|
||||
result = Fra_Add(result, toBeAdd);
|
||||
i++;
|
||||
toBeAdd.denominator = 2 * i - 1;
|
||||
result = Fra_Sub(result, toBeAdd);
|
||||
i++;
|
||||
}
|
||||
result.numerator *= 4;
|
||||
result = reduceFrac(result);
|
||||
printf("Result: %lld/%lu, or %lf", result.numerator, result.denominator, ((double) result.numerator)/ result.denominator);
|
||||
return 0;
|
||||
}
|
||||
|
||||
FRACTION Fra_Add(FRACTION f1, FRACTION f2) {
|
||||
FRACTION result;
|
||||
if (f1.denominator == 0 || f2.denominator == 0) {
|
||||
result.denominator = 0;
|
||||
result.numerator = 0;
|
||||
return result;
|
||||
}
|
||||
long long gcd = findGcd((long long)f1.denominator, (long long)f2.denominator);
|
||||
result.denominator = (f1.denominator / gcd) * f2.denominator;
|
||||
result.numerator = f2.denominator / gcd * f1.numerator + f1.denominator / gcd * f2.numerator;
|
||||
return result;
|
||||
}
|
||||
|
||||
FRACTION Fra_Sub(FRACTION f1, FRACTION f2) {
|
||||
FRACTION result;
|
||||
if (f1.denominator == 0 || f2.denominator == 0) {
|
||||
result.denominator = 0;
|
||||
result.numerator = 0;
|
||||
return result;
|
||||
}
|
||||
long long gcd = findGcd((long long)f1.denominator, (long long)f2.denominator);
|
||||
result.denominator = (f1.denominator / gcd) * f2.denominator;
|
||||
result.numerator = f2.denominator / gcd * f1.numerator - f1.denominator / gcd * f2.numerator;
|
||||
return result;
|
||||
}
|
||||
|
||||
long long findGcd(long long a, long long b) {
|
||||
long long c = a % b;
|
||||
switch (c) {
|
||||
case 0:
|
||||
return b;
|
||||
break;
|
||||
default:
|
||||
return findGcd(b, c);
|
||||
}
|
||||
}
|
||||
|
||||
FRACTION reduceFrac(FRACTION origin) {
|
||||
long long gcd;
|
||||
FRACTION result;
|
||||
gcd = findGcd(origin.numerator, origin.denominator);
|
||||
result.numerator = origin.numerator / gcd;
|
||||
result.denominator = origin.denominator / gcd;
|
||||
return result;
|
||||
}
|
||||
154
POP/13/Optional01.c
Normal file
154
POP/13/Optional01.c
Normal file
@@ -0,0 +1,154 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define LEN sizeof(NODE)
|
||||
|
||||
typedef struct tagNode{
|
||||
int index;
|
||||
int value;
|
||||
struct tagNode * prev;
|
||||
struct tagNode * next;
|
||||
} NODE;
|
||||
|
||||
NODE* insert(NODE* head, int index, int value);
|
||||
NODE* delete_by_index(NODE* head, int index);
|
||||
NODE* create(int index, int value);
|
||||
void printOut(NODE* head);
|
||||
|
||||
int main() {
|
||||
char command;
|
||||
int index, value;
|
||||
NODE* head = NULL;
|
||||
while (1) {
|
||||
printf("Type a command. (P)rint, (I)nsert, (D)elete, (Q)uit: ");
|
||||
fflush(stdin);
|
||||
scanf("%c", &command);
|
||||
fflush(stdin);
|
||||
switch (command) {
|
||||
case 'P':
|
||||
case 'p':
|
||||
printOut(head);
|
||||
break;
|
||||
case 'I':
|
||||
case 'i':
|
||||
printf("Index and value: ");
|
||||
scanf("%d %d", &index, &value);
|
||||
if (head == NULL) {
|
||||
head = create(index, value);
|
||||
}
|
||||
else {
|
||||
insert(head, index, value);
|
||||
}
|
||||
break;
|
||||
case 'D':
|
||||
case 'd':
|
||||
printf("Index: ");
|
||||
scanf("%d", &index);
|
||||
head = delete_by_index(head, index);
|
||||
break;
|
||||
case 'Q':
|
||||
case 'q':
|
||||
return 0;
|
||||
default:
|
||||
printf("Unkown command.\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
NODE* insert(NODE* head, int index, int value) {
|
||||
NODE* currentNode;
|
||||
NODE* newNodePtr;
|
||||
currentNode = head;
|
||||
if ((*currentNode).index > index) {
|
||||
newNodePtr = malloc(LEN);
|
||||
(*newNodePtr).index = index;
|
||||
(*newNodePtr).value = value;
|
||||
(*newNodePtr).prev = NULL;
|
||||
(*newNodePtr).next = currentNode;
|
||||
(*currentNode).prev = newNodePtr;
|
||||
return newNodePtr;
|
||||
}
|
||||
while((*currentNode).next != NULL) {
|
||||
if (index == (*currentNode).index) {
|
||||
(*currentNode).value = value;
|
||||
return currentNode;
|
||||
}
|
||||
if (index >= (*((*currentNode).next)).index) {
|
||||
currentNode = (*currentNode).next;
|
||||
continue;
|
||||
}
|
||||
newNodePtr = malloc(LEN);
|
||||
(*newNodePtr).index = index;
|
||||
(*newNodePtr).value = value;
|
||||
(*newNodePtr).prev = currentNode;
|
||||
(*newNodePtr).next = (*currentNode).next;
|
||||
(*(*currentNode).next).prev = newNodePtr;
|
||||
(*currentNode).next = newNodePtr;
|
||||
return newNodePtr;
|
||||
}
|
||||
newNodePtr = malloc(LEN);
|
||||
(*newNodePtr).index = index;
|
||||
(*newNodePtr).value = value;
|
||||
(*newNodePtr).prev = currentNode;
|
||||
(*newNodePtr).next = NULL;
|
||||
(*currentNode).next = newNodePtr;
|
||||
return newNodePtr;
|
||||
}
|
||||
|
||||
NODE* create(int index, int value) {
|
||||
NODE* newNodePtr;
|
||||
newNodePtr = malloc(LEN);
|
||||
(*newNodePtr).index = index;
|
||||
(*newNodePtr).value = value;
|
||||
(*newNodePtr).prev = NULL;
|
||||
(*newNodePtr).next = NULL;
|
||||
return newNodePtr;
|
||||
}
|
||||
|
||||
void printOut(NODE* head) {
|
||||
NODE current;
|
||||
if (head == NULL) {
|
||||
printf("(Empty list)\n");
|
||||
return;
|
||||
}
|
||||
current = *head;
|
||||
while (1) {
|
||||
printf("Index: %d Value: %d\n", current.index, current.value);
|
||||
if (current.next == NULL) {
|
||||
break;
|
||||
}
|
||||
current = *(current.next);
|
||||
}
|
||||
}
|
||||
|
||||
NODE* delete_by_index(NODE* head, int index) {
|
||||
NODE* current = head;
|
||||
NODE* newHead;
|
||||
if ((*current).index == index) {
|
||||
if ((*current).next != NULL) {
|
||||
(*((*current).next)).prev = NULL;
|
||||
}
|
||||
newHead = (*current).next;
|
||||
free(current);
|
||||
return newHead;
|
||||
}
|
||||
while ((*current).next != NULL) {
|
||||
if ((*current).index == index) {
|
||||
(*((*current).prev)).next = (*current).next;
|
||||
(*((*current).next)).prev = (*current).prev;
|
||||
free(current);
|
||||
return head;
|
||||
}
|
||||
current = (*current).next;
|
||||
}
|
||||
if ((*current).index == index) {
|
||||
if (((*current).prev) != NULL) {
|
||||
(*((*current).prev)).next = NULL;
|
||||
}
|
||||
newHead = (*current).prev;
|
||||
free(current);
|
||||
return newHead;
|
||||
}
|
||||
printf("No such node whose index is %d!\n", index);
|
||||
return head;
|
||||
}
|
||||
59
POP/13/fakedata
Normal file
59
POP/13/fakedata
Normal file
@@ -0,0 +1,59 @@
|
||||
345
|
||||
aaa
|
||||
0
|
||||
2020/3/4
|
||||
3.5
|
||||
|
||||
465
|
||||
bbb
|
||||
1
|
||||
2020/3/5
|
||||
4.5
|
||||
|
||||
933
|
||||
ddd
|
||||
0
|
||||
2020/4/6
|
||||
5
|
||||
|
||||
9688
|
||||
dadg
|
||||
0
|
||||
2020/12/6
|
||||
6.8
|
||||
|
||||
33
|
||||
adfidadff
|
||||
0
|
||||
2020/4/6
|
||||
7.9
|
||||
|
||||
34555
|
||||
aaa4wt
|
||||
0
|
||||
2020/3/4
|
||||
2.5
|
||||
|
||||
463
|
||||
bbberr
|
||||
1
|
||||
2020/3/5
|
||||
6.5
|
||||
|
||||
93364
|
||||
dddade
|
||||
0
|
||||
2020/4/6
|
||||
8
|
||||
|
||||
9688243
|
||||
dadgjj
|
||||
0
|
||||
2020/12/6
|
||||
10
|
||||
|
||||
33345
|
||||
adfidafd
|
||||
0
|
||||
2020/4/6
|
||||
9
|
||||
Reference in New Issue
Block a user