⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getint(chengchu).txt

📁 输入含数字的式子(可以用多层括号嵌套)
💻 TXT
字号:
CString	CCalcDlg::GetInt(CString str)
{
	int count[10][2];for(int i=0;i<10;i++){count[i][0]=-1;count[i][1]=-1;}
	/////以上初始化数组count,使之全为-1
	int cou=0;//用于填充数组count的第一维
	int sgn=0;//用于记录无用的“(”和“)”,遇“(”加1,遇“)”减1。
	for(i=str.GetLength()-1;i>=0;i--)
	{
		if(str.GetAt(i)==41)
		{
			if(sgn==0)
				count[cou][0]=i;
			sgn++;
		}
		if(str.GetAt(i)==40)
		{
			if(sgn==1)
			{
				count[cou][1]=i;
				cou++;
			}
			sgn--;
		}
	}
		//以上得到m_str是几个(...)组成的
	if(str.GetAt(0)=='+')str.Delete(0);
	if(str.SpanIncluding("0123456789").GetLength()==str.GetLength())
		return str;
	CString s;s=str;
		for(i=0;i<str.GetLength();i++)//负负得正;“--”号变“+”。
		{
			if(str.GetAt(i)=='-'&&i!=0&&str.GetAt(i+1)=='-')
			{
				str.Delete(i,2);
				str.Insert(i,'+');
			}
		}
		s=str;
		for(i=0;i<str.GetLength();i++)//减法变加法;“-”号变“+-”。
		{
			if(str.GetAt(i)=='-'&&i!=0&&str.GetAt(i-1)!='+')
				str.Insert(i,'+');
		}
		s=str;
		for(i=0;i<str.GetLength();i++)//去掉多余加号
		{
			if(str.GetAt(i)=='+'&&str.GetAt(i+1)=='+')
			{s.Delete(i);}
		}
		str=s;
//		MessageBox(s);
	if(cou==0)//式中没有括号
	{int cheng=0,chengdown=0,chengup=0;
		CString chengstr,strup="9",strdown="8";
		for(i=0;i<str.GetLength();i++)//实现乘法
		{
			if(str.GetAt(i)=='*')
			{
				for(chengup=i-1;chengup>=0;chengup--)
				{
					if(str.GetAt(chengup)<48||str.GetAt(chengup)>57)
					{break;	}
					//	strup.Format("%d\n%d",chengup,i);
				}
				for(chengdown=i+1;chengdown<str.GetLength();chengdown++)
				{
					if(str.GetAt(chengdown)<48||str.GetAt(chengdown)>57)
						break;
				}
				break;
			}
		}
		if(chengup&&chengdown)
		{
			strup=str.Mid(chengup+1,i-chengup-1);
			strdown=str.Mid(i+1,chengdown-i-1);
			cheng=GetData(strup)*GetData(strdown);
			chengstr.Format("%d",cheng);
			str.Delete(chengup+1,chengdown-chengup-1);
			str.Insert(chengup+1,chengstr);
			return GetInt(str);
		}
//		strdown.Format("chengup=%d\ni=%d\nchengdown=%d",chengup,i,chengdown);
//						MessageBox(str);
		
//		MessageBox(strdown);
//				int chenganswer=
//					GetData();
	//			chengstr.Format("%d",chenganswer);
//				str.Delete(chengup+1,chengdown-chengup-1);
//				str.Insert(chengup+1,chengstr);
int chu=0,chudown=0,chuup=0;
		CString chustr,struup="9",strudown="8";
		for(i=0;i<str.GetLength();i++)//实现乘法
		{
			if(str.GetAt(i)=='/')
			{
				for(chuup=i-1;chuup>=0;chuup--)
				{
					if(str.GetAt(chuup)<48||str.GetAt(chuup)>57)
					{break;	}
					//	strup.Format("%d\n%d",chengup,i);
				}
				for(chudown=i+1;chudown<str.GetLength();chudown++)
				{
					if(str.GetAt(chudown)<48||str.GetAt(chudown)>57)
						break;
				}
				break;
			}
		}
		if(chuup&&chudown)
		{
			strup=str.Mid(chuup+1,i-chuup-1);
			strdown=str.Mid(i+1,chudown-i-1);
			chu=GetData(struup)/GetData(strudown);
			chustr.Format("%d",chu);
			str.Delete(chuup+1,chudown-chuup-1);
			str.Insert(chuup+1,chustr);
			return GetInt(str);
		}

		for(i=0;i<str.GetLength();i++)//实现加法
		{
			if(str.GetAt(i)=='+')
			{
				CString temp1,temp2;int xx=0;
				temp1=str.Left(i);
				temp2=GetInt(str.Mid(i+1));
				xx=GetData(temp1)+GetData(temp2);
				s.Format("%d",xx);
//				MessageBox(temp1);
				return s;
			}
		}
	}
	return s;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -