一个案例...... [英] A case for a case...

查看:90
本文介绍了一个案例......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我刚刚编写的一些代码有点麻烦。

它在ASP.NET MVC应用程序中,所以我可以处理以下场景后端(C#)或前端(JavaScript)。这对于使用过的方法来说应该不重要。

基本上我要做的是以下内容:



我有以下列表(第三方组件,不能更改此内容):

标记1开始
某些对象1
某些对象2
一些对象3
标记1结束
标记2开始
一些对象4
标记3开始
一些对象5
标记3结束
标记2结束
某些对象6





我想要以下结构:

< pre lang =text> Marker 1
Some object 1
Some object 2
Some object 3
Marker 2
some object 4
Marker 3
一些对象5
匿名标记
一些对象6





所以我看到它我可以做以下(伪代码):

标记; 
currentMarker;
foreach(obj in list)
switch(obj)
{
case'Marker start':
newMarker = new {...};
if(currentMarker!= null){
currentMarker.markers.add(newMarker);
newMarker.parent = currentMarker;
} else {
markers.add(newMarker);
}
currentMarker = newMarker;
case'Marker end':
if(currentMarker.parent){
currentMarker = currentMarker.parent;
} else {
currentMarker = null;
}
默认值:
if(currentMarker == null){
currentMarker = new anonymous Marker {...};
markers.add(currentMarker);
}
currentMarker.objects.add(obj);
}
返回标记;

这是很多情况,如果是和其他的......

我的方法已经超过40行代码而我我很确定我会添加一些行,如果时间过去了(如果标记开始没有结束标记怎么办?等等)。

是否有更好,更易读,更易维护的方式来处理场景?也许某些模式或算法可以节省一天?

我不能成为第一个遇到这个问题的人:)



谢谢!

解决方案

为什么不首先使用更结构化的方式来表示数据呢?比如使用JSON,XML或YAML。



-------------------------------



每次遇到启动标记时,都会将其推入堆栈,增加IndentedTextWriter的标识并将标记文本写入其中。对于每个结束标记,您弹出堆栈并将此标记号与结束标记号进行比较(如果数字不匹配则给出错误)。然后你也减少了作者的身份。您刚写入IndentedTextWriter的所有其他项目。当您在标记外的根处遇到项目时,您将添加匿名标记。



希望这能让您对如何处理文件有所了解。



https://msdn.microsoft.com/en-us/library/system.collections.stack%28v=vs.110%29.aspx [ ^ ]



https://msdn.microsoft.com/en-us/library/system.codedom.compiler.indentedtextwriter%28v=vs.110%29.aspx [ ^ ]



祝你好运!


Hi all,
I'm having a little trouble with a bit of code I just wrote.
It's in an ASP.NET MVC application, so I can handle the following scenario back-end (C#) or front-end (JavaScript). It shouldn't really matter for the used method though.
Basically what I'm trying to do is the following:

I have the following list (third party component, can't change this):

Marker 1 start
Some object 1
Some object 2
Some object 3
Marker 1 end
Marker 2 start
Some object 4
Marker 3 start
Some object 5
Marker 3 end
Marker 2 end
Some object 6



And I want the following structure:

Marker 1
   Some object 1
   Some object 2
   Some object 3
Marker 2
   Some object 4
   Marker 3
      Some object 5
Anonymous marker
   Some object 6



So as I see it I can do the following (pseudo code):

markers;
currentMarker;
foreach (obj in list)
switch (obj)
{
   case 'Marker start':
      newMarker = new {...};
      if (currentMarker != null) {
         currentMarker.markers.add(newMarker);
         newMarker.parent = currentMarker;
      } else {
         markers.add(newMarker);
      }
      currentMarker = newMarker;
   case 'Marker end':
      if (currentMarker.parent) {
         currentMarker = currentMarker.parent;
      } else {
         currentMarker = null;
      }
   default:
      if (currentMarker == null) {
         currentMarker = new anonymous Marker {...};
         markers.add(currentMarker);
      }
      currentMarker.objects.add(obj);
}
return markers;

That's a lot of cases and if's and else's...
My method is already over 40 lines of code and I'm pretty sure I'll add some lines and if's as time passes (what if a Marker start has no end marker? etc.).
Is there a better, more readable, more maintainable way to handle the scenario? Maybe some pattern or algorithm that can save the day?
I can't be the first one to have this problem :)

Thanks!

解决方案

Why not use a more structured way of representing the data in the first place? Like using JSON, XML or YAML for example.

-------------------------------

Every time you encounter something a start marker you push it onto the stack, increase the ident of the IndentedTextWriter and write the marker text to it. For every end marker you pop the stack and compare this marker number with the end marker number (and give an error if the numbers don't match). You then also decrease the ident of the writer. Every other item you just write to the IndentedTextWriter. When you encounter an item at the root outside a marker you add a Anonymous marker.

Hope this gives you a good idea on how to approach processing the file.

https://msdn.microsoft.com/en-us/library/system.collections.stack%28v=vs.110%29.aspx[^]

https://msdn.microsoft.com/en-us/library/system.codedom.compiler.indentedtextwriter%28v=vs.110%29.aspx[^]

Good luck!


这篇关于一个案例......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