0
点赞
收藏
分享

微信扫一扫

CSDN编程竞赛,让更多的人学会编程

椰果玩安卓 2022-09-15 阅读 207

前言/背景

大赛简介

参赛流程

参赛经历

解题思路

CompareCsvXml.h
#pragma once
#include <string>
using namespace std;
namespace extract{
	string slice(string str, int start, int end)
	{
		string str1 = "";
		for (int i = 0; i < str.size(); ++i)
		{
			if (i >= start && i <= end)
			{
				str1 += str[i];
			}
		}
		return str1;
	}
}
CompareCsvXml.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
#include <windows.h>
#include <vector>
#include "CompareCsvXml.h"
using namespace std;
using namespace extract;

int main()
{
	
	//1、读取CSV文件
	ifstream inCsv("RelayBoardMap.csv", ios::in);//只读的方式打开一个已存在的文件
	if (!inCsv)
	{
		cout << "打开文件失败!" << endl;
		exit(1);
	}
	//1.1 处理CSV文件
	string lineCSV;//定义行
	
	int i = 0;//记录行数
	int n = 0;//记录错误行数

	//2、读取XML文件
	ifstream inXml("RelayBoardMap.xml", ios::in);//只读的方式打开一个已存在的文件
	if (!inXml)
	{
		cout << "打开文件失败!" << endl;
		exit(1);
	}
	//2.2 处理XML文件
	string line;//定义行
	string filed;
	//int i = 0;//记录行数
	//写入标题行

	string strRelayBoardType;
    string strVersion;
	//string strVersion;
	string strRelay;
	string strDelay;
	string strAddr;
	string strBit;

	string Version;
	string RelayBoardType;
	string Relay;
	string Delay;
	string Addr;
	string Bit;
	string str = "";
	string Begin = "Rev,BoardType,Relay,Addr,Bit,Delay";

    bool flag = true;
	while (getline(inCsv, lineCSV))//表示按行读取CSV文件中的数据
	{

		string field;

		istringstream sin(lineCSV);//将整行字符串line读入到字符串流给sin

		getline(sin, field);//将sin中的字符读入到field字符串中,以逗号为分隔符

			
		if (flag)//直接跳过第一行
		{
			flag = false;
			continue;
		}

		while (getline(inXml, line))
		{

			//以下是XML文件的处理
			string strStr;
			string fieldXML;
			vector<char> vec;
			istringstream sin(line);//将整行字符串line读入到字符串流给sin

			getline(sin, fieldXML);//将sin中的字符读入到field字符串中,

        if (string::npos != strLine.find("<Relay Value="))
				str += strLine;
			else if (string::npos != strLine.find("<Delay"))
				str += strLine;
			else if (string::npos != strLine.find("<RelayAddr Addr"))
				str += strLine;

			if (str.size() > 70)
			{
				//重新按行做一个整理.将下面的代码实现写到这里。
				if (string::npos != str.find("Relay Value"))//这行如果成立代表已经找到
				{
					int pos_1 = str.find("Value=");
					int pos_2 = str.find(">");
					Relay = slice(str, pos_1 + 7, pos_2 - 2);//查找引号中的内容
				}

					//实现
				if (strRelay != Relay || Bit != strBit) //只要前后两个继电器或者比特位不相同就可以输出
				{
						//1
					if (strVersion != Version)
					{
						strVersion = Version;
						strStr += strVersion;
						strStr += ",";
						//cout << strVersion;
					}
					else
						strStr += ",";;


						// 2
					if (strRelayBoardType != RelayBoardType)
					{
						strRelayBoardType = RelayBoardType;
						strStr += strRelayBoardType;
						strStr += ",";
						//cout << strRelayBoardType;

	                strRelay = Relay;
					strStr += strRelay;
					strStr += ",";
					strAddr = Addr;
					strStr += strAddr;
					strStr += ",";
					strBit = Bit;
					strStr += strBit;
					strStr += ",";
					strDelay = Delay;
					strStr += strDelay;
				//		cout << "读取CSV:" << field.c_str() << endl; //c_str()返回的是field的首地址
				//		cout << "读取XML:" << strStr << endl;//位置不能变
						//++i;
					++i;
					int num=strStr.compare(field.c_str());
					if (num != 0)
					{
						cout << "读取XML:" << strStr << endl;
						cout << "读取CSV:" << field.c_str() << endl;
						cout << "第" << i+1 << "行不相同" << endl;
						n++;
					}

					break;
				}

			}

			if (string::npos != strLine.find("</Relay>"))
				str = "";
				
		}
	}

    cout << "**********************************************" << endl;
	cout << "以下行数是以CSV文件为参考" << endl;
	cout << "一共遍历了" << i+1 << "行" << endl;
	cout << "其中有" << n << "行出现了问题" << endl;
	system("pause");
	return 0;
}

 

经验心得

资料分享

举报

相关推荐

0 条评论