按格式除了0017其他的数字全部翻倍,并且翻倍后的数字能按照原本的格式正常的输出
<string>
这样选择
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 1024
int isDigit(char c)
{
return (int)c <= (int)'9' && (int)c >= (int)'0' ? 1 : 0;
}
//根据标签值有无花括号作为区别
void outputString(char *line)
{
int len = strlen(line), i = 0;
char buffer[MAXSIZE] = "\0"; //缓冲区
int mark = 0;
int j = 0;
int value;
for (i = 0; i < len; i++)
{
if (line[i] == '{')
{
printf("%c", line[i]);
mark += 1;
}
else if (line[i] == '}' && j > 0)
{
mark -= 1;
buffer[j] = '\0';
// str -> int
value = atoi(buffer);
printf("%d", 2 * value);
printf("%c", line[i]);
j = 0;
}
else if (mark > 0 && isDigit(line[i]))
{
buffer[j++] = line[i];
}
else if (mark > 0 && line[i] == ',' && j > 0)
{
buffer[j] = '\0';
// str -> int
value = atoi(buffer);
printf("%d", 2 * value);
printf("%c", line[i]);
j = 0;
}
else
{
printf("%c", line[i]);
}
}
}
//根据标签作为区别
void outputString1(char *line, char *targetMark)
{
int len = strlen(line), i = 0;
char numBuffer[MAXSIZE] = "\0"; //缓冲区
char markBuffer[MAXSIZE] = "\0"; //标签
int label_mark = 0, num_mark = 0, target_mark = 0;//表示 是否为标签范围、数字范围、目标标签
int m_j = 0, n_j = 0; //标签表示,数字表示
int value;
for (i = 0; i < len; i++)
{
if (line[i] == '<')
{
label_mark = 1;
m_j = 0;
markBuffer[m_j++] = line[i];
printf("%c", line[i]);
}
else if (label_mark == 1)
{
markBuffer[m_j++] = line[i];
printf("%c", line[i]);
if (line[i] == '>')
{
label_mark = 0;
markBuffer[m_j] = '\0';
m_j = 0;
if(strcmp(markBuffer,targetMark)==0){
target_mark = 1;
}
}
}else if(target_mark==1&&isDigit(line[i])){
numBuffer[num_mark++] = line[i];
}else if(target_mark==1&&num_mark>0&&!isDigit(line[i])){
numBuffer[num_mark]='\0';num_mark=0;
// str -> int *2 -> output
value = atoi(numBuffer);
printf("%d", 2 * value);
printf("%c",line[i]);
} else{
printf("%c",line[i]);
}
}
}
int main()
{
char line[MAXSIZE + 1] ;//= "<string>{{0,0},{140,44}}</string>";
// outputString1(line, "<string>");
// system("pause");
char*filename="C:/Users/Lenovo/Desktop/1.xml";
FILE*fp=fopen(filename,"r+");
if(fp==NULL) return -1;
while(!feof(fp)){
fgets(line,MAXSIZE,fp);
outputString1(line,"<string>");
}
printf("\n");
fclose(fp);
}
1.从前往后遍历字符,遇到数字从尾部入队列
2.遇到非数字字符分为两种情况,如果队列为空则把字符写入文件或者缓冲区,如果队列非空则依次把字符出队,并把他们进行翻倍运算,然后转换成字符写入到缓冲区。
最后再把非数字字符也写进去。
3.如果你要对某个数字进行过滤,可以在上个步骤出队时判断一下,如果是需要忽略的就不翻倍直接写入缓存或者文件。