Lunes 07 de diciembre de 2009 a las 23:28
Tengo que hacer este programa y lo e intentado pero no entiendo... Es mi 1ra clase de programacion y es en lenguaje c y no debe estar todo dentro de main
You are tasked with creating a program that will carry out two main tasks.
The program will verify that words given as input to the program are indicated in a dictionary,
and that those words that are in the dictionary, are ordered alphabetically and displayed on screen.
The program will rely on two text files described next.
The first text file will be named dictionary.txt and will contain as many words as you want, separated by commas (i.e. “,”); the last word is special in that it is followed by the will be followed by the character @. All words should be written using lowercase letters and you may assume they have
a maximum amount of 10 characters each.
The dictionary text file may look like this:
hello,person,to,wish,flunk,test,all,students@
(Note: No blanks are employed.)
The second text file will be named input.txt, and will contain 5 words, each 10 characters or less, separated by a comma character (delimiter “,”). Again, the last word will be followed by the character @.
The input text file may look like this:
This,homework,is,very,hard@
or
Will,this,homework,ever,END@
(Note: in this file, the characters may be either uppercase or lowercase; however avoid blank spaces.)
As an overview, the project should do the following.
1) Verify the words given in the input.txt file that are in the dictionary.txt file.
This should be case insensitive. In other words, if in input.txt you have the word "Hello",
and in dictionary.txt you have the words "hello", this counts as having found the word.
The words not found in dictionary.txt, should be printed out with the message:
"The word %s was not found in the dictionary" where %s is to be substituted by the word not found.
2) The words from input.txt that were found in the dictionary.txt file, on the other hand, should be inserted into an array.
3) The array of words found in the dictionary, should then be ordered alphabetically, and then printed on screen with the header: "The words that were found are:" preceded by the order in which they were found.
For example:
The words that were found are:
3) give
2) gonna
1) never
4) up
4) Capital letters have precedence over lower case ones when sorting.
For example, if the words to order were: "Hello, alf, big, Zoey", then the order would be:
1) Hello
2) Zoey
3) alf
4) big
#include<stdio.h>
#define WORD_AMOUNT 5
#define LETTERS_PER_WORD 10
#define CASE_OFFSET 32
#define DICT "dictionary.txt"
#define INPUT "input.txt"
void test_populate_and_print();
void test_compares();
void test_find();
void test_swap();
void test_order_alphabetically();
void test_dictionary_find_and_order();
void populate_array_from_text_file
(char array_strings[][LETTERS_PER_WORD], char file_name[]);
int is_word_in_dictionary(char word[], char file_name[]);
void print_all_words
(char array_words[][LETTERS_PER_WORD], int word_amount);
void verify_all_input_file_words_in_dictionary
(char input_words[][LETTERS_PER_WORD],
char dictionary_file[], char input_file[], int word_amount);
void organize_words_alphabetically
(char array_words[][LETTERS_PER_WORD]);
void swap_words(char word1[], char word2[]);
int compare_strings_case_insensitive(char *str1, char *str2);
int compare_strings(char *str1, char *str2);
int compare_two_letters_case_insensitive(char letter1, char letter2);
void copy_from_first_to_second(char word1[], char word2[], int string_size);
void clean_up_words(char array_words[][LETTERS_PER_WORD], int word_amount);
void start_program();
int main()
{
start_program();
return 0;
}
// Function that will be called that has the logic of the program.
//
// Self-defined functions used:
// verify_all_input_file_words_in_dictionary
// organize_words_alphabetically
// clean_up_words
// print_all_words
void start_program()
{
char input_words[WORD_AMOUNT][LETTERS_PER_WORD]={0};
printf("Verifying which words aren't in file:\n");
verify_all_input_file_words_in_dictionary(input_words,DICT,INPUT, WORD_AMOUNT);
organize_words_alphabetically(input_words);
clean_up_words(input_words, WORD_AMOUNT);
printf("Showing words found that are in file:\n");
print_all_words(input_words, WORD_AMOUNT);
}
void test_dictionary_find_and_order()
{
char input_words[WORD_AMOUNT][LETTERS_PER_WORD]={0};
verify_all_input_file_words_in_dictionary(input_words,DICT,INPUT, WORD_AMOUNT);
organize_words_alphabetically(input_words);
clean_up_words(input_words, WORD_AMOUNT);
print_all_words(input_words, WORD_AMOUNT);
}
void test_order_alphabetically()
{
char input_words[WORD_AMOUNT][LETTERS_PER_WORD]={0};
populate_array_from_text_file(input_words,"input.txt");
print_all_words(input_words, WORD_AMOUNT);
printf("\n\n");
organize_words_alphabetically(input_words);
print_all_words(input_words, WORD_AMOUNT);
}
void test_swap()
{
void swap_words(char word1[], char word2[]);
void copy_from_first_to_second(char word1[], char word2[], int string_size);
char str1[LETTERS_PER_WORD] = "str1";
char str2[LETTERS_PER_WORD] = "str2";
printf("str1:%s, str2:%s\n",str1,str2);
swap_words(str1,str2);
printf("str1:%s, str2:%s\n",str1,str2);
}
void test_populate_and_print()
{
char input_words[WORD_AMOUNT][LETTERS_PER_WORD]={0};
populate_array_from_text_file(input_words,"input.txt");
print_all_words(input_words, WORD_AMOUNT);
swap_words(input_words[0], input_words[4]);
print_all_words(input_words, WORD_AMOUNT);
}
void test_compares()
{
char str1[]="h";
char str2[]="H";
printf("Case insensitive comparison:\n%s vs. %s : %d\n"
,str1,str2,compare_strings_case_insensitive(str1,str2));
printf("Case sensitive comparison:\n%s vs. %s : %d\n"
,str1,str2,compare_strings(str1,str2));
}
void test_find()
{
int is_word_in_dictionary(char word[], char file_name[]);
char word_to_find[] = "Digg";
printf("Is word %s in dictionary:%d\n",
word_to_find,
is_word_in_dictionary(word_to_find, "dictionary.txt"));
}
// Verifies the words in the file specified by "input_file" are in
// the file "dictionary_file" and prints the words that aren't in it.
// It also populates the array input_words with the words from the
// "input_file" file that ARE in the "dictionary_file".
//
// Parameters:
// input_words = an array of strings.
// dictionary_file = the file name of the dictionary file to read.
// input_file = the file name of the input file to read.
// word_amount = amount of words in array.
//
// Self-defined functions used:
// populate_array_from_text_file
// is_word_in_dictionary
void verify_all_input_file_words_in_dictionary
(char input_words[][LETTERS_PER_WORD],
char dictionary_file[], char input_file[], int word_amount)
{
}
// Function will be used to determined whether two strings are the same
// being case insensitive.
// return 0, when there is NO difference between the two letters case insensitive.
// return 1, when there is a difference between the two letters case insensitive.
//
// Parameters:
// str1 = first string to compare.
// str2 = second string to compare.
//
// Self-defined functions used:
// compare_two_letters_case_insensitive
int compare_strings_case_insensitive(char str1[], char str2[])
{
}
// Function will compare two strings while being case sensitive.
// return 0, when there is NO difference between the two letters case sensitive.
// return positive when str1 is first in alphabetical order against str2.
// return negative when str1 is second in alphabetical order against str2.
//
// Parameters:
// str1 = first string to compare.
// str2 = second string to compare.
//
// NO self-defined functions used
int compare_strings(char *str1, char *str2)
{
}
// Compares two letters to verify that they are the same letter, regardless
// of case.
// return 0, when there is NO difference between the two letters case insensitive.
// return 1, when there is a difference between the two letters case insensitive.
//
// Parameters:
// letter1 = first character to compare.
// letter2 = second character to compare.
//
// NO self-defined functions used
int compare_two_letters_case_insensitive(char letter1, char letter2)
{
}
// Populate the array "array_strings" with the words in "file_name" file.
//
// Parameters:
// array_strings = array in which all words will be stored.
// file_name = file name of file that contains the words to be stored.
//
// NO self-defined functions used
void populate_array_from_text_file
(char array_strings[][LETTERS_PER_WORD], char file_name[])
{
}
// Function that verifies if "word" word is in the file named
// "file_name"
// return 1 when the word is in the dictionary
// return 0 when the word is not in the dictionary
//
// Parameters:
// word = the word to verify if it is in the file.
// file_name = the name of the file in which to check the word.
//
// Self-defined functions used:
// compare_strings_case_insensitive
int is_word_in_dictionary(char word[], char file_name[])
{
}
// Function that organizes alphabetically the words.
// Other considerations for the alphabetical order are:
// *Words with capital letters are first.
//
// Parameters:
// array_words = the array of words to put in alphabetical order.
//
// Self-defined functions used:
// compare_strings
// swap_words
void organize_words_alphabetically(char array_words[][LETTERS_PER_WORD])
{
}
// Copies the first string to the second string
//
// Parameters:
// word1 = the string to copy to another variable.
// word2 = will be copied to the word1 string.
// string_size = the size of the string to copy.
// This size should not be larger than the size of word2 nor word1.
//
// NO self-defined functions used
void copy_from_first_to_second(char word1[], char word2[], int string_size)
{
}
// Function that swaps the two strings given.
// Parameters:
// word1 = first string
// word2 = second string
//
// Self-defined functions used:
// copy_from_first_to_second
void swap_words(char word1[], char word2[])
{
}
// Function that prints all the words in the array.
//
// Parameters:
// array_words = the array of words to print out.
// word_amount = the amount of words in the array.
//
// NO self-defined functions used
void print_all_words(char array_words[][LETTERS_PER_WORD], int word_amount)
{
}
// Function that moves down in the array the position of a certain key value,
// like a null word.
//
// Parameters:
// array_words = the array of words.
// word_amount = the amount of words in the array.
//
// Self-defined functions used:
// swap_words
void clean_up_words(char array_words[][LETTERS_PER_WORD], int word_amount)
{
}
algo asi pero no c definirlas, talvez podria ser mas sencillo, enverdad que estoy block no c q hacer, le agradecere su ayuda