Skip to content

Commit a799a17

Browse files
committed
Format hw1 makeFileDemo & add 2022 fix
1 parent 4f59068 commit a799a17

File tree

6 files changed

+49
-52
lines changed

6 files changed

+49
-52
lines changed

hw/hw1/makeFileDemo/binary.c

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
#include "functions.h"
22

3-
4-
void decimalToBinary (int num) {
5-
6-
//Initialize mask
7-
unsigned int mask = 0x80000000;
8-
size_t bits = sizeof(num) * CHAR_BIT;
9-
10-
for (int count = 0 ;count < bits; count++) {
11-
12-
//print
13-
(mask & num ) ? printf("1") : printf("0");
14-
15-
//shift one to the right
16-
mask = mask >> 1;
17-
}
3+
void decimalToBinary(int num) {
4+
// Initialize mask
5+
unsigned int mask = 0x80000000;
6+
size_t bits = sizeof(num) * CHAR_BIT;
7+
8+
for (int count = 0; count < bits; count++) {
9+
// print
10+
(mask & num) ? printf("1") : printf("0");
11+
12+
// shift one to the right
13+
mask = mask >> 1;
14+
}
1815
}

hw/hw1/makeFileDemo/factorial.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "functions.h"
22

3-
int factorial(int n){
4-
if(n!=1){
5-
return(n * factorial(n-1));
6-
} else return 1;
3+
int factorial(int n) {
4+
if (n != 1) {
5+
return (n * factorial(n - 1));
6+
} else
7+
return 1;
78
}
8-

hw/hw1/makeFileDemo/functions.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
#include <limits.h>
12
#include <stdio.h>
23
#include <stdlib.h>
34
#include <string.h>
4-
#include <limits.h>
55

66
void print_hello();
77
int factorial(int n);
8-
void decimalToBinary (int num);
8+
void decimalToBinary(int num);

hw/hw1/makeFileDemo/hello.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "functions.h"
22

3-
void print_hello(){
4-
printf("Hello World!\n");
3+
void print_hello() {
4+
printf("Hello World!\n");
55
}
66

7-
void print_anotherhello(){
8-
printf("Hello from hello.c main function!\n");
7+
void print_anotherhello() {
8+
printf("Hello from hello.c main function!\n");
99
}

hw/hw1/makeFileDemo/main.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
#define BUFFERLENGTH 64
44

5-
int main(){
6-
print_hello();
5+
int main() {
6+
print_hello();
77

8-
printf("Key in a number to obtain its factorial:\n");
9-
char inputNum[BUFFERLENGTH];
8+
printf("Key in a number to obtain its factorial:\n");
9+
char inputNum[BUFFERLENGTH];
1010

11-
fgets(inputNum, BUFFERLENGTH, stdin);
11+
fgets(inputNum, BUFFERLENGTH, stdin);
1212

13-
int num = atoi(inputNum);
14-
printf("The factorial of %d is %d \n", num, factorial(5));
13+
int num = atoi(inputNum);
14+
printf("The factorial of %d is %d \n", num, factorial(num));
1515

16-
printf("The 32-bit binary representation of %d is ", num);
17-
decimalToBinary(num);
18-
printf("\n");
16+
printf("The 32-bit binary representation of %d is ", num);
17+
decimalToBinary(num);
18+
printf("\n");
1919

20-
return 0;
20+
return 0;
2121
}

hw/hw1/makeFileDemo/makefile

+15-15
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@
22
REMOVE = rm
33
CC = gcc
44
DEPENDENCIES = main.c hello.c factorial.c binary.c
5-
OUT = myexecoutprogMacro.o
5+
OUT = prog.o
66

77
# Explicit rules, all the commands you can call with make
88
# (note: the <tab> in the command line is necessary for make to work)
9-
# target: dependency1 dependency2 ...
9+
# target: dependency1 dependency2 ...
1010
# <tab> command
1111

12-
#Called by: make myexecout
13-
#also executed when you just called make. This calls the first target.
14-
myexecout: main.c hello.c factorial.c binary.c
15-
gcc -o myexecoutprog.o main.c hello.c factorial.c binary.c
12+
# Called by: make prog
13+
# Also executed when you just called make. This calls the first target.
14+
prog: main.c hello.c factorial.c binary.c
15+
gcc -o prog.o main.c hello.c factorial.c binary.c
1616

17-
myexecoutFromMacro: $(DEPENDENCIES)
17+
prog1: $(DEPENDENCIES)
1818
$(CC) $(DEPENDENCIES) -o $(OUT)
1919

20-
myexecout2: main.o hello.o factorial.o binary.o
21-
gcc -o myexecoutprog2 main.o hello.o factorial.o binary.o
20+
prog2: main.o hello.o factorial.o binary.o
21+
gcc -o prog2 main.o hello.o factorial.o binary.o
2222

2323

24-
##make clean will remove myexecoutprog.o from the directory
24+
## make clean will remove prog.o from the directory
2525
clean:
26-
rm myexecoutprog.o
26+
rm prog.o
2727

28-
cleanFromMacro:
28+
clean1:
2929
$(REMOVE) $(OUT)
3030

3131
clean2:
32-
rm myexecoutprog2 *.o
32+
rm prog2 *.o
3333

34-
#Implicit rules
35-
main.o : main.c functions.h
34+
# Implicit rules
35+
main.o: main.c functions.h
3636
factorial.o: factorial.c functions.h
3737
hello.o: hello.c functions.h
3838
binary.o: binary.c functions.h

0 commit comments

Comments
 (0)