Code:
// PoetryInCode.c : Defines the entry point for the console application.
// Output Mapping: 3*adj=adjective
// 3*nou=noun
// 3*ver=verb
// 1*adv=adverb
// 2*pre=preposition
//
//
// "A (adj1) (nou1)"
// -----------------------------------
// A[n] (adj1) (nou1) (ver1) (pre1) the (adj2) (nou2).
// (adv1) the (nou1) (ver2)
// The (nou2)(ver3) (pre2) a[n] (adj3) (nou3)
//
//
//functions:
// char* openfile() Opens noun.csv, adjective.csv, verb.csv, adverb.csv or preposition.csv for read
// returning a pointer to a pointer array such that each pointer in the array
// points to the file. {*nou,*adj,*ver,*adv,*pre}.
//
// char* getword(char*) Takes {*nou/*adj/*ver/*adv/*pre} and returns a pointer to the first letter of a string
// from a random line of the file specified in openfile().
//
// int makeAn(char*) Takes a pointer to a string and returns 0 if A is the proper indefinite article and 1
// if it should be An.
//
// int getword_no(FILE*) returns the number of words in the dictionary file
//
// int getword_max(FILE*) returns the maximum word length of words in the dictionary file
//
//caveats:
// to ensure the wordlist can be added to and modified, the wordlists are read in several times during the program
// to read the number of words, and the maximum word length, governing the modulus of the random function and the
// maximum char[] length respectivly.
//
// when the word lengths are counted, the delimiting character is counted too, so the counter would read above as
// having 6 characters, allowing for the null end of string character in the arrays
//#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int getword_no(FILE *filename)
{ FILE *ip;
ip=filename;
int word_number=0;
char string[20];
int i;
//for(i=0;i<10;i++) //used as test loop
while(!(feof(ip)))
{
//printf("No. %d\n ",word_number);
fgets(string,20,ip);
printf("%s\n",string);
word_number++;
}
printf("Number of words in dictionary file is %d",word_number);
return(word_number);
}
int getword_max(FILE *filename)
{ FILE *ip;
ip=filename;
int word_length,max_length;
char c=' ';
char string[20];
word_length=max_length=0;
while(!(feof(ip)))
{
fgets(string,20,ip);
if(strlen(string)>max_length)max_length=strlen(string);
}
return(max_length);
}
void getword(FILE* file, char* string)
{
FILE *ip;
ip=file;
int i,j;
char c;
//printf("random %d\n",rand());
printf("wordno %d\n",(getword_no(ip)));
j=(rand())%(getword_no(ip)); //this restricts the random number to within the length of the file
printf("Random number=%d",j);
for(i=0;i!=j;)
{
fgets(NULL,20,ip);
//printf("getword\n");
}
}
int main()
{
//declare file pointers
FILE *adj,*nou,*ver,*adv,*pre,*current;
//seed random number generator
srand((unsigned)time(NULL));
//open files as readable while checking if it exists, if not, quit
if((nou=fopen("noun.csv", "r"))==0)
{
printf("No Nouns\n");
exit(EXIT_FAILURE);
}
if((adj=fopen("adjective.csv", "r"))==0)
{
printf("No Adjectives\n");
exit(EXIT_FAILURE);
}
if((ver=fopen("verb.csv", "r"))==0)
{
printf("No Verbs\n");
exit(EXIT_FAILURE);
}
if((adv=fopen("adverb.csv", "r"))==0)
{
printf("No Adverbs\n");
exit(EXIT_FAILURE);
}
if((pre=fopen("preposition.csv", "r"))==0)
{
printf("No Prepositions\n");
exit(EXIT_FAILURE);
}
/**********************************************************************************************/
//get maximum word lengths
int adj_max,nou_max,ver_max,adv_max,pre_max,max_max;
max_max=0;
current = adj;
adj_max=getword_max(current);
if(adj_max>max_max)max_max=adj_max;
current = nou;
nou_max=getword_max(current);
if(nou_max>max_max)max_max=nou_max;
current = ver;
ver_max=getword_max(current);
if(ver_max>max_max)max_max=ver_max;
current = adv;
adv_max=getword_max(current);
if(adv_max>max_max)max_max=adv_max;
current = pre;
pre_max=getword_max(current);
if(pre_max>max_max)max_max=pre_max;
//get maximum word lengths
int adj_x,nou_x,ver_x,adv_x,pre_x,max_x;
max_x=0;
current = adj;
adj_x=getword_no(current);
if(adj_x>max_x)max_x=adj_x;
current = nou;
nou_x=getword_no(current);
if(nou_x>max_x)max_x=nou_x;
current = ver;
ver_x=getword_no(current);
if(ver_x>max_x)max_x=ver_x;
current = adv;
adv_x=getword_no(current);
if(adv_x>max_x)max_x=adv_x;
current = pre;
pre_x=getword_no(current);
if(pre_x>max_x)max_x=pre_x;
printf("Longest Words:\nAdj=%d\nNou=%d\nVer=%d\nAdv=%d\nPre=%d\nMax=%d\n",adj_max, nou_max, ver_max, adv_max, pre_max,max_max);
printf("Longest Dictionary:\nAdj=%d\nNou=%d\nVer=%d\nAdv=%d\nPre=%d\nMax=%d\n",adj_x, nou_x, ver_x, adv_x, pre_x,max_x);
//declare string arrays[lnumber][length]
char adjective[3][adj_max];
char noun[3][nou_max];
char verb[3][ver_max];
char adverb[1][adv_max];
char preposition[2][pre_max];
char ans[3][2];
getword(adj,adjective[0]);
//get adjectives
// current=adj;
// for(i=0;i<3;i++)strcpy(adjective[i],getword[current]);
//get nouns
// current=nou;
// for(i=0;i<3;i++)strcpy(adjective[i],getword[current]);
//get verbs
// current = ver;
// for(i=0;i<3;i++)strcpy(adjective[i],getword[current]);
//get adverbs
// current = adv;
// strcpy(adjective[0],getword[current]);
//get prepositions
// current = pre;
// for(i=0;i<2;i++)strcpy(adjective[i],getword[current]);
//form a(n)'s
// if (adjective[0][0] == 'a'||adjective[0][0] == 'e'||adjective[0][0] == 'i'||adjective[0][0] == 'o'||adjective[0][0] == 'u')
// ans[0]=strcat(ans[0],'n');
//print poem
// "A (adj1) (nou1)"
// -----------------------------------
// A[n] (adj1) (nou1) (ver1) (pre1) the (adj2) (nou2).
// (adv1) the (nou1) (ver2)
// The (nou2)(ver3) (pre2) a[n] (adj3) (nou3)
return 0;
}
returning an output of