Skip to content

Commit

Permalink
Submission ready
Browse files Browse the repository at this point in the history
  • Loading branch information
maalvikabhat committed Feb 13, 2020
1 parent 4b06a15 commit 2d807e2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 38 deletions.
61 changes: 30 additions & 31 deletions exercises/ex02.5/bigint.c
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
/* Example code for Exercises in C.
This program shows a way to represent a BigInt type (arbitrary length integers)
using C strings, with numbers represented as a string of decimal digits in reverse order.
using C strings, with numbers represents as a string of decimal digits in reverse order.
Follow these steps to get this program working:
1) Read through the whole program so you understand the design.
2) Compile and run the program. It should run three tests, and they should fail.
3) Fill in the body of reverse_string(). When you get it working, the first test should pass.
4) Fill in the body of itoc(). When you get it working, the second test should pass.
5) Fill in the body of add_digits(). When you get it working, the third test should pass.
6) Uncomment the last test in main. If your three previous tests pass, this one should, too.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <ctype.h>

/* reverse_string: Returns a new string with the characters reversed.
It is the caller's responsibility to free the result.
s: string
returns: string
*/
char *reverse_string(char *s) {
//TODO: Fill this in.
return "";
int i = 0;
size_t len_s = strlen(s);
char *reversed = malloc(sizeof(char) * (len_s + 1));
char *t = s + len_s - 1;
while (t >= s) {
reversed[i] = *t;
i++;
t--;
}
return reversed;
}

/* ctoi: Converts a character to integer.
c: one of the characters '0' to '9'
returns: integer 0 to 9
*/
Expand All @@ -48,29 +45,35 @@ int ctoi(char c) {
}

/* itoc: Converts an integer to character.
i: integer 0 to 9
returns: character '0' to '9'
*/
char itoc(int i) {
//TODO: Fill this in, with an appropriate assertion.
return '0';
assert(i <= 9);
assert(0 <= i);
return i + '0';
}

/* add_digits: Adds two decimal digits, returns the total and carry.
For example, if a='5', b='6', and carry='1', the sum is 12, so
For example, if a='5', b='6', and c='1', the sum is 12, so
the output value of total should be '2' and carry should be '1'
a: character '0' to '9'
b: character '0' to '9'
c: character '0' to '9'
total: pointer to char
carry: pointer to char
*/
void add_digits(char a, char b, char c, char *total, char *carry) {
//TODO: Fill this in.
// printf("a: %c | b: %c | c: %c\n", a, b, c);
int res = ctoi(a) + ctoi(b) + ctoi(c);
int new_carry = 0;
while (res >= 10) {
res -= 10;
new_carry++;
}
*carry = itoc(new_carry);
*total = itoc(res);
// printf("carry: %s | ones: %s\n\n", carry, total);
}

/* Define a type to represent a BigInt.
Expand All @@ -80,9 +83,7 @@ void add_digits(char a, char b, char c, char *total, char *carry) {
typedef char * BigInt;

/* add_bigint: Adds two BigInts
Stores the result in z.
x: BigInt
y: BigInt
carry_in: char
Expand Down Expand Up @@ -128,7 +129,6 @@ void add_bigint(BigInt x, BigInt y, char carry_in, BigInt z) {
}

/* print_bigint: Prints the digits of BigInt in the normal order.
big: BigInt
*/
void print_bigint(BigInt big) {
Expand All @@ -139,14 +139,13 @@ void print_bigint(BigInt big) {
}

/* make_bigint: Creates and returns a BigInt.
Caller is responsible for freeing.
s: string of digits in the usual order
returns: BigInt
*/
BigInt make_bigint(char *s) {
char *r = reverse_string(s);
// puts("MAKE_BIGINT_PASSED");
return (BigInt) r;
}

Expand Down Expand Up @@ -181,14 +180,14 @@ void test_add_digits() {

void test_add_bigint() {
char *s = "1";
char *t = "99999999999999999999999999999999999999999999";
char *t = "99999999999999999999999999999999999999999999";
char *res = "000000000000000000000000000000000000000000001";

BigInt big1 = make_bigint(s);
BigInt big2 = make_bigint(t);
BigInt big3 = malloc(100);

add_bigint(big1, big2, '0', big3);
add_bigint(big1, big2, '0', big3);

if (strcmp(big3, res) == 0) {
printf("add_bigint passed\n");
Expand All @@ -205,6 +204,6 @@ int main (int argc, char *argv[])

//TODO: When you have the first three functions working,
// uncomment the following, and it should work.
// test_add_bigint();
test_add_bigint();
return 0;
}
28 changes: 23 additions & 5 deletions exercises/ex02.5/find_track.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* Example code for Exercises in C.
Modified version of an example from Chapter 2.5 of Head First C.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>

#define NUM_TRACKS 5

Expand Down Expand Up @@ -37,7 +36,26 @@ void find_track(char search_for[])
// Prints track number and title.
void find_track_regex(char pattern[])
{
// TODO: fill this in
char msgbuf[100];
int i;
int res;
regex_t preg;

res = regcomp(&preg, pattern, REG_ICASE);

if (res) {
puts("ERROR: Regex could not be compiled.");
exit(1);
}

for (i = 0; i < NUM_TRACKS; ++i) {
res = regexec(&preg, tracks[i], 0, NULL, 0);
if (!res) {
printf("Track found! Track %i: '%s'\n", i, tracks[i]);
}
}

regfree(&preg);
}

// Truncates the string at the first newline, if there is one.
Expand All @@ -58,8 +76,8 @@ int main (int argc, char *argv[])
fgets(search_for, 80, stdin);
rstrip(search_for);

find_track(search_for);
//find_track_regex(search_for);
// find_track(search_for);
find_track_regex(search_for);

return 0;
}
2 changes: 0 additions & 2 deletions exercises/ex02.5/recurse.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* Example code for Exercises in C.
*/

#include <stdio.h>
Expand All @@ -20,7 +19,6 @@ int factorial(int n) {


/* fibonacci: Computes fibonacci(n).
Specifically, the Fibonacci series with F(0) = 0 and F(1) = 1
*/
int fibonacci(int n) {
Expand Down

0 comments on commit 2d807e2

Please sign in to comment.