2018-04-20 17:12:47

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<string>
#include<sstream>
#include<climits>
#define MAXN 10001
using namespace std;
int vis(string); //判断类型
string xs(string);  //小数
string bf(string);  //百分数
string fs(string);  //分数
string zs(string);  //整数

int main(){
string s;
cin>>s;
if(vis(s)==1){
cout<<zs(s)<<endl;
return 0;
}
if(vis(s)==2){
cout<<xs(s)<<endl;
return 0;
}
if(vis(s)==4){
cout<<bf(s)<<endl;
return 0;
}
if(vis(s)==3){
cout<<fs(s)<<endl;
return 0;
}
}
int vis(string x){ //判断是什么类型
for(int i=0;i<x.size();++i){
if(x[i]<='9'&&x[i]>='0') continue;
if(x[i]=='.') return 2; //读到.返回2
if(x[i]=='/') return 3;//读到/返回3
if(x[i]=='%') return 4;//读到%返回4
}
return 1;  //都没有就是整数
}

string xs(string x){
string x1,x2,ans;
int tmp=MAXN; //赋值一个很大得数判断是不是只有零
x1=x.substr(0,x.find('.')); //截取0到.的字符串
x2=x.substr(x.find('.')+1,x.size());
reverse(x1.begin(),x1.end()); //STL反转函数
reverse(x2.begin(),x2.end());
for(int i=0;i<x1.size();++i){
if(x1[i]=='0') continue; //去前导零
else{
tmp=i;
break;
}
}
if(tmp==MAXN) x1="0",tmp=0;
x1=x1.substr(tmp,x1.size()); //截取
tmp=MAXN;
for(int i=x2.size()-1;i>=0;--i){ //去前导零
if(x2[i]=='0') continue;
else{
tmp=i;
break;
}
}
if(tmp==MAXN) x2="0";
x2=x2.substr(0,tmp+1);//截取
ans=x1+'.'+x2;
return ans;
}
string bf(string x){
string x1,ans;
int tmp;
x1=x.substr(0,x.find('%'));
reverse(x1.begin(),x1.end());
for(int i=0;i<x1.size();++i){//去前导零
if(x1[i]=='0') continue;
else{
tmp=i;
break;
}
}
x1=x1.substr(tmp,x1.size());//截取
ans=x1+'%';
return ans;
}
string fs(string x){
string x1,x2,ans;
int tmp=MAXN;
x1=x.substr(0,x.find('/'));
x2=x.substr(x.find('/')+1,x.size());
reverse(x1.begin(),x1.end());
reverse(x2.begin(),x2.end());
for(int i=0;i<x1.size();++i){ //去前导零
if(x1[i]=='0') continue;
else{
tmp=i;
break;
}
}
if(tmp==MAXN) x1="0",tmp=0;
x1=x1.substr(tmp,x1.size());
for(int i=x1.size()-1;i>=0;--i){ //去后面的零
if(x1[i]=='0') continue;
else{
tmp=i;
break;
}
}
x1=x1.substr(0,tmp+1);
for(int i=x2.size()-1;i>=0;--i){//去前导零
if(x2[i]=='0') continue;
else{
tmp=i;
break;
}
}
x2=x2.substr(0,tmp+1);
for(int i=0;i<x2.size();++i){//去后面的零
if(x2[i]=='0') continue;
else{
tmp=i;
break;
}
}
x2=x2.substr(tmp,x2.size());
ans=x1+'/'+x2;
return ans;
}
string zs(string x){
int tmp=MAXN;
reverse(x.begin(),x.end());
for(int i=0;i<x.size();++i){ //去前导零
if(x[i]=='0') continue;
else{
tmp=i;
break;
}
}
if(tmp==MAXN) x="0",tmp=0;
x=x.substr(tmp,x.size());
return x;
}

• star
首页