第一个像样的桌面程序

qt学了半个月了,终于搞懂了基本的东西。

1 写ui界面,终于不是黑白的命令行了。

2 qt的信号槽机制以及qstring等与char,int,double的转化。

3 qt程序的打包发布。

看一下源码

  1 #include "mainwindow.h"
  2 #include "ui_mainwindow.h"
  3 #include<QString>
  4 
  5 
  6 char *q;
  7 int k=0;
  8 
  9 
 10 
 11 double biaodas();
 12 double xiang();
 13 double yinz()//求一个因子的值
 14 {
 15     double res = 0;
 16     char tem = q[k];
 17     if (tem == '(')//因子可能是一个括号包起来的表达式,递归到求表达式
 18     {
 19         k++;
 20         res = biaodas();
 21         k++;
 22     }
 23     else//计算出多个十进制数字符连续组成的数的大小
 24     {
 25         while (isdigit(tem))//判断tem是不是十进制的字符
 26         {
 27             res = res * 10 + tem - '0';
 28             k++;
 29             tem = q[k];
 30         }
 31         if (tem == '.')//判断有没有小数
 32         {
 33             double k1 = 10;
 34             k++;
 35             tem=q[k];
 36             while (isdigit(tem))
 37             {
 38                 res += (tem-'0') / k1;
 39                 k1 *= 10;
 40                 k++;
 41                 tem = q[k];
 42             }
 43         }
 44     }
 45     return res;
 46 }
 47 double xiang()//求一个多项式的值
 48 {
 49     double res = yinz();
 50     int flag = 1;
 51     while (flag)
 52     {
 53         char tem = q[k];
 54         if (tem == '*' || tem == '/')//判断乘除把多项式的求解分解为求多个因子的值
 55         {
 56            k++;
 57             double va = yinz();
 58             if (tem == '*')
 59                 res *= va;
 60             else
 61                 res /= va;
 62         }
 63         else flag = 0;
 64     }
 65     return res;
 66 }
 67 double biaodas()//求表达式的值
 68 {
 69     double res = xiang();
 70     int flag = 1;
 71     while (flag)
 72     {
 73         char tem = q[k];
 74         if (tem == '+' || tem == '-')//判断乘除把表达式的求解分解为求多多项式的值
 75         {
 76             k++;
 77             double va = xiang();
 78             if (tem == '+')
 79                 res += va;
 80             else
 81                 res -= va;
 82         }
 83         else flag = 0;
 84     }
 85     return res;
 86 }
 87 
 88 
 89 
 90 
 91 
 92 
 93 
 94 
 95 
 96 
 97 
 98 
 99 
