Наши проекты:
Журнал · Discuz!ML · Wiki · DRKB · Помощь проекту |
||
ПРАВИЛА | FAQ | Помощь | Поиск | Участники | Календарь | Избранное | RSS |
[18.224.33.107] |
|
Сообщ.
#1
,
|
|
|
В строке задаю числа в виде диапазонов и отдельных чисел, например "0, 1, 5 - 7, 8 - 12, 14- 16, 20-29, 35, 76". Нужно перевести строку в ряд числе с учетом заданных диапазонов и отдельных чисел. Программа:
using System; using System.Collections.Generic; using System.Text; namespace CsStudyCons { class Program { public static void StringProcessing(String str) { if(String.IsNullOrEmpty(str)) { Console.WriteLine("Cтрока не может быть преобразована"); Environment.Exit(1); } // разделяем строку Char delim = ','; str = str.Trim(); String [] words = str.Split(delim); foreach (String s in words) { Console.WriteLine(s); } Console.WriteLine("-----------------------------"); // удаляем пробелы в массиве for (Int32 i = 0; i < words.Length; i++) { String st = ""; for (Int32 j = 0; j < words[i].Length; j++) { if(words[i][j] == ' ') continue; else st += words[i][j]; } words[i] = st; Console.WriteLine(words[i]); } Console.WriteLine("-----------------------------"); // Формируем список цисел и разделителей "-" List<String> sList = new List<String>(); for (Int32 i = 0; i < words.Length; i++) { Int32 nS = words[i].IndexOf('-'); if(nS == -1) sList.Add(words[i]); else { sList.Add(words[i].Substring(0, nS)); sList.Add("-"); sList.Add(words[i].Substring(nS + 1, words[i].Length - nS - 1)); } } foreach (String s1 in sList) { Console.WriteLine(s1); } Console.WriteLine("-----------------------------"); // Формируем список цисел List<Int32> nList = new List<Int32>(); for (Int32 i = 0; i < sList.Count; i++) { if (sList[i] == "-") { for (Int32 j = Int32.Parse(sList[i - 1]) + 1; j < Int32.Parse(sList[i + 1]); j++) { nList.Add(j); } } else { nList.Add(Int32.Parse(sList[i])); } } foreach (Int32 n in nList) { Console.WriteLine(n); } } //void StringProcessing static void Main(string[] args) { String str = "0, 1, 5 - 7, 8 - 12, 14- 16, 20-29, 35, 76"; StringProcessing(str); Console.ReadKey(); } } // class Program } // namespace CsStudyCons Прикреплённый файлCsStudyCons.zip (2,76 Кбайт, скачиваний: 88) |
Сообщ.
#2
,
|
|
|
Почитать о RegEx (регулярных выражениях), возможно применить в своем коде, вместо него.
|
Сообщ.
#3
,
|
|
|
Если вместо строки "1,3, 11,15-17, 22-27, 51 ,52" надо получить строку "1 3 11 15 16 17 22 23 24 25 26 27 51 52", то вот так работает:
void tuchin() { char *str="1,3, 11,15-17, 22-27, 51 ,52"; for( int t=0,next,prev; str[t]; ) { while( str[t]==',' || str[t]==' ' ) t++; next = atoi(str+t); if( next>0 ) { printf("%d ",prev=next); while( str[t]>='0' && str[t]<='9' ) t++; } else { while( ++prev<=-next ) printf("%d ",prev); while( str[t]>='-' && str[t]<='9' ) t++; } } } Добавлено Эх, виноват. Всякие пробелы чуть испортили картинку. Так ужо работает и с моим примером и с примером автора: void tuchin() { char *str=//"0, 1, 5 - 7, 8 - 12, 14- 16, 20-29, 35, 76";// "1,3, 11,15-17, 22-27, 51 ,52"; for( int t=0,next,prev; str[t]; ) { while( str[t]==',' || str[t]==' ' ) t++; next = str[t]!='-' ? atoi(str+t) : -atoi(str+t+1); if( next>=0 ) { printf("%d ",prev=next); while( str[t]>='0' && str[t]<='9' ) t++; } else { while( ++prev<=-next ) printf("%d ",prev); t++; while( (str[t]>='0' && str[t]<='9') || str[t]==' ' ) t++; } } } |
Сообщ.
#4
,
|
|
|
Спасибо за ответ. Код на C получился коротким, попробую его перевести на C#.
|