Пример #1
0
//功能:检测字符串被打散后的字符串中是否含有魔鬼数字
//参数:line是字符串的行数
void checkNum(string test, int line)
{
		int i;
		float temp = 0;
		string single[255] = {""};
		int slen = 0;
		slen = covertCmd(test, single);

		for(i=0; i<slen; i++)
		{
			if(onlyNum(single[i]))
			{
				temp = atof(single[i].c_str());
				if(0 != temp && 1 != temp)
				{
					errNum[errLen] = line;
					errType[errLen] = 4;
					errLen++;
				}
			}
		}
}
Пример #2
0
//情况二:表达式为纯数字运算式 如"-1+(2*3/5)", 可以有大中小括号
void if2()
{
	int i, j;
	string temp = "";
	string single[255] = {""};
	int slen = 0;
	int numFlag = 1;  // 是否为纯数字运算的标志, 是为1,不是为-1
	for(i=0; i<cmdCount; i++)
	{
		numFlag = 1;
		temp = command[i];
		slen = covertCmd(temp, single);
		int pos[10] = {0};
		int posCount = isContain("if",single, slen, pos);
		if(-1 != posCount)  // 含有关键字if
		{
			for(j=pos[0]+1; j<slen; j++) // 遍历if后的所有字符,看是否是纯数字表达式
			{
				if("(" != single[j] && ")" != single[j] && "[" != single[j] && "]" != single[j] &&
					"{" != single[j] && "}" != single[j] &&
					"+" != single[j] && "-" != single[j] && "*" != single[j] && "/" != single[j]
					&& !onlyNum(single[j]))     // 纯数字表达式可能出现的字符, 满足所有条件时不是纯数字
				{
					numFlag = -1;
					break;
				}
			}
			if(1 == numFlag)
			{
				errNum[errLen] = i+1;
				errType[errLen] = 3;
				errLen++;
			}
		}
	}
}
Пример #3
0
//功能:情况三考虑在一个函数中的情况,判断思想是找到if语句中所有的变量名,然后在if语句之前往上查找,如果最后一次出现该变量时是
//+"a=常数"的情况,如果最后是"a>、<、>=、<=、!=、+、-、*、/"的情况就跳过继续往上查,则判断为该变量是不变的,不过这样会有很多的bug,待解决啊……
int ifFunc3(int begin)
{
	string func[255] = {""};
	int flen = 0;
	int rowNum = getFunc(func, begin, flen);	

	int i, j, k;
	if(-1 != rowNum)
	{
		int varEqualNum = 0;
		int useFlag = -1;

		string single[255] = {""};
		int slen = 0;
		string temp = "";

		string oneCmdVarName[255] = {""};
		int cmdVarLen = 0;

		int pos[10] = {-1};
		int posCount = -1;
		for(i=0; i<flen; i++)
		{
			temp = func[i];
			slen = covertCmd(temp, single);

			if(isIn("if", single, slen))  // 在func[i]中检测到if语句
			{
				cmdVarLen = getVarFromOneCmd(begin, temp, oneCmdVarName);  // 得到if条件中变量名

				for(j=0; j<cmdVarLen; j++)    // 遍历if中所有变量
				{
					useFlag = -1;
					varEqualNum = 0;
					for(k=i-1; k>=0; k--)   // 在if语句之前进行检测变量的赋值情况
					{
						temp = func[k];
						slen = covertCmd(temp, single);
						posCount = isContain(oneCmdVarName[j], single, slen, pos);
						int m = 0; // 变量在一行中出现的次数遍历
						if(-1 != posCount)   // 如果出现了变量, 检测最后出现变量的位置是不是a=1的简单形式
						{
							for(m=posCount-1; m>=0; m--) // 从一行最后开始遍历变量
							{
								//single[pos[m]+1] 变量后的第一个字符
								if(">" == single[pos[m]+1] || "<" == single[pos[m]+1] || ">=" == single[pos[m]+1] || 
									"<=" == single[pos[m]+1] || "!=" == single[pos[m]+1] || "+" == single[pos[m]+1] || "-" == single[pos[m]+1] ||
									"*" == single[pos[m]+1] || "/" == single[pos[m]+1])
								{
									continue;
								}
								if("=" == single[pos[m]+1] && onlyNum(single[pos[m]+2])) // a = 数字 的情况
								{
									varEqualNum = 1;
									break;
								}
								else
								{
									varEqualNum = -1;
									break;
								}
							}
							if(1 == varEqualNum || -1 == varEqualNum) // 如果continue了就要继续找下一行,而不是退出for循环
								break;
						}	
					}

					if(-1 == varEqualNum)
					{
						// 说明有变量会变
						useFlag = 1;
						break;   // 不是无效分去
					}
				}

				if(-1 == useFlag)  // 说明一直没有变量变,无效分支
				{
					errNum[errLen] = rowNum-flen+i+1;
					errType[errLen] = 3;
					errLen++;
				}
			}
		}
	}

	return rowNum;
}
Пример #4
0
//功能:在一个函数中检测无效的switch语句
//返回值:返回函数结尾下一行减1
int switchInfunc(int beginFunc)
{
	string func[255] = {""};
	int flen = 0;
	int rowNum = getFunc(func, beginFunc, flen);	

	int i, j, k;
	if(-1 != rowNum)
	{
		int	useFlag = -1;
		int varEqualNum = 0;

		string switchValue = "";  // switch 的值

		string temp = "";
		string single[255] = {""};
		int slen = 0;
		string oneCmdVarName[255] = {""};
		int oneCmdVarLen = 0;
		string switchCmd[255] = {""};
		int switchLen = 0;
		int beginInfunc = 0;

		int pos[10] = {-1};
		int posCount = -1;
		for(i=0; i<flen; i++)
		{
			temp = func[i];
			slen = covertCmd(temp, single);
			if(isIn("switch", single, slen)) //在函数第i行位置 检测到switch语句
			{
				beginInfunc = i;
				int switchPos = getOneSwitch(func, flen, beginInfunc, switchCmd, switchLen); // 得到第一个switch语句
				temp = switchCmd[0];  // switch
				slen = covertCmd(temp, single);
				oneCmdVarLen = getVarFromOneCmd(beginFunc, switchCmd[0], oneCmdVarName);// 得到switch中的变量名
				if(1 == oneCmdVarLen)  //switch中只有一个变量时
				{
					for(j=0; j<oneCmdVarLen; j++)    // 遍历switch中所有变量
					{
						useFlag = -1;
						varEqualNum = 0;
						for(k=i-1; k>=0; k--)   // 在switch语句之前进行检测变量的赋值情况
						{
							temp = func[k];
							slen = covertCmd(temp, single);
							posCount = isContain(oneCmdVarName[j], single, slen, pos);
							int m = 0; // 变量在一行中出现的次数遍历
							if(-1 != posCount)   // 如果出现了变量, 检测最后出现变量的位置是不是a=1的简单形式
							{
								for(m=posCount-1; m>=0; m--) // 从一行最后开始遍历变量
								{
									//single[pos[m]+1] 变量后的第一个字符
									if(">" == single[pos[m]+1] || "<" == single[pos[m]+1] || ">=" == single[pos[m]+1] || 
										"<=" == single[pos[m]+1] || "!=" == single[pos[m]+1] || "+" == single[pos[m]+1] || "-" == single[pos[m]+1] ||
										"*" == single[pos[m]+1] || "/" == single[pos[m]+1])
									{
										continue;
									}
									if("=" == single[pos[m]+1] && onlyNum(single[pos[m]+2])) // a = 数字 的情况
									{
										switchValue = single[pos[m]+2]; // 得到switch的值
										varEqualNum = 1;
										break;
									}
									else
									{
										varEqualNum = -1;
										break;
									}
								}
								if(1 == varEqualNum || -1 == varEqualNum) // 如果continue了就要继续找下一行,而不是退出for循环
									break;
							}	
						}

						if(-1 == varEqualNum)
						{
							// 说明有变量会变
							useFlag = 1;
							break;   // 不是无效分去
						}
					}
					int defaultFlag = -1;
					if(-1 == useFlag)  // 说明一直没有变量变,无效分支
					{
						defaultFlag = -1;
						for(k=0; k<switchLen; k++)
						{
							temp = switchCmd[k];
							slen = covertCmd(temp, single);
							if(isIn("case", single, slen))  // 出现case 分支
							{
								if(switchValue != single[1]) // 当这一行的case值不是switch的时候则报错
								{
									errNum[errLen] = (rowNum-flen+i+1+k);
									errType[errLen] = 3;
									errLen++;
								}
								else   // case值是switch值时,说明default也要报错
									defaultFlag = 1;
							}
						}
						for(k=0; k<switchLen; k++)
						{
							temp = switchCmd[k];
							slen = covertCmd(temp, single);
							if(isIn("default", single, slen) && 1 == defaultFlag)  // 出现case 分支
							{
								errNum[errLen] = (rowNum-flen+i+1+k);
								errType[errLen] = 3;
								errLen++;
							}
						}
					}
				}//switch中只有一个变量时
				
				int defaultFlag = -1;
				if(0 == oneCmdVarLen)  // 说明switch里面没有变量
				{
					temp = switchCmd[0];
					slen = covertCmd(temp, single);
					if(onlyNum(single[2]) && ")" == single[3])   // switch(1) 的形式
					{
						switchValue = single[2];
						defaultFlag = -1;
						for(k=0; k<switchLen; k++)
						{
							temp = switchCmd[k];
							slen = covertCmd(temp, single);
							if(isIn("case", single, slen))  // 出现case 分支
							{
								if(switchValue != single[1]) // 当这一行的case值不是switch的时候则报错
								{
									errNum[errLen] = (rowNum-flen+i+1+k);
									errType[errLen] = 3;
									errLen++;
								}
								else   // case值是switch值时,说明default也要报错
									defaultFlag = 1;
							}
						}
						for(k=0; k<switchLen; k++)
						{
							temp = switchCmd[k];
							slen = covertCmd(temp, single);
							if(isIn("default", single, slen) && 1 == defaultFlag)  // 出现case 分支
							{
								errNum[errLen] = (rowNum-flen+i+1+k);
								errType[errLen] = 3;
								errLen++;
							}
						}
					}
				}
			}   // 检测到switch的位置
		}
	}
	return rowNum;
}
Пример #5
0
/* This is an algorithm that attempts to sort image list into 'A' images and
   'B' images. */
