文件mydoc.txt是一篇英文文档,编写分析此文档的c程序,将包含teacher或student关键词的段落单独摘录出来,存放到edu.txt文档中。
我搜了一篇英文文档
There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real!
May teacher have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy?If student feel that it hurts you,it probably hurts the other person, too.
The happiest of people don’t necessarily have the best of everything;they just make the most of everything that comes along their way.Happiness lies for those who cry,those who hurt, those who have searched,and those who have tried,for only they can appreciate the importance of people
who have touched their lives.Love begins with a smile,grows with a kiss and ends with a tear.The brightest future will always be based on a forgotten past, teacher can’t go on well in lifeuntil you let go of your past failures and heartaches.
When student were born,you were crying and everyone around you was smiling.Live your life so that when you die,you're the one who is smiling and everyone around you is crying.
Please send this message to those people who mean something to you,to those who have touched your life in one way or another.
#include<string.h>
char CMP[99],A[9999];
char CMP1[] = "teacher",CMP2[]="student";
int next_word(int i)
{
int p=i;
while(A[p]>='A')p++;
while(A[p]==32)p++;
if(Ap]==0)return 0;
return p;
}
int cmp(int i)
{
int p=i,q=0;
memset(CMP,0,sizeof(CMP));
while(A[p]>=65)CMP[q++]=A[p++];
if(strcmp(CMP,CMP1)!=0&&strcmp(CMP,CMP2)!=0)return 0;
return 1;
}
int main()
{
FILE* fin = fopen("mydoc.txt","r");
FILE* fot = fopen("edu.txt","w");
while(fscanf("%[^\n]",&A)!=EOF)
{
int x=0,y=0;
do()
{
if(cmp(x))
{
y=1;
break;
}
x=next_word(x);
}while(x);
if(y)fputs(A,fot);
}
fclose(fin);fclose(fot);
free(fin);free(fot);
return 0;
}
//纯手打,求采纳追问
运行有误耶!
你可做一下注释吗?我有些代码不知道什么意思?谢谢啦
//因为是手打,所以没有检错。。
#include
#include
char CMP[99],A[9999];
char CMP1[] = "teacher",CMP2[]="student";
int next_word(int i)
{
int p=i;
while(A[p]>='A')p++;
while(A[p]==32)p++;
if(Ap]==0)return 0;
return p;
}//该函数调用一个下标为参数,返回紧跟其后的下一个单词开头的下标,如果该单词为字符串最后一个单词,返回0
int cmp(int i)
{
int p=i,q=0;
memset(CMP,0,sizeof(CMP));
while(A[p]>=65)CMP[q++]=A[p++];
if(strcmp(CMP,CMP1)!=0&&strcmp(CMP,CMP2)!=0)return 0;
return 1;
}//该函数调用一个下标为参数,若以该下标为开始的单词与teacher或student相等,则返回1,若都不相同返回0
int main()
{
FILE* fin = fopen("mydoc.txt","r");
FILE* fot = fopen("edu.txt","w");
while(fscanf("%[^\n]",&A)!=EOF)//%[^\n]为读入以换行符为结尾的一个字符串,当读到文件结束时停止循环
{
int x=0,y=0;//x为目前的下标位置,y为是否含有单词
do()
{
if(cmp(x))
{
y=1;
break;
}
x=next_word(x);//若不能匹配,则进入下一个单词
}while(x);
if(y)fputs(A,fot);//如果匹配,输出字符串
}
fclose(fin);fclose(fot);
free(fin);free(fot);
return 0;
}
//纯手打,求采纳
while(fscanf("%[^\n]",&A)!=EOF)这句有错误
error C2664: 'fscanf' : cannot convert parameter 1 from 'char [6]' to 'struct _iobuf *'
。。。十分抱歉。。。忘了写文件指针了。。。
修改为while(fscanf(fin,"%[^\n]",&A)!=EOF)即可