# 华为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;
}