public static IEnumerable <QuestionResponseData_v2> GetQuestionResponses_v2(IEnumerable <Question_v2> questions, IEnumerable <Response_v2> responses) { Dictionary <int, Response_v2> responseDictionary = CreateResponseDictionary_v2(responses); foreach (Question_v2 question in questions) { int id = GetQuestionId(question.Year, question.QuestionNumber); //if (!responseDictionary.ContainsKey(id)) // throw new PBFileException($"response not found {question.Year} - question no {question.Number}", question.SourceFile, question.SourceLine); Response_v2 response = null; if (responseDictionary.ContainsKey(id)) { response = responseDictionary[id]; } else { Trace.WriteLine($" warning response not found {question.Year} - question no {question.QuestionNumber} - line {question.SourceLine} - file {zPath.GetFileName(question.SourceFile)}"); } yield return(new QuestionResponseData_v2 { Question = question, Response = response }); } }
private QuestionResponse_v2 ReadResponse(QuestionData value) { Response_v2 response = null; if (_responseQuestion != null && value.Type != QuestionDataType.Response) { if (_trace) { //Trace.WriteLine($"save response question (line {value.LineNumber} \"{value.Filename}\")"); Trace.WriteLine($"save response question : {_responseQuestion.Year} - {_responseQuestion.QuestionNumber}"); } SaveResponseQuestion(_responseQuestion); _responseQuestion = null; } switch (value.Type) { case QuestionDataType.EmptyLine: if (_emptyLineCount == _responseMaxEmptyLine && _responseYears.Count > 0) { _responseEnd = true; } break; case QuestionDataType.ResponseCategory: _responseCategory = ((QuestionDataResponseCategory)value).Category; if (_trace) { Trace.WriteLine($"response category : {_responseCategory} (line {value.LineNumber} \"{value.Filename}\")"); } ControlRemindResponseQuestions(); _responseYears = new List <ResponseReader.ResponseYear>(); _responseQuestionBlock = false; break; case QuestionDataType.ResponseYear: QuestionDataResponseYear responseYear = (QuestionDataResponseYear)value; if (_responseQuestionBlock) { throw new PBException($"wrong response year position, line {responseYear.LineNumber} column {responseYear.ColumnIndex + 1} file \"{responseYear.Filename}\""); } if (_trace) { Trace.WriteLine($"response year : {responseYear.Year} (line {value.LineNumber} \"{value.Filename}\")"); } ResponseAddYear(responseYear); break; case QuestionDataType.QuestionNumber: //responseQuestion = NewQuestion(namedValue.Value.Value, namedValue.Value.Index, namedValue.Value.Length, responseQuestion); QuestionDataQuestionNumber questionNumber = (QuestionDataQuestionNumber)value; if (_trace) { Trace.WriteLine($"response question : {questionNumber.QuestionNumber} (line {value.LineNumber} \"{value.Filename}\")"); } _responseQuestion = ResponseCreateResponseQuestion(questionNumber); _responseQuestionBlock = true; break; case QuestionDataType.Response: QuestionDataResponse responseValue = (QuestionDataResponse)value; if (_responseQuestion != null && _responseQuestion.SourceLineNumber != responseValue.LineNumber) { SaveResponseQuestion(_responseQuestion); _responseQuestion = null; } response = SetResponse(responseValue, _responseQuestion); if (_trace) { Trace.WriteLine($"response : {response.Responses} question {response.Year} - {response.QuestionNumber} (line {value.LineNumber} \"{value.Filename}\")"); } _responseQuestion = null; break; } ControlResponseQuestions(); return(response); }
private void ExportResponseCategory_v2(StreamWriter sw, IEnumerable <Response_v2> responses, string category) { // 2012 // Q102: ABCD SortedDictionary <int, IEnumerator <Response_v2> > years = new SortedDictionary <int, IEnumerator <Response_v2> >(); foreach (Response_v2 response in responses) { if (response.Category != category) { continue; } if (!years.ContainsKey(response.Year)) { years.Add(response.Year, responses.Where(response2 => response2.Year == response.Year && response2.Category == category).GetEnumerator()); } } StringBuilder sb = new StringBuilder(); bool first = true; foreach (int year in years.Keys.Reverse()) { if (!first) { sb.Append(new string(' ', _yearWidth - 5)); } sb.Append($" {year}"); first = false; } sw.WriteLine(); sw.WriteLine(sb.ToString()); sw.WriteLine(); sb.Clear(); int l = 0; while (true) { bool found = false; int lastIndex = -1; int index = -1; foreach (int year in years.Keys.Reverse()) { index++; IEnumerator <Response_v2> yearResponse = years[year]; if (!yearResponse.MoveNext()) { continue; } found = true; Response_v2 response = yearResponse.Current; if (index <= lastIndex) { sw.WriteLine(sb.ToString()); sw.WriteLine(); sb.Clear(); lastIndex = -1; l = 0; } if (index != lastIndex - 1 || l > 0) { sb.Append(new string(' ', (index - lastIndex - 1) * _yearWidth + l)); } string text = $" Q{response.QuestionNumber}: {response.Responses}"; l = _yearWidth - text.Length; sb.Append(text); lastIndex = index; } if (!found) { break; } if (sb.Length > 0) { sw.WriteLine(sb.ToString()); } sw.WriteLine(); sb.Clear(); lastIndex = -1; l = 0; } }