一個統計程式碼數的程式C語言

來源:互聯網
上載者:User

  我這麼說其實是有歧義的,因為這個東西根本沒有那麼通用.不過有很簡單的辦法讓這個程式更加通用,我介紹下運行此程式前的前期準備.

  首先, 將所有代碼檔案放置到同一個空檔案夾中.選定所有代碼,重新命名為1.於是有了下面的效果:

 

 

 

  所有檔案都是排列好的了,這樣方便我操作.下面貼出來,還真是令我驚訝的一個結果呢.

 

 

  從5月24號開始自己已經寫了這麼多代碼了啊.

 下面附上源碼,很簡單的實現方式.不過居然也費了我兩個小時,真搞不懂這兩個小時我都幹嘛了...可能就是我沒瞧得起這麼簡單的一個任務,計劃不周詳,才導致這個結果的吧.還可能什麼啊..就是這個樣子!

#include <stdio.h><br />#include <string.h><br />#include <stdlib.h><br />#define INITIAL_VALUE 1<br />#define NUMBER_LENTH 4<br />#define BIG_SPACE 10<br />#define FULL_NAME_LENTH 15<br />#define PF '/n'<br />#define FINISH printf ("全部檔案統計完畢,報告如下:/n")<br />#define ERROR(i) printf ("第%d個檔案關閉失敗/n", i) </p><p>struct item<br />{<br /> int the_number_of_char ;<br /> int the_number_of_enter ;<br />} ; </p><p>int main (void) ;<br />void get_full_name_base_on_number (const int integer, char * string) ; /* 根據數字值獲得檔案全名 */<br />struct item count_and_printf (FILE * const fp, const int i) ; /* 計算並返回當前檔案字元數以及行數 */ </p><p>int main (void)<br />{<br /> FILE * fp ;<br /> char file_name[FULL_NAME_LENTH] ;<br /> int i, max_char_file, max_enter_file ;<br /> struct item temp, sum, max ; </p><p> sum.the_number_of_char = sum.the_number_of_enter = 0 ;<br /> max.the_number_of_char = max.the_number_of_enter = 0 ;<br /> for (i = INITIAL_VALUE; ; i++)<br /> {<br /> get_full_name_base_on_number (i, file_name) ;<br /> if (NULL == (fp = fopen (file_name, "r")))<br /> {<br /> FINISH ;<br />i-- ;<br /> break ;<br /> }<br /> else<br /> {<br /> temp = count_and_printf (fp, i) ;<br /> sum.the_number_of_char += temp.the_number_of_char ;<br /> sum.the_number_of_enter += temp.the_number_of_enter ;<br /> if (temp.the_number_of_char > max.the_number_of_char)<br /> {<br /> max.the_number_of_char = temp.the_number_of_char ;<br /> max_char_file = i ;<br /> }<br /> if (temp.the_number_of_enter > max.the_number_of_enter)<br /> {<br /> max.the_number_of_enter = temp.the_number_of_enter ;<br /> max_enter_file = i ;<br /> }<br /> }<br /> if (fclose (fp) != 0)<br /> {<br /> ERROR (i) ;<br /> exit (EXIT_FAILURE) ;<br /> }<br /> }<br /> printf (" 檔案總數:%-5d全部字元數:%-10d全部行數:%-10d/n"<br /> , i, sum.the_number_of_char, sum.the_number_of_enter) ;<br />if (i)<br />printf (" 最大字元數:%-10d出現在檔案:1 (%d).c中/n 最大行數:%-10d出現在檔案:1 (%d).c中/n",<br />max.the_number_of_char, max_char_file, max.the_number_of_enter, max_enter_file) ;<br /> getchar () ; </p><p> return 0 ;<br />} </p><p>void get_full_name_base_on_number (const int integer, char * string)<br />{<br /> char number[NUMBER_LENTH] ; </p><p> strcpy (string, "1 (") ;<br /> sprintf (number, "%d", integer) ;<br /> strcat (string, number) ;<br /> strcat (string, ").c") ;<br />} </p><p>struct item count_and_printf (FILE * const fp, const int i)<br />{<br /> struct item sum ;<br /> char ch ; </p><p> sum.the_number_of_char = 0, sum.the_number_of_enter = 0 ;<br /> while ((ch = getc (fp)) != EOF)<br /> {<br /> if (PF == ch)<br /> sum.the_number_of_enter++ ;<br />else<br />sum.the_number_of_char++ ;<br /> }<br /> sum.the_number_of_enter++ ;<br /> printf ("當前第:%-2d個檔案: 字元數:%-5d 行數:%-10d/n", i, sum.the_number_of_char, sum.the_number_of_enter) ; </p><p> return sum ;<br />}<br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.