juinda

Untitled

May 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.40 KB | None | 0 0
  1. #include "calculater.h"
  2. #include<QApplication>
  3. #include<QKeyEvent>
  4.  
  5. Calculater::Calculater(QWidget *parent) :
  6. QMainWindow(parent)
  7. {
  8. QFont style1("Calibri Light", 12, QFont::Normal);//left section
  9. QFont style2("Calibri Light", 18, QFont::Normal);//right section
  10. QFont style3("Arial", 18, QFont::Normal);//label
  11. QFont style4("Arial", 18, QFont::Bold); //number key
  12. QFont style5("Arial", 12, QFont::Normal);
  13.  
  14.  
  15. label=new QLabel("0",this);
  16. label->setGeometry(QRect(QPoint(10, 10), QSize(580, 315)));
  17. label->setTextInteractionFlags(Qt::TextEditorInteraction);//let user select the text and revise it
  18. label->setFont(style3);
  19. label->setAlignment(Qt::AlignTop);
  20. label->setAlignment(Qt::AlignRight);
  21.  
  22. print_button=new QPushButton("Print",this) ;
  23. print_button->setGeometry(QRect(QPoint(0,325),QSize(150,55)));
  24. print_button->setFont(style1);
  25. connect(print_button,SIGNAL(released()),this,SLOT(print()));
  26.  
  27. setInteger_button=new QPushButton("Set Integer",this) ;
  28. setInteger_button->setGeometry(QRect(QPoint(0,380),QSize(150,55)));
  29. setInteger_button->setFont(style1);
  30. connect(setInteger_button,SIGNAL(released()),this,SLOT(setInteger()));
  31.  
  32. setDecimal_button=new QPushButton("Set Decimal",this);
  33. setDecimal_button->setGeometry(QRect(QPoint(0,435),QSize(150,55)));
  34. setDecimal_button->setFont(style1);
  35. connect(setDecimal_button,SIGNAL(released()),this,SLOT(setDecimal()));
  36.  
  37. setComplex_button=new QPushButton("Set Complex",this);
  38. setComplex_button->setGeometry(QRect(QPoint(0,490),QSize(150,55)));
  39. setComplex_button->setFont(style1);
  40. connect(setComplex_button,SIGNAL(released()),this,SLOT(setComplex()));
  41.  
  42. submit_button=new QPushButton("Submit",this);
  43. submit_button->setGeometry(QRect(QPoint(0,545),QSize(150,55)));
  44. submit_button->setFont(style1);
  45. connect(submit_button,SIGNAL(released()),this,SLOT(submit()));
  46.  
  47. power_button=new QPushButton("power(a,b)",this);
  48. power_button->setGeometry(QRect(QPoint(0,600),QSize(150,55)));
  49. power_button->setFont(style1);
  50. connect(power_button,SIGNAL(released()),this,SLOT(power()));
  51.  
  52. factorial_button=new QPushButton("N!",this);
  53. factorial_button->setGeometry(QRect(QPoint(0,655),QSize(150,55)));
  54. factorial_button->setFont(style1);
  55. connect(factorial_button,SIGNAL(released()),this,SLOT(factorial()));
  56.  
  57. add_button=new QPushButton("+",this);
  58. add_button->setGeometry(QRect(QPoint(160,325),QSize(110,77)));
  59. add_button->setFont(style2);
  60. connect(add_button,SIGNAL(released()),this,SLOT(add()));
  61.  
  62. min_button=new QPushButton("-",this);
  63. min_button->setGeometry(QRect(QPoint(160,402),QSize(110,77)));
  64. min_button->setFont(style2);
  65. connect(min_button,SIGNAL(released()),this,SLOT(min()));
  66.  
  67. mul_button=new QPushButton("*",this);
  68. mul_button->setGeometry(QRect(QPoint(160,479),QSize(110,77)));
  69. mul_button->setFont(style2);
  70. connect(mul_button,SIGNAL(released()),this,SLOT(mul()));
  71.  
  72. div_button=new QPushButton("/",this);
  73. div_button->setGeometry(QRect(QPoint(160,556),QSize(110,77)));
  74. div_button->setFont(style2);
  75. connect(div_button,SIGNAL(released()),this,SLOT(div()));
  76.  
  77. leftBucket_button=new QPushButton("(",this);
  78. leftBucket_button->setGeometry(QRect(QPoint(160,633),QSize(110,77)));
  79. leftBucket_button->setFont(style2);
  80. connect(leftBucket_button,SIGNAL(released()),this,SLOT(leftBucket()));
  81.  
  82. deleteAll_button=new QPushButton("CE",this);
  83. deleteAll_button->setGeometry(QRect(QPoint(270,325),QSize(110,77)));
  84. deleteAll_button->setFont(style2);
  85. connect(deleteAll_button,SIGNAL(released()),this,SLOT(deleteAll()));
  86.  
  87. seven_button=new QPushButton("7",this);
  88. seven_button->setGeometry(QRect(QPoint(270,402),QSize(110,77)));
  89. seven_button->setFont(style4);
  90. connect(seven_button,SIGNAL(released()),this,SLOT(seven()));
  91.  
  92. four_button=new QPushButton("4",this);
  93. four_button->setGeometry(QRect(QPoint(270,479),QSize(110,77)));
  94. four_button->setFont(style4);
  95. connect(four_button,SIGNAL(released()),this,SLOT(four()));
  96.  
  97. one_button=new QPushButton("1",this);
  98. one_button->setGeometry(QRect(QPoint(270,556),QSize(110,77)));
  99. one_button->setFont(style4);
  100. connect(one_button,SIGNAL(released()),this,SLOT(one()));
  101.  
  102. rightBucket_button=new QPushButton(")",this);
  103. rightBucket_button->setGeometry(QRect(QPoint(270,633),QSize(110,77)));
  104. rightBucket_button->setFont(style2);
  105. connect(rightBucket_button,SIGNAL(released()),this,SLOT(rightBucket()));
  106.  
  107. deleteOne_button=new QPushButton("DELETE",this);
  108. deleteOne_button->setGeometry(QRect(QPoint(380,325),QSize(110,77)));
  109. deleteOne_button->setFont(style2);
  110. connect(setDecimal_button,SIGNAL(released()),this,SLOT(deleteOne()));
  111.  
  112. eight_button=new QPushButton("8",this);
  113. eight_button->setGeometry(QRect(QPoint(380,402),QSize(110,77)));
  114. eight_button->setFont(style4);
  115. connect(eight_button,SIGNAL(released()),this,SLOT(eight()));
  116.  
  117. five_button=new QPushButton("5",this);
  118. five_button->setGeometry(QRect(QPoint(380,479),QSize(110,77)));
  119. five_button->setFont(style4);
  120. connect(five_button,SIGNAL(released()),this,SLOT(five()));
  121.  
  122. two_button=new QPushButton("2",this);
  123. two_button->setGeometry(QRect(QPoint(380,556),QSize(110,77)));
  124. two_button->setFont(style4);
  125. connect(two_button,SIGNAL(released()),this,SLOT(two()));
  126.  
  127. zero_button=new QPushButton("0",this);
  128. zero_button->setGeometry(QRect(QPoint(380,633),QSize(110,77)));
  129. zero_button->setFont(style4);
  130. connect(zero_button,SIGNAL(released()),this,SLOT(zero()));
  131.  
  132. send_button=new QPushButton("=",this);
  133. send_button->setGeometry(QRect(QPoint(490,325),QSize(110,77)));
  134. send_button->setFont(style2);
  135. connect(send_button,SIGNAL(released()),this,SLOT(send()));
  136.  
  137. nine_button=new QPushButton("9",this);
  138. nine_button->setGeometry(QRect(QPoint(490,402),QSize(110,77)));
  139. nine_button->setFont(style4);
  140. connect(nine_button,SIGNAL(released()),this,SLOT(nine()));
  141.  
  142. six_button=new QPushButton("6",this);
  143. six_button->setGeometry(QRect(QPoint(490,479),QSize(110,77)));
  144. six_button->setFont(style4);
  145. connect(six_button,SIGNAL(released()),this,SLOT(six()));
  146.  
  147. three_button=new QPushButton("3",this);
  148. three_button->setGeometry(QRect(QPoint(490,556),QSize(110,77)));
  149. three_button->setFont(style4);
  150. connect(three_button,SIGNAL(released()),this,SLOT(three()));
  151.  
  152. dot_button=new QPushButton(".",this);
  153. dot_button->setGeometry(QRect(QPoint(490,633),QSize(110,77)));
  154. dot_button->setFont(style2);
  155. connect(dot_button,SIGNAL(released()),this,SLOT(dot()));
  156. }
  157.  
  158. void Calculater::keyPressEvent(QKeyEvent *event)
  159. {
  160. if(event->key()==Qt::Key_Enter)
  161. Calculater::print();
  162. else
  163. {
  164. /*削掉shift*/
  165. if(((event->key() == Qt::Key_0 )||(event->key()==Qt::Key_9)) && (event->modifiers() & Qt::Key_Shift))
  166. formula+=(event->key()== Qt::Key_0 ?")":"(");
  167. else if((event->key()==Qt::Key_Backspace)||event->key()==Qt::Key_Delete)//delete
  168. deleteOne();
  169. else if(event->key()==Qt::Key_Escape)//ce
  170. deleteAll();
  171. else
  172. formula+=(char(event->key()));
  173. label->setText(formula);
  174. }
  175. }
  176.  
  177. void Calculater::print()
  178. {
  179. QString num = "123";
  180. std::string num1 = num.toUtf8().constData();
  181. QString str = QString::fromUtf8(num1.c_str());
  182. label->setText(str);
  183. }
  184. void Calculater::setInteger()
  185. {
  186. formula+="Set Integer ";
  187. label->setText(formula);
  188. }
  189. void Calculater::setDecimal()
  190. {
  191. formula+="Set Decimal ";
  192. label->setText(formula);
  193. }
  194. void Calculater::setComplex()
  195. {
  196. formula+="Set Complex ";
  197. label->setText(formula);
  198. }
  199. void Calculater::submit()
  200. {
  201.  
  202. }
  203. void Calculater::power()
  204. {
  205. formula+="power(";
  206. label->setText(formula);
  207. }
  208. void Calculater::factorial()
  209. {
  210. formula+="!";
  211. label->setText(formula);
  212. }
  213. void Calculater::add()
  214. {
  215. formula+="+";
  216. label->setText(formula);
  217. }
  218. void Calculater::min()
  219. {
  220. formula+="-";
  221. label->setText(formula);
  222. }
  223. void Calculater::mul()
  224. {
  225. formula+="*";
  226. label->setText(formula);
  227. }
  228. void Calculater::div()
  229. {
  230. formula+="/";
  231. label->setText(formula);
  232. }
  233. void Calculater::leftBucket()
  234. {
  235. formula+="(";
  236. label->setText(formula);
  237. }
  238. void Calculater::deleteAll()
  239. {
  240. formula.resize(0);
  241. label->setText(formula);
  242. }
  243. void Calculater::seven()
  244. {
  245. formula+="7";
  246. label->setText(formula);
  247. }
  248. void Calculater::four()
  249. {
  250. formula+="4";
  251. label->setText(formula);
  252. }
  253. void Calculater::one()
  254. {
  255. formula+="1";
  256. label->setText(formula);
  257. }
  258. void Calculater::rightBucket()
  259. {
  260. formula+=")";
  261. label->setText(formula);
  262. }
  263. void Calculater::deleteOne()
  264. {
  265. formula.resize(formula.size()-1);
  266. label->setText(formula);
  267. }
  268. void Calculater::eight()
  269. {
  270. formula+="8";
  271. label->setText(formula);
  272. }
  273. void Calculater::five()
  274. {
  275. formula+="5";
  276. label->setText(formula);
  277. }
  278. void Calculater::two()
  279. {
  280. formula+="2";
  281. label->setText(formula);
  282. }
  283. void Calculater::zero()
  284. {
  285. formula+="0";
  286. label->setText(formula);
  287. }
  288. void Calculater::send()
  289. {
  290.  
  291. }
  292. void Calculater::nine()
  293. {
  294. formula+="9";
  295. label->setText(formula);
  296. }
  297. void Calculater::six()
  298. {
  299. formula+="6";
  300. label->setText(formula);
  301. }
  302. void Calculater::three()
  303. {
  304. formula+="3";
  305. label->setText(formula);
  306. }
  307. void Calculater::dot()
  308. {
  309. formula+=".";
  310. label->setText(formula);
  311. }
  312.  
  313. Calculater::~Calculater()
  314. {
  315.  
  316. }
Advertisement
Add Comment
Please, Sign In to add comment