C Comment conversion to C + + annotation Project

Source: Internet
Author: User

#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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.