forked from aqingsao/length
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
158 lines (131 loc) · 3.06 KB
/
main.cpp
File metadata and controls
158 lines (131 loc) · 3.06 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include<iostream>
#include<string>
#include <sstream>
#include<fstream>
#include<iomanip>
using namespace std;
char name[6][8]; //各个单位的名称
float zhl[6]; //各个单位的转换率存储在此数组中
float result[10] ; //10个结果
void read(); //从文件读入并计算
void write(); //写入到文件
int isOperator(string str); //判断是否是运算符,加为7,减为8
int isUnit(string str); //判断单位
float str2float(string str); //string转float
int main()
{
read(); //读取数据
write(); //写入数据
return 0;
}
void read()
{
int num; //读取不需要的数字
char str[10]; //读取不需要的字符串
ifstream infile("input.txt");
if(!infile)
{
cerr << "open error!" << endl;
exit(1);
}
//将文件前6行读入到数组中
for(int i = 0;i < 6;i++)
{
infile >> num; //读取第一个数字
infile >> name[i]; //读取单位名字
infile >> str; //读取不需要的字符,“=”
infile >> zhl[i]; //单位转换率
infile >> str; //读取不需要的字符,“m”
}
//读取需要计算的部分,并加以计算
float tmp; //计算存储数字
string cal; //要计算的字符串(部分)
string line; //要计算的字符串(行)
int statius1 = 0,statius2 = 0; //是否换行的标志(运算符,单位)
int l = 0; //第几行
int zf = 1; //判断加减
while( !(infile.eof()) )
{
infile >> cal;
if(!isUnit(cal) && !isOperator(cal)) //判断是否为数字
{
statius1 = statius2;
statius2 = 1;
tmp = str2float(cal);
if(statius1 == 2 && statius2 == 1) //换行标志(单位2,数字1)
{
l++;
zf=1; //换行后符号要重置
}
continue;
}
if(isUnit(cal))
{
statius1 = statius2;
statius2 = 2;
result[l] += tmp * zhl[isUnit(cal)-1] *zf;
continue;
}
if(isOperator(cal))
{
statius1 = statius2;
statius2 = 3;
if(isOperator(cal)-7)
zf = -1;
else
zf = 1;
continue;
}
}
infile.close();
}
void write()
{
ofstream outfile("output.txt");
if(!outfile)
{
cerr << "open output.txt error!" << endl;
exit(1);
}
//输出结果
outfile << "447407567@qq.com" << endl; //第1行是您在渣打编程马拉松官网上报名时的注册邮箱
//第2行是空行
//输出计算结果
for(int i = 0 ; i < 10 ; i++)
outfile << endl << setiosflags(ios::fixed) << setprecision(2) << result[i] << " m"; //从上一行处输出,本次输出不换行
//最后一行不需要换行
outfile.close();
}
int isOperator(string str)
{
if(str == "+")
return 7;
if(str == "-")
return 8;
return 0;
}
int isUnit(string str)
{
if((str == "mile") || (str == "miles"))
return 1;
if((str == "yard") || (str == "yards"))
return 2;
if((str == "inch") || (str == "inches"))
return 3;
if((str == "foot") || (str == "feet"))
return 4;
if((str == "fath") || (str == "faths"))
return 5;
if((str == "furlong") || (str == "furlongs"))
return 6;
else return 0;
}
float str2float(string str)
{
stringstream sstr(str);
float num;
if((sstr >> num).fail())
{ //ERROR
}
return num;
}