|
|
Регистрация Восстановить пароль |
Повторная активизация e-mail |
Регистрация | Задать вопрос |
Заплачу за решение |
Новые сообщения |
Сообщения за день |
Расширенный поиск |
Правила |
Всё прочитано |
|
Опции темы | Поиск в этой теме |
08.05.2011, 08:52 | #1 |
Новичок
Джуниор
Регистрация: 08.05.2011
Сообщений: 2
|
Regex, регулярные выражения.
Нужна помощь, программа заменяет экспоненциальный вид числа в файле на число с фиксированной точкой и наоборот. Проблема состоит в том, что программа может заменять к примеру 5.5E-1 на 0.55000, но не может 5.5E+1 на 55.00000, хотя принцип замены у 1-го и 2-го примера одинаков.
Вот код: using System; using System.Text.RegularExpressions; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Globalization; namespace lab_8_new { class Program { static void Main(string[] args) { string exp_pattern = @"\d\.\d+[E][+]\d+\b"; string second_exp_pattern = @"\d\.\d+[E][-]\d+\b"; string fixpoint_pattern = @"\d+\.\d{1,4}\b"; string exp_replacement = null; string fixpoint_replacement = null; string[] input = new string[500]; string[] output = new string[500]; StreamReader sr = new StreamReader("1.txt"); int i = 0; while (sr.EndOfStream == false) { input[i] = sr.ReadLine(); Regex find_exp = new Regex(exp_pattern); Match match_exp = find_exp.Match(input[i]); string found = null; while (match_exp.Success) { found = match_exp.Value; string first_found = found; string[] stringSeparators = new string[] { "E+" }; string[] split_found; split_found = found.Split(stringSeparators, StringSplitOptions.None); Regex find_pount = new Regex(@"\."); split_found[0] = find_pount.Replace(split_found[0], @","); double result_found = Convert.ToDouble(split_found[0])*Math.Pow(10, Convert.ToDouble(split_found[1])); exp_replacement = result_found.ToString("F5", CultureInfo.InvariantCulture); Regex rgx = new Regex(first_found); output[i] = rgx.Replace(input[i], exp_replacement); input[i] = output[i]; match_exp = match_exp.NextMatch(); } Regex find_secondexp = new Regex(second_exp_pattern); Match match_secondexp = find_secondexp.Match(input[i]); found = null; while (match_secondexp.Success) { found = match_secondexp.Value; string first_found = found; string[] stringSeparators = new string[] { "E-" }; string[] split_found; split_found = found.Split(stringSeparators, StringSplitOptions.None); Regex find_pount = new Regex(@"\."); split_found[0] = find_pount.Replace(split_found[0], @","); double result_found = Convert.ToDouble(split_found[0]) / Math.Pow(10, Convert.ToDouble(split_found[1])); exp_replacement = result_found.ToString("F5", CultureInfo.InvariantCulture); Regex rgx = new Regex(first_found); output[i] = rgx.Replace(input[i], exp_replacement); input[i] = output[i]; match_secondexp = match_secondexp.NextMatch(); } Regex find_fixpoint = new Regex(fixpoint_pattern); Match match_fixpoint = find_fixpoint.Match(input[i]); found = null; while (match_fixpoint.Success) { found = match_fixpoint.Value; string first_found = found; Regex find_pount = new Regex(@"\."); found = find_pount.Replace(found, @","); double result_found = Convert.ToDouble(found); fixpoint_replacement = result_found.ToString("E4", CultureInfo.InvariantCulture); Regex rgx = new Regex(first_found); output[i] = rgx.Replace(input[i], fixpoint_replacement); input[i] = output[i]; match_fixpoint = match_fixpoint.NextMatch(); } if (found == null) { output[i] = input[i]; } i++; } sr.Close(); StreamWriter sw = new StreamWriter("1.txt"); for (int j = 0; j < i; j++) { sw.WriteLine(output[j]); } sw.Close(); } } } |
08.05.2011, 13:13 | #2 |
Старожил
Регистрация: 19.08.2009
Сообщений: 2,119
|
программа заменяет экспоненциальный вид числа в файле на число с фиксированной точкой
var s = d.ToString("F5", NumberFormatInfo.InvariantInfo); а это чем хуже?
А вы почему со мной не соглашаетесь, у вас что, импотенция? (c) ACE Valery
|
08.05.2011, 14:16 | #3 |
Новичок
Джуниор
Регистрация: 08.05.2011
Сообщений: 2
|
А разница то. проблема всё та же
output[i] = rgx.Replace(input[i], exp_replacement); заместо exp_replacement поставь хоть что всё равно не заменяет, но правда только в случае с экспоненциальным видом числа положительной степени, а осли же отриц-я степень то та же output[i] = rgx.Replace(input[i], exp_replacement); без проблем работает. |
Похожие темы | ||||
Тема | Автор | Раздел | Ответов | Последнее сообщение |
регулярные выражения | Farrel | PHP | 3 | 06.02.2011 18:43 |
регулярные выражения | Namolem | Общие вопросы по Java, Java SE, Kotlin | 0 | 03.05.2010 08:08 |
Регулярные выражения | Syltan | JavaScript, Ajax | 3 | 24.04.2010 23:08 |
Регулярные выражения | Marsik | Помощь студентам | 4 | 14.11.2009 14:13 |
Регулярные выражения | Adamant | Общие вопросы C/C++ | 1 | 26.11.2007 19:44 |