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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
| import telebot
from telebot import types
from fractions import Fraction
import sympy as sp
bot = telebot.TeleBot('Мой токен')
user_data = {}
@bot.message_handler(commands=['start'])
def start(message):
markup = types.InlineKeyboardMarkup()
markup.add(types.InlineKeyboardButton(text='Тригонометрические задачи', callback_data='Z'))
markup.add(types.InlineKeyboardButton(text='Тригонометрические уравнения', callback_data='U'))
bot.send_message(message.chat.id, 'Привет! Выбери какой тип задач тебе нужно решить:', reply_markup=markup)
@bot.callback_query_handler(func=lambda call: call.data == 'Z')
def choose_interval(call):
message = call.message
chat_id = message.chat.id
markup2 = types.InlineKeyboardMarkup()
btn1 = types.InlineKeyboardButton(text='0<x<π/2 (1 Ч)', callback_data='1')
btn2 = types.InlineKeyboardButton(text='π/2<x<π (2 Ч)', callback_data='2')
markup2.row(btn1, btn2)
btn3 = types.InlineKeyboardButton(text='π<x<3π/2 (3 Ч)', callback_data='3')
btn4 = types.InlineKeyboardButton(text='3π/2<x<2π (4 Ч)', callback_data='4')
markup2.row(btn3, btn4)
bot.edit_message_text(chat_id=chat_id, message_id=message.message_id, text='Выбери чему принадлежит x:', reply_markup=markup2)
@bot.callback_query_handler(func=lambda call: call.data in ['1', '2', '3', '4'])
def choose_function(call):
message = call.message
chat_id = message.chat.id
user_data[chat_id] = {'interval': call.data}
markup1 = types.InlineKeyboardMarkup()
btn1 = types.InlineKeyboardButton(text='sinx', callback_data='sinx')
btn2 = types.InlineKeyboardButton(text='cosx', callback_data='cosx')
markup1.row(btn1, btn2)
btn3 = types.InlineKeyboardButton(text='tgx', callback_data='tgx')
btn4 = types.InlineKeyboardButton(text='ctgx', callback_data='ctgx')
markup1.row(btn3, btn4)
bot.edit_message_text(chat_id=chat_id, message_id=message.message_id, text='Выбери какая функция в дано:', reply_markup=markup1)
@bot.callback_query_handler(func=lambda call: call.data in ['sinx', 'cosx', 'tgx', 'ctgx'])
def ask_value(call):
message = call.message
chat_id = message.chat.id
user_data[chat_id]['function'] = call.data
bot.send_message(chat_id, 'А теперь напиши чему равна эта функция, если число с корнем то напиши как в примере [K(3)/2], если число целое то напиши дробью, ну а если в знаменателе есть корень то убери корень из знаменателя [1/K(3)=K(3)/3], минус ставить не надо')
@bot.message_handler(func=lambda message: True)
def echo_message(message):
chat_id = message.chat.id
if chat_id in user_data and 'function' in user_data[chat_id]:
try:
value = message.text
interval = user_data[chat_id]['interval']
func = user_data[chat_id]['function']
if func == 'sinx':
sin = value
def process_fraction(fraction_str):
def process_part(part):
part_cleaned = part.replace('К', '').replace('K', '').replace('(', '').replace(')', '')
if 'К' in part or 'K' in part:
return int(part_cleaned)
else:
return int(part_cleaned) ** 2
num, den = fraction_str.split('/')
numerator_processed = process_part(num)
denominator_processed = process_part(den)
return numerator_processed, denominator_processed
num0, den0 = process_fraction(sin)
sinx = Fraction(num0, den0)
cos = 1 - sinx
c = Fraction(cos)
num1 = c.numerator
den1 = c.denominator
expression = sp.sqrt(sp.Rational(num1, den1))
cosx = sp.simplify(expression)
cosk = str(cosx).replace('sqrt', 'К')
tg = sinx / cosx ** 2
t = Fraction(tg)
num2 = t.numerator
den2 = t.denominator
kort = sp.sqrt(sp.Rational(num2, den2))
tgx = sp.simplify(kort)
tgk = str(tgx).replace('sqrt', 'К')
ctg = 1 / tgx
ctgk = str(ctg).replace('sqrt', 'К')
if interval == '1':
bot.send_message(chat_id, f"sinx = {value}\ncosx = {cosk}\ntanx = {tgk}\nctgx = {ctgk}")
elif interval == '2':
bot.send_message(chat_id, f"sinx = {value}\ncosx = -{cosk}\ntanx = -{tgk}\nctgx = -{ctgk}")
elif interval == '3':
bot.send_message(chat_id, f"sinx = -{value}\ncosx = -{cosk}\ntanx = {tgk}\nctgx = {ctgk}")
elif interval == '4':
bot.send_message(chat_id, f"sinx = -{value}\ncosx = {cosk}\ntanx = -{tgk}\nctgx = -{ctgk}")
elif func == 'cosx':
cos = value
def process_fraction(fraction_str):
def process_part(part):
part_cleaned = part.replace('К', '').replace('K', '').replace('(', '').replace(')', '')
if 'К' in part or 'K' in part:
return int(part_cleaned)
else:
return int(part_cleaned) ** 2
num, den = fraction_str.split('/')
numerator_processed = process_part(num)
denominator_processed = process_part(den)
return numerator_processed, denominator_processed
num0, den0 = process_fraction(cos)
cosx = Fraction(num0, den0)
sin = 1 - cosx
s = Fraction(sin)
num1 = s.numerator
den1 = s.denominator
expression = sp.sqrt(sp.Rational(num1, den1))
sinx = sp.simplify(expression)
sink = str(sinx).replace('sqrt', 'К')
ctg = cosx / (sinx ** 2)
kort = sp.sqrt(ctg)
ctgx = sp.simplify(kort)
ctgk = str(ctgx).replace('sqrt', 'K')
tg = 1 / ctgx
tgk = str(tg).replace('sqrt', 'К')
if interval == '1':
bot.send_message(chat_id, f"sinx = {sink}\ncosx = {value}\ntanx = {tgk}\nctgx = {ctgk}")
elif interval == '2':
bot.send_message(chat_id, f"sinx = {sink}\ncosx = -{value}\ntanx = -{tgk}\nctgx = -{ctgk}")
elif interval == '3':
bot.send_message(chat_id, f"sinx = -{sink}\ncosx = -{value}\ntanx = {tgk}\nctgx = {ctgk}")
elif interval == '4':
bot.send_message(chat_id, f"sinx = -{sink}\ncosx = {value}\ntanx = -{tgk}\nctgx = -{ctgk}")
elif func == 'tgx':
tg = value
def process_fraction(fraction_str):
def process_part(part):
part_cleaned = part.replace('К', '').replace('K', '').replace('(', '').replace(')', '')
if 'К' in part or 'K' in part:
return int(part_cleaned)
else:
return int(part_cleaned) ** 2
num, den = fraction_str.split('/')
numerator_processed = process_part(num)
denominator_processed = process_part(den)
return numerator_processed, denominator_processed
num0, den0 = process_fraction(tg)
tgx = Fraction(num0, den0)
ctg = 1 / tgx
ct = Fraction(ctg)
num1 = ct.numerator
den1 = ct.denominator
expression = sp.sqrt(sp.Rational(num1, den1))
ctgx = sp.simplify(expression)
ctgk = str(ctgx).replace('sqrt', 'К')
cos = 1 / (1 + tgx)
c = Fraction(cos)
num2 = c.numerator
den2 = c.denominator
expression = sp.sqrt(sp.Rational(num2, den2))
cosx = sp.simplify(expression)
cosk = str(cosx).replace('sqrt', 'К')
sin = 1 / (1 + ct)
sinx = sp.sqrt(sin)
sinx = sp.simplify(sinx)
sink = str(sinx).replace('sqrt', 'К')
if interval == '1':
bot.send_message(chat_id, f"sinx = {sink}\ncosx = {cosk}\ntanx = {value}\nctgx = {ctgk}")
elif interval == '2':
bot.send_message(chat_id, f"sinx = {sink}\ncosx = -{cosk}\ntanx = -{value}\nctgx = -{ctgk}")
elif interval == '3':
bot.send_message(chat_id, f"sinx = -{sink}\ncosx = -{cosk}\ntanx = {value}\nctgx = {ctgk}")
elif interval == '4':
bot.send_message(chat_id, f"sinx = -{sink}\ncosx = {cosk}\ntanx = -{value}\nctgx = -{ctgk}")
elif func == 'ctgx':
ctg = value
def process_fraction(fraction_str):
def process_part(part):
part_cleaned = part.replace('К', '').replace('K', '').replace('(', '').replace(')', '')
if 'К' in part or 'K' in part:
return int(part_cleaned)
else:
return int(part_cleaned) ** 2
num, den = fraction_str.split('/')
numerator_processed = process_part(num)
denominator_processed = process_part(den)
return numerator_processed, denominator_processed
num0, den0 = process_fraction(ctg)
ctgx = Fraction(num0, den0)
tg = 1 / ctgx
t = Fraction(tg)
num1 = t.numerator
den1 = t.denominator
expression = sp.sqrt(sp.Rational(num1, den1))
tgx = sp.simplify(expression)
tgk = str(tgx).replace('sqrt', 'К')
cos = 1 / (1 + tg)
c = Fraction(cos)
num2 = c.numerator
den2 = c.denominator
expression = sp.sqrt(sp.Rational(num2, den2))
cosx = sp.simplify(expression)
cosk = str(cosx).replace('sqrt', 'К')
sin = 1 / (1 + ctgx)
sinx = sp.sqrt(sin)
sinx = sp.simplify(sinx)
sink = str(sinx).replace('sqrt', 'К')
if interval == '1':
bot.send_message(chat_id, f"sinx = {sink}\ncosx = {cosk}\ntanx = {tgk}\nctgx = {value}")
elif interval == '2':
bot.send_message(chat_id, f"sinx = {sink}\ncosx = -{cosk}\ntanx = -{tgk}\nctgx = -{value}")
elif interval == '3':
bot.send_message(chat_id, f"sinx = -{sink}\ncosx = -{cosk}\ntanx = {tgk}\nctgx = {value}")
elif interval == '4':
bot.send_message(chat_id, f"sinx = -{sink}\ncosx = {cosk}\ntanx = -{tgk}\nctgx = -{value}")
except Exception as e:
bot.send_message(chat_id, f"Произошла ошибка: {e}")
else:
bot.send_message(chat_id, "Пожалуйста, выберите функцию и интервал сначала.")
@bot.callback_query_handler(func=lambda call: call.data == 'U')
def choose_function(call):
message = call.message
chat_id = message.chat.id
user_data[chat_id] = {'interval': call.data}
markup1 = types.InlineKeyboardMarkup()
btn1 = types.InlineKeyboardButton(text='sin', callback_data='sin')
btn2 = types.InlineKeyboardButton(text='cos', callback_data='cos')
markup1.row(btn1, btn2)
btn3 = types.InlineKeyboardButton(text='tg', callback_data='tg')
btn4 = types.InlineKeyboardButton(text='ctg', callback_data='ctg')
markup1.row(btn3, btn4)
bot.edit_message_text(chat_id=chat_id, message_id=message.message_id, text='Выбери какая функция в дано:', reply_markup=markup1)
@bot.callback_query_handler(func=lambda call: call.data in ['sin', 'cos', 'tg', 'ctg'])
def ask_value(call):
message = call.message
chat_id = message.chat.id
user_data[chat_id]['function'] = call.data
bot.send_message(chat_id, 'Напиши аргумент этой функции и чему она равна, разделив пробелом, если число с корнем то напиши как в примере [K(3)/2], если число целое то напиши дробью, ну а если в знаменателе есть корень то убери корень из знаменателя [1/K(3)=K(3)/3], если есть π то напиши П')
@bot.message_handler(func=lambda message: True)
def echo_message(message):
chat_id = message.chat.id
if chat_id in user_data and 'function' in user_data[chat_id]:
try:
value = message.text
if ' ' in value:
x, o = value.split(' ', 1)
else:
x, o = value, ''
def replace_math_symbols(expression):
expression = expression.replace("К(", "sqrt(").replace("K(", "sqrt(")
expression = expression.replace("П", "pi")
return expression
x = replace_math_symbols(x)
o = replace_math_symbols(o)
x2 = sp.symbols('x')
n = sp.symbols('n', integer=True)
if not x:
x1 = 0
else:
x1 = sp.sympify(x)
if not o:
o1 = 0
else:
o1 = sp.sympify(o)
k = o1.coeff(x2)
b = o1.subs(x2, 0)
func = user_data[chat_id]['function']
if func == 'sin':
arcsin_value = sp.asin(x1)
otv = ((-1) ** n * arcsin_value + sp.pi * n - b) / k
elif func == 'cos':
arccos_value = sp.acos(x1)
term1 = sp.simplify((2 * sp.pi * n) / k)
term2 = sp.simplify(-b / k)
otv = f"±{sp.simplify(arccos_value / k)} + {term1 + term2}"
elif func == 'tg':
arctg_value = sp.atan(x1)
otv = (arctg_value + sp.pi * n - b) / k
elif func == 'ctg':
arcctg_value = sp.acot(x1)
otv = (arcctg_value + sp.pi * n - b) / k
bot.send_message(chat_id, f'x={otv}, n∈Z')
except Exception as e:
bot.send_message(chat_id, f"Произошла ошибка: {e}")
bot.polling(none_stop=True) |