高分求解 - 网络游戏公司面试题(Java)

mmzhping 2004-04-23 05:23:13
行数统计器程序需求
~~~~~~~~~~~~~~~~~~

程序目的:
给定多个源程序文件,统计每个源程序的物理行数、逻辑行数(非注释行)和注释行数;以及所有文件上述三种统计值的总计值。
注意:如果一行内有超过一处行内注释,则本行可以同时算作逻辑行和注释行。

程序输入:
1个以上的文件名。

程序输出:
根据命令行参数不同,可以分别得到下面两种输出:
1.纯文本输出。
2.HTML表格输出。
具体输出的格式由程序员从用户角度考虑,自行设计。

其他要求:
1.多线程:如果是多个文件,则每一个文件的统计需要同时进行。
2.易于扩充:程序未来可能需要扩充,比如其他输出格式。

定义:
被统计的源文件是合法(编译器可编译通过)的文件。
...全文
282 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
haha1903 2004-04-27
  • 打赏
  • 举报
回复
回复人: flyxxxxx(空指針) ( ) 信誉:99 2004-04-26 15:33:00 得分:0


不好意思,我在公司用的临时电脑上没jdk,上面是记事本写的。
其实上面的代码也写的差不多了,如果不急的话,晚上回去写给你。


厉害,我没编译器的话,写程序会出无数个错误,blush

高手就是高手。

另:
java 1.4 以上就有正则表达式了,我觉得做这个可能会容易一些,现在在家里,到公司写写试试看,^_^
swiminthesea 2004-04-27
  • 打赏
  • 举报
回复
进来学习
haoqingshi 2004-04-26
  • 打赏
  • 举报
回复
帅哥,给我传一个吧,多谢

haoqingshi@hotmail.com
jinsfree 2004-04-26
  • 打赏
  • 举报
回复
up
kaiyim 2004-04-26
  • 打赏
  • 举报
回复
我也想要一份,能麻烦也给我mail一份吗?
是 kaiyimwong@yahoo.com.hk
mmzhping 2004-04-26
  • 打赏
  • 举报
回复
邮件接受失败,请重发一遍好吗?
wwwasp 2004-04-26
  • 打赏
  • 举报
回复
能给我发一个多线程的程序吗?
desweb@sohu.com
qqbz 2004-04-26
  • 打赏
  • 举报
回复
面试还要你写程序吗?从来没有遇到过
lyzkkzong 2004-04-26
  • 打赏
  • 举报
回复
给我发一个。lyz_chinese@sohu.com
donghx1234 2004-04-26
  • 打赏
  • 举报
回复
mmzhping 2004-04-26
  • 打赏
  • 举报
回复
ding
chenyaoguo23 2004-04-26
  • 打赏
  • 举报
回复
我也想要一份,能麻烦也给我mail一份吗?
是chenyaoguo23@tom.com
mmzhping 2004-04-26
  • 打赏
  • 举报
回复
如此多谢!
flyxxxxx 2004-04-26
  • 打赏
  • 举报
回复
不好意思,我在公司用的临时电脑上没jdk,上面是记事本写的。
其实上面的代码也写的差不多了,如果不急的话,晚上回去写给你。
mmzhping 2004-04-26
  • 打赏
  • 举报
回复
to flyxxxxx(空指針):

能将完整的编译通过的代码mailto:mmzhping@163.com吗?
3x!
Arias 2004-04-26
  • 打赏
  • 举报
回复
程序这么复杂?看来工作不简单哦!
flyxxxxx 2004-04-26
  • 打赏
  • 举报
回复
Sorry,
t[i]=new Thread(fl[i],p);//创建线程并启动
应为:
t[i]=new Thread(new Counter(fl[i],p));
flyxxxxx 2004-04-26
  • 打赏
  • 举报
回复
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){}//根据参数确定输出格式
}

gyscsdn 2004-04-26
  • 打赏
  • 举报
回复
up
mmzhping 2004-04-25
  • 打赏
  • 举报
回复
这里有段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;
}
加载更多回复(9)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