高分求解 - 网络游戏公司面试题(Java) 联系方式: Emial:[email protected] 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 物理行数 Easy ,遍历 换行符逻辑行数就是 检查 " ; " 、 " { " 、 " } " 注释行数 是检查 " // " 、 " /* " 、" /** " 、 " */ "大概这样吧 物理行数 Easy ,遍历 换行符逻辑行数就是 检查 " ; " 、 " { " 、 " } " 注释行数 是检查 " // " 、 " /* " 、" /** " 、 " */ "大概这样吧 这里有段C++代码,哪位大侠能用Java改写,并用多线程实现???/********************************************************* 统计c++文件*********************************************************/int CCountingDlg::GetCppFileLines(LPCTSTR strFileName, int* pnLength, int *pnCodeLines, int* pnCommentLines, int* pnBlankLines){ //there are two methods to count lines in files, by CStdioFile or CFile *pnLength = 0; *pnCodeLines = 0; *pnCommentLines = 0; *pnBlankLines = 0; CStdioFile file; if(file.Open(strFileName, CFile::modeRead)==FALSE) return 0; int nLines = 0; int nCodeLines = 0; int nCommentLines = 0; int nBlankLines = 0; BOOL bCommentSet = FALSE; //注释行统计标识 有"/*"时TRUE, "*/"时FALSE BOOL bQuatoSet = FALSE; //字符串统计标识 首次一行有奇数个"时TRUE, 下一行有奇数个"时FALSE int nLength = file.GetLength(); *pnLength = nLength; CString bufRead; int nLineCommentBegin = 0; while(file.ReadString(bufRead)!=FALSE) { BOOL bStatedComment = FALSE;//本行作为注释行是否已统计过 BOOL bStatedCode = FALSE; //本行作为代码行是否已统计过 nLines++; bufRead.TrimLeft(); //先将文件头的空格或制表符去掉 if(bufRead.GetLength()==0) //为空白行 { nBlankLines++; continue; } if(bCommentSet && bufRead.Find("*/")==-1) { nCommentLines++; continue; } if(bufRead.Find("//")==-1 && bufRead.Find("/*")==-1 && bufRead.Find("*/")==-1) {//如果本行根本就无注释符,则要不是注释符,要不是代码行 if(bCommentSet) { nCommentLines++; continue; } else { if(bufRead.Find('"')==-1) { nCodeLines++; continue; } } } if(bufRead.Find("//")==0 && !bCommentSet && !bQuatoSet) { nCommentLines++; continue; } BOOL bDoubleSplashFound = FALSE; BOOL bSplashStarFound = FALSE; for(int i=0; i<bufRead.GetLength()-1; i++) { char cTemp = bufRead[i]; if(bufRead[i]=='/' && bufRead[i+1]=='/' && !bCommentSet && !bQuatoSet) { if(!bStatedComment && (m_nStatMethod==1 || m_nStatMethod ==2)) { bStatedComment = TRUE; nCommentLines++; } bDoubleSplashFound = TRUE; i++;//应该+1,但也没有什么用处 break; } else if(bufRead[i]=='/' && bufRead[i+1]=='*' && !bCommentSet && !bQuatoSet) { if(!bStatedComment && (m_nStatMethod==1 || m_nStatMethod ==2)) { bStatedComment = TRUE; nCommentLines++; } bCommentSet = TRUE; bSplashStarFound = TRUE; i++; } //计算代码行必须在bCommentSet关闭之前 else if(bufRead[i]!=' ' && bufRead[i]!='\t' && !bCommentSet) { if(!bStatedCode) { bStatedCode = TRUE; nCodeLines++; } if(bufRead[i]=='\\') {//\之后的字符要跳过 i++; continue; } if(bufRead[i]=='\'') { if(bufRead[i+1]=='\\') i+=2; else i+=1; continue; } if(bufRead[i]=='"') {//"必须引起重视,感谢ltzhou bQuatoSet = !bQuatoSet; } } else if(bufRead[i]=='*' && bufRead[i+1]=='/' && bCommentSet && !bQuatoSet) { if(!bStatedComment && (m_nStatMethod==1 || m_nStatMethod ==2)) { bStatedComment = TRUE; nCommentLines++; } bCommentSet = FALSE; bSplashStarFound = TRUE; i++; } } if(bDoubleSplashFound) { if(m_nStatMethod==2 && bStatedCode) //如果统计方法为第三种,且同时有代码行与注释行,则只计注释行 { nCodeLines--; } if(m_nStatMethod==0 && !bStatedCode)//如果统计方法为第一种,且未作为代码行统计过,那么必为注释行 { nCommentLines++; } continue; } if(bufRead[bufRead.GetLength()-1]=='"'&&!bCommentSet) {//若某行最后一个是",则必定用来关闭bQuatoSet,记代码行一行,否则报错 bQuatoSet = !bQuatoSet; if(!bQuatoSet) { if(!bStatedCode) { bStatedCode = TRUE; nCodeLines++; } } else { CStdioFile fileLog; if(fileLog.Open(m_strLogFile, CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate)==TRUE) { CString strMsg; if(fileLog.GetLength()==0) { strMsg.Format("文件\t行\t问题\n", strFileName, nLines); fileLog.WriteString(strMsg); } strMsg.Format("%s\t%d\t字符串换行未用\\\n", strFileName, nLines); fileLog.WriteString(strMsg); fileLog.Close(); } } continue; } if(bufRead[bufRead.GetLength()-1]!=' ' && bufRead[bufRead.GetLength()-1]!='\t' && !bCommentSet && bufRead[bufRead.GetLength()-2]!='*' && bufRead[bufRead.GetLength()-1]!='/') {//如果最后一个字符非空格或制表符,且前面无/*,最后两个字符不是*/,则为代码行 if(!bStatedCode) { bStatedCode = TRUE; nCodeLines++; } } if(bSplashStarFound) { if(m_nStatMethod==2 && bStatedCode) //如果统计方法为第三种,且同时有代码行与注释行,则只计注释行 { nCodeLines--; } if(m_nStatMethod==0 && !bStatedCode && !bStatedComment) //若该行无代码如 /*abc*/ //222 //但是统计方法是第一种,则需要追加注释行计数一次 { bStatedComment = TRUE; nCommentLines++; } } if(!bStatedComment && bCommentSet)//可能是前面有/*,在第一种统计方法中,未作为代码行计算过,那么本行肯定是注释行 { if(m_nStatMethod==0 && !bStatedCode) { bStatedComment = TRUE; nCommentLines++; } }// if(bQuatoSet && bufRead[bufRead.GetLength()-1]=='"')// {// bQuatoSet = FALSE;// } if(bQuatoSet && bufRead[bufRead.GetLength()-1]!='\\') { CStdioFile fileLog; if(fileLog.Open(m_strLogFile, CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate)==TRUE) { CString strMsg; if(fileLog.GetLength()==0) { strMsg.Format("文件\t行\t问题\n", strFileName, nLines); fileLog.WriteString(strMsg); } strMsg.Format("%s\t%d\t字符串换行未用\\\n", strFileName, nLines); fileLog.WriteString(strMsg); fileLog.Close(); } } } *pnCodeLines = nCodeLines; *pnCommentLines = nCommentLines; *pnBlankLines = nBlankLines; file.Close(); return nLines;} 给我发一个。[email protected] 能给我发一个多线程的程序吗?[email protected] 帅哥,给我传一个吧,多谢[email protected] class Result{}//存储单一文件的统计结果interface Printer{//格式输出类 public void print(Result res);//输出数据 public void close();//释放资源}class TextPrinter{//输出纯文本文件 public TextPrinter(String file){}//打开文件file作为输出 synchronized public void print(Result res){}//输出数据,一定要同步处理 public void close(){}//释放资源}class HtmlPrinter同TextPrinter(输出Html格式文件)class Counter implements Runnable{ File f; Printer p; public Counter(File f,Printer p){ this.f=f; this.p=p; } public void run(){ //统计数据并生成结果 Result res=new Result(); //res.set...; p.print(res); }}public class Count{ public static void main(String args[]){ File f=new File(args[0]);//根据参数确定要处理的文件 File fl[]=null; if(f.isDirectory()){ fl=f.listFiles(); } else{ fl=new File[1]; fl[0]=f; } Printer p=getPrinter(args[1]);//根据参数确定输出格式 Thread[] t=new Thread[fl.length]; for(int i=0;i<fl.length;i++){ t[i]=new Thread(fl[i],p);//创建线程并启动 t[i].start(); } for(int i=0;i<fl.length;i++){ try{ t[i].join();//等待线程结束 }catch(Exception e){} } p.close();//释放资源 } private static Printer getPrinter(String args){}//根据参数确定输出格式} Sorry,t[i]=new Thread(fl[i],p);//创建线程并启动应为:t[i]=new Thread(new Counter(fl[i],p)); to flyxxxxx(空指針):能将完整的编译通过的代码mailto:[email protected]吗?3x! 不好意思,我在公司用的临时电脑上没jdk,上面是记事本写的。其实上面的代码也写的差不多了,如果不急的话,晚上回去写给你。 我也想要一份,能麻烦也给我mail一份吗?是[email protected] 我也想要一份,能麻烦也给我mail一份吗?是 [email protected] 回复人: flyxxxxx(空指針) ( ) 信誉:99 2004-04-26 15:33:00 得分:0 不好意思,我在公司用的临时电脑上没jdk,上面是记事本写的。其实上面的代码也写的差不多了,如果不急的话,晚上回去写给你。 厉害,我没编译器的话,写程序会出无数个错误,blush高手就是高手。另:java 1.4 以上就有正则表达式了,我觉得做这个可能会容易一些,现在在家里,到公司写写试试看,^_^ Java变量的问题 我想使用下面的一个程序下载一个网页 时间问题 求助! java中如何能得到windows中当前活动窗口标题 Oracle中的Date问题,很奇怪,高手帮帮忙了,急啊~~~ 流和文件 java 职场菜鸟提问 垃圾收集器一题!??,, SOCKET编程遇到的麻烦,我一直解决不了,哪个大哥帮我。 JAVA tcp server 管理多个客户端连接 语句 package mypack;请问这个自定义的包 mypack 放在什么地方? 有谁能够帮忙提供关于搜索引擎方面资料或者是参考书呀? 急呀?谢谢
统计c++文件
*********************************************************/
int CCountingDlg::GetCppFileLines(LPCTSTR strFileName, int* pnLength, int *pnCodeLines, int* pnCommentLines, int* pnBlankLines)
{
//there are two methods to count lines in files, by CStdioFile or CFile
*pnLength = 0;
*pnCodeLines = 0;
*pnCommentLines = 0;
*pnBlankLines = 0; CStdioFile file;
if(file.Open(strFileName, CFile::modeRead)==FALSE)
return 0; int nLines = 0;
int nCodeLines = 0;
int nCommentLines = 0;
int nBlankLines = 0; BOOL bCommentSet = FALSE; //注释行统计标识 有"/*"时TRUE, "*/"时FALSE
BOOL bQuatoSet = FALSE; //字符串统计标识 首次一行有奇数个"时TRUE, 下一行有奇数个"时FALSE int nLength = file.GetLength();
*pnLength = nLength; CString bufRead; int nLineCommentBegin = 0;
while(file.ReadString(bufRead)!=FALSE)
{
BOOL bStatedComment = FALSE;//本行作为注释行是否已统计过
BOOL bStatedCode = FALSE; //本行作为代码行是否已统计过 nLines++; bufRead.TrimLeft(); //先将文件头的空格或制表符去掉 if(bufRead.GetLength()==0) //为空白行
{
nBlankLines++;
continue;
} if(bCommentSet && bufRead.Find("*/")==-1)
{
nCommentLines++;
continue;
} if(bufRead.Find("//")==-1 && bufRead.Find("/*")==-1 && bufRead.Find("*/")==-1)
{//如果本行根本就无注释符,则要不是注释符,要不是代码行
if(bCommentSet)
{
nCommentLines++; continue;
}
else
{
if(bufRead.Find('"')==-1)
{
nCodeLines++; continue;
}
}
} if(bufRead.Find("//")==0 && !bCommentSet && !bQuatoSet)
{
nCommentLines++;
continue;
} BOOL bDoubleSplashFound = FALSE;
BOOL bSplashStarFound = FALSE;
for(int i=0; i<bufRead.GetLength()-1; i++)
{
char cTemp = bufRead[i];
if(bufRead[i]=='/' && bufRead[i+1]=='/' && !bCommentSet && !bQuatoSet)
{
if(!bStatedComment && (m_nStatMethod==1 || m_nStatMethod ==2))
{
bStatedComment = TRUE;
nCommentLines++;
}
bDoubleSplashFound = TRUE;
i++;//应该+1,但也没有什么用处
break;
}
else if(bufRead[i]=='/' && bufRead[i+1]=='*' && !bCommentSet && !bQuatoSet)
{
if(!bStatedComment && (m_nStatMethod==1 || m_nStatMethod ==2))
{
bStatedComment = TRUE;
nCommentLines++;
}
bCommentSet = TRUE;
bSplashStarFound = TRUE;
i++;
}
//计算代码行必须在bCommentSet关闭之前
else if(bufRead[i]!=' ' && bufRead[i]!='\t' && !bCommentSet)
{
if(!bStatedCode)
{
bStatedCode = TRUE;
nCodeLines++;
}
if(bufRead[i]=='\\')
{//\之后的字符要跳过
i++;
continue;
}
if(bufRead[i]=='\'')
{
if(bufRead[i+1]=='\\')
i+=2;
else
i+=1;
continue;
}
if(bufRead[i]=='"')
{//"必须引起重视,感谢ltzhou
bQuatoSet = !bQuatoSet;
}
}
else if(bufRead[i]=='*' && bufRead[i+1]=='/' && bCommentSet && !bQuatoSet)
{
if(!bStatedComment && (m_nStatMethod==1 || m_nStatMethod ==2))
{
bStatedComment = TRUE;
nCommentLines++;
}
bCommentSet = FALSE;
bSplashStarFound = TRUE;
i++;
}
} if(bDoubleSplashFound)
{
if(m_nStatMethod==2 && bStatedCode) //如果统计方法为第三种,且同时有代码行与注释行,则只计注释行
{
nCodeLines--;
}
if(m_nStatMethod==0 && !bStatedCode)//如果统计方法为第一种,且未作为代码行统计过,那么必为注释行
{
nCommentLines++;
}
continue;
} if(bufRead[bufRead.GetLength()-1]=='"'&&!bCommentSet)
{//若某行最后一个是",则必定用来关闭bQuatoSet,记代码行一行,否则报错
bQuatoSet = !bQuatoSet;
if(!bQuatoSet)
{
if(!bStatedCode)
{
bStatedCode = TRUE;
nCodeLines++;
}
}
else
{
CStdioFile fileLog;
if(fileLog.Open(m_strLogFile, CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate)==TRUE)
{
CString strMsg;
if(fileLog.GetLength()==0)
{
strMsg.Format("文件\t行\t问题\n", strFileName, nLines);
fileLog.WriteString(strMsg);
}
strMsg.Format("%s\t%d\t字符串换行未用\\\n", strFileName, nLines);
fileLog.WriteString(strMsg);
fileLog.Close();
}
}
continue;
} if(bufRead[bufRead.GetLength()-1]!=' ' && bufRead[bufRead.GetLength()-1]!='\t' && !bCommentSet
&& bufRead[bufRead.GetLength()-2]!='*' && bufRead[bufRead.GetLength()-1]!='/')
{//如果最后一个字符非空格或制表符,且前面无/*,最后两个字符不是*/,则为代码行
if(!bStatedCode)
{
bStatedCode = TRUE;
nCodeLines++;
}
} if(bSplashStarFound)
{
if(m_nStatMethod==2 && bStatedCode) //如果统计方法为第三种,且同时有代码行与注释行,则只计注释行
{
nCodeLines--;
} if(m_nStatMethod==0 && !bStatedCode && !bStatedComment) //若该行无代码如 /*abc*/ //222
//但是统计方法是第一种,则需要追加注释行计数一次
{
bStatedComment = TRUE;
nCommentLines++;
}
} if(!bStatedComment && bCommentSet)//可能是前面有/*,在第一种统计方法中,未作为代码行计算过,那么本行肯定是注释行
{
if(m_nStatMethod==0 && !bStatedCode)
{
bStatedComment = TRUE;
nCommentLines++;
}
}// if(bQuatoSet && bufRead[bufRead.GetLength()-1]=='"')
// {
// bQuatoSet = FALSE;
// } if(bQuatoSet && bufRead[bufRead.GetLength()-1]!='\\')
{
CStdioFile fileLog;
if(fileLog.Open(m_strLogFile, CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate)==TRUE)
{
CString strMsg;
if(fileLog.GetLength()==0)
{
strMsg.Format("文件\t行\t问题\n", strFileName, nLines);
fileLog.WriteString(strMsg);
}
strMsg.Format("%s\t%d\t字符串换行未用\\\n", strFileName, nLines);
fileLog.WriteString(strMsg);
fileLog.Close();
}
} } *pnCodeLines = nCodeLines;
*pnCommentLines = nCommentLines;
*pnBlankLines = nBlankLines; file.Close(); return nLines;
}
[email protected]
public void print(Result res);//输出数据
public void close();//释放资源
}class TextPrinter{//输出纯文本文件
public TextPrinter(String file){}//打开文件file作为输出
synchronized public void print(Result res){}//输出数据,一定要同步处理
public void close(){}//释放资源
}class HtmlPrinter同TextPrinter(输出Html格式文件)class Counter implements Runnable{
File f;
Printer p;
public Counter(File f,Printer p){
this.f=f;
this.p=p;
}
public void run(){
//统计数据并生成结果
Result res=new Result();
//res.set...;
p.print(res);
}
}public class Count{
public static void main(String args[]){
File f=new File(args[0]);//根据参数确定要处理的文件
File fl[]=null;
if(f.isDirectory()){
fl=f.listFiles();
}
else{
fl=new File[1];
fl[0]=f;
}
Printer p=getPrinter(args[1]);//根据参数确定输出格式
Thread[] t=new Thread[fl.length];
for(int i=0;i<fl.length;i++){
t[i]=new Thread(fl[i],p);//创建线程并启动
t[i].start();
}
for(int i=0;i<fl.length;i++){
try{
t[i].join();//等待线程结束
}catch(Exception e){}
}
p.close();//释放资源
} private static Printer getPrinter(String args){}//根据参数确定输出格式
}
t[i]=new Thread(fl[i],p);//创建线程并启动
应为:
t[i]=new Thread(new Counter(fl[i],p));
3x!
其实上面的代码也写的差不多了,如果不急的话,晚上回去写给你。
是[email protected]
是 [email protected]
不好意思,我在公司用的临时电脑上没jdk,上面是记事本写的。
其实上面的代码也写的差不多了,如果不急的话,晚上回去写给你。
厉害,我没编译器的话,写程序会出无数个错误,blush高手就是高手。另:
java 1.4 以上就有正则表达式了,我觉得做这个可能会容易一些,现在在家里,到公司写写试试看,^_^