Created
October 3, 2011 04:02
-
-
Save johnhmj/1258413 to your computer and use it in GitHub Desktop.
telnet://ptt.cc C/C++ 18007 jolinsens
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Integrated Development Environment | |
| // Visual C++ | |
| #include <iostream> | |
| #include <cstdlib> | |
| #include <string> | |
| #include <sstream> | |
| #include <vector> | |
| #include <algorithm> | |
| // | |
| typedef std::vector<std::string> vStr; | |
| void converMachNo(std::string& s, std::string& sMachs); | |
| // | |
| // 主函式 | |
| int main(int argc, char** argv) | |
| { | |
| std::string strin = "c23456,b12345,d34567,a01234", strout; | |
| converMachNo(strout, strin); | |
| system("PAUSE"); | |
| return (0); | |
| } | |
| void converMachNo(std::string& s, std::string& sMachs) | |
| { | |
| for (unsigned int i = 0; i < sMachs.size(); i ++) | |
| { | |
| if (sMachs[i] == ',') | |
| { | |
| sMachs[i] = ' '; | |
| } | |
| } | |
| std::stringstream ss(sMachs); | |
| vStr forsorting; | |
| std::string temp; | |
| while (ss >> temp) | |
| { | |
| forsorting.push_back(temp); | |
| } | |
| sort(forsorting.begin(), forsorting.end()); | |
| int num1, num2, count = 1; | |
| sMachs.clear(); | |
| sMachs = forsorting[0]; | |
| // | |
| //字串轉整數,建議另寫函式 | |
| num1 = atoi(forsorting[0].substr(3).c_str()); | |
| for(unsigned int i = 1; i < forsorting.size(); i++) | |
| { | |
| num2 = atoi(forsorting[i].substr(3).c_str()); | |
| if((num2 - num1) != 1) | |
| { | |
| if(count != 1) | |
| sMachs += ('-' + forsorting[i - 1] + ',' + forsorting[i]); | |
| else | |
| sMachs += (',' + forsorting[i]); | |
| count = 0; | |
| } | |
| num1 = num2; | |
| count++; | |
| } | |
| if(count != 1) | |
| sMachs += ('-' + forsorting[forsorting.size() - 1]); | |
| s = sMachs; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class strsorting | |
| { | |
| String converMachNo(String sMachs) | |
| { | |
| //以逗號分割字串,並儲存至字串陣列 | |
| //strtok | |
| String [] temp = sMachs.split(","); | |
| //串列:字串 | |
| //STL list, vector | |
| ArrayList<String> forsorting = new ArrayList<String>(); | |
| //新增至串列 | |
| for(int i = 0; i < temp.length;i++) | |
| forsorting.add(temp[i]); | |
| //排序串列的所有元素 | |
| //STL sort | |
| Collections.sort(forsorting); | |
| //將所有元素轉換成字串陣列,再指定給temp | |
| temp = forsorting.toArray(new String[0]); | |
| int num1, num2, count =1; | |
| //指定給原字串變數 | |
| sMachs = temp[0]; | |
| //自第四個字元起取出子字串,轉換成整數 | |
| num1 = Integer.parseInt(temp[0].substring(3)); | |
| for(int i = 1; i < temp.length; i++) | |
| { | |
| num2 = Integer.parseInt(temp[i].substring(3)); | |
| if((num2 - num1) != 1) | |
| { | |
| if(count != 1) | |
| sMachs += ("-" + temp[i-1] + "," + temp[i]); | |
| else | |
| sMachs += ("," + temp[i]); | |
| count = 0; | |
| } | |
| num1 = num2; | |
| count++; | |
| } | |
| if(count != 1) | |
| sMachs += ("-" + temp[temp.length - 1]); | |
| return sMachs; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment