前言:
应该每一个学编程的人都很希望知道自己打了多少行代码,而网上又没有好用的统计软件(至少我没找到)。于是我做了一个代码行统计程序。
原理:
由于是上课时打的,只能统计.cpp文件,其它文件暂时没搞。只适用于windows系统,原理使用cmd扫出指定目录下所有文件,再判断后缀名。用freopen和fstream打开文件,freopen用于生成一个.bin文件来存放cmd扫目录时的输出,fstream+while+getline用于统计代码行。由于未加注释特判,统计的数量是所有行之和。最后在程序当前目录下生成统计文件,.txt后缀。里面是你所有代码,最后一行是统计数。
代码:
#include<bits/stdc++.h>
#include<windows.h>
#include<fstream>
using namespace std;
int main()
{
long long cnt=0;
string b;
puts("请输入要统计的磁盘(带:):");
cin>>b;
system(b.c_str());
getline(cin,b);
puts("请输入要统计的绝对目录(单\\要用\\\\,双\\\\要用\\\\\\\\):");
string n;
getline(cin,n);
n="cd "+n;
system(n.c_str());
freopen("date.bin","w",stdout);
system("dir /O");
freopen("代码统计.txt","w",stdout);
ifstream date;
date.open("date.bin");
string a;
for(int i=1;i<=7;i++)
getline(date,a);
while(getline(date,a))
{
if(a[a.size()-1]!='p'&&a[a.size()-2]!='p'&&a[a.size()-3]!='c')
continue;
int len=a.size();
string x=a.substr(36,a.size());
cout<<x<<":"<<endl;
ifstream film;
film.open(x.c_str());
string t;
while(getline(film,t))
{
cnt++;
puts(t.c_str());
}
film.close();
}
puts("总计(Total):");
cout<<cnt;
freopen("CON","w",stdout);
puts("总计(Total):");
cout<<cnt;
}
//