杂谈

与魔鬼战斗的人,应当小心自己不要成为魔鬼。当你凝视深渊时,深渊也在凝视着你。

善恶的彼岸尼采

一时心血来潮,写了个多项式计算器。一方面是对于表弟的要求,另一方面也许就是因为自己太过于无聊了。所以你们看见了,写了个这么无聊的东东,其实自己的C++水平也不是很高,但是还是在这里不断的尝试。在今天下午和昨天的建造Blog的过程中也有一个感觉就是。这个网站从一开始就不可能被百度等搜索引擎所收录,我在这里打的东西可能都只有自己能够看到。所以,对于自己所做的还是有些怀疑的吧。怀疑这样做的一个意义。但是下午一下也就明白了,尽管只有一个人看见有有何不可。就当是自己的一个垃圾箱吧。嗯,就这样了。

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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include<iostream>
#include<cstdio>
#include<cctype>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
void eatspaces(char* str);//去除输入中的空格,保持输入的准确性。
double number(char* str,int & index);//用于将字符串(str)类型的数据转换为数值型
double term(char* str, int & index);
double expr(char* str);
char* extract(char* str, int& index);

const int MAX = 800;

char* extract(char* str, int& index)
{
char* pstr(nullptr);
int numl(0);
int bufindex(index);

do
{
switch(*(str + index))
{
case ')':
if(0 == numl)
{
++index;
pstr = new char[index - bufindex];
if(!pstr)
{
throw "Memory allowcation failed.";
}
strncpy_s(pstr, index-bufindex, str+bufindex, index-bufindex-1);

return pstr;
}
else
numl--;
break;
case '(':
numl++;
break;
}
}while(*(str + index++) != '\0');
throw "ran off the end of the expression, must be bad input.";
}
double number(char* str, int & index)
{
double value(0.0);

if(*(str + index) = '(')
{
char* psubstr(nullptr);
psubstr = extract(str, ++index);
value = expr(psubstr);
delete[]psubstr;
return value;
}

if(!isdigit(*(str + index)))
{
char message[31] = "Invalid character in number:";
strncat_s(message, str + index, 1);
throw message;
}

while(isdigit(*(str + index)))
value = 10*value + (*(str + index++) -'0');

if(*(str + index) != '.')//实数的计算,用小数点作为一个区分的条件
return value; //如果不是的话就直接返回数字

double factor(1.0);

while(isdigit(*(str + (++index))))
{
factor *= 0.1;
value = value + (*(str + index) - '0')*factor;
}

return value;
}
double number_two(char* str, int & index)
{
double value(0.0);
if(!isdigit(*(str + index)))
{
char message[31] = "Invalid character in number:";
strncat_s(message, str + index, 1);
throw message;
}

while(isdigit(*(str + index)))
value = 10*value + (*(str + index++) -'0');

if(*(str + index) != '.')//实数的计算,用小数点作为一个区分的条件
return value; //如果不是的话就直接返回数字

double factor(1.0);

while(isdigit(*(str + (++index))))
{
factor *= 0.1;
value = value + (*(str + index) - '0')*factor;
}

return value;
}
//应该没问题了 函数收起
double term(char* str,int & index)
{
double value = 0.0;

value = number(str, index); //获取这个字符串中的第一个字符

while(true)
{
if(*(str + index) == '+') //加法处理
value *= number(str, ++index);
else if(*(str + index) == '/')//除法处理 必要时用以高精度运算
value /= number(str, ++index);
else
break;//一直循环知道最后一个不属于这些的项
}
return value;
}
double expr(char* str)
{
double value = 0.0;
int index = 0;

value = term(str, index);
for(;;)
{
switch(*(str + index++))
{
case '\0':
return value;

case '+':
value += term(str, index);
break;

case '-':
value -= term(str, index);
break;

default:
char message[38] = "你的输入有错。错误为:";
strncat_s(message, str + index - 1,1);//加入错误throw
throw message;
break;
}
}

}
void eatspaces(char* str)
{
int i(0);
int j(0);

while((*(str + i) = *(str + j++)) != '\0')
if(*(str + i) != ' ')
i++;
return;
}
int main()
{
char buffer[MAX] = {0};
cout<<endl
<<"欢迎使用"
<<endl
<<"输入一个字符串,或是一个空行退出。"
<<endl;
for(;;)
{
cin.getline(buffer, sizeof buffer);
eatspaces(buffer);

if(!buffer[0])
return 0;
try
{
cout << "\t= "<< expr(buffer)
<< endl << endl;
}
catch( const char* pEx)
{
cerr << pEx << endl;
cerr << "Ending progream." << endl;
return 1;
}
}
}

既然看了33写的文章,不打算投喂一下再走吗?哼!