#include<iostream>
#include <stdio.h>
#include <tchar.h>
#include<fstream>
#include<windows.h>
#include<time.h>
#include<string>
#include<conio.h>
using namespace std;
const int Max_len = 20;//栈的长度
class shuzizhan//数字栈
{
public:
	shuzizhan() :base(NULL), top(NULL), maxsize(OP){}
	void initzhan(int size=Max_len)//构造一个空栈
	{
		maxsize = size;
		if (base)delete []base;//新建失败
		top = base = new int[size];
	}
	bool kongzhan()//空返回1,非空返回0;
	{
		return base == top;
			
	}
	bool zhanman()//满栈返回1,否则返回0
	{
		return base - top >= maxsize;
	}
	void push(int x)
	{
		if (!zhanman())
		{
			*(top++) = x;
		}
	}
	void pop(int &x)
	{
		if (!kongzhan())
		{	
			x = *--top;
		}
	}
	int gettop()
	{
		return *(top - 1);
	}
	~shuzizhan()
	{
		delete[]base;
	}
private:
	int	maxsize;
	int *base, *top;
};
//==========================================================================================
class ysf//运算符栈
{
public:
	ysf() :base(NULL), top(NULL), maxsize(OP){}
	void initzhan(int size = Max_len)//构造一个空栈
	{
		maxsize = size;
		if (base)delete[]base;//新建失败
		top = base = new char[size];
	}
	bool kongzhan()//空返回0,非空返回1;
	{
		if (base == top)
			return 0;
		return 1;
	}
	bool zhanman()//满栈返回0,否则返回1
	{
		if (base - top == maxsize)return 0;
		return 1;
	}
	void push(char x)
	{
		if (zhanman())
		{
			*top++ = x;
		}
	}
	void pop(char &x)
	{
		if (kongzhan())
		{
			x = *--top;
		}
	}
	int gettop()
	{
		return *(top - 1);
	}
	~ysf()
	{
		delete[]base;
	}
private:
	int	maxsize;
	char *base, *top;
};
class qz//求值类
{
private:
	int	maxsize;
	char *base, *top;
	shuzizhan shuju; 
	ysf ys;
public:
	~qz()
	{
	}
bool yunsuanfu(char x)//是运算符返回1;不是运算符返回0
{	
	char yunsuanf[8] = "+-*/(#)";
	for ( int i=0; yunsuanf[i] != '\0'; i++)
		{
		if (x == yunsuanf[i])
			return true;
		}
		return false;
}
int youxianji(char x)//优先级
	{
		switch (x)
		{
			case'#':return 1;  
			case')':return 2;  
			case'+':return 3;  
			case'-':return 3; 
			case'*':return 4; 
			case'/':return 4;
			case'(':return 5; 
			default:return 0;
		}
	}
int yunsuan(int a,char x,int b)
	{
	switch (x)
		{
			case'+':return (a + b);
			case'-':return (a - b);
			case'*':return (a * b);
			case'/':return (a / b);
		}
	}
char youxianjibijiao(char x, char y)
	{
	if (x == '('&&y == ')')
		return '=';
	else if (youxianji(x) - youxianji(y) >= 0 || x == '(')
		return '>';
	return '<';
	}
int qiuzhi(char *str)
	{	
	char s1[100]="\0";
	strcpy_s(s1, str);
	int a=0, b=0, i = 0;
	char op='\0';
	shuju.initzhan();
	ys.initzhan();
	strcat_s(s1, "#");
	ys.push('#');
	while (s1[i] != '#' || ys.gettop() != '#')
		{
		if (!yunsuanfu(s1[i]))
			{
				shuju.push(atoi(&s1[i]));
				while (!yunsuanfu(s1[++i]));
			}
		else switch (youxianjibijiao(ys.gettop(), s1[i]))
				{
					case'=': ys.pop(op); i++; break; 
					case'<': ys.push(s1[i++]);break; 
					case'>': shuju.pop(a); 
							ys.pop(op); 
							shuju.pop(b);
							shuju.push(yunsuan(a, op, b));
							break;
				}

		}
	s1[strlen(s1) - 1] = '\0';//删除#
	return abs(shuju.gettop());
	}

};
void tuichu()
	{
	system("cls");
	cout << "======================================================\n";
	cout << "=====================感谢你的使用=====================\n";
	cout << "=======================再见!!=======================\n";
	cout << "======================================================\n";
	system("pause");
	exit(0);
	}
void jiemian()
	{
		system("cls");
		cout<<"======================================================\n";
		cout<<"===================欢迎使用自测系统===================\n";
	    cout<<"=============(1)=======创建题目=======================\n";
		cout<<"=============(2)=======开始答题=======================\n";
		cout<<"=============(3)=======退出系统=======================\n";
		cout<<"======================================================\n";
	}
void chuangjian()
{
	system("cls");
	fstream  file("F:\\data.txt", ios::app);
	int n=0;
	string timu[20];
	for (int i = 0;; i++)
	{
		cout << "请输入你创建的题目回车结束输入....\n";
		cin >> timu[i];
		cout << "是否继续录入题目.....\n";
		cout << "(0)否=====\n";
		cout << "(1)是=====\n";
		n++;
		if (_getch() == '0')break;
		fflush(stdin);
	}
	cout << "你输入的题目为:\n";
	for (int i = 0; i < n; i++)
		{
			cout << timu[i] << endl;
			file <<'\n'<< timu[i];
		}
	file.close();
	}
void shuiji()
{
	system("cls");
	qz a;
	int i, j, n=0;
	int jieguo[5];
	int sz[5], jg[5];
	char timu[5][20];
	srand(time(NULL));
	char shuju[20][30];
	ifstream  file("F:\\data.txt");
	for (int i = 0; i < 20; i++)
	{
		file >> shuju[i];
	}
	sz[0] = rand() % 20;
	for (i = 1; i < 5; i++)//产生5个0-20不相同的随机数
	{
		sz[i] = rand() % 20;
		for (j = 0; j < i; j++)
		{

			while (sz[i] == sz[j])
			{
				sz[i] = rand() % 20;
			}
		}
	}
	for (int i = 0; i < 5; i++)
	{
		strcpy_s(timu[i], shuju[sz[i]]);
		cout << timu[i] << endl;
		jg[i] = a.qiuzhi(timu[i]);
	}
	cout << "开始答题,计时开始!!!"<<endl<<"每题20分"<<endl;
	for (int i = 0; i < 5; i++)
	{
		cout << "第" << i + 1 << "题结果为:" << endl;
		cin >> jieguo[i];
		if (abs(jieguo[i]) == jg[i])n++;
		cout << "第" << i + 1 << "题正确答案为:" << jg[i]<< endl;
	}
	cout << "你的得分为:"<<n*20<<"分"<<endl;
	file.close();
}
int _tmain(int argc, _TCHAR* argv[])
{
	time_t tm1, tm2;
	tm1 = GetTickCount();
	jiemian();
	s:switch (_getch())
	{
	case'1':chuangjian(); break;
	case'2':shuiji(); break;
	case'3':tuichu();
	default:cout << "输入错误!!,请重新输入" << endl; goto s;
	}

	tm2 = GetTickCount();
	cout << "你用了" << (tm2 - tm1) / 1000 << "秒" << endl;
	return 0;
}