gpt4 book ai didi

c - 我的用于删除C文件中注释的代码版本不起作用

转载 作者:行者123 更新时间:2023-11-30 20:08:57 25 4
gpt4 key购买 nike

好吧,我目前不在考虑以下问题

char*s="HELLO WORLD /*ABCD*/";


我的主要目的是要知道为什么下面的代码如此糟糕,无法按我希望的那样工作

#include <stdio.h>
int main()
{
FILE *src=fopen("Program.c","rb");
if(src==0)
{
printf("ERROR OPENING THE SOURCE FILE\n");
return 0;
}
FILE *des=fopen("NewProgram.txt","wb");
if(des==0)
{
printf("ERROR OPENING THE DESTINATION FILE\n");
return 0;
}

char ch;
while((ch=fgetc(src))!=EOF)
{
if(ch=='/')
{
ch=fgetc(src);
if(ch=='/')//single line comment encountered
{
while((ch=fgetc(src))!='\n');//scan till an 'ENTER' is found
fputc(ch,des);//adding the '\n' to the file

}
else if(ch=='*')//multi-line or documentation encountered
while((ch=fgetc(src))!='/');//scan till the closing '/' is encountered
else
{
fputc('/',des);
fputc(ch,des);
}

}
else
fputc(ch,des);
}
return 0;
}


下面是Program.c

#include <stdio.h>
int nthNonFibonacci(int n,int a,int b);
//call the function with (n(the desired term),fib1,fib2)
//fib1 and fib2 are 2 and 3 respectively
//they are starting consecutive fibonacci numbers
//0 and 1 avoided as they shall unnecessarily increase stack depth
int main()
{
int n;//to store the upper limit
printf("ENTER THE UPPER LIMIT OF NON FIBONACCI SERIES\n");
scanf("%d",&n);//input of upper limit
printf("\nTHE SERIES :\n");
for(int i=1;i<=n;i++)
printf("%d ",nthNonFibonacci(i,2,3));
return 0;
}


/** a and b are two consecutive fibonacci numbers
* (b-a-1) => gives the number of non fibonacci numbers b/w a and b
* we subtract (b-a-1) from n to make sure that we have logically traversed
* the non fibonacci numbers b/w a and b
* this is done when n-(b-a-1)>0 and also at the same time we update a
* and b to the next consecutive fibonacci numbers
* now our base case is when in a pass n-(b-a-1) <=0 which means that
* our required nth non fibonacci number lies b/w current a and b
* so in that situation we simply return the LOWER_LIMIT_FIBONACCI(i.e. a)
* added to current n
*/
int nthNonFibonacci(int n,int a,int b)
{
if((n-(b-a-1))<=0)
return (n+a);
else
return nthNonFibonacci(n-(b-a-1),b,a+b);
}


下面是NewProgram.txt

#include <stdio.h>
int nthNonFibonacci(int n,int a,int b);




int main()
{
int n;
printf("ENTER THE UPPER LIMIT OF NON FIBONACCI SERIES\n");
scanf("%d",&n);
printf("\nTHE SERIES :\n");
for(int i=1;i<=n;i++)
printf("%d ",nthNonFibonacci(i,2,3));
return 0;
}


w a and b
* we subtract (b-a-1) from n to make sure that we have logically traversed
* the non fibonacci numbers b/w a and b
* this is done when n-(b-a-1)>0 and also at the same time we update a
* and b to the next consecutive fibonacci numbers
* now our base case is when in a pass n-(b-a-1) <=0 which means that
* our required nth non fibonacci number lies b/w current a and b
* so in that situation we simply return the LOWER_LIMIT_FIBONACCI(i.e. a)
* added to current n
*/
int nthNonFibonacci(int n,int a,int b)
{
if((n-(b-a-1))<=0)
return (n+a);
else
return nthNonFibonacci(n-(b-a-1),b,a+b);
}


我也检查了其他悬挂物。
但是我不明白为什么在文档或多行注释的情况下最后一个else块也被执行

我正在经历一些不确定的行为吗?

fgetc(int,FILE *)=>我知道它是以顺序方式工作的,一旦它导致文件指针前进,就不应返回,
那情况是怎么发生的呢?

最佳答案

您需要检查* /,而不仅是/,因为您的某些注释本身已经存在,请检查。请注意,您的代码已从/ ** ..... b /中删除了该部分。

下面给出的是实现该问题的代码的修改版本。

 #include <stdio.h>
void removeComments();//removes the comments from the file
//pointed by src
FILE *src,*des;//file pointers for the source and destination
int main()
{
src=fopen("Program.c","rb");//opening the source program
//file in read binary mode
if(src==0)//fopen return NULL if there is error in opening
{ //file
printf("ERROR OPENING THE SOURCE FILE\n");
return 0;
}
des=fopen("NewProgram.txt","wb");//opening/creating the
//destination file in write binary mode
if(des==0)//fopen returns NULL if there is error
{
printf("ERROR OPENING THE DESTINATION FILE\n");
return 0;
}
removeComments();//calls the remove comments function to
//remove the comments
fclose(src);//closing the source
fclose(des);//closing the destination
return 0;
}//end of main

void removeComments()
{
int curr,next;//creating the current and next character as
//int as they might require comparison with EOF i.e.-1
while((curr=fgetc(src))!=EOF)//the while loop runs till the
{ //end of file is reached
if(curr=='/')//probable beginning of a comment
{
curr=fgetc(src);//reading the next character from src
if(curr=='/')//single-line comment encountered
{
while((curr=fgetc(src))!='\n');//scan till an Enter is encountered
fputc(curr,des);//writing the '\n' scanned last
//to the destination file
}//end of if
else if(curr=='*')//a multi-line or documentation encountered
{
next=fgetc(src);//scaning the next character
while(!(curr=='*'&&next=='/'))//the loop runs till
{ //the */ is encountered
curr=next; //moving through the file in turn of
next=fgetc(src);//two characters
}//end of while
}//end of else if
else//situation where / was scanned but the next element was not a /
//because if / was scanned and the next is * then definitely a block comment
//has started
{
fputc('/',des);
fputc(curr,des);
}
}//end of other if
else if(curr=='"')//handling the string
{
fputc(curr,des);//writing the opening " to the file
while((curr=fgetc(src))!='"')
fputc(curr,des);
fputc(curr,des);//writing the closing " to the file
}//end of else if
else//dealing the normal code
fputc(curr,des);//writing them to the destination file
}
}

关于c - 我的用于删除C文件中注释的代码版本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54599634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com