fork(2) download
  1. #include<iostream>
  2. #include<string>
  3. #include<sstream>
  4.  
  5. using namespace std;
  6.  
  7. class Var
  8. {
  9. enum Type {I, D, S};
  10. int nVar;
  11. double dVar;
  12. string sVar;
  13. Type type;
  14.  
  15. template <typename T>
  16. string toString(T val);
  17. void DiffString( string& str1, int dl); // реализация математических операций - для строк
  18. void MultString(string& s1, const string& s2); // доступны только для данного класса
  19. void DivString(string& s1, const string& s2);
  20.  
  21. public:
  22. Var(int n);
  23. Var(double d);
  24. Var(string s);
  25.  
  26. Var& operator+=(const Var&);
  27. Var& operator-=(const Var&);
  28. Var& operator*=(const Var&);
  29. Var& operator/=(const Var&);
  30.  
  31. Var operator+ (const Var&);
  32. Var operator- (const Var&);
  33. Var operator* (const Var&);
  34. Var operator/ (const Var&);
  35.  
  36. bool operator==(const Var&);
  37. bool operator>(const Var&);
  38. bool operator<(const Var&);
  39. bool operator>=(const Var&);
  40. bool operator<=(const Var&);
  41. bool operator!=(const Var&);
  42.  
  43. void Show() const;
  44.  
  45. operator int();
  46. operator double();
  47. operator const char*();
  48. };
  49.  
  50. template <typename T>
  51. string Var::toString(T val)
  52. {
  53. ostringstream oss;
  54. oss<< val;
  55. return oss.str();
  56. }
  57.  
  58. void Var::DiffString(string& str1, int dl)
  59. {
  60. int t = 0;
  61. if (str1.size() > dl)
  62. t = str1.size() - dl;
  63. str1.resize(t);
  64. }
  65.  
  66. void Var::MultString(string& s1, const string& s2)
  67. {
  68. string tmp;
  69. for (int i = 0; i < s1.size(); i++)
  70. if (s2.find(s1[i]) != -1)
  71. tmp+=s1[i];
  72. s1 = tmp;
  73. }
  74.  
  75. void Var::DivString(string& s1, const string& s2)
  76. {
  77. string tmp;
  78. for (int i = 0; i < s1.size(); i++)
  79. if (s2.find(s1[i]) == -1)
  80. tmp+=s1[i];
  81. s1 = tmp;
  82. }
  83.  
  84. Var::Var(int n = 0)
  85. {
  86. type = I;
  87. nVar = n;
  88. dVar = NULL;
  89. sVar = "";
  90. }
  91.  
  92. Var::Var(double d)
  93. {
  94. type = D;
  95. nVar = NULL;
  96. dVar = d;
  97. sVar = "";
  98. }
  99.  
  100. Var::Var(string s)
  101. {
  102. type = S;
  103. nVar = NULL;
  104. dVar = NULL;
  105. sVar = s;
  106. }
  107.  
  108. Var& Var::operator+=(const Var& V)
  109. {
  110. if (type == I)
  111. {
  112. if (V.type == I)
  113. {
  114. nVar += V.nVar;
  115. return *this;
  116. }
  117. if (V.type == D)
  118. {
  119. nVar += int(V.dVar);
  120. return *this;
  121. }
  122. if (V.type == S)
  123. {
  124. nVar += int((V.sVar).c_str());
  125. return *this;
  126. }
  127. }
  128. if (type == D)
  129. {
  130. if (V.type == I)
  131. {
  132. dVar += double(V.nVar);
  133. return *this;
  134. }
  135. if (V.type == D)
  136. {
  137. dVar += V.dVar;
  138. return *this;
  139. }
  140. if (V.type == S)
  141. {
  142. dVar += double(int((V.sVar).c_str()));
  143. return *this;
  144. }
  145. }
  146. if (type == S)
  147. {
  148. if (V.type == I)
  149. {
  150. sVar += toString(V.nVar);
  151. return *this;
  152. }
  153. if (V.type == D)
  154. {
  155. sVar += toString(V.dVar);
  156. return *this;
  157. }
  158. if (V.type == S)
  159. {
  160. sVar += V.sVar;
  161. return *this;
  162. }
  163. }
  164. }
  165.  
  166. Var& Var::operator-=(const Var& V)
  167. {
  168. if (type == I)
  169. {
  170. if (V.type == I)
  171. {
  172. nVar -= V.nVar;
  173. return *this;
  174. }
  175. if (V.type == D)
  176. {
  177. nVar -= int(V.dVar);
  178. return *this;
  179. }
  180. if (V.type == S)
  181. {
  182. nVar -= int((V.sVar).c_str());
  183. return *this;
  184. }
  185. }
  186. if (type == D)
  187. {
  188. if (V.type == I)
  189. {
  190. dVar -= double(V.nVar);
  191. return *this;
  192. }
  193. if (V.type == D)
  194. {
  195. dVar -= V.dVar;
  196. return *this;
  197. }
  198. if (V.type == S)
  199. {
  200. dVar -= double(int((V.sVar).c_str()));
  201. return *this;
  202. }
  203. }
  204. if (type == S)
  205. {
  206. if (V.type == I)
  207. {
  208. DiffString(sVar,(toString(V.nVar)).size());
  209. return *this;
  210. }
  211. if (V.type == D)
  212. {
  213. DiffString(sVar,(toString(V.dVar)).size());
  214. return *this;
  215. }
  216. if (V.type == S)
  217. {
  218. DiffString(sVar,(V.sVar).size());
  219. return *this;
  220. }
  221. }
  222. }
  223.  
  224. Var& Var::operator*=(const Var& V)
  225. {
  226. if (type == I)
  227. {
  228. if (V.type == I)
  229. {
  230. nVar *= V.nVar;
  231. return *this;
  232. }
  233. if (V.type == D)
  234. {
  235. nVar *= int(V.dVar);
  236. return *this;
  237. }
  238. if (V.type == S)
  239. {
  240. nVar *= int((V.sVar).c_str());
  241. return *this;
  242. }
  243. }
  244. if (type == D)
  245. {
  246. if (V.type == I)
  247. {
  248. dVar *= double(V.nVar);
  249. return *this;
  250. }
  251. if (V.type == D)
  252. {
  253. dVar *= V.dVar;
  254. return *this;
  255. }
  256. if (V.type == S)
  257. {
  258. dVar *= double(int((V.sVar).c_str()));
  259. return *this;
  260. }
  261. }
  262. if (type == S)
  263. {
  264. if (V.type == I)
  265. {
  266. MultString(sVar,toString(V.nVar));
  267. return *this;
  268. }
  269. if (V.type == D)
  270. {
  271. MultString(sVar, toString(V.dVar));
  272. return *this;
  273. }
  274. if (V.type == S)
  275. {
  276. MultString(sVar, V.sVar);
  277. return *this;
  278. }
  279. }
  280. }
  281.  
  282. Var& Var::operator/=(const Var& V)
  283. {
  284. if (type == I)
  285. {
  286. if (V.type == I)
  287. {
  288. if (!V.nVar)
  289. {
  290. cout<<"Деление на 0!\n";
  291. return *this;
  292. }
  293. nVar /= V.nVar;
  294. return *this;
  295. }
  296. if (V.type == D)
  297. {
  298. if (!V.dVar)
  299. {
  300. cout<<"Деление на 0!\n";
  301. return *this;
  302. }
  303. nVar /= int(V.dVar);
  304. return *this;
  305. }
  306. if (V.type == S)
  307. {
  308. if (!int((V.sVar).c_str()))
  309. {
  310. cout<<"Деление на 0!\n";
  311. return *this;
  312. }
  313. nVar /= int((V.sVar).c_str());
  314. return *this;
  315. }
  316. }
  317. if (type == D)
  318. {
  319. if (V.type == I)
  320. {
  321. if (!V.nVar)
  322. {
  323. cout<<"Деление на 0!\n";
  324. return *this;
  325. }
  326. dVar /= double(V.nVar);
  327. return *this;
  328. }
  329. if (V.type == D)
  330. {
  331. if (!V.dVar)
  332. {
  333. cout<<"Деление на 0!\n";
  334. return *this;
  335. }
  336. dVar /= V.dVar;
  337. return *this;
  338. }
  339. if (V.type == S)
  340. {
  341. if (!int((V.sVar).c_str()))
  342. {
  343. cout<<"Деление на 0!\n";
  344. return *this;
  345. }
  346. dVar /= double(int((V.sVar).c_str()));
  347. return *this;
  348. }
  349. }
  350. if (type == S)
  351. {
  352. if (V.type == I)
  353. {
  354. DivString(sVar, toString(V.nVar));
  355. return *this;
  356. }
  357. if (V.type == D)
  358. {
  359. DivString(sVar, toString(V.dVar));
  360. return *this;
  361. }
  362. if (V.type == S)
  363. {
  364. DivString(sVar, V.sVar);
  365. return *this;
  366. }
  367. }
  368. }
  369.  
  370. Var Var::operator+ (const Var& V)
  371. {
  372. if (type == I)
  373. {
  374. if (V.type == I)
  375. {
  376. return Var(nVar + V.nVar);
  377. }
  378. if (V.type == D)
  379. {
  380. return Var(nVar + int(V.dVar));
  381. }
  382. if (V.type == S)
  383. {
  384. return Var(nVar + int((V.sVar).c_str()));
  385. }
  386. }
  387. if (type == D)
  388. {
  389. if (V.type == I)
  390. {
  391. return Var(dVar + double(V.nVar));
  392. }
  393. if (V.type == D)
  394. {
  395. return Var(dVar + V.dVar);
  396. }
  397. if (V.type == S)
  398. {
  399. return Var(dVar + double(int((V.sVar).c_str())));
  400. }
  401. }
  402. if (type == S)
  403. {
  404. if (V.type == I)
  405. {
  406. return Var(sVar + toString(V.nVar));
  407. }
  408. if (V.type == D)
  409. {
  410. return Var(sVar + toString(V.dVar));
  411. }
  412. if (V.type == S)
  413. {
  414. return (sVar + V.sVar);
  415. }
  416. }
  417. }
  418.  
  419. Var Var::operator- (const Var& V)
  420. {
  421. if (type == I)
  422. {
  423. if (V.type == I)
  424. {
  425. return Var(nVar - V.nVar);
  426. }
  427. if (V.type == D)
  428. {
  429. return Var(nVar - int(V.dVar));
  430. }
  431. if (V.type == S)
  432. {
  433. return Var(nVar - int((V.sVar).c_str()));
  434. }
  435. }
  436. if (type == D)
  437. {
  438. if (V.type == I)
  439. {
  440. return Var(dVar - double(V.nVar));
  441. }
  442. if (V.type == D)
  443. {
  444. return Var(dVar - V.dVar);
  445. }
  446. if (V.type == S)
  447. {
  448. return Var(dVar - double(int((V.sVar).c_str())));
  449. }
  450. }
  451. if (type == S)
  452. {
  453. string tmp = sVar;
  454. if (V.type == I)
  455. {
  456. DiffString(tmp, (toString(V.nVar)).size());
  457. return Var(tmp);
  458. }
  459. if (V.type == D)
  460. {
  461. DiffString(tmp, (toString(V.dVar)).size());
  462. return Var(tmp);
  463. }
  464. if (V.type == S)
  465. {
  466. DiffString(tmp, V.sVar.size());
  467. return (tmp);
  468. }
  469. }
  470. }
  471.  
  472. Var Var::operator* (const Var& V)
  473. {
  474. if (type == I)
  475. {
  476. if (V.type == I)
  477. {
  478. return Var(nVar * V.nVar);
  479. }
  480. if (V.type == D)
  481. {
  482. return Var(nVar * int(V.dVar));
  483. }
  484. if (V.type == S)
  485. {
  486. return Var(nVar * int((V.sVar).c_str()));
  487. }
  488. }
  489. if (type == D)
  490. {
  491. if (V.type == I)
  492. {
  493. return Var(dVar * double(V.nVar));
  494. }
  495. if (V.type == D)
  496. {
  497. return Var(dVar * V.dVar);
  498. }
  499. if (V.type == S)
  500. {
  501. return Var(dVar * double(int((V.sVar).c_str())));
  502. }
  503. }
  504. if (type == S)
  505. {
  506. string tmp = sVar;
  507. if (V.type == I)
  508. {
  509. MultString(tmp, toString(V.nVar));
  510. return Var(tmp);
  511. }
  512. if (V.type == D)
  513. {
  514. MultString(tmp, toString(V.dVar));
  515. return Var(tmp);
  516. }
  517. if (V.type == S)
  518. {
  519. MultString(tmp, V.sVar);
  520. return (tmp);
  521. }
  522. }
  523. }
  524.  
  525. Var Var::operator/ (const Var& V)
  526. {
  527. if (type == I)
  528. {
  529. if (V.type == I)
  530. {
  531. if (!V.nVar)
  532. {
  533. cout<<"Деление на 0!\n";
  534. return Var();
  535. }
  536. return Var(nVar / V.nVar);
  537. }
  538. if (V.type == D)
  539. {
  540. if (!V.dVar)
  541. {
  542. cout<<"Деление на 0!\n";
  543. return Var();
  544. }
  545. return Var(nVar / int(V.dVar));
  546. }
  547. if (V.type == S)
  548. {
  549. if (!int((V.sVar).c_str()))
  550. {
  551. cout<<"Деление на 0!\n";
  552. return Var();
  553. }
  554. return Var(nVar / int((V.sVar).c_str()));
  555. }
  556. }
  557. if (type == D)
  558. {
  559. if (V.type == I)
  560. {
  561. if (!V.nVar)
  562. {
  563. cout<<"Деление на 0!\n";
  564. return Var();
  565. }
  566. return Var(dVar / double(V.nVar));
  567. }
  568. if (V.type == D)
  569. {
  570. if (!V.dVar)
  571. {
  572. cout<<"Деление на 0!\n";
  573. return Var();
  574. }
  575. return Var(dVar / V.dVar);
  576. }
  577. if (V.type == S)
  578. {
  579. if (!int((V.sVar).c_str()))
  580. {
  581. cout<<"Деление на 0!\n";
  582. return Var();
  583. }
  584. return Var(dVar / double(int((V.sVar).c_str())));
  585. }
  586. }
  587. if (type == S)
  588. {
  589. string tmp = sVar;
  590. if (V.type == I)
  591. {
  592. DivString(tmp, toString(V.nVar));
  593. return Var(tmp);
  594. }
  595. if (V.type == D)
  596. {
  597. DivString(tmp, toString(V.dVar));
  598. return Var(tmp);
  599. }
  600. if (V.type == S)
  601. {
  602. DivString(tmp, V.sVar);
  603. return (tmp);
  604. }
  605. }
  606. }
  607.  
  608. bool Var::operator==(const Var& V)
  609. {
  610. if (type == I)
  611. {
  612. if (V.type == I)
  613. return (nVar == V.nVar);
  614. if (V.type == D)
  615. return (nVar == int(V.dVar));
  616. if (V.type == S)
  617. return (nVar == int((V.sVar).c_str()));
  618. }
  619. if (type == D)
  620. {
  621. if (V.type == I)
  622. return (dVar == double(V.nVar));
  623. if (V.type == D)
  624. return (dVar == V.dVar);
  625. if (V.type == S)
  626. return (dVar == double(int((V.sVar).c_str())));
  627. }
  628. if (type == S)
  629. {
  630. if (V.type == I)
  631. return (sVar == toString(V.nVar));
  632. if (V.type == D)
  633. return (sVar == toString(V.dVar));
  634. if (V.type == S)
  635. return (sVar == V.sVar);
  636. }
  637. }
  638.  
  639. bool Var::operator>(const Var& V)
  640. {
  641. if (type == I)
  642. {
  643. if (V.type == I)
  644. return (nVar > V.nVar);
  645. if (V.type == D)
  646. return (nVar > int(V.dVar));
  647. if (V.type == S)
  648. return (nVar > int((V.sVar).c_str()));
  649. }
  650. if (type == D)
  651. {
  652. if (V.type == I)
  653. return (dVar > double(V.nVar));
  654. if (V.type == D)
  655. return (dVar > V.dVar);
  656. if (V.type == S)
  657. return (dVar > double(int((V.sVar).c_str())));
  658. }
  659. if (type == S)
  660. {
  661. if (V.type == I)
  662. return (sVar > toString(V.nVar));
  663. if (V.type == D)
  664. return (sVar > toString(V.dVar));
  665. if (V.type == S)
  666. return (sVar > V.sVar);
  667. }
  668. }
  669.  
  670. bool Var::operator<(const Var& V)
  671. {
  672. if (type == I)
  673. {
  674. if (V.type == I)
  675. return (nVar < V.nVar);
  676. if (V.type == D)
  677. return (nVar < int(V.dVar));
  678. if (V.type == S)
  679. return (nVar < int((V.sVar).c_str()));
  680. }
  681. if (type == D)
  682. {
  683. if (V.type == I)
  684. return (dVar < double(V.nVar));
  685. if (V.type == D)
  686. return (dVar < V.dVar);
  687. if (V.type == S)
  688. return (dVar < double(int((V.sVar).c_str())));
  689. }
  690. if (type == S)
  691. {
  692. if (V.type == I)
  693. return (sVar < toString(V.nVar));
  694. if (V.type == D)
  695. return (sVar < toString(V.dVar));
  696. if (V.type == S)
  697. return (sVar < V.sVar);
  698. }
  699. }
  700. bool Var::operator>=(const Var& V)
  701. {
  702. if (type == I)
  703. {
  704. if (V.type == I)
  705. return (nVar >= V.nVar);
  706. if (V.type == D)
  707. return (nVar >= int(V.dVar));
  708. if (V.type == S)
  709. return (nVar >= int((V.sVar).c_str()));
  710. }
  711. if (type == D)
  712. {
  713. if (V.type == I)
  714. return (dVar >= double(V.nVar));
  715. if (V.type == D)
  716. return (dVar >= V.dVar);
  717. if (V.type == S)
  718. return (dVar >= double(int((V.sVar).c_str())));
  719. }
  720. if (type == S)
  721. {
  722. if (V.type == I)
  723. return (sVar >= toString(V.nVar));
  724. if (V.type == D)
  725. return (sVar >= toString(V.dVar));
  726. if (V.type == S)
  727. return (sVar >= V.sVar);
  728. }
  729. }
  730.  
  731. bool Var::operator<=(const Var& V)
  732. {
  733. if (type == I)
  734. {
  735. if (V.type == I)
  736. return (nVar <= V.nVar);
  737. if (V.type == D)
  738. return (nVar <= int(V.dVar));
  739. if (V.type == S)
  740. return (nVar <= int((V.sVar).c_str()));
  741. }
  742. if (type == D)
  743. {
  744. if (V.type == I)
  745. return (dVar <= double(V.nVar));
  746. if (V.type == D)
  747. return (dVar <= V.dVar);
  748. if (V.type == S)
  749. return (dVar <= double(int((V.sVar).c_str())));
  750. }
  751. if (type == S)
  752. {
  753. if (V.type == I)
  754. return (sVar <= toString(V.nVar));
  755. if (V.type == D)
  756. return (sVar <= toString(V.dVar));
  757. if (V.type == S)
  758. return (sVar <= V.sVar);
  759. }
  760. }
  761. bool Var::operator!=(const Var& V)
  762. {
  763. if (type == I)
  764. {
  765. if (V.type == I)
  766. return (nVar != V.nVar);
  767. if (V.type == D)
  768. return (nVar != int(V.dVar));
  769. if (V.type == S)
  770. return (nVar != int((V.sVar).c_str()));
  771. }
  772. if (type == D)
  773. {
  774. if (V.type == I)
  775. return (dVar != double(V.nVar));
  776. if (V.type == D)
  777. return (dVar != V.dVar);
  778. if (V.type == S)
  779. return (dVar != double(int((V.sVar).c_str())));
  780. }
  781. if (type == S)
  782. {
  783. if (V.type == I)
  784. return (sVar != toString(V.nVar));
  785. if (V.type == D)
  786. return (sVar != toString(V.dVar));
  787. if (V.type == S)
  788. return (sVar != V.sVar);
  789. }
  790. }
  791. void Var::Show() const
  792. {
  793. if (type == I)
  794. cout<<"Тип переменной: int. Значение: "<<nVar<<endl;
  795. if (type == D)
  796. cout<<"Тип переменной: double. Значение: "<<dVar<<endl;
  797. if (type == S)
  798. cout<<"Тип переменной: string. Значение: "<<sVar<<endl;
  799. }
  800.  
  801. Var::operator int()
  802. {
  803. if (type == I)
  804. return nVar;
  805. if (type == D)
  806. return int(dVar);
  807. if (type == S)
  808. return int(sVar.c_str());
  809. }
  810. Var::operator double()
  811. {
  812. if (type == I)
  813. return double(nVar);
  814. if (type == D)
  815. return dVar;
  816. if (type == S)
  817. return double(int(sVar.c_str()));
  818. }
  819. Var::operator const char*()
  820. {
  821. if (type == I)
  822. {
  823. string tmp = toString(nVar);
  824. return tmp.c_str();
  825. }
  826. if (type == D)
  827. {
  828. string tmp = toString(dVar);
  829. return tmp.c_str();;
  830. }
  831. if (type == S)
  832. return sVar.c_str();
  833. }
  834.  
  835. int main()
  836. {
  837. // SetConsoleCP (1251);
  838. // SetConsoleOutputCP (1251);
  839.  
  840. Var a(123456), b(2), f(0);
  841. cout<<"a = ";
  842. a.Show();
  843. cout<<"b = ";
  844. b.Show();
  845. cout<<"f = ";
  846. f.Show();
  847.  
  848. cout<<"(a!=b)"<<(a!=b)<<endl;
  849. cout<<"(a<=b)"<<(a<=b)<<endl;
  850.  
  851.  
  852.  
  853. cout<<"int(a) "<<int(a)<<endl;
  854. cout<<"double(a) "<<double(a)<<endl;
  855. cout<<"char*(a) "<<(const char*)a<<endl;
  856.  
  857.  
  858.  
  859.  
  860. return 0;
  861. }
Success #stdin #stdout 0s 3292KB
stdin
Standard input is empty
stdout
a = Тип переменной: int. Значение: 123456
b = Тип переменной: int. Значение: 2
f = Тип переменной: int. Значение: 0
(a!=b)1
(a<=b)0
int(a) 123456
double(a) 123456
char*(a) 123456