求解,关于getline读取的空格和去除代码注释

h496022202 2012-03-27 10:12:34
好吧,在编一个程序,用来计算源代码中的行数以及取出代码中for语句与if语句的一个程序,以下是代码:
#include <iostream>
#include<string>
#include<fstream>
using namespace std;
int ReadLine();
//int ReadFor();
void main()
{
int L;
L=ReadLine();
cout<<"代码总共"<<L<<"行"<<endl;
int m;
cin>>m;

}
int ReadLine()
{
fstream dataFile;
string File;
int num=0;

cout<<"请输入文件位置和文件名:";
cin>>File;
dataFile.open(File,ios::in|ios::out);
if(!dataFile)
{
cout<<"文件打开失败,检查文件名或文件路径是否输错!"<<endl;
exit(0);
}
while (!dataFile.eof())
{
string arr;
//dataFile>>arr;
getline(dataFile,arr);
int n=arr.length();
//for(int j=0;j<n;j++)
// {
// if(n-j>2)
// {if(arr[j]=='/'&&arr[j+1]=='/')
// arr=arr.substr(0,j);}}
num++;
//string F="for";
//string I="if";
if(arr=="\n")
--num;

for(int i=0;i<n;i++)
{
if(n-i>3)
{
if(arr[i]=='f'&&arr[i+1]=='o'&&arr[i+2]=='r')
cout<<num<<"行"<<"for语句"<<arr<<endl;
}
if(n-i>2)
{
if(arr[i]=='i'&&arr[i+1]=='f')
cout<<num<<"行"<<"if语句"<<arr<<endl;
}
}
}
return num;
}
在测试时 在计数的时候,把代码文件中的空行也计算进去了,以上红色字体为,写的取出空行的代码,貌似没有用,求教各位大神,这个该如何弄呢,而且把代码行读入到arr中时,空格也进去了,如何在读取时就忽略掉空格呢,剩下的就是那个去除注释了。。。。
...全文
332 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
h496022202 2012-03-28
  • 打赏
  • 举报
回复
{
string arr;
string::size_type pos;
//dataFile>>arr;
getline(dataFile,arr);
int n=arr.length();
pos=arr.find("//");
if(pos!=string::npos)
{
arr.replace(arr.begin()+pos,arr.end(),"\n");
}
//for(int j=0;j<n;j++)
// {
// if(n-j>2)
// {if(arr[j]=='/'&&arr[j+1]=='/')
// arr=arr.substr(0,j);}}
if(!blank_lines(arr))
{
num++;
}
但是,经过以上改动后,当程序一遇到有//的行数时 运行到if(!blak_line(arr))时 就出错,求解决
hongwenjun 2012-03-27
  • 打赏
  • 举报
回复
读入一行 比如
pos = str_line.find(str1); // 查找 str1 替换成 str2
查找 // 的位置,然后替换成 "空" 就可以了

#include <iostream>

using namespace std;

int main()
{
string strline = "pos = str_line.find(str1); // 查找 str1 替换成 str2";

string::size_type pos;

pos = strline.find("//");
if (pos != string::npos) {
strline.replace(strline.begin() + pos, strline.end(), "删除注解");
}
cout << strline << endl;
return 0;
}
h496022202 2012-03-27
  • 打赏
  • 举报
回复
谢谢哈,但这个似乎就解决了空行的问题。。。。
hongwenjun 2012-03-27
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cctype>

using namespace std;
void help(); // 调用使用帮助
bool blank_lines(const string& str); // 检测空行
string& find_replace(string& str_line, const string& str1 , const string& str2); // 查找替换字符窜
string& Chinese_Punctuation_Conver_English(string& str_line); // 中文标点符号为英文

int main(int argc, char* argv[])
{
if ((1 == argc)) { //错误输入处理
help();
return -1;
}
ifstream infile(argv[1]);
if (!infile.good()) {
printf("文件错误:不能打开输入文件: %s \n\a\n", argv[1]);
help();
return -1;
}
// 主模块功能:中文标点符号为英文,按条件删除空行
stringstream oss; // 输出先写 oss 缓冲区
string str_line;
bool CodeBlocks_flag = false;
while (getline(infile, str_line)) {
Chinese_Punctuation_Conver_English(str_line);
if (!blank_lines(str_line)) // 检测不是空行
oss << str_line << endl;
else if (CodeBlocks_flag) {
oss << str_line << endl;
CodeBlocks_flag = false;
}
// 如果当前语句块结束,保留下一行空行标记
if (str_line[str_line.size() - 1] == '}')
CodeBlocks_flag = true;
}
infile.close();
ofstream outfile;
if (2 < argc)
outfile.open(argv[2]); // 输出到新文件
else
outfile.open(argv[1]); // 替换源文件
outfile << oss.str(); // 缓冲区写文件
cout << "本工具整理代码中的中文标点符号为英文,删除空行 BY Hong Wenjun\n\n"
"Chinese Punctuation Conver English Punctuation OK! ...." << endl;
return 0;
}

void help()
{
printf("本工具整理代码中的中文标点符号为英文,删除空行 BY Hong Wenjun\n\n"
"CodeTool filename [输出filename] \n\n"
"示例 1 :D:\\>CodeTool D:\\app.cpp \n"
"示例 1 :D:\\>CodeTool D:\\app.cpp D:\\app_new.cpp \n"
"\n输出文件不填,覆盖原输入文件\n");
}


bool blank_lines(const string& str)
{
string::const_iterator it;
for (it=str.begin() ; it != str.end(); it++ ) {
if (!isspace(*it)) // 如果不是空格,就不是空行
return false;
}
return true;
}

string& find_replace(string& str_line, const string& str1 , const string& str2)
{
string::size_type pos;
size_t CPsize = str1.size();

pos = str_line.find(str1); // 查找 str1 替换成 str2
while (pos != string::npos) {
str_line.replace(pos, CPsize, str2);
pos = str_line.find(str1, pos + 1);
}
return str_line;
}

string& Chinese_Punctuation_Conver_English(string& str_line)
{
find_replace(str_line, "“", "\"");
find_replace(str_line, "”", "\"");

find_replace(str_line, "‘", "\'");
find_replace(str_line, "’", "\'");

find_replace(str_line, ",", ",");
find_replace(str_line, "。", ".");

find_replace(str_line, "!", "!");
find_replace(str_line, "?", "?");

find_replace(str_line, ";", ";");
find_replace(str_line, ":", ":");

find_replace(str_line, " ", " "); // 全角空格
find_replace(str_line, "(", "(");
find_replace(str_line, ")", ")");
find_replace(str_line, "〈", "<");
find_replace(str_line, "〉", ">");

return str_line;
}


我的代码工具给你参考一下

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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