该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
$ ./analyse_text
Enter your text:
Iconic United States attractions The Statue of Liberty and the Grand Canyonhave reopened to the public amid the federal government shutdown. Anotherkey attraction, Mount Rushmore, opens today. Ferry trips from Manhattan tothe Statue of Liberty resumed yesterday morning.
-------------------
Mean sentence length=13.67 (standard deviation 7.64).
Mean word length=5.56 (standard deviation 2.40).
Longest sentence=22 words.
Shortest sentence=7 words.
$
读取文段的时候一行一行地读,读完后会删除,但标准差如何在一行读后就计算?读完下一行又更改一次,怎么写啊?
下面主要看analyse 和main。。。
#include
#include
#include
#include "fgetline.h"
/*
* fgetline(FILE *f) - returns a pointer to the start of a line read
* from f, or the null pointer on any error or eof.
* Like fgets(), you may well want to get rid of the
* \n at the end (if any), and check feof(f)! This is
* not a wonderful interface, but it mimics fgets().
* Caller should free memory if desired.
*/
#ifndef FGETLINE_BUFFSIZE
# define FGETLINE_BUFFSIZE 20
#endif
#if FGETLINE_BUFFSIZE < 2
#error FGETLINE_BUFFSIZE must be > 1
#endif
void analyse(char *t,int *totalsentence,int *totalword,int *longest,int *shortest,int *howmanywords,int *num,float meanwordlenth)
{
int i;
int ifword=1;
for (i=0; t[i]!='\0'; i++) {
(*num)++;
if(i>=1){
if (t[i]==' '&&t[i-1]!=' ') {
ifword=1;
(*num)--;
}
else if (ifword==1){(*howmanywords)++;
(*totalword)++;
ifword=0;}
if ((t[i]=='.'||t[i]=='!'||t[i]=='?')&&!(t[i-1]=='.'||t[i-1]=='?'||t[i-1]=='!')) {
(*totalsentence)++;
(*num)--;
if ((*howmanywords)>=(*longest)) {
*longest=*howmanywords;
}
else if ((*howmanywords)<=(*shortest)){*shortest=*howmanywords;}
*howmanywords=0;
}
if ((t[i]=='.'||t[i]=='!'||t[i]=='?')&&(t[i-1]=='.'||t[i-1]=='?'||t[i-1]=='!')){(*num)--;}
}//ifi>=1
else {if (t[i]==' ') {
ifword=1;
(*num)--;
}
else if (ifword==1){(*howmanywords)++;
(*totalword)++;
ifword=0;}
if ((t[i]=='.'||t[i]=='!'||t[i]=='?')&&!(t[i-1]=='.'||t[i-1]=='?'||t[i-1]=='!')) {
(*totalsentence)++;
(*num)--;
if (howmanywords>=longest) {
*longest=*howmanywords;
}
else if (howmanywords<=shortest){*shortest=*howmanywords;}
*howmanywords=0;
}}
}//for
(*num)--;
}//void
char *
fgetline(FILE *f)
{
char *buffer = (char *)malloc(FGETLINE_BUFFSIZE);
char *result;
size_t length;
if (buffer == NULL) return NULL; /*out of memory*/
result= fgets(buffer, FGETLINE_BUFFSIZE, f);
if (result == NULL) { free(buffer); return NULL; } /* error/eof */
length= strlen(buffer);
if (buffer[length-1] == '\n' || feof(f))
return (char *)realloc(buffer, length+1);
{ /* longer line than buffer can hold */
char *restofline= fgetline(f);
if (restofline == NULL) return NULL; /*eof or error*/
buffer= (char *)realloc(buffer, length+strlen(restofline)+1);
if (buffer == NULL) return NULL; /*out of memory*/
(void) strcat(buffer, restofline );
free(restofline);
return buffer;
}
}
int main(int argc, char* argv[])
{
/* set file pointer to default to 'stdin'*/
FILE *ifp=stdin; /*must be capital*/
char *t;
int totalsentence=0;
int totalword=0;
int longest=0;
int shortest=1000000;
int howmanywords=0;
int num=0;
float meansentencelenth;
float meanwordlenth;
/* deal with command line arguments (if any)*/
if(argc==2)
{
if((ifp=fopen(argv[1],"rb"))==NULL)
{
printf("\nERROR: Can't open input file ");
exit(1); }
} else {
printf("Enter text to analyse:");
}
/* read, and analyse all the input */
while (t= fgetline(ifp), t != NULL)
{
analyse(t, &totalsentence, &totalword, &longest, &shortest, &howmanywords, &num);
/* we no longer need this memory buffer, so free it*/
free(t); }
meansentencelenth=(float )totalword/(float )totalsentence;
meanwordlenth=(float )num/(float )totalword;
printf("totalsentence %d\n",totalsentence);
printf("totalword %d\n",totalword);
printf("num %d\n",num);
printf("Mean sentence lenth=%.2f\n",meansentencelenth);
printf("Mean word lenth=%.2f\n",meanwordlenth);
printf("Longest sentence=%d words\n",longest);
printf("Shortest sentence=%d words\n",shortest);
}