华为2018年7月实习生招聘编程题目三解答

1.题目描述:


2.代码如下:

#include <sstream>
#include <conio.h>
#include <vector>
#include <iosfwd>
#include <iostream>


using namespace std;
int CalCount(vector<string> str,int &len)
{
	int Firstbyte = 0;
	int Secondbyte = 0;
	if (str.size()==2)//有符号的情况
	{
		if (str[0] == "char")
		{
			Firstbyte = 1;
		}
		if (str[0] == "short")
		{
			Firstbyte = 2;
		}
		if (str[0] == "int" || str[0] == "long")
		{
			Firstbyte = 4;
		}
		string a = str[1];
		if (a[0]=='*')
		{
			len = 4;
		}
		else
		{
			string number = a.substr(a.find("[") + 1, a.find("]") - a.find("[") - 1);//找到中括号里的数字(字符串形式)
			//将字符串形式的数字转换为int型
			stringstream stream(number);
			stream >> Secondbyte; 
			len = Firstbyte*Secondbyte; //计算所占用的字节数
		}
		
	}

	if (str.size() == 3)//无符号的情况
	{
		if (str[0]=="unsigned")
		{
			if (str[1] == "char")
			{
				Firstbyte = 1;
			}
			if (str[1] == "short")
			{
				Firstbyte = 2;
			}
			if (str[1] == "int" || str[1] == "long")
			{
				Firstbyte = 4;
			}
			string a = str[2];
			if (a[0] == '*')
			{
				len = 4;
			}
			else
			{
				string number = a.substr(a.find("[") + 1, a.find("]") - a.find("[") - 1);
				stringstream stream(number);
				stream >> Secondbyte;
				len = Firstbyte*Secondbyte;
			}
		}
	}
	return 0;
}

int main()
{
	char str[100];	
	int length = 0;
	while (1)
	{
		cin.getline(str, 100);//从用户输入获得字符串数组
		string word(str);//将字符串数组转换为字符串
		vector<string> res;//存放分割后的字符
		string result;//暂时存放从word中读取的字符串
		stringstream input(word);//将string类型的word复制给input
		while (input >> result) //在stringstream类中构造字符串流时,空格会成为字符串参数的内部分界
		{
			res.push_back(result);
		}
		CalCount(res, length);
		cout << length << endl;
		length = 0;
		if ( _getch() == 27)
		{
			break;
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值