`
xumingrencai
  • 浏览: 1183563 次
文章分类
社区版块
存档分类
最新评论

华为 机试题

 
阅读更多
// MyC.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <assert.h>
#include <string.h>
#include <math.h>

/*
 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”
*/
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
{
	char cTmp = 0;
	long lIdx = 0;

	/* 输入参数判断 */
	assert(NULL != pInputStr);
	assert(NULL != pOutputStr);
	if (0 == lInputLen)
	{
		return;
	}

	while ((lIdx < lInputLen) && (pInputStr[lIdx] != '\0')) /* 遍历输入字符串的字符 */
	{ 
		cTmp = pInputStr[lIdx++]; /*  */
		
		unsigned int i = 0;
		unsigned int uiOutputLen = strlen(pOutputStr);
		while (i < uiOutputLen) /* 判断输出数组中是否存在相同的字符 */
		{
			if (cTmp == pOutputStr[i])
			{
				break;
			}

			i++;
		}

		if (i == uiOutputLen) /* 说明输出数组中没有相同的字符 */
		{
			pOutputStr[i] = cTmp;
		}
		else
		{
			continue;
		}
	}

	return ;
}

void stringFilter_test()
{
	const char acInputStr[100] = "pppppppp";
	char acOutputStr[100] = {0};

	stringFilter(acInputStr, 100, acOutputStr);

	printf("%s\n", acOutputStr);
}

/*
 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
压缩规则:
1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc".
2. 压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz" 
*/
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
{
	/* 入参合法性判断 */
	assert(NULL != pInputStr);
	assert(NULL != pOutputStr);
	if (lInputLen <= 0)
	{
		assert(0);
		return ;
	}

	long lCount = 1;         /* 相同字符计数器 */
	long lIdx = 1;           /* 从第1个字符开始遍历,0表示字符数组起始 */
	long lOutputIdx = 0;
	char cTmp;
	while (lIdx < lInputLen) /* 遍历输入字符串 */
	{
		cTmp = pInputStr[lIdx];
		if (cTmp == pInputStr[lIdx - 1]) /* 与前一个值进行比较 */
		{
			lCount++;
		}
		else
		{

			if (lCount > 1)  /* 判断重复字符的格式,只有重复字符大于1才能进行压缩 */
			{
				pOutputStr[lOutputIdx++] = (char)lCount+'0'; /* 数字转换为字符 */
				pOutputStr[lOutputIdx++] = pInputStr[lIdx - 1];
			}
			else
			{
				pOutputStr[lOutputIdx++] = pInputStr[lIdx - 1];
			}

			lCount = 1; /* 重新初始化计数器 */

			if (pInputStr[lIdx] == '\0') /* 如果输入字符串结束,则跳出循环 */
			{
				break;
			}

		}

		lIdx++ ;
	}

	return ;
}

void stringZip_test()
{
	const char acInputStr[100] = ""; //pppppppp  cccddecc
	char acOutputStr[100] = {0};

	stringZip(acInputStr, 100, acOutputStr);

	printf("%s\n", acOutputStr);
}


/*
 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。

补充说明:
1. 操作数为正整数,不需要考虑计算结果溢出的情况。
2. 若输入算式格式错误,输出结果为“0”。 

本程序可以计算超出100的整数。
*/
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{
	/* 入参合法性判断 */
	assert(NULL != pInputStr);
	assert(NULL != pOutputStr);
	if (lInputLen <= 0)
	{
		assert(0);
		return ;
	}

	long lIdx = 0;
	long lTmp = 0;
	int iOp1Num = 0;
	int iOp2Num = 0;
	char cOp = 0; /* 运算符 */

	/* 取出第一个操作数 */
	while (pInputStr[lIdx] != ' ') /* 计算lIdx,使其定位到第一个空格处 */
	{
		lIdx++;
	}
	while (lTmp < lIdx)
	{
		iOp1Num += (pInputStr[lTmp] - '0') * (int)pow(10.0,(lIdx - lTmp - 1));
		lTmp++;
	}
	printf("Op1 %d\n", iOp1Num);

	/* 取出操作符 */
	lTmp = lIdx;
	lIdx++;
	while (pInputStr[lIdx] != ' ') /* 计算lIdx,使其定位到第二个空格处 */
	{
		lIdx++;
	}
	if ((lIdx - lTmp) > 2)
	{
		printf("Input param error. \n");
		return ;
	}
	else if ((lIdx - lTmp) == 2)
	{
		cOp = pInputStr[lIdx - 1];
	}
	printf("Op %c\n", cOp);

	/* 取出第二个操作数 */
	lTmp = lIdx;
	while (pInputStr[lIdx] != '\0') /* 计算lIdx,使其定位到输入字符串的最后 */
	{
		lIdx++; 
	}
	lTmp++;
	while (lTmp < lIdx)
	{
		iOp2Num +=  (pInputStr[lTmp] - '0')  * (int)pow(10.0, lIdx - lTmp -1);
		lTmp++ ;
	}
	printf("Op2 %d\n", iOp2Num);

	int iResult = 0;
	switch (cOp)
	{
	case '+':
		{
			iResult = iOp1Num + iOp2Num;
			break;
		}
	case '-':
		{
			iResult = iOp1Num - iOp2Num;
			break;
		}
	default:
		assert(0);
	}

	printf("iResult = %d\n", iResult);

	
	/* 先翻转结果值 */
	int iRetTmp = 0;
	while (iResult)
	{
		iRetTmp *= 10;
		iRetTmp += iResult % 10;
		iResult /= 10;
	}
	printf("iRetTmp = %d\n", iRetTmp);

	/* 将计算结果写入输出字符数组中 */
	int iOutputIdx = 0;
	while (iRetTmp)
	{
		pOutputStr[iOutputIdx++] = (iRetTmp % 10 + '0'); /* 注意需要将整数转换为字符哦 */
		iRetTmp /= 10;
	}


	return;
}

void arithmetic_test()
{
	const char acInputStr[100] = "22 + 3"; //99 ++ 77   22 + 3
	char acOutputStr[100] = {0};

	arithmetic(acInputStr, 100, acOutputStr);

	printf("%s\n", acOutputStr);
}


int _tmain(int argc, _TCHAR* argv[])
{
	//stringZip_test();
	arithmetic_test();
	
	return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics