- gitcloud
-
读写文件,教科书上有。都写出来是你做作业还是我做作业?
还有别乱用叹号,这不是问人的态度
只给出关键代码。请自行阅读并组合。
0.读入文件
1.分割文件成为token(关键点)
2.统计token数目
3.输出统计信息
1.分割token用C标准库中的strtok()函数完成。
strtok()的原型为:
char *strtok( char *strToken, const char *strDelimit );
strToken为目标字符串,strDelimit为分割符集合。返回值是一个分割好的Token。
例子程序:
#include <string.h>
#include <stdio.h>
char string[] = "A string of ,,tokens and some more tokens"; // 目标字符串
char seps[] = " , "; // 分割符集
char *token;
void main( void )
{
printf( "%s Tokens: ", string );
/* 将目标字符串和分割符集给入strtok,返回第一个token */
token = strtok( string, seps );
while( token != NULL )
{
/* 打印一个token */
printf( " %s ", token );
/* 获取下一个token */
token = strtok( NULL, seps );
}
}
输出结果如下:
moose@iBookG4:~/Projects/svn_repo/trunk/data_struct/glibc$ ./a.out
A string of ,,tokens
and some more tokens
Tokens:
A
string
of
tokens
and
some
more
tokens
可见所有单词都被分割出来了。
另外,ANSI还可以用wcstok()对汉字(宽字符集)进行分割。
再另,如果你的作业考查点在实现一个自己的strtok(),请自行分析之。
其他几个功能都是比较基本的功能。不再赘述。请自行思考。
给出一个我做的样本,很多情况没有考虑(不用文件输入/输出,不考虑内存消耗,不考虑token数量等等),仅作参考:
#include <string.h>
#include <stdio.h>
char string[] = "DESCRIPTION For strtok(): [CX] [Option Start] The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to the ISO C standard. [Option End] A sequence of calls to strtok() breaks the string pointed to by s1 into a sequence of tokens, each of which is delimited by a byte from the string pointed to by s2. The first call in the sequence has s1 as its first argument, and is followed by calls with a null pointer as their first argument. The separator string pointed to by s2 may be different from call to call. The first call in the sequence searches the string pointed to by s1 for the first byte that is not contained in the current separator string pointed to by s2. If no such byte is found, then there are no tokens in the string pointed to by s1 and strtok() shall return a null pointer. If such a byte is found, it is the start of the first token. The strtok() function then searches from there for a byte that is contained in the current separator string. If no such byte is found, the current token extends to the end of the string pointed to by s1, and subsequent searches for a token shall return a null pointer. If such a byte is found, it is overwritten by a null byte, which terminates the current token. The strtok() function saves a pointer to the following byte, from which the next search for a token shall start. Each subsequent call, with a null pointer as the value of the first argument, starts searching from the saved pointer and behaves as described above.";
char seps[] = " ,.[]: ";
char *token;
struct _token_stat{
char token[64];
int stat;
};
// 至多允许出现1024个单词
struct _token_stat gTokens[1024];
void main( void )
{
int i;
for (i = 0; i < 1024; i++)
{
gTokens[i].token[0]=" ";
gTokens[i].stat=0;
}
printf( "%s Tokens: ", string );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
// 统计信息
{
int i;
for (i=0;i<1024;i++)
{
if ( strlen(gTokens[i].token)==0)
{
strcpy(gTokens[i].token, token);
gTokens[i].stat++;
break;
}
else
{
if ( strcmp(gTokens[i].token,token)==0)
{
gTokens[i].stat++;
break;
}
}
}
}
/* Get next token: */
token = strtok( NULL, seps );
}
for (i=0;i<1024;i++)
{
if (strlen(gTokens[i].token)>0)
{
printf("token=%s, stat=%d ", gTokens[i].token, gTokens[i].stat);
}
}
}
输出结果:
moose@iBookG4:~/Projects/svn_repo/trunk/data_struct/glibc$ ./a.out
DESCRIPTION
For strtok(): [CX] [Option Start] The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to the ISO C standard. [Option End]
A sequence of calls to strtok() breaks the string pointed to by s1 into a sequence of tokens, each of which is delimited by a byte from the string pointed to by s2. The first call in the sequence has s1 as its first argument, and is followed by calls with a null pointer as their first argument. The separator string pointed to by s2 may be different from call to call.
The first call in the sequence searches the string pointed to by s1 for the first byte that is not contained in the current separator string pointed to by s2. If no such byte is found, then there are no tokens in the string pointed to by s1 and strtok() shall return a null pointer. If such a byte is found, it is the start of the first token.
The strtok() function then searches from there for a byte that is contained in the current separator string. If no such byte is found, the current token extends to the end of the string pointed to by s1, and subsequent searches for a token shall return a null pointer. If such a byte is found, it is overwritten by a null byte, which terminates the current token. The strtok() function saves a pointer to the following byte, from which the next search for a token shall start.
Each subsequent call, with a null pointer as the value of the first argument, starts searching from the saved pointer and behaves as described above.
Tokens:
token=DESCRIPTION, stat=1
token=For, stat=1
token=strtok(), stat=5
token=CX, stat=1
token=Option, stat=2
token=Start, stat=1
token=The, stat=6
token=functionality, stat=1
token=described, stat=3
token=on, stat=1
token=this, stat=1
token=reference, stat=1
token=page, stat=1
token=is, stat=12
token=aligned, stat=1
token=with, stat=3
token=the, stat=24
token=ISO, stat=3
token=C, stat=3
token=standard, stat=3
token=Any, stat=1
token=conflict, stat=1
token=between, stat=1
token=requirements, stat=1
token=here, stat=1
token=and, stat=5
token=unintentional, stat=1
token=This, stat=1
token=volume, stat=1
token=of, stat=7
token=IEEE, stat=1
token=Std, stat=1
token=1003, stat=1
token=1-2001, stat=1
token=defers, stat=1
token=to, stat=12
token=End, stat=1
token=A, stat=1
token=sequence, stat=4
token=calls, stat=2
token=breaks, stat=1
token=string, stat=8
token=pointed, stat=7
token=by, stat=10
token=s1, stat=5
token=into, stat=1
token=a, stat=13
token=tokens, stat=2
token=each, stat=1
token=which, stat=3
token=delimited, stat=1
token=byte, stat=9
token=from, stat=5
token=s2, stat=3
token=first, stat=7
token=call, stat=5
token=in, stat=5
token=has, stat=1
token=as, stat=4
token=its, stat=1
token=argument, stat=3
token=followed, stat=1
token=null, stat=5
token=pointer, stat=6
token=their, stat=1
token=separator, stat=3
token=may, stat=1
token=be, stat=1
token=different, stat=1
token=searches, stat=3
token=for, stat=4
token=that, stat=2
token=not, stat=1
token=contained, stat=2
token=current, stat=4
token=If, stat=4
token=no, stat=3
token=such, stat=4
token=found, stat=4
token=then, stat=2
token=there, stat=2
token=are, stat=1
token=shall, stat=3
token=return, stat=2
token=it, stat=2
token=start, stat=2
token=token, stat=5
token=function, stat=2
token=extends, stat=1
token=end, stat=1
token=subsequent, stat=2
token=overwritten, stat=1
token=terminates, stat=1
token=saves, stat=1
token=following, stat=1
token=next, stat=1
token=search, stat=1
token=Each, stat=1
token=value, stat=1
token=starts, stat=1
token=searching, stat=1
token=saved, stat=1
token=behaves, stat=1
token=above, stat=1
- 安徽路人假
-
记得以前有过这麽一个面试题,真要做好挺复杂的.需要考虑文章过大引起内存不足的问题吗?
- 余辉
-
1、输入1000个整数,用选择法将它们从大到小排序输出。
#include<stdio.h>
#define N 1000
void main()
{
int a[N],i,j,temp;
for(i=0;i<N;i++) scanf("%d",&a[i]);
for(i=0;i<N;i++) {
for(j=i+1;j<N;j++)
if(a[j]>a[i])
printf("%6d",a[i]);
}
getchar();
}
2、输入正整数n、正实数x后,计算x的n平方的值并输出。要求将计算x的n平方的运算写成函数mypow(x,n),函数返回值的类型为double。
#include<stdio.h>
double mypow(float x,int n){
int i=0;
double k=1;
while(i<n)
return k;
}
void main()
{
int i;
float j;
scanf("%d%f",&i,&j);
printf("%f",mypow(i,j));
getchar();
}
3、输入浮点数x,计算并输出下列分段函数sign(x)的值,y=sign(x)={
-1 x<0
0 x=0
1 x>0
#include<stdio.h>
void main()
{
int i=0;
float j;
scanf("%f",&j);
if(j<0) i=-1;
else if(j>0) i=1;
printf("%d ",i);
getchar();
}
4、输入100个正整数,求所有偶数之和。
#include<stdio.h>
#define N 100
void main()
{
int i,j=0,k=0;
for(i=0;i<N;i++){
scanf("%d",&j);
if(j%2==0) k+=j;
}
printf("%d ",k);
}