Commit a821cd80 by Yongshan Ding

Initial commit

parents
CCBIN=/usr/bin/gcc
CC=$(CCBIN) -Wall -Wextra -std=c99 -pedantic -g
C_LIBS=lib/array_util.c lib/string_util.c
default: validity
duplicates: $(C_LIBS) lib/*.h duplicates.c duplicates_test.c
$(CC) -O3 -o duplicates $(C_LIBS) duplicates.c duplicates_test.c
trending: $(C_LIBS) lib/*.h trending.c trending_test.c
$(CC) -O3 -o trending $(C_LIBS) trending.c trending_test.c
validity: $(C_LIBS) lib/*.h validity.c validity_test.c
$(CC) -O3 -o validity $(C_LIBS) validity.c validity_test.c
.PHONY: clean
clean:
rm -Rf *.o lib/*.o duplicates trending validity *.dSYM
.PHONY: package
package:
tar -cvzf lab2-handin.tgz duplicates.c trending.c validity.c
/* CMSC 16200 - Lab 2
* File: duplicates.c
*
* Name: (YOUR NAME HERE)
* CNet: (YOUR CNET ID HERE)
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lib/duplicates.h"
int number_distinct(const char **words, int n) {
// Implement this function
return 42;
}
void indices_distinct(const char **words, int *result, int n) {
// Implement this function
return;
}
char **remove_duplicates(const char **words, int n) {
// Implement this function
return NULL;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "lib/duplicates.h"
int main(void) {
// Testing number_distinct
printf("%s\n", "Testing number_distinct ...");
const char **a = calloc(3, sizeof(char*)); // create an array of strings on heap
a[0] = "hello";
a[1] = "world";
a[2] = "world";
assert(2 == number_distinct(a, 3));
free(a);
// Add your own tests here.
printf("%s\n", "Passed all tests!");
// Testing indices_distinct
printf("%s\n", "Testing indices_distinct ...");
// Add your own tests here.
printf("%s\n", "Passed all tests!");
// Testing remove_duplicates
printf("%s\n", "Testing remove_duplicates ...");
// Add your own tests here.
printf("%s\n", "Passed all tests!");
return 0;
}
/* CMSC 16200 - Lab 2
* File: array_util.c
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// Linear search
int linear_search(const char *s, const char **A, int n) {
// Assume A is sorted
for (int i = 0; i < n; i++) {
int cmp = strcmp(A[i], s);
if (cmp == 0) {
return i;
} else if (cmp > 0) {
//A[i] > s
return -1;
}
//A[i] < s
}
return -1;
}
// Binary search
int binary_search(const char *s, const char **A, int n){
int lower = 0;
int upper = n;
while (lower < upper) {
int mid = lower + (upper-lower)/2;//(lower + upper)/2 could overflow
// printf("mid:%d, %s\n", mid, A[mid]);
int cmp = strcmp(A[mid], s);
if (cmp == 0) {
return mid;
} else if (cmp > 0) {
//A[mid] > s
upper = mid;
} else {
//A[mid] < s
lower = mid + 1;
}
}
return -1;
}
/* CMSC 16200 - Lab 2
* File: array_util.h
*/
#ifndef _ARRAY_UTIL_H_
#define _ARRAY_UTIL_H_
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// Linear search
int linear_search(const char *s, const char **A, int n);
// Binary search
int binary_search(const char *s, const char **A, int n);
#endif
This source diff could not be displayed because it is too large. You can view the blob instead.
/* CMSC 16200 - Lab 2
* File: duplicates.h
*/
#ifndef _DUPLICATES_H_
#define _DUPLICATES_H_
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int number_distinct(const char **words, int n);
void indices_distinct(const char **words, int *result, int n);
char **remove_duplicates(const char **words, int n);
#endif
/* CMSC 16200 - Lab 2
* File: string_util.c
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "array_util.h"
// Look up dictionary
bool is_word(char *s){
int WORDMAXLINES = 109585;
int WORDBUFSIZ = 50;
int i = 0;
FILE *fp = fopen("dictionary", "r");
if (fp == 0) {
fp = fopen("lib/dictionary", "r");
if (fp == 0) {
fprintf(stderr, "failed to open dictionary\n");
exit(1);
}
}
char **lines = malloc(WORDMAXLINES * sizeof(char*));
for (int j = 0; j < WORDMAXLINES; ++j) {
lines[j] = (char *)malloc(WORDBUFSIZ+1);
}
while (i < WORDMAXLINES && fgets(lines[i], 50, fp))
{
lines[i][strlen(lines[i])-1] = '\0';
i = i + 1;
}
fclose(fp);
int r = binary_search(s, (const char **)lines, i);
// printf("isWord: %d\n", r);
for (int j = 0; j < WORDMAXLINES; ++j) {
free(lines[j]);
}
free(lines);
return (-1 != r);
}
// Extract substring
char *substring(char *dest, char *src, int i, int len) {
memcpy( dest, &src[i], len );
dest[len] = '\0';
return dest;
}
// int main(void) {
// char *s = "hello";
// bool r = is_word(s);
// printf(r ? "true\n" : "false\n");
// return 42;
// }
/* CMSC 16200 - Lab 2
* File: string_util.h
*/
#ifndef _STRING_UTIL_H_
#define _STRING_UTIL_H_
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "array_util.h"
// Look up dictionary
bool is_word(char *s);
// Extract substring
char *substring(char *dest, char *src, int i, int len);
#endif
/* CMSC 16200 - Lab 2
* File: trending.h
*/
#ifndef _TRENDING_H_
#define _TRENDING_H_
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int count_frequencies(const char **words, int *frequencies, int n,
const char **feed, int feedlength, bool fast);
void top_three(int *frequencies, int *result, int n);
#endif
/* CMSC 16200 - Lab 2
* File: validity.h
*/
#ifndef _VALIDITY_H_
#define _VALIDITY_H_
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
void sentence2hashtag(char *sentence);
bool is_valid_hashtag(char *hashtag);
#endif
\newcommand{\labname}{Lab 2}
\documentclass[11pt]{article}
\usepackage[left=1in, right=1in, top=1in, bottom=1in]{geometry}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{graphicx}
\usepackage{fancyhdr}
% TODO: CHANGE THESE
\newcommand{\myname}{Change Me} % Full Name
\newcommand{\mycnetid}{changeme} % CNetID
% header
\pagestyle{fancy}
\fancyhf{}
\lhead{CS 162 \labname}
\chead{\myname{} (\mycnetid)}
\rhead{Page \thepage{} of \pageref{mylastpagelabel}}
% document starts
\begin{document}
\paragraph{Task XX}
% Answer for Task XX goes in here.
% This label is used in the header to keep track of the page count.
\label{mylastpagelabel}
\end{document}
/* CMSC 16200 - Lab 2
* File: trending.c
*
* Name: (YOUR NAME HERE)
* CNet: (YOUR CNET ID HERE)
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "lib/trending.h"
#include "lib/array_util.h"
int count_frequencies(const char **words, int *frequencies, int n,
const char **feed, int feedlength, bool fast) {
// Implement this function
return 42;
}
void top_three(int *frequencies, int *result, int n) {
// Implement this function
return;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "lib/trending.h"
int main(void) {
// Testing count_frequencies
printf("%s\n", "Testing count_frequencies ...");
// Add your own tests here.
printf("%s\n", "Passed all tests!");
// Testing top_three
printf("%s\n", "Testing top_three ...");
// Add your own tests here.
printf("%s\n", "Passed all tests!");
return 0;
}
/* CMSC 16200 - Lab 2
* File: validity.c
*
* Name: (YOUR NAME HERE)
* CNet: (YOUR CNET ID HERE)
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "lib/validity.h"
#include "lib/string_util.h"
void sentence2hashtag(char *sentence) {
// Implement this function
return;
}
bool is_valid_hashtag(char *hashtag) {
// Implement this function
return false;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "lib/validity.h"
int main(void) {
// Testing sentence2hashtag
printf("%s\n", "Testing sentence2hashtag ...");
// Add your own tests here.
printf("%s\n", "Passed all tests!");
// Testing is_valid_hashtag
printf("%s\n", "Testing is_valid_hashtag ...");
// Add your own tests here.
printf("%s\n", "Passed all tests!");
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment