第六次课程作业

课程作业六

算式核心算法

这里举例一个类型的算式(如(3+5)/4-1)
生成算式的代码如下

srand(time(0));
	double result = 0;
	int result2 = 0;
	int n1, n2, n3, n4, o1, o2, o3, judge = 1;
	char op1, op2, op3;
	while (true)
	{
		n1 = randomnum();
		n2 = randomnum();
		n3 = randomnum();
		n4 = randomnum();//生成4个数字
		o1 = randomoperator();
		o2 = randomoperator();
		o3 = randomoperator();//生成3个运算符代表的数字
		op1 = transform(o1);
		op2 = transform(o2);
		op3 = transform(o3);//将3个运算符代表队数字转化为运算符
		char formula[9];
		formula[0] = '(';
		formula[1] = n1 + '0';
		formula[2] = op1;
		formula[3] = n2 + '0';
		formula[4] = ')';
		formula[5] = op2;
		formula[6] = n3 + '0';
		formula[7] = op3;
		formula[8] = n4 + '0';//将运算符连同数字拼接起来放进字符数组里
		Calculation calculation(formula, 9);//将字符数组传递给计算算式的函数

计算算式的代码如下

stack <double> numstack;//申请一个存放数字的栈
	stack <char> operatorstack;//申请一个存放字符的栈
	for (int i = 0; i<len; i++)
	{
		if (isdigit(formula[i]))//如果是数字 放入numstack中
		{
			int count=0;
			numstack.push(getfornum(i, count));//getfornum是提取运算符前面的数字的函数,可以提取单位数,多位数,小数
			i = i + count - 1;
		}
		else if (formula[i] == '*')//如果是*号 提取栈顶的一个数字以及*号后面的数字 两数字相乘 放入numstack中
		{
			if (formula[i + 1] == '(')//如果*号后面是括号 将*号放入字符栈中
			{
				operatorstack.push(formula[i]);
				continue;
			}
			double n1, n2;
			n1 = numstack.top();
			numstack.pop();
			int count;
			n2 = getfornum(i, count);
			numstack.push(n1*n2);
			i = i + count;
		}
		else if (formula[i] == '/')//如果是/号 提取栈顶的一个数字以及/号后面的数字 两数字相除 放入numstack中
		{
			if (formula[i + 1] == '(')//如果/号后面是括号将/号放入字符栈中
			{
				operatorstack.push(formula[i]);
				continue;
			}
			double n1, n2;
			int count;
			n2 = getfornum(i, count);
			if (n2 == 0.0)
			{
				Flag = 0;
				break;
			}
			n1 = numstack.top();
			numstack.pop();
			numstack.push(n1 / n2);
			i = i + count;
		}
		else if (formula[i] == '(')//如果是左括号 放入operatorstack中
			operatorstack.push(formula[i]);
		else if (formula[i] == ')')//如果是右括号
		{
			double sum = 0;//sum为括号中计算后的总值
			if (operatorstack.top() == '(')//如果是两个括号中间没有运算符 去掉左括号 继续下一步操作
			{
				operatorstack.pop();//去除左括号
				continue;
			}
			while (operatorstack.top() != '(')//计算到右括号到左括号中间的总值
			{
				if (operatorstack.top() == '-')
				{
					sum = sum + 0 - numstack.top();
					numstack.pop();
				}
				else
				{
					sum = sum + numstack.top();
					numstack.pop();
				}
				operatorstack.pop();
				if (operatorstack.top() == '(')
				{
					sum = sum + numstack.top();
					numstack.pop();
				}
			}
			numstack.push(sum);//将总值放入numstack中
			operatorstack.pop();//去除左括号
		}
		else
			operatorstack.push(formula[i]);//如果是+或者-放入栈中
	}
	while (!numstack.empty()&&Flag==1)//计算处理完后栈内剩余的值
	{
		int flag = 0;//flag是用来判断operatorstack是否为空
		if (!operatorstack.empty())//判断operatorstack是否为空
		{
			flag = 1;
			if (operatorstack.top() == '*')
			{
				int n1, n2;
				n1 = numstack.top();
				numstack.pop();
				n2 = numstack.top();
				result += n1*n2;
			}
			else if (operatorstack.top() == '/')
			{
				int n1, n2;
				n1 = numstack.top();
				if (n1 == 0)
				{
					Flag = 0;//Flag是判断除数是否为0
					break;
				}
				numstack.pop();
				n2 = numstack.top();
				result += n2 / n1;
			}
			else if (operatorstack.top() == '-')//如果是-号 result减去numstack栈顶的值
				result = result - numstack.top();
			else
				result = result + numstack.top();
		}
		if (!operatorstack.empty())
			operatorstack.pop();
		if (flag == 0)
			result += numstack.top();
		numstack.pop();
		if (operatorstack.empty() == true && !numstack.empty())
		{
			result += numstack.top();
			numstack.pop();
		}
	}

然后检验算式的结果是否为整数

result=calculation.getResult();
		if (result - (int)result == 0)
			break;

程序界面的学习

我选择qt进行程序界面的学习。我照着教程做了一个登陆界面

image

登陆后是一个文本编辑器

image

能够写入文本保存文本以及另存文本三个功能,后面的功能我会在之后陆续实现
还有的问题是中文乱码的问题,这个由于时间有限,暂时还未解决,后面会搞定的。

下面是主窗口的代码

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QMessageBox>
#include<QPushButton>
#include<QFileDialog>
#include<QTextStream>
#include<QTextCodec>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    isUntitled=true;
    curFile=tr("未命名");
    setWindowTitle(curFile);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::newFile()
{
    if(maybeSave()){
        isUntitled = true;
        curFile = "未命名";
        setWindowTitle(curFile);
        ui->textEdit->clear();
        ui->textEdit->setVisible(true);
    }
}

bool MainWindow::maybeSave()
{
    //如果文档被修改了
    if(ui->textEdit->document()->isModified())
    {
        //自定义一个警告框
        QMessageBox box;
        box.setWindowTitle("警告");
        box.setIcon(QMessageBox::Warning);
        box.setText(curFile+tr("尚未保存,是否保存"));
        QPushButton *yesBtn =box.addButton(tr("是(&Y)"),QMessageBox::YesRole);
        QPushButton *cancelBut =box.addButton(tr("取消"),QMessageBox::RejectRole);
        box.exec();
        if(box.clickedButton()==yesBtn)
            return save();
        else if(box.clickedButton()==cancelBut)
            return true;
    }
}

bool MainWindow::save()
{
    if(isUntitled)
    {
        return saveAs();
    }
    else
        return saveFile(curFile);
}

bool MainWindow::saveAs()
{
    QString filename=QFileDialog::getSaveFileName(this,tr("另存为"),curFile);
    if(filename.isEmpty())
        return false;
    return saveFile(filename);
}

bool MainWindow::saveFile(const QString &filename)
{
    QFile file(filename);
    if(!file.open(QFile::WriteOnly|QFile::Text))
    {
        QMessageBox::warning(this,tr("多文档编辑器"),tr("无法写入 %1:/n %2").arg(filename).arg(file.errorString()));
        return false;
    }
        QTextStream out(&file);
        QApplication::setOverrideCursor(Qt::WaitCursor);
        out<<ui->textEdit->toPlainText();
        QApplication::restoreOverrideCursor();
        isUntitled=false;
        curFile=QFileInfo(filename).canonicalFilePath();
        setWindowTitle(curFile);
        return true;
}

void MainWindow::on_actionNEW_N_triggered()
{
    newFile();
}

void MainWindow::on_actionSAVE_S_triggered()
{
    save();
}

void MainWindow::on_actionANOTHER_SAVE_A_triggered()
{
    saveAs();
}

这些都是看教程写的,代码不难看懂,难的是不知道那些函数是干嘛的,要去百度,这需要花费时间,我会着手去解决。

posted on 2017-05-31 17:58  caiziyang  阅读(148)  评论(0编辑  收藏  举报