c++代码行统计程序

前言:

应该每一个学编程的人都很希望知道自己打了多少行代码,而网上又没有好用的统计软件(至少我没找到)。于是我做了一个代码行统计程序。

原理:

由于是上课时打的,只能统计.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;
}
//               

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值