华为笔试2014---练习

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;
/*
通过键盘输入一串小写字母(a~z)组成的字符串。
请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”。
*/
void stringFilter(const  char *pInputStr,long lInputLen,char *pOutputStr)
{// 主意这里的const 不可修改 pInputStr的值
    int Outputlen = 0;
    bool flag = false;
    for (int i = 0; i < lInputLen; i++)
    {
        flag = true; 
        int j = Outputlen-1;
        while (j >= 0)
        {
            if (pInputStr[i] == pOutputStr[j])
            {
                flag = false;
                break;
            }
            else
            {
                if (j == 0)
                    flag = true;
            }

            j--;
        }
        if (flag == true)
        {
            Outputlen++;
            pOutputStr[Outputlen-1] = pInputStr[i];
        }
    }

    for (int i = 0; i < Outputlen; i++)
    {
        printf("%c",pOutputStr[i]);
    }
}


/**
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
压缩规则:
1、仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc"。
2、压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"。

要求实现函数:
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);
*/

void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
{
    string str(pInputStr, pInputStr + lInputLen);
    int lOuputlen = 0;
    string strtemp="";
    int temp = 0;
    while (temp != lInputLen)
    {
        int count = 0;
        for (int j = temp; j < lInputLen; j++)
        {
            if (str[temp] == str[j])
                count++;
            else
                break;
        }
        if (count != 1)
        {
            pOutputStr[lOuputlen] = count + '0';
            pOutputStr[lOuputlen + 1] = str[temp];
            lOuputlen = lOuputlen + 2;
        }
        else
        {   
            // 如果字符传中有个字母没有重复。
            pOutputStr[lOuputlen++] = str[temp];
        }
        temp = temp + count;
    }

    for (int i = 0; i < lOuputlen; i++)
    {
        printf("%c",pOutputStr[i]);
    }
}

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

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

要求实现函数:
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);
*/

void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{
    int res = 0;
    string str(pInputStr, pInputStr + lInputLen);
    int firstIndex = str.find_first_of(" ");
    string firstNum= str.substr(0,firstIndex);
    int lastIndex = str.find_last_of(" ");
    if (lastIndex == firstIndex||(lastIndex-firstIndex>3))
    {
        pOutputStr[0] = '0'; // 表示只能找到一个空格或者没有空格,或者空格较多,格式错误
    }
    string lastNum = str.substr(lastIndex + 1, lInputLen);
    string strOper = str.substr(firstIndex + 1, 1);
    int first = atoi(firstNum.c_str());
    int last = atoi(lastNum.c_str());

    if (strOper == "+")
    {
        res = first + last;
        _itoa(res,pOutputStr,10);
    }
    else if (strOper == "-")
    {
        res = first - last;
        _itoa(res, pOutputStr, 10);
    }
    else
        pOutputStr[0] = '0';  // 表示格式错误,操作符是其他的或者没操作符。

}
int main()
{
    char a[1000];
    char b[1000];
    int  n;
    //scanf("%d %s", &n,a);

    //stringFilter(a, n, b);

    //stringZip(a, n, b);


    scanf("%d ", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%c",&a[i]);
    }
    arithmetic(a, n, b);
    system("pause");
    return 0;
}

下面是原文地址还是人家写的好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值