void ImportImages::autoConfig (QStringList importList)
{
    importList.sort();

    int count, i, j, k, rows, nameLength, sizeCount, minLength, minNumber;
    QString pass, str, str1, str2, common;
    QStringList tempList, tempA, tempB;
    QList<int> numList;
    bool stop = false, more = true;
    bool *taken;

    rows = importList.size();
    pass = importList.at(0);
    taken = new bool [rows + 1];
    nameLength = pass.length();
    sizeCount = 0;
    for (i = 0; i < rows; i++)
    {
        pass = importList.at(i);
        taken[i] = false;
        if (pass.length() != nameLength) sizeCount++;
    }

    if (sizeCount == 0)
    {
        j = 0;
        for (i = 0; i < rows; i = i + 2)
        {
            if ((i+1) < importList.size())
            {
                str1 = importList.at(i);
                str2 = importList.at(i+1);
                frameA.insert(j,str1);
                frameB.insert(j,str2);
                j++;
            }
        }
    }

    else
    {
        /* Thoughts on algorithm

        - Remove preceding part of string that is common to all files in the list
        - need to make a temporary QStringList
        - Remove the end of string that is common to all files in the list
        - Now we have two options (if files are ordered in some sensical way) :
        1) strings with 1a, 1b, 2a, 2b, ... 100a, 100b, .... (possible '_a' or preceding 'a', etc.)
        2) strings with 1, 2, 3, ...., 100, 101, ....
        - Test for option (2).  If true then distribute the list alternately and hope for the best.  If false
        then find matching 'a' and 'b' pairs and distribute them between the lists
        */
        common = importList.at(0);
        j = common.size();
        stop = false;
        while (!stop)
        {
            j--;
            common.resize(j);
            count = 0;
            for (i = 0; i < rows; i++)
            {
                pass = importList.at(i);
                if (pass.contains(common)) count++;
            }
            if (count == rows || common.size() == 0) stop = true;
        }

        for (i = 0; i < rows; i++)
        {
            pass = importList.at(i);
            tempList.append(pass.remove(common));
        }

        common = tempList.at(0);
        stop = false;
        count = 0;
        while (!stop)
        {
            common.remove(0,1);
            for (i = 0; i < rows; i++)
            {
                pass = tempList.at(i);
                if (pass.contains(common)) count++;
            }
            if (count == rows || common.size() == 0) stop = true;
        }

        count = 0;
        for (i = 0; i < rows; i++)
        {
            pass = tempList.at(i);
            pass.remove(common);
            tempList.insert(i,pass);
            if (onlyNum(pass)) count++;
        }

        // The case when there are no 'a' or 'b' marking or such, i.e. numbered 1,2,3,...
        if (count > 0)
        {
            for (i = 0; i < rows; i++) numList.append(tempList.at(i).toInt());
            k = 0;
            while (k < (importList.size() / 2))
            {
                int minIndex = -1;
                minNumber = 100000000;
                for (j = 0; j < rows; j++)
                {
                    if (numList.at(j) < minNumber)
                    {
                        minNumber = numList.at(j);
                        minIndex = j;
                    }
                }
                numList.replace(minIndex,100000000);
                numList.replace(minIndex + 1,100000000);
                tempList.removeAt(minIndex);
                tempList.removeAt(minIndex + 1);
                frameA.insert(k,importList.at(minIndex));
                frameB.insert(k,importList.at(minIndex + 1));
                k++;
            }
        }
        // The case of '1a,1b,2a,2b,...'
        else {
            tempA.clear(); tempB.clear();
            for (i = 0; i < rows; i = i + 2)
            {
                str1 = importList.at(i);
                str2 = importList.at(i+1);
                tempA.append(str1);
                tempB.append(str2);
            }

            // Now need to rank the lists
            k = 0;
            while (!tempA.isEmpty())
            {
                minLength = 1000000000; more = true;
                for (i = 0; i < tempA.size(); i++)
                {
                    str1 = tempA.at(i);
                if (str1.size() < minLength) minLength = str1.size();
                }
                while (more)
                {
                    count = 0;
                    for (j = 0; j < tempA.size(); j++)
                    {
                        str1 = tempA.at(j);
                        str2 = tempB.at(j);
                        if (str1.size() == minLength)
                        {
                            frameA.insert(k,str1);
                            frameB.insert(k,str2);
                            tempA.removeAt(j);
                            tempB.removeAt(j);
                            k++;
                        }
                        else count++;
                    }
                    if (count == tempA.size()) more = false;
                }
            }
        }
    }
    delete [] taken;
}