#define _CRT_SECURE_NO_WARNINGS 1
#include "CommentConvert.h"
StateType State;
void Donullstate (file *read, file *write)
{
int first = FGETC (read);
int second = 0;
Switch (first)
{
Case '/':
Second = fgetc (read);
if (second = = ' * ')
{
FPUTC (First, write);
FPUTC ('/', write);
state = C_state;
}
else if (second = = '/')
{
FPUTC (First, write);
FPUTC (second, write);
state = Cpp_state;
}
Else
{
FPUTC (First, write);
FPUTC (second, write);
}
Break
Case EOF:
FPUTC (First,write);
state = End_state;
Default
FPUTC (First, write);
Break
}
}
void Docstate (file *read, file *write)
{
int first = FGETC (read);
int second = 0;
Switch (first)
{
Case ' * ':
Second = fgetc (read);
if (second = = '/')//Discard */
{
int third = 0;
Third = fgetc (read);
if (third! = ' \ n ' &&third! = EOF)
{
Fseek (Read,-1, seek_cur);
FPUTC (' \ n ', write);
}
Else
{
FPUTC (' \ n ', write);
}
state = Null_state;
}
else if (second = = ' * ')
{
UNGETC (second, read);
FPUTC (First, write);
}
Else
{
FPUTC (First, write);
FPUTC (second, write);
}
Break
Case ' \ n ':
FPUTC (First, write);
FPUTC ('/', write);
FPUTC ('/', write);
Break
Case EOF:
FPUTC (First,write);
state = End_state;
Break
Default
FPUTC (First, write);
Break
}
}
void Docppstate (file *read, file *write)
{
int first = FGETC (read);
int second = 0;
Switch (first)
{
Case ' \ n ':
FPUTC (First, write);
state = Null_state;
Break
Case EOF:
state = End_state;
Break
Default
FPUTC (First, write);
Break
}
}
void Convertwork (file *read, file *write)
{
state = Null_state;
while (state = end_state)
{
Switch (state)
{
Case Null_state:
Donullstate (read, write);
Break
Case C_state:
Docstate (read, write);
Break
Case Cpp_state:
Docppstate (read, write);
Break
}
}
}
void Commentconvert ()
{
file* pwrite = NULL;
file* PRead = fopen (InputFileName, "R");
if (PRead = = NULL)
{
Perror ("Open file for read");
Exit (Exit_failure);
}
Pwrite = fopen (OutputFileName, "w");
if (Pwrite = = NULL)
{
Fclose (PRead);
Perror ("Open File for write");
Exit (Exit_failure);
}
Convertwork (Pread,pwrite); Conversion functions
Fclose (PRead);
Fclose (Pwrite);
}
Input.c
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7D/BE/wKiom1butHqx-vzkAAAgGFw9bjQ226.png "style=" float: none; "title=" 1.png "alt=" Wkiom1buthqx-vzkaaaggfw9bjq226.png "/>
Output.c
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7D/BA/wKioL1butRLTofNoAAAdltDJB0s227.png "style=" float: none; "Title=" 2 Output.png "alt=" Wkiol1butrltofnoaaadltdjb0s227.png "/>
The state machine is used to divide the problem into 3 states, that is, C state, C + + state, null state, when traversing to a certain \n,/*,//, to divide the situation between the three states to convert
C Comment conversion to C + + annotation Project