每天都在头秃 2023-10-03 02:46 采纳率: 96.7%
浏览 10
已结题

C++删除注释程序上的一些问题

我在写一个用于去除注释的代码时,感觉我应该没理解错,但是自己敲出来的结果不能说完全正确。看了很久没看明白自己的问题出在哪儿了,希望能帮我看看。
我的思路大体是:
1,遇到“//”时,之后的文字直接除掉
2,遇到“/”,先看后面有没有“/”与之匹配,没有再换行匹配,一对“/”和“/”之间的文字都删掉。
如果我有什么情况忘记考虑了也请帮忙指点一下,谢谢了。
但是我修改后不知道哪儿的问题“//”后的文字删不掉。
然后我需要中间可能有空行出现会保留,但是我输入时读到空行就停了。
以下是代码:

#include <iostream>
#include <iomanip>
using namespace std;

void removeComments(istream & is, ostream & os) {
   //TODO
   //first get the string
    int state=0;//it's important 1:same line 2:different line
    int pos1 =0;//the read start
    int pos2=0;//the start use to output the after content
    string r;
    while (getline(is, r)){
        //Boolean whether the string is blank
        int k =r.length();
        if(k==0){
            os<<'\n';
            return;
        }//have characters
        else{
            for(int i=pos1;i<k;i++){
                //if it has "//"
                if(r[i]=='/'&&r[i+1]=='/'){
                    if(i<k-2){
                    os<< r.substr(0,i)<<endl;
                    continue;}
                    else{
                        os<<'\n';
                    }
                }
                
                //if it has "/*"
                if(r[i]=='/'&&r[i+1]=='*'){
                    os<<r.substr(0,i);//don't change the line 

                    //change the state
                    state =1;
                    //the position after *
                    
                    pos1=i+2;
                    
                
                }

                //if it has "*/"
                if(r[i]=='*'&&r[i+1]=='/'){
                    //recover
                    state =0;

                    if(i<k-2){
                    //continue the output
                    pos1=i+2; 
                    pos2=i+2;         
                    }
                    
                }

                //when we come to the final
                if(i==k-1){
                    if(state==1){
                        //different line
                        state=2;
                        pos1=0;
                        os<<'\n';
                        
                    }
                    else if(state==2){
                        pos1=0;
                        
                    
                    }
                    else if(state==0){
                        os<<r.substr(pos2)<<endl;
                        pos1=0;
                    }
                }

            }
        }
    }
    
    

    } 
    





void TestRemoveComments(){
    removeComments(cin, cout);
}


int main(int argc, char* argv[]) {
    TestRemoveComments();
    return 0;
}



  • 写回答

3条回答 默认 最新

  • 关注

    (1)读的空行就结束是因为你在 k=0的时候,直接return了。
    (2)你处理每行字符串的逻辑有问题,如果有 //,那么这一行// 后面的所有内容都应该忽略,而不应该continue;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 10月11日
  • 已采纳回答 10月3日
  • 创建了问题 10月3日

悬赏问题

  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题
  • ¥20 基于matlab的航迹融合 航迹关联 航迹插补
  • ¥15 用Matlab实现图中的光线追迹
  • ¥15 联想笔记本开机出现系统更新界面