日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

50行以上c语言程序代码,C语言非常简单的字符统计程序50行

發(fā)布時間:2024/9/27 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 50行以上c语言程序代码,C语言非常简单的字符统计程序50行 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

該樓層疑似違規(guī)已被系統(tǒng)折疊?隱藏此樓查看此樓

該程序用于實現(xiàn)linux系統(tǒng)中wc命令的最簡單模式

wc 命令用于統(tǒng)計文件中字符信息。

[xx@localhost 1.5]$ wc 01.c 02.c 03.c

15 23 131 01.c

13 18 127 02.c

14 20 128 03.c

42 61 386 總用量

使用c語言寫出這種小程序。

/*

* Name: count.c

* Title: the number of line, word, characters in file

* Descripts: count the number of line, word, characters in file

* Author: lnesuper

* Copyrighte: GPL

* Date: 2015.5.25

* Use: count [file1] [file2]...

*/

#include

#include

#define IN 1 /* inside a word */

#define OUT 0 /* outside a word */

int main(int argc, char * argv[])

{

if (argc == 1) {

printf("\aUsing: count [file1] [file2]...\n");

exit(EXIT_FAILURE);

}

int c, nl, nw, nc, state; /* line, word, character */

int nl_total, nw_total, nc_total;

nl_total = nw_total = nc_total = 0;

int n;

for (n = 1; n < argc; n++) {

FILE * file = fopen(argv[n], "r");

if (file == NULL) {

printf("Can't open file %s\n", argv[n]);

exit(EXIT_FAILURE);

}

nl = nw = nc = 0;

state = OUT;

while ((c = fgetc(file)) != EOF) {

++nc;

if (c == '\n')

++nl;

if (c == ' ' || c == '\t' || c == '\n')

state = OUT;

else if (state == OUT) {

++nw;

state = IN;

}

}

nl_total += nl;

nw_total += nw;

nc_total += nc;

printf("%d %d %d %s\n", nl, nw, nc, argv[n]);

fclose(file);

}

// printf("total information:\n");

printf("%d %d %d total\n", nl_total, nw_total, nc_total);

return 0;

}

linux下編譯運行結(jié)果

gcc -Wall -o count count.c

[lhf@localhost 1.5]$ ./count 01.c 02.c 03.c

15 23 131 01.c

13 18 127 02.c

14 20 128 03.c

42 61 386 total

[lhf@localhost 1.5]$ wc 01.c 02.c 03.c

15 23 131 01.c

13 18 127 02.c

14 20 128 03.c

42 61 386 總用量

可以看出與wc命令結(jié)果一致。

總結(jié)

以上是生活随笔為你收集整理的50行以上c语言程序代码,C语言非常简单的字符统计程序50行的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。