UVAOJ 537字符串处理
2013-09-05 20:17
411 查看
注意有小数点的字符串的处理
熟悉字符串操作
熟悉字符串操作
#include <iostream> #include <string> #include <vector> #include <cstdio> #include <cstdlib> #include <cctype> #include <cmath> #include <iomanip> using namespace std; double getNum(string k){ int find = k.find("."); double res =0, xs =0; int cnt = (find==-1)?k.size():find; int xnt = (find==-1)?0:(k.size()-find); // cout<<"k:"<<k<<"find:"<<find<<endl; for(int i=0; i<cnt;i++) res = res*10+(k[i] - '0'); for(int j=1; j<xnt;j++) xs = xs*10+(k[find+j] - '0'); return res + xs* pow(10.0, -1.0*(k.size()-1-find)); } double isP(string str, int k){ double fg = 0; int i; string num; for(i=k; i<str.size();i++){ if(str[i] == 'A' || str[i] == 'V' || str[i] == 'W') break; if(isdigit(str[i])||str[i]=='.') num = num + str[i]; } char c = str[i-1]; if(c=='m') fg = 0.001; else if(c=='k') fg = 1000; else if(c=='M') fg = 1000000; else fg = 1; return fg*getNum(num); } void pro(string str){ int p = str.find("P="); int q = str.find("U="); int r = str.find("I="); double pi = (p!=-1)?isP(str, p):0; double qi = (q!=-1)?isP(str, q):0; double ri = (r!=-1)?isP(str, r):0; // cout<<pi<<" "<<qi<<" "<<ri<<endl; if(p!=-1 && q!=-1){ printf("I=%.2fA\n", pi/qi); }else if(p!=-1 && r!=-1){ printf("U=%.2fV\n", pi/ri); }else if(q!=-1 && r!=-1){ printf("P=%.2fW\n", qi*ri); } } int main(){ //freopen("537.txt", "r", stdin); vector<string> re; int totl, k=0; string res; cin>>totl; cin.ignore(); while(k++ < totl){ getline(cin, res); re.push_back(res); } for(int i=0; i<totl;i++){ cout<<"Problem #"<<(i+1)<<endl; pro(re[i]); cout<<endl; } //fclose(stdin); //freopen("CON", "r", stdin); // system("pause"); return 0; }
相关文章推荐
- uva 537 Artificial Intelligence?(字符串处理)
- Artificial Intelligence? - UVa 537 字符串处理
- 数据清洗小记:分类进行字符串转日期(某日期字段存在多类格式处理)
- 批处理学习--字符串的扩充
- 【C】一些字符串处理函数
- 面试OR笔试18——哈希表处理字符串
- shell中字符串的处理
- C#字符串处理:截取、替换、移除
- Python字符串处理
- C#温故而知新学习系列之字符串处理—指定字符串的显示格式(一)
- c# 字符串常用的处理方法
- [WIKIOI1975]化学方程式(字符串处理+线性方程组)
- 正则表达式处理字符串指定位置插入【高级】
- A.5.2-C# 中对字符串(string)的常用处理
- 字符串处理函数
- C# 字符串,数组,日期常遇到的处理方法(持续更新)
- 「粗」bold() //使用粗体显示字符串 20140808 ①文本处理
- C# 字符串,数组,日期常遇到的处理方法(持续更新)
- VC++下处理UTF8编码的字符串
- C 语言字符串处理函数实现合集