-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp1002.cpp
More file actions
50 lines (46 loc) · 832 Bytes
/
Copy pathp1002.cpp
File metadata and controls
50 lines (46 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int main(){
char str[500], alp;
int isleft, coe, con, sgn, num;
cin>> str;
isleft = 1;
coe = con = 0;
sgn = 1;
for(int i = 0; i < strlen(str); i++){
num = 0;
while(isdigit(str[i])){
num *= 10;
num += str[i] - '0';
i++;
}
if(i < strlen(str) && isalpha(str[i])){
alp = str[i];
if(!isleft)
sgn = -sgn;
if(num == 0)
num = 1;
coe += sgn * num;
// cout<< "coe=" << coe << endl;
}else{
if(isleft)
sgn = -sgn;
con += sgn * num;
// cout<< "con=" << con << endl;
}
if(str[i] == '+'){
sgn = 1;
}else if(str[i] == '-'){
sgn = -1;
}else if(str[i] == '='){
isleft = 0;
sgn = 1;
}
}
if(con == 0)
coe = fabs(coe);
printf("%c=%.3f", alp, con / (coe * 1.0));
}