噁心的行標,動手擦掉它

來源:互聯網
上載者:User

不知道你會不會遇到這種情況,在瀏覽器裡看人家的代碼的時候,一般網頁內建的代碼框是有行標的,當然這是方便你查看代碼,但是,當你想要把其中一段代碼貼出來,放到IDE裡面Run一下的時候,杯具來了,每行開頭都有幾個可愛的數字……

如果比較短的話,可以手動刪掉,但是當你貼的是一段好幾十行,甚至幾百行的代碼的時候,一行一行刪除那幾個數字,嗯,是很銷魂的事情……

該程式其實就是在檔案重新導向的同時,去掉每行代碼前面的行標,然後簡單的加上了縮排(是C/C++語言風格),歡迎板磚,歡迎補充

如何使用:

將代碼編譯運行產生的exe檔案,以及需要整理的代碼(儲存為 code.txt )放在同一個目錄下,然後運行exe檔案,產生的 code_new.txt 即為整理好的代碼

 

源碼:

#include<stdio.h>#include<stdlib.h>#include<string.h>char find_last(char s[]){//找到每行有效代碼的最後一個字元,也就是考慮 "//" 以及 "/**/" 的注釋情況//當然,我只考慮了每行行尾的/**/,也就是說如果代碼中間嵌有的 /**/ 的情況我木有考慮,求補充int i,k;for(i=0;s[i];i++){if(s[i]=='/' && s[i+1]=='*' || s[i]=='/' && s[i+1]=='/')break;}for(k=i-1;k>=0;k--){if(s[k]==' ')continue;else return s[k];}return -1;}void main(){char str[10000];int flag[100000];int i,j=0,space=0;freopen("code.txt","r",stdin);freopen("code_new.txt","w",stdout);memset(flag,0,sizeof(flag));while(gets(str)){for(i=0;str[i];i++){if(str[i]>='a'&&str[i]<='z' || str[i]>='A'&&str[i]<='Z' || str[i]=='#' || str[i]=='/' || str[i]=='{' || str[i]=='}')break;else str[i]=' ';}for(i=0;str[i];i++){if(str[i]==' ')continue;else {if(str[i]=='}')flag[j]=flag[j-1]-1;break;}}for(i=0;i<4*flag[j];i++)printf(" ");int fuck=0;for(i=0;str[i];i++){if(str[i]!=' ' && fuck==0)fuck=1;if(fuck != 0)printf("%c",str[i]);}puts("");char tmp=find_last(str);if(tmp=='>' || tmp==';' || tmp=='}' || tmp==')' || tmp==':' ||tmp==-1){flag[j+1]=flag[j];}else if(tmp=='{'){flag[j+1]=flag[j]+1;}j++;}}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.