100 
101 
102 
103 MainWindow::MainWindow(QWidget *parent) :
104     QMainWindow(parent),
105     ui(new Ui::MainWindow)
106 {
107     ui->setupUi(this);
108 }
109 QString str;
110 int i=0;
111 
112 MainWindow::~MainWindow()
113 {
114     delete ui;
115 }
116 
117 void MainWindow::on_pushButton_clicked()
118 {
119     char *p=str.toLatin1().data();
120     if(i==0)
121     {
122         str+='1';
123         i++;
124     }
125     else
126     {
127         if(p[i-1]==')');
128         else
129         {
130             str+='1';
131             i++;
132         }
133     }
134     ui->textBrowser->setText(str);
135 }
136 
137 void MainWindow::on_pushButton_2_clicked()
138 {
139     char *p=str.toLatin1().data();
140     if(i==0)
141     {
142         str+='2';
143         i++;
144     }
145     else
146     {
147         if(p[i-1]==')');
148         else
149         {
150             str+='2';
151             i++;
152         }
153     }
154     ui->textBrowser->setText(str);
155 }
156 
157 void MainWindow::on_pushButton_3_clicked()
158 {
159     char *p=str.toLatin1().data();
160     if(i==0)
161     {
162         str+='3';
163         i++;
164     }
165     else
166     {
167         if(p[i-1]==')');
168         else
169         {
170             str+='3';
171             i++;
172         }
173     }
174     ui->textBrowser->setText(str);
175 }
176 
177 void MainWindow::on_pushButton_4_clicked()
178 {
179     char *p=str.toLatin1().data();
180     if(i==0)
181     {
182         str+='4';
183         i++;
184     }
185     else
186     {
187         if(p[i-1]==')');
188         else
189         {
190             str+='4';
191             i++;
192         }
193     }
194     ui->textBrowser->setText(str);
195 }
196 
197 void MainWindow::on_pushButton_5_clicked()
198 {
199     char *p=str.toLatin1().data();
200     if(i==0)
201     {
202         str+='5';
203         i++;
204     }
205     else
206     {
207         if(p[i-1]==')');
208         else
209         {
210             str+='5';
211             i++;
212         }
213     }
214     ui->textBrowser->setText(str);
215 }
216 
217 void MainWindow::on_pushButton_6_clicked()
218 {
219     char *p=str.toLatin1().data();
220     if(i==0)
221     {
222         str+='6';
223         i++;
224     }
225     else
226     {
227         if(p[i-1]==')');
228         else
229         {
230             str+='6';
231             i++;
232         }
233     }
234     ui->textBrowser->setText(str);
235 }
236 
237 void MainWindow::on_pushButton_7_clicked()
238 {
239     char *p=str.toLatin1().data();
240     if(i==0)
241     {
242         str+='7';
243         i++;
244     }
245     else
246     {
247         if(p[i-1]==')');
248         else
249         {
250             str+='7';
251             i++;
252         }
253     }
254     ui->textBrowser->setText(str);
255 }
256 
257 void MainWindow::on_pushButton_8_clicked()
258 {
259     char *p=str.toLatin1().data();
260     if(i==0)
261     {
262         str+='8';
263         i++;
264     }
265     else
266     {
267         if(p[i-1]==')');
268         else
269         {
270             str+='8';
271             i++;
272         }
273     }
274     ui->textBrowser->setText(str);
275 }
276 
277 void MainWindow::on_pushButton_9_clicked()
278 {
279     char *p=str.toLatin1().data();
280     if(i==0)
281     {
282         str+='9';
283         i++;
284     }
285     else
286     {
287         if(p[i-1]==')');
288         else
289         {
290             str+='9';
291             i++;
292         }
293     }
294     ui->textBrowser->setText(str);
295 }
296 
297 void MainWindow::on_pushButton_10_clicked()
298 {
299     char *p=str.toLatin1().data();
300     if(i==0)
301     {
302         str+='.';
303         i++;
304     }
305     else
306     {
307         if(p[i-1]=='+'||p[i-1]=='-'||p[i-1]=='*'||p[i-1]=='/'||p[i-1]=='('||p[i]==')');
308         else
309         {
310             str+='.';
311             i++;
312         }
313     }
314     ui->textBrowser->setText(str);
315 }
316 
317 void MainWindow::on_pushButton_11_clicked()
318 {
319     char *p=str.toLatin1().data();
320     if(i==0)
321     {
322         str+='0';
323         i++;
324     }
325     else
326     {
327         if(p[i]==')');
328         else
329         {
330             str+='0';
331             i++;
332         }
333     }
334     ui->textBrowser->setText(str);
335 }
336 
337 void MainWindow::on_pushButton_12_clicked()
338 {
339     char *p=str.toLatin1().data();
340     if(i==0);
341     else
342     {
343         if(p[i-1]=='+'||p[i-1]=='-'||p[i-1]=='*'||p[i-1]=='/'||p[i-1]=='.'||p[i]=='(');
344         else
345         {
346             str+='+';
347             i++;
348         }
349     }
350     ui->textBrowser->setText(str);
351 }
352 
353 void MainWindow::on_pushButton_13_clicked()
354 {
355     char *p=str.toLatin1().data();
356     if(i==0);
357     else
358     {
359         if(p[i-1]=='+'||p[i-1]=='-'||p[i-1]=='*'||p[i-1]=='/'||p[i-1]=='.'||p[i]=='(');
360         else
361         {
362             str+='-';
363             i++;
364         }
365     }
366     ui->textBrowser->setText(str);
367 }
368 
369 void MainWindow::on_pushButton_14_clicked()
370 {
371     char *p=str.toLatin1().data();
372     if(i==0);
373     else
374     {
375         if(p[i-1]=='+'||p[i-1]=='-'||p[i-1]=='*'||p[i-1]=='/'||p[i-1]=='.'||p[i]=='(');
376         else
377         {
378             str+='*';
379             i++;
380         }
381     }
382     ui->textBrowser->setText(str);
383 }
384 
385 void MainWindow::on_pushButton_15_clicked()
386 {
387     char *p=str.toLatin1().data();
388     if(i==0);
389     else
390     {
391         if(p[i-1]=='+'||p[i-1]=='-'||p[i-1]=='*'||p[i-1]=='/'||p[i-1]=='.'||p[i]=='(');
392         else
393         {
394             str+='/';
395             i++;
396         }
397     }
398     ui->textBrowser->setText(str);
399 }
400 
401 void MainWindow::on_pushButton_16_clicked()
402 {
403     char *p=str.toLatin1().data();
404     if(i==0)
405     {
406         str+='(';
407         i++;
408     }
409     else
410     {
411         if(p[i-1]=='+'||p[i-1]=='-'||p[i-1]=='*'||p[i-1]=='/')
412         {
413             str+='(';
414             i++;
415         }
416     }
417     ui->textBrowser->setText(str);
418 }
419 
420 void MainWindow::on_pushButton_17_clicked()
421 {
422     char *p=str.toLatin1().data();
423     if(i==0)
424     {
425         str+=')';
426         i++;
427     }
428     else
429     {
430         if(p[i-1]=='+'||p[i-1]=='-'||p[i-1]=='*'||p[i-1]=='/'||p[i-1]=='.'||p[i-1]=='('||p[i-1]==')');
431         else
432         {
433             str+=')';
434             i++;
435         }
436     }
437     ui->textBrowser->setText(str);
438 }
439 
440 void MainWindow::on_pushButton_18_clicked()
441 {
442     str.chop(1);
443     i--;
444     ui->textBrowser->setText(str);
445 }
446 
447 void MainWindow::on_pushButton_19_clicked()
448 {
449     q=str.toLatin1().data();
450     k=0;
451     QString str2=QString("%1").arg(biaodas());
452     ui->textBrowser->setText(str2);
453     i=0;
454     str.clear();
455 }

其实关键算法还是之前的递归求多项式,只是把从那个黑白控制台输入的表达式换成了一个字符数组q,先用button把多项式输入进qstring类型的str,然后利用q=str.toLatin1().data()把str转化

为字符数组q,接着就是递归求表达式了。

程序写好之后还要进行打包,在qt的命令行中输入windeployqt xxxx.exe即可把程序的资源文件复制到程序目录下,大概就是这样了。

最后附上程序

链接:https://pan.baidu.com/s/1o9z37OA

密码:5dtb

原文地址:https://www.cnblogs.com/miaos/p/miaoz-2-1.html