在我们的C/C++的项⽬目当中会有C、C++两种注释⻛风格,有时为了方便,会将注释全部转为同一种,我这里做的是将c语言风格注释转为c++注释;
c注释为:/int i=1;/
c++注释为://int i=1;
因为在注释转换中有很多情况需要考虑周全,所以可通过下图的梳理来一步步实现代码
接下来就是代码部分,首先是头文件“convert.h”
#ifndef __CONVERT_H__
#define __CONVERT_H__
#include<stdio.h>
#include<stdlib.h>
enum { NULSTAT, CSTAT, CPPSTAT, EOFSTAT };
#define INPUT "input.c"//定义输入文件
#define OUTPUT "output.c"//定义输出文件
static void convert_work(FILE *ipf, FILE *opf);
void do_null_stat(FILE*ipf, FILE*opf);
void do_cpp_stat(FILE*ipf, FILE*opf);
void do_c_stat(FILE *ipf, FILE *opf);
void convert_main();
#endif __CONVERT_H__
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
源文件”convert.c”:
#define _CRT_SECURE_NO_WARNINGS 1
#include"convert.h"
static int status = NULSTAT;
void do_null_stat(FILE*ipf, FILE*opf)
{
int c = fgetc(ipf);
switch (c)
{
case'/':
{
int s = fgetc(ipf);
switch (s)
{
case'*':
fputc('/', opf);
fputc('/', opf);
status = CSTAT;
break;
case'/':
fputc('/', opf);
fputc('/', opf);
status = CPPSTAT;
break;
default:
fputc(c, opf);
fputc(s, opf);
status = NULSTAT;
break;
}
}break;
case EOF:
fputc(c, opf);
status = EOFSTAT;
default:
fputc(c, opf);
break;
}
}
void do_c_stat(FILE *ipf, FILE *opf)
{
int c = fgetc(ipf);
switch (c)
{
case'*':
{
int s = fgetc(ipf);
switch (s)
{
case'/':
{
int r = fgetc(ipf);
if (r == '\n')
{
fputc(r, opf);
}
else
fputc('\n', opf);
ungetc(r, ipf);
status = NULSTAT;
break;
default:
fputc(c, opf);
ungetc(s, ipf);
break;
}
}
}break;
case'\n':
fputc(c, opf);
fputc('/', opf);
fputc('/', opf);
break;
default:
fputc(c, opf);
break;
}
}
void do_cpp_stat(FILE*ipf, FILE*opf)
{
int c = fgetc(ipf);
switch (c)
{
case'\n':
fputc(c, opf);
status = NULSTAT;
default:
fputc(c, opf);
break;
}
}
static void convert_work(FILE *ipf, FILE *opf)
{
while (status != EOFSTAT)
{
switch (status)
{
case NULSTAT:
do_null_stat(ipf, opf);
break;
case CPPSTAT:
do_cpp_stat(ipf, opf);
break;
case CSTAT:
do_c_stat(ipf, opf);
break;
default:
printf("ERROR!!!");
break;
}
}
}
void convert_main()
{
FILE *ipf = NULL;
FILE *opf = NULL;
ipf = fopen("input.c", "r");
if (ipf == NULL)
{
perror("input file open: ");
exit(EXIT_FAILURE);
}
opf = fopen("output.c", "w");
if (opf == NULL)
{
perror("output file open: ");
fclose(ipf);
exit(EXIT_FAILURE);
}
convert_work(ipf, opf);
fprintf(opf, "%s\n");
printf("转换成功\n");
fclose(ipf);
fclose(opf);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
主函数main()
#define _CRT_SECURE_NO_WARNINGS 1
#include"convert.h"
int main()
{
convert_main();
return 0;
}
以上就是我所实现的代码程序,接下来可写入输入文件input.c
int num = 0;
int j = 0;
int m = 0;
int k = 0;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
接下来就是执行程序,成功后可在文件资源管理器中查看输出文件”output.c”,看输出结果是否符合预期,下面是我的输出文件:
int num = 0;
int j = 0;
int m = 0;
int k = 0;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
可见是符合转换要求的。
在写程序的过程中最需要注意的是细节!细节!细节!细节!细节!…………
重要的事说上不止三遍