Ai
1 Star 0 Fork 0

晨曦/第四次PTA作业

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
wc.c 1.53 KB
一键复制 编辑 原始数据 按行查看 历史
晨曦 提交于 2018-05-06 21:10 +08:00 . Upload wc.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int zifushu(FILE *fp);
int cishu(FILE *fp);
int line(FILE *fp);
int main(int argc,char *argv[])
{
char filename[20];
printf("Please input you want to open the file name:\n");
gets(filename);
FILE *fp;
if((fp=fopen(filename,"r"))==NULL){
printf("cannot open file\n");
exit(0);
}
char a;
int count=0;
printf("请输入一个字符:(c统计字符数/w统计单词数/l统计行数)\n");
scanf("%c",&a);
if(a=='c'){
count=zifushu(fp);
}else if(a=='w'){
count=cishu(fp);
}else if(a=='l'){
count=line(fp);
}else{
printf("请输入正确的命令字符\n");
fclose(fp);
return 0;
}
printf("%d",count);
fclose(fp);
return 0;
}
int zifushu(FILE *fp){
/*FILE *fp;
if(fp=fopen(file.c,"r")==NULL){
printf("cannot open file\n");
exit(0);
}*/
char c;
int count=0;
while(!feof(fp)){
c=fgetc(fp);
if(c>=0&&c<=127){
count++;
}
}
//fclose(fp);
return count;
// printf("%d",count);
}
int cishu(FILE *fp){
/*FILE *fp;
if(fp=fopen(file.c,"r")==NULL){
printf("cannot open file\n");
exit(0);
}*/
char c;
int count=0;
while(!feof(fp)){
c=fgetc(fp);
if((c>='A'&&c<='Z')||(c>='a'&&c<='z')) count++;
}
//fclose(fp);
return count;
// printf("%d",count);
}
int line(FILE *fp){
/*FILE *fp;
if(fp=fopen(file.c,"r")==NULL){
printf("cannot open file\n");
exit(0);
}*/
char c;
int count=0;
while(!feof(fp)){
c=fgetc(fp);
if(c=='\n') count++;
}
//fclose(fp);
return count;
// printf("%d",count);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tmjl/fourth_pta_operation.git
git@gitee.com:tmjl/fourth_pta_operation.git
tmjl
fourth_pta_operation
第四次PTA作业
master

搜索帮助