UVA - 537 Artificial Intelligence?

来源:互联网 发布:java应用前景 编辑:程序博客网 时间:2024/04/29 10:23

题目大意:从字符串中读取信息,然后根据公式计算

解题思路:从字符串中提取信息,并保存和标记,然后在根据公式进行计算

#include<cstdio>#include<cmath>#include<cstring>double find_n(char *str, int i) {int count = 0;double recode[20];double times = 1;double  ren_n = 0;int number = 0;int mark = 0;double sign = 1;int start = 2;if(str[i+2] == '-') { sign = -1;start++;}for(int j = i + start; ; j++) {if(str[j] >= '0' && str[j] <= '9') {recode[count] = str[j] - '0';count++;if(mark == 1) number--;}if(str[j] == '.'){mark = 1;continue;}if(str[j] == 'k'){times = 1000;break;}if(str[j] == 'M') {times = 1000000;break;}if(str[j] == 'm') {times = 0.001;break;}if(str[j] == 'A' || str[j] == 'V' || str[j] == 'W')break;}int j;double k;for( j = count - 1 , k = pow(10,number); j >= 0; j--, k = k * 10) {ren_n = ren_n + recode[j] * k;}return ren_n * times * sign;}void put(double *num, int *mark, int count) {double end;if(mark[0] == 1 && (mark[1] == 2 || mark[1] == 3))end = num[0] / num[1];if(mark[1] == 1 && (mark[0] == 2 || mark[0] == 3))end = num[1] / num[0];if( (mark[1] == 2 && mark[0] == 1) || (mark[0] == 2 && mark[1] == 1))printf("Problem #%d\nI=%.2fA\n\n", count, end);if( (mark[1] == 3 && mark[0] == 1) || (mark[0] == 3 && mark[1] == 1))printf("Problem #%d\nU=%.2fV\n\n", count, end);if((mark[0] == 2 && mark[1] == 3) || (mark[1] == 2 && mark[0] == 3))printf("Problem #%d\nP=%.2fW\n\n", count, num[1] * num[0]);}int main() {int mark[2];int number;int send = 0;char str[100];double  num[2];int count ;scanf("%d\n",&number);for(int L= 1; L <= number; L++) { gets(str);count = 0;for(int i = 0; i < strlen(str); i++) {if(str[i + 1] == '=' && (str[i] == 'U' || str[i] == 'I' || str[i] == 'P') ) {if(str[i] == 'U' ) mark[count] = 2;if(str[i] == 'I' )mark[count] = 3;if(str[i] == 'P'  )mark[count] = 1;num[count] = find_n(str,i -1);count++;}}put(num,mark,L);}return 0;}


0 0
原创粉丝点击