课程作业六

第六次课程作业

QT 学习

Hello World学习
#include <qapplication.h>
#include <qpushbutton.h>


int main( int argc, char **argv )
{
    QApplication a( argc, argv );

    QPushButton hello( "Hello world!", 0 );
    hello.resize( 100, 30 );

    a.setMainWidget( &hello );
    hello.show();
    return a.exec();
}
#include <qapplication.h>

包含了QApplication类的定义,管理了应用程序的广泛资源,如默认字体和光标.

#include <qpushbutton.h>
 QPushButton hello( "Hello world!", 0 );

QPushButton.h包含QPushButton类的定义,用于创建图形用户界面按钮.
QPushButton hello( "Hello world!", 0 )创建了一个 hello按钮,其显示hello world字样。

hello.resize( 100, 30 );

此句用于设置hello按钮宽高。

hello.show();

把hello按钮变为可见。


QT一直安装失败所以还未能实践操作。


尝试用栈实现四则运算

将中缀表达式转换为逆波兰表达式算法:
  • 构造一个运算符栈,运算符在栈内遵循越往栈顶优先级越高的原则
  • 读入中序表达式,从左至右扫描算术表达式,判断字符如果是数字则分析到该字符串结束,并将其直接输出。如果是运算符,则需比较优先级关系:
  • 将该字符与栈顶运算符优先级比较,如果该字符优先级高于栈顶运算符,则将该运算符入栈。否则将栈顶元素弹出,直到栈顶运算符低于当前运算符,将当前字符入栈。
  • 重复步骤2直到所有字符得到正确处理即将中序算式转换成逆波兰表达式.
- 编码实现
定义字符串优先级
int priority(char ch) 
 {
 	int i;
 	switch(ch)
 	{
 		case '(':i=;break;
 		case '+':
 		case '-':i=2;break;
 		case '*':
 		case '/':i=3;break;
 		case ')':i=4;break;
 	}
 	return i;
 }
转换成逆波兰表达式
void tonibolan(char *ch,char retch[100])
 {
 	stack<char> st2;
 	int i=0;
 	while(*ch!='\0')
 	{
 		if(*ch>='0'&&*ch<='9')
 		{
 			retch[i++]=*ch;
 		}
 		else if(*ch=='(')
 		{
 			st2.push(*ch);
 		}
 		else if(*ch==')')
 		{
 			while(st2.top()!='(')
 			{
 				retch[i++]=st2.top();
 				st2.pop();
 			}
 			if(st2.top()=='(')
 			{
 				st2.pop();
 			}
 		}
 		else if(st2.empty()||priority(*ch)>priority(st2.top()))
 		{
 			st2.push(*ch);
 		}
 		else
 		{
 			while(priority(*ch)<=priority(st2.top()))
 			{
 				retch[i++]=st2.top();
 				st2.pop();
 				if(st2.empty())
 				{
 					break;
 				}
 			}
 			st2.push(*ch);
 		}
 		ch++;
	 }
	 while(!st2.empty())
	 {
	 	retch[i++]=st2.top();
	 	st2.pop();
	 }
 }

posted on 2017-06-01 21:50  code-123  阅读(133)  评论(0编辑  收藏  举报

导航