-
Notifications
You must be signed in to change notification settings - Fork 0
/
46.c
106 lines (94 loc) · 2.22 KB
/
46.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
struct digit {
int num;
struct digit *next;
};
struct digit * createDigit(int dig);
struct digit * append(struct digit * end, struct digit * newDigptr);
void printNumber(struct digit *start);
void freeNumber(struct digit *start);
int divisibleByThree(struct digit * start);
struct digit * readNumber(void);
//Add your own function prototypes here
int changeThrees(struct digit *start);
int main(void) {
struct digit *start;
start = readNumber();
printf("The number ");
printNumber(start);
printf("was modified in %d places.\n", changeThrees(start));
printf("The new number is ");
printNumber(start);
freeNumber(start);
return 0;
}
struct digit * createDigit(int dig) {
struct digit *ptr;
ptr = (struct digit *) malloc(sizeof(struct digit));
ptr->num = dig;
ptr->next = NULL;
return ptr;
}
struct digit * append(struct digit * end, struct digit * newDigptr) {
end->next = newDigptr;
return(end->next);
}
void printNumber(struct digit *start) {
struct digit * ptr = start;
while (ptr!=NULL) {
printf("%d", ptr->num);
ptr = ptr->next;
}
printf("\n");
}
void freeNumber(struct digit *start) {
struct digit * ptr = start;
struct digit * tmp;
while (ptr!=NULL) {
tmp = ptr->next;
free(ptr);
ptr = tmp;
}
}
struct digit * readNumber(void) {
char c;
int d;
struct digit *start, *end, *newptr;
start = NULL;
scanf("%c", &c);
while (c != '\n') {
d = c-48;
newptr = createDigit(d);
if (start == NULL) {
start = newptr;
end = start;
} else {
end = append(end, newptr);
}
scanf("%c", &c);
}
return(start);
}
int divisibleByThree(struct digit * start) {
struct digit * ptr = start;
int qsum = 0;
while (ptr!=NULL) {
qsum += ptr->num;
ptr = ptr->next;
}
if (qsum%3==0) return 1;
else return 0;
}
// Write your changeThrees() function here
int changeThrees(struct digit *start){
int n = 0; // number of times changed
while(start != NULL){
if(start->num == 3){
n++;
start->num = 9;
}
start = start->next;
}
return n;
}