免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6619 | 回复: 4
打印 上一主题 下一主题

C语言新手写的一个模拟linux下wc命令的小程序,实现部分功能 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-06-19 12:58 |只看该作者 |倒序浏览
本帖最后由 xiaowh00 于 2012-06-19 17:03 编辑

#include <stdio.h>
#include <stdlib.h>
/* my program simulate linux command wc */
/* author: xiaowh0 */
/* date: 2012-6-19 */

void my_wc(int [],char);
extern int flag=1;
int main(int argc,char *argv[])
{
        FILE *fp;
        int c,i,j;
        int res_current[3]; /* res_current[0] represent current file's line,res_current[1] represent current file's word,res_current[2] represent
                                            current file's character */
        int res_total[3]; /* res_total[0] represent all files's line,res_total[1] represent all files's word,res_total[2] represent all files's character */

        for(i=0;i<3;i++)
        {
                res_current=0;
                res_total=res_current;
        }

        if(argc==1) /* no input file,input come from stdin */
        {
                while((c=getchar())!=EOF)
                        my_wc(res_total,c);
                printf("\t%d\t%d\t%d\n",res_total[0],res_total[1],res_total[2]);
        }
        else
        {
                j=0;
                while(++j<argc)
                {
                        if((fp=fopen(argv[j],"r"))==NULL)
                        {
                                printf("Can not open the file %s\n",argv[j]);
                                exit(0);
                        }
                        c=fgetc(fp);
                        while(c!=EOF)
                        {
                                my_wc(res_current,c);
                                c=fgetc(fp);
                        }
                        fclose(fp);
                        printf(" %d  %d %d %s\n",res_current[0],res_current[1],res_current[2],argv[j]);
                        for(i=0;i<3;i++)
                        {
                                res_total+=res_current;
                                res_current=0;
                        }
                }
                printf(" %d  %d %d total\n",res_total[0],res_total[1],res_total[2]);
        }
}

void my_wc(int res_current[],char c)
{
        int flag;
        res_current[2]++;
        if(c=='\n')
        {
                res_current[0]++;
        }
        if(c=='\t' || c== ' ' || c=='\n')
        {
                if(flag==0)
                {
                        res_current[1]++;
                         flag=1;
                }
        }
        else
                flag=0;       
       
}

论坛徽章:
0
2 [报告]
发表于 2012-06-19 13:27 |只看该作者
没任何意义。

如果你能做到如下:
1.判断非英语语言单词(cjk词)
2.判断非ascii字符(cjk字)
3.判断非英语断句(cjk句)

这些完成了才能算得上有价值。

论坛徽章:
0
3 [报告]
发表于 2012-06-19 13:33 |只看该作者
刚开始学C语言,新手回复 2# nilgod


   

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
4 [报告]
发表于 2012-06-19 18:54 |只看该作者
回复 3# xiaowh00


    you do it good.

论坛徽章:
2
CU十二周年纪念徽章
日期:2013-10-24 15:41:34处女座
日期:2013-12-27 22:22:41
5 [报告]
发表于 2012-06-19 20:57 |只看该作者
大一上学第一天慕名买了K&R C。看到的例子是实现wc,再想想以前在老潭书上看到的例子…………
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP