读取cpp文件处理

来源:互联网 发布:java中tomcat安装教程 编辑:程序博客网 时间:2024/06/08 09:52

代码如下:

#include<Windows.h>#include<iostream> #include<iomanip>#include<fstream> #include<Shlwapi.h>#include<string>#include<tchar.h>using namespace std;int Filetotalnum=0;//总行数int avgtotalnum = 0;//各函数平均行数//链表中的数据结构(函数名字,代码行数)typedef struct Link_data{int     numline;char   Name[20];//构造函数必须写上Link_data() {numline = 0;memset(Name,0,sizeof(Name));}}Node_data;//链表节点结构typedef struct Link_node{Node_data    data;struct Link_node  *pNext;}Node;//创建链表Node* CreateList(void){Node *pHead = NULL;//申请的头指针pHead = (Node *)malloc(sizeof(Node));//判断是否申请成功if (NULL == pHead){return NULL;}//针对具体结构进行初始化pHead->data.numline = 0;memset(pHead->data.Name, 0, 20*sizeof(char));pHead->pNext = NULL;return pHead;}//为每个函数添加一个节点Node* Insert2ListTail(Node *pHead, Node_data  *pAutoInfo){Node* pNode = NULL;Node* pNewNode = NULL;if ((NULL == pHead) || (NULL == pAutoInfo)){return NULL;}pNode = pHead;while (pNode->pNext != NULL){pNode = pNode->pNext;}pNewNode = (Node *)malloc(sizeof(Node));if (NULL == pNewNode){return NULL;}pNode->pNext = pNewNode;pNewNode->pNext = NULL;memcpy(&(pNewNode->data), pAutoInfo, sizeof(Node_data));return pHead;}//删除函数链表int RemoveList(Node *pHead){Node *pNode = NULL;Node *pb = NULL;if (NULL == pHead){return 0;}pNode = pHead;pb = pNode->pNext;if (NULL == pb){free(pNode);}else{while (NULL != pb){free(pNode);pNode = pb;pb = pb->pNext;}free(pNode);}pNode = NULL;return 1;}//输出每个函数的代码行数void  outList(Node *pHead){int sumline = 0;int avgline = 0;int i = 0;Node *pNode = NULL;if (NULL == pHead){return;}pNode = pHead->pNext;while ((NULL != pNode)){//printf("\r\n a is %d   b is  %d", pNode->data.a, pNode->data.b);sumline = sumline + pNode->data.numline;i++;cout << "函数" << i << "行数: "<< pNode->data.numline << endl;pNode = pNode->pNext;}avgline = sumline / i;avgtotalnum = avgline;//cout << "函数总个数: " << i<< "占比:" << nHsP << "%" << endl;cout << "函数平均行数: " << avgline<<endl;}//统计函数个数及行数,占比void  FunList(Node *pHead){int sumline = 0;int avgline = 0;int i = 0;Node *pNode = NULL;if (NULL == pHead){return;}pNode = pHead->pNext;while ((NULL != pNode)){//printf("\r\n a is %d   b is  %d", pNode->data.a, pNode->data.b);sumline = sumline + pNode->data.numline;i++;//cout << "函数" << i << "行数: " << pNode->data.numline << endl;pNode = pNode->pNext;}avgline = sumline / i;avgtotalnum = avgline;int  nHsP = (float)sumline / Filetotalnum * 100;cout << "函数总个数: " << i<< "占比:" << nHsP << "%" << endl;}//获取文件总行数int GetToalnum(string filename){int pos = filename.find_last_of('/');string s(filename.substr(pos + 1));cout << "文件名:" <<s<< endl;int num=0;string lineBuff = "";//存放读入每行内容ifstream fin;fin.open(filename.c_str());if (!fin){cout << "文件打开失败" << endl;return 1;}//cout << "文件名:" << PathFindFileName(filename.c_str()) << endl;while (!fin.eof()){getline(fin, lineBuff);    //读取一行文件num++;//总行数}fin.close();Filetotalnum = num;cout << "总行数:" << Filetotalnum << endl;return 0;}//代码等级BOOL CodeGrade(int zsp,int khp){if (avgtotalnum < 50) {if ((zsp >= 15) && (zsp <= 25)&& (khp>=15) && (khp<=25)){cout << "等级A" << endl; return TRUE;}if((((zsp>=10) && (zsp<=14)) || ((zsp>=26) && (zsp<=30)))&& (((khp>=10) && (khp<=14)) || ((khp>=26) && (khp<=30)))){cout << "等级B" << endl; return TRUE;}if(( ((zsp>=5) && (zsp<=9)) || ((zsp>=31) && (zsp<=35)))&& ( ((khp>=5) && (khp<=9)) || ((khp>=31) && (khp<=35)) )){ cout << "等级C" << endl; return TRUE;}if (((zsp<=5) || (zsp>=35))&& ((khp<=5) || (khp>=35))){ cout << "等级D" << endl; return TRUE;}else { cout << "等级不在范围" << endl; return FALSE;}}if ((avgtotalnum > 50) && (avgtotalnum < 100)) {if ((zsp >= 15) && (zsp <= 25)&& (khp >= 15) && (khp <= 25)){cout << "等级A" << endl; return TRUE;}if ((((zsp >= 10) && (zsp <= 14)) || ((zsp >= 26) && (zsp <= 30)))&& (((khp >= 10) && (khp <= 14)) || ((khp >= 26) && (khp <= 30)))){cout << "等级B" << endl; return TRUE;}if ((((zsp >= 5) && (zsp <= 9)) || ((zsp >= 31) && (zsp <= 35)))&& (((khp >= 5) && (khp <= 9)) || ((khp >= 31) && (khp <= 35)))){cout << "等级C" << endl; return TRUE;}if (((zsp <= 5) || (zsp >= 35))&& ((khp <= 5) || (khp >= 35))){cout << "等级D" << endl; return TRUE;}else {cout << "等级不在范围" << endl; return FALSE;}}if ((avgtotalnum > 100) && (avgtotalnum < 150)) {if ((zsp >= 15) && (zsp <= 25)&& (khp >= 15) && (khp <= 25)){cout << "等级A" << endl; return TRUE;}if ((((zsp >= 10) && (zsp <= 14)) || ((zsp >= 26) && (zsp <= 30)))&& (((khp >= 10) && (khp <= 14)) || ((khp >= 26) && (khp <= 30)))){cout << "等级B" << endl; return TRUE;}if ((((zsp >= 5) && (zsp <= 9)) || ((zsp >= 31) && (zsp <= 35)))&& (((khp >= 5) && (khp <= 9)) || ((khp >= 31) && (khp <= 35)))){cout << "等级C" << endl; return TRUE;}if (((zsp <= 5) || (zsp >= 35))&& ((khp <= 5) || (khp >= 35))){cout << "等级D" << endl; return TRUE;}else {cout << "等级不在范围" << endl; return FALSE;}}if (avgtotalnum >= 150) {if ((zsp >= 15) && (zsp <= 25)&& (khp >= 15) && (khp <= 25)){cout << "等级A" << endl; return TRUE;}if ((((zsp >= 10) && (zsp <= 14)) || ((zsp >= 26) && (zsp <= 30)))&& (((khp >= 10) && (khp <= 14)) || ((khp >= 26) && (khp <= 30)))){cout << "等级B" << endl; return TRUE;}if ((((zsp >= 5) && (zsp <= 9)) || ((zsp >= 31) && (zsp <= 35)))&& (((khp >= 5) && (khp <= 9)) || ((khp >= 31) && (khp <= 35)))){cout << "等级C" << endl; return TRUE;}if (((zsp <= 5) || (zsp >= 35))&& ((khp <= 5) || (khp >= 35))){cout << "等级D" << endl; return TRUE;}else {cout << "等级不在范围" << endl; return FALSE;}}else { return FALSE; }}int main(){///////////////////////////////////////////////////////////////标记: 双引号""  斜杠星 /*   函数{}    代码     注释//或者/*int bSyh = 0, bXgx = 0, bHs = -1, bCode = 0, bZs = 0;                                   /////////////////////////////////////////////////////////////////个数:          空行     注释    代码      公共     函数     int      nKh = 0, nZs = 0, nDm = 0, nGg = 0, nHs = 0;int totalnum = 0;//文件行数的游标int Funcline[20] = { 0 };//存放每个函数的行数。Node_data my_FunInfo;Node * g_LinkHead = NULL;g_LinkHead = CreateList();    int hsHead = -1;int numofhs = 0;string lineBuff = "";//存放读入每行内容string filename;cout << "输入文件路径(用斜杠/): " << endl;cin >> filename;cout << endl << "===========================" << endl;if (1 == GetToalnum(filename)){return 1;}ifstream fin;fin.open(filename.c_str());if (!fin){cout << "文件打开失败" << endl;return 1;}while (!fin.eof()){getline(fin, lineBuff);    //读取一行文件totalnum++;//总行数    int i = 0;bCode = 0;            //该行没有代码bZs = 0;              //该行没有注释if (bXgx)             //bXgx 斜杠星注释标记bZs = 1;          //该行有注释//过滤无效符号 while (lineBuff[i] == ' ' || lineBuff[i] == '\t' || lineBuff[i] == '\r' || lineBuff[i] == '\n'){++i;}//“以下为空行统计区域:开始”if (!bXgx && lineBuff[i] == '\0')  //空行{++nKh;continue;}//“空行统计:结束”//非空,则检查内容while (1){//第一次遇到双引号              引号为非转义字符(\")if (!bSyh && lineBuff[i] == '\"' && ((i > 0 && lineBuff[i - 1] != '\\') || (i == 0))){++i;bSyh = 1;continue;}//“正在进行双引号屏蔽....”if (bSyh){//“ \”结束”if (lineBuff[i] == '\"' && ((i > 0 && lineBuff[i - 1] != '\\') || (i == 0))){bSyh = 0;}else if (lineBuff[i] == '\0')  //行末尾{if (bZs)++nZs;if (bCode)++nDm;if (bZs && bCode)++nGg;break;}++i;continue;}//遇到单引号(避免'{','}'),且非转义字符\',连续跳过3个(第二个'后位置)if (lineBuff[i] == '\'' && ((i > 0 && lineBuff[i - 1] != '\\') || (i == 0))){i += 3;continue;}//“//注释行”if (!bXgx && lineBuff[i] == '/' && lineBuff[i + 1] == '/'){if (bCode)     //“前有代码,混合注释行”{++nZs;     //注释++nDm;     //代码++nGg;     //公共}else          //纯注释行{++nZs;}break;  //跳出当前行(即,内while循环),“//”后代码不做判断}//“/*注释开始”if (!bXgx && lineBuff[i] == '/' && lineBuff[i + 1] == '*'){i += 2;        //跳过/*符号bXgx = 1;      //标记“/*”开始bZs = 1;       //“发现注释”continue;}//“正在进行多行注释....”if (bXgx){//“*/注释结束”if (lineBuff[i] == '*' && lineBuff[i + 1] == '/'){++i;     //“跳过*/”注意后有一个 ++i;bXgx = 0;}else if (lineBuff[i] == '\0')  //行末尾{if (bCode)       //注释前有代码,即“混合行”{++nDm;++nZs;++nGg;}else{++nZs;       //“纯注释”}break;}++i;continue;}if (lineBuff[i] == '\0'){if (bZs)++nZs;if (bCode)++nDm;if (bZs && bCode)++nGg;break;}//“以下全是有效代码区域”//“函数个数统计区域:开始”if (lineBuff[i] == '{')      //记录函数左括号{++bHs;++hsHead;if (hsHead == 0){numofhs = totalnum;}}else if (lineBuff[i] == '}') //遇到函数右括号{if (bHs == 0)        //“发现一个函数”{++nHs;numofhs = totalnum - numofhs;//cout <<"函数"<<nHs<<"行数:" << numofhs+1<<endl;my_FunInfo.numline = numofhs + 1;Insert2ListTail(g_LinkHead, &my_FunInfo);hsHead = -1;//再重新准备标记第二个函数}--bHs;}                                    //“函数统计:结束”++i;bCode = 1;    //能执行到这里,说明该行存在代码}}fin.close();Filetotalnum = totalnum;cout << setiosflags(ios::fixed);  //只有在这项设置后,setprecision才是设置小数的位数。int  nZsP = (float)nZs / totalnum *100;int  nDmP = (float)nDm /  totalnum* 100;int  nKhP = (float)nKh /  totalnum * 100;int  nGgP = (float)nGg /  totalnum * 100;int  nHsP = (float)nHs /  totalnum * 100;cout << "注释行数: " << nZs << "占比:"  << nZsP << "%" << endl;cout << "代码行数: " << nDm <<"占比:" <<  nDmP <<"%"<< endl;cout << "空行行数: " << nKh << "占比:" <<  nKhP << "%" << endl;cout << "公共行数: " << nGg << "占比:" <<  nGgP << "%" << endl;FunList(g_LinkHead);cout << endl;outList(g_LinkHead);if (RemoveList(g_LinkHead)){g_LinkHead = NULL;}CodeGrade(nZsP, nKhP);cout << endl << "===========================" << endl;system("pause");return 0;}

主要参考文章:

http://www.cnblogs.com/nchar/p/3915889.html?utm_source=tuicool&utm_medium=referral

原创粉丝点击