#include "widget.h"
#include "ui_widget.h"
#include <QString>
#include <QMessageBox>
char num1[20] = {0};
char num2[20] = {0};
char *p = num1;
char *p01 = num1;
char *p02 = num2;
char *flag = num1;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_but_1_clicked()
{
*p = '1';
p++;
if(*p01 != '.'){
ui->label_now->setText(QString::fromUtf8(p01));
}else{
ui->label_now->setText(QString::fromUtf8(p01-1));
}
}
void Widget::on_but_2_clicked()
{
*p = '2';
p++;
if(*p01 != '.'){
ui->label_now->setText(QString::fromUtf8(p01));
}else{
ui->label_now->setText(QString::fromUtf8(p01-1));
}
}
void Widget::on_but_3_clicked()
{
*p = '3';
p++;
if(*p01 != '.'){
ui->label_now->setText(QString::fromUtf8(p01));
}else{
ui->label_now->setText(QString::fromUtf8(p01-1));
}
}
void Widget::on_but_4_clicked()
{
*p = '4';
p++;
if(*p01 != '.'){
ui->label_now->setText(QString::fromUtf8(p01));
}else{
ui->label_now->setText(QString::fromUtf8(p01-1));
}
}
void Widget::on_but_5_clicked()
{
*p = '5';
p++;
if(*p01 != '.'){
ui->label_now->setText(QString::fromUtf8(p01));
}else{
ui->label_now->setText(QString::fromUtf8(p01-1));
}
}
void Widget::on_but_6_clicked()
{
*p = '6';
p++;
if(*p01 != '.'){
ui->label_now->setText(QString::fromUtf8(p01));
}else{
ui->label_now->setText(QString::fromUtf8(p01-1));
}
}
void Widget::on_but_7_clicked()
{
*p = '7';
p++;
if(*p01 != '.'){
ui->label_now->setText(QString::fromUtf8(p01));
}else{
ui->label_now->setText(QString::fromUtf8(p01-1));
}
}
void Widget::on_but_8_clicked()
{
*p = '8';
p++;
if(*p01 != '.'){
ui->label_now->setText(QString::fromUtf8(p01));
}else{
ui->label_now->setText(QString::fromUtf8(p01-1));
}
}
void Widget::on_but_9_clicked()
{
*p = '9';
p++;
if(*p01 != '.'){
ui->label_now->setText(QString::fromUtf8(p01));
}else{
ui->label_now->setText(QString::fromUtf8(p01-1));
}
}
void Widget::on_but_0_clicked()
{
*p = '0';
p++;
if(*p01=='0')
{
p01++;
}
ui->label_now->setText(QString::fromUtf8(p01-1));
}
void Widget::on_but_poi_clicked()
{
char *point = num1;
while(*point)
{
point++;
if(*point == '.')
return;
}
*p = '.';
p++;
if(*p01 != '.'){
ui->label_now->setText(QString::fromUtf8(p01));
}else{
ui->label_now->setText(QString::fromUtf8(p01-1));
}
}
void Widget::on_but_div_clicked()
{
char *div = num1;
while(*div)
{
div++;
if(*div == '/')
return;
}
*p = '/';
flag = p;
p++;
if(*p01 != '.'){
ui->label_last->setText(QString::fromUtf8(p01));
}else{
ui->label_last->setText(QString::fromUtf8(p01-1));
}
p = num2;
p02 = p01;
p01 = num2;
}
void Widget::on_but_mul_clicked()
{
char *mul = num1;
while(*mul)
{
mul++;
if(*mul == '*')
return;
}
*p = '*';
flag = p;
p++;
if(*p01 != '.'){
ui->label_last->setText(QString::fromUtf8(p01));
}else{
ui->label_last->setText(QString::fromUtf8(p01-1));
}
p = num2;
p02 = p01;
p01 = num2;
}
void Widget::on_but_sub_clicked()//sub
{
char *sub = num1;
while(*sub)
{
sub++;
if(*sub == '-')
return;
}
*p = '-';
flag = p;
p++;
if(*p01 != '.'){
ui->label_last->setText(QString::fromUtf8(p01));
}else{
ui->label_last->setText(QString::fromUtf8(p01-1));
}
p = num2;
p02 = p01;
p01 = num2;
}
void Widget::on_but_add_clicked()
{
char *add = num1;
while(*add)
{
add++;
if(*add == '+')
return;
}
*p = '+';
flag = p;
p++;
if(*p01 != '.'){
ui->label_last->setText(QString::fromUtf8(p01));
}else{
ui->label_last->setText(QString::fromUtf8(p01-1));
}
p = num2;
p02 = p01;
p01 = num2;
}
void Widget::on_but_equ_clicked()
{
float result;
if(*flag == '/')
{
/*if(0 == atof(p01))
{
QMessageBox::warning(this, "计算器", "除数不能为0!!!");
on_but_CE_clicked();
return;
}*/
result = atof(p02) / atof(p01);
}else if(*flag == '*')
result = atof(p02) * atof(p01);
else if(*flag == '+')
result = atof(p02) + atof(p01);
else if(*flag == '-')
result = atof(p02) - atof(p01);
else
result = atof(p02);
char *equ = num1;
while(*equ)
{
equ++;
if(*equ == '=')
return;
}
*p = '=';
flag = p;
p++;
strcat(num1,num2);
ui->label_last->setText(QString::fromUtf8(num1));
ui->label_now->setText(QString::number(result));
memset(num1,0,sizeof(num1));
memset(num2,0,sizeof(num2));
p = num1;
p01 = num1;
}
void Widget::on_but_CE_clicked()
{
ui->label_now->setText("0");
ui->label_last->clear();
memset(num1,0,sizeof(num1));
memset(num2,0,sizeof(num2));
p = num1;
p01 = num1;
p02 = num2;
flag = num1;
}
评论0