#include <iostream>
#include <vector>
#include<string>
#include<sstream>
using namespace std;
int Num(vector<string>& res, int& arraySize)
{
int countFirst = 0;
int countSecond = 0;
if (2 == res.size()) //两个情况
{
if (res[0] == "char")
{
countFirst = 1;
cout << res[0] << endl;
}
else if (res[0] == "short")
countFirst = 2;
else if (res[0] == "int" || res[0] == "long")
countFirst = 4;
string arrayInput = res[1];
const char* arrs = arrayInput.c_str();
if (arrs[0] == '*')
{
cout << "pointer" << endl;
arraySize = 4;
countFirst = 1;
}
else
{
//取出两个字符之间的字符串
string strSize = arrayInput.substr(arrayInput.find("[") + 1, arrayInput.find("]") - arrayInput.find("[") - 1);
//arraySize = atoi(strSize.c_str());
stringstream stream(strSize);
stream >> arraySize;
//实现小数点前后数据的分隔
float test;
stream >> test;
cout << test << endl;
float book = test + arraySize;
cout << book << endl;
}
return countFirst;
}
else if (3 == res.size()) //三个的情况
{
if (res[1] == "char")
countSecond = 1;
if (res[1] == "short")
countSecond = 2;
if (res[1] == "int" || res[1] == "long")
countSecond = 4;
string arrayInput = res[2];
const char* arrs = arrayInput.c_str();
if (arrs[0] == '*')
{
cout << "pointer" << endl;
arraySize = 4;
countSecond = 1;
}
else
{
string strSize = arrayInput.substr(arrayInput.find("[") + 1, arrayInput.find("]") - arrayInput.find("[") - 1);
// arraySize = atoi(strSize.c_str());
stringstream stream(strSize);
stream >> arraySize;
}
return countSecond;
}
}
void main()
{
char getStrs[50];
cin.getline(getStrs, 50); //从屏幕获得字符串
string word(getStrs); //把字符数组转化为字符串
vector<string> res; //用于存放分割后的字符串
//待分割的字符串,含有很多空格
string word = " Hello, I want to learn C++! ";
//string word = "char str[100] ";
//word = "unsigned long test[100] ";
//word = "unsigned long *test ";
//word = "int *test ";
//暂存从word中读取的字符串
string result;
//将字符串读到input中
stringstream input(word);
//依次输出到result中,并存入res中
while (input >> result)
res.push_back(result);
//输出res
for (int i = 0; i<res.size(); i++){
cout << res[i] << endl;
}
int len = 0;
int num = Num(res, len);
int allLength = num*len;
cout << "all size is " << allLength << endl;
system("pause");
}
华为机试程序
最新推荐文章于 2024-07-13 13:11:55 发布