Ну, я сейчас не думаю о таких проблемах, как
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 *) => Я знаю, что работает последовательно, и как только он заставляет указатель файла двигаться вперед, он не должен возвращаться обратно, тогда как происходит ситуация?