求教将C语言转化python!!
usingnamespacestd;typedeflonglongll;constintmaxn=1e4+5;constintmod=1e9+7;constintINF=...
using namespace std;
typedef long long ll;
const int maxn = 1e4+5;
const int mod = 1e9+7;
const int INF = 1<<30;
struct node
{
string section, key, value;
node(string s="", string k="", string v="")
{
section = s;
key = k;
value = v;
}
bool operator<(const node& n) const
{
if(section != n.section)
return section<n.section;
return key<n.key;
}
}in[maxn];
void trim(string& s)
{
s.erase(s.find_last_not_of(" ") + 1);
s.erase(0, s.find_first_not_of(" "));
}
int main( )
{
//freopen("input.txt", "r", stdin);
char ch;
string preSection, line;
int num = 0, len;
while(getline(cin, line))
{
trim(line);
if(line == "" || line[0]==';')
continue;
ch = line[0];
len = line.length();
if(ch=='[')
{
preSection = "";
for(int i=1; i<len-1; i++)
{
if(line[i] == ';' || line[i]==']')
break;
preSection += line[i];
}
trim(preSection);
preSection = "{" + preSection + "}";
}
else
{
string curKey = "", curValue = "";
int i;
for(i=0; i<len; i++)
{
if(line[i] == '=')
break;
curKey += line[i];
}
i++;
for(; i<len; i++)
{
if(line[i] == ';')
break;
curValue += line[i];
}
trim(curKey);
curKey = "{" + curKey + "}";
trim(curValue);
curValue = "{" + curValue + "}";
in[num++] = node(preSection, curKey, curValue);
}
}
sort(in, in+num);
for(int i=0; i<num; i++)
cout<<in[i].section<<in[i].key<<in[i].value<<endl;
return 0;
} 展开
typedef long long ll;
const int maxn = 1e4+5;
const int mod = 1e9+7;
const int INF = 1<<30;
struct node
{
string section, key, value;
node(string s="", string k="", string v="")
{
section = s;
key = k;
value = v;
}
bool operator<(const node& n) const
{
if(section != n.section)
return section<n.section;
return key<n.key;
}
}in[maxn];
void trim(string& s)
{
s.erase(s.find_last_not_of(" ") + 1);
s.erase(0, s.find_first_not_of(" "));
}
int main( )
{
//freopen("input.txt", "r", stdin);
char ch;
string preSection, line;
int num = 0, len;
while(getline(cin, line))
{
trim(line);
if(line == "" || line[0]==';')
continue;
ch = line[0];
len = line.length();
if(ch=='[')
{
preSection = "";
for(int i=1; i<len-1; i++)
{
if(line[i] == ';' || line[i]==']')
break;
preSection += line[i];
}
trim(preSection);
preSection = "{" + preSection + "}";
}
else
{
string curKey = "", curValue = "";
int i;
for(i=0; i<len; i++)
{
if(line[i] == '=')
break;
curKey += line[i];
}
i++;
for(; i<len; i++)
{
if(line[i] == ';')
break;
curValue += line[i];
}
trim(curKey);
curKey = "{" + curKey + "}";
trim(curValue);
curValue = "{" + curValue + "}";
in[num++] = node(preSection, curKey, curValue);
}
}
sort(in, in+num);
for(int i=0; i<num; i++)
cout<<in[i].section<<in[i].key<<in[i].value<<endl;
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询