generated from kenryuS/report-temp
finished cls04
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
include ../common.mk
|
||||
|
||||
PROJECT_NAME:=cls04
|
||||
|
||||
TGTS:=$(patsubst %.c,%,$(wildcard *.c))
|
||||
|
||||
all: $(TGTS)
|
||||
|
||||
%: %.c
|
||||
@mkdir -p $(BUILD_PATH)/$(PROJECT_NAME)
|
||||
$(CC) $(CFLAGS) -I. $^ -o $(BUILD_PATH)/$(PROJECT_NAME)/$@
|
||||
|
||||
clean:
|
||||
$(RM) -drf $(BUILD_PATH)/$(PROJECT_NAME)
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "reduce.h"
|
||||
#include "utils.h"
|
||||
|
||||
int addFrac(int x[], int y[]) {
|
||||
if (x[1] == 0 || y[1] == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int l = lcm(x[1], y[1]);
|
||||
int z[] = {
|
||||
(x[0] * l)/x[1] + (y[0] * l)/y[1],
|
||||
l
|
||||
};
|
||||
|
||||
reduce(z);
|
||||
formatNeg(z);
|
||||
|
||||
x[0] = z[0];
|
||||
x[1] = z[1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int x[2] = {0};
|
||||
int y[2] = {0};
|
||||
|
||||
inputFrac(x);
|
||||
inputFrac(y);
|
||||
|
||||
int ret = addFrac(x, y);
|
||||
|
||||
if (ret == 0) {
|
||||
printFrac(x);
|
||||
putchar('\n');
|
||||
} else {
|
||||
fprintf(stderr, "Error: detected zero div\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "reduce.h"
|
||||
#include "utils.h"
|
||||
|
||||
int subFrac(int x[], int y[]) {
|
||||
if (x[1] == 0 || y[1] == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int l = lcm(x[1], y[1]);
|
||||
int z[] = {
|
||||
(x[0] * l)/x[1] - (y[0] * l)/y[1],
|
||||
l
|
||||
};
|
||||
|
||||
reduce(z);
|
||||
formatNeg(z);
|
||||
|
||||
x[0] = z[0];
|
||||
x[1] = z[1];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int x[2] = {0};
|
||||
int y[2] = {0};
|
||||
|
||||
inputFrac(x);
|
||||
inputFrac(y);
|
||||
|
||||
int ret = subFrac(x, y);
|
||||
|
||||
if (ret == 0) {
|
||||
printFrac(x);
|
||||
putchar('\n');
|
||||
} else {
|
||||
fprintf(stderr, "Error: detected zero div\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "reduce.h"
|
||||
#include "utils.h"
|
||||
|
||||
int mulFrac(int x[], int y[]) {
|
||||
if (x[1]*y[1] == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int z[] = {
|
||||
x[0] * y[0],
|
||||
x[1] * y[1]
|
||||
};
|
||||
|
||||
reduce(z);
|
||||
formatNeg(z);
|
||||
|
||||
x[0] = z[0];
|
||||
x[1] = z[1];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int x[2] = {0};
|
||||
int y[2] = {0};
|
||||
|
||||
inputFrac(x);
|
||||
inputFrac(y);
|
||||
|
||||
int ret = mulFrac(x, y);
|
||||
|
||||
if (ret == 0) {
|
||||
printFrac(x);
|
||||
putchar('\n');
|
||||
} else {
|
||||
fprintf(stderr, "Error: Detected zero div\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int getnum(void) {
|
||||
int ch, n = 0;
|
||||
|
||||
int stack[10] = {0}; // ceil(log_10(2^31))
|
||||
int i = 0;
|
||||
while ((ch = getchar()) != '\n') {
|
||||
int x = ch - '0';
|
||||
if (x < 0 || x > 9) {
|
||||
continue;
|
||||
}
|
||||
if (i <= 9) stack[i++] = x; // stack push
|
||||
}
|
||||
|
||||
int d = 1;
|
||||
for (int j = i - 1; j >= 0; j--) {
|
||||
n += stack[j] * d; // stack pop
|
||||
d = d * 10;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int ret = getnum();
|
||||
|
||||
printf("got: %d\n", ret);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef __REDUCE_H__
|
||||
#define __REDUCE_H__
|
||||
|
||||
int gcm(int a, int b) {
|
||||
int x = a, y = b;
|
||||
if (x < y) {
|
||||
x = b;
|
||||
y = a;
|
||||
}
|
||||
int r = a % b;
|
||||
if (r == 0) return b;
|
||||
return gcm(b, r);
|
||||
}
|
||||
|
||||
int lcm(int a, int b) {
|
||||
int g = gcm(a, b);
|
||||
return b * a / g;
|
||||
}
|
||||
|
||||
void reduce(int x[]) {
|
||||
int a = x[0], b = x[1];
|
||||
int g = gcm(a, b);
|
||||
x[0] = a / g;
|
||||
x[1] = b / g;
|
||||
}
|
||||
|
||||
|
||||
#endif // __REDUCE_H__
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef __UTILS_H__
|
||||
#define __UTILS_H__
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void inputFrac(int x[]) {
|
||||
printf("Input fraction(a/b): ");
|
||||
scanf("%d/%d", &x[0], &x[1]);
|
||||
}
|
||||
|
||||
void formatNeg(int x[]) {
|
||||
size_t sz = sizeof(int);
|
||||
int cnt = 0;
|
||||
for (size_t i = 0; i < 2; i++) {
|
||||
cnt += x[i] < 0 ? 1 : 0;
|
||||
x[i] = x[i] < 0 ? x[i] * -1 : x[i];
|
||||
}
|
||||
|
||||
if (cnt == 1)
|
||||
x[0] = -1 * x[0];
|
||||
}
|
||||
|
||||
void printFrac(int x[]) {
|
||||
if (x[1] == 1) {
|
||||
printf("%d", x[0]);
|
||||
} else {
|
||||
printf("%d/%d", x[0], x[1]);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // __UTILS_H__
|
||||
Reference in New Issue
Block a user