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>
int getword_no(FILE *filename)
{ FILE *ip;
ip=filename;
int word_number=0;
char c='*';
int i;
//while(c!='&')
for(i=0;i<10;i++) //used as test loop
{ printf("Letter %c ",c);
printf("No. %d\n ",word_number);
c=getc(ip);
printf("getc(ip) %c\n",c);
if(c=='#')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=' ';
word_length=max_length=0;
while(c!='&')
{
c=getc(ip);
if(c=='#')
{
if(word_length>max_length)max_length=word_length;
word_length=0;
//printf("getword_max\n");
}
else{word_length++;}
}
return(max_length);
}
FILE* getword(FILE* file)
{
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;)
{
if(c=='#')i++; //runs the pointer foward to the random word
c=getc(ip);
printf("getword\n");
}
return(ip);
}
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;
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);
//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];
printf("Random Word %s",getword(adj));
//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;
}
NB i promise that when i get it working I'll tidy up the comments and close the opened files