9.4华为笔试第二题32位(用大数相加)C++代码求大佬查看
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <map>
using namespace std;
bool isnum(char c)
{
if (c >= '0' && c <= '9')
return true;
else
return false;
}
//字符串去空格
char *strremov(char* dst, const char* src, char ch = ' ')
{
int i = -1, j = 0;
while (src[++i])
if (src[i] != ch)
dst[j++] = src[i];
dst[j] = '\0';
return dst;
}
//含+号 分割每个数
void splitstring(const string& srcstr, vector<string>& vec, const string& separator)
{
string::size_type possubstringstart;
string::size_type posseparator;
posseparator = srcstr.find(separator);
possubstringstart = 0;
while (string::npos != posseparator)
{
vec.push_back(srcstr.substr(possubstringstart, posseparator - possubstringstart));
possubstringstart = posseparator + separator.size();
posseparator = srcstr.find(separator, possubstringstart);
}
if (possubstringstart != srcstr.length())
vec.push_back(srcstr.substr(possubstringstart));
}
//字符串 两数相加
string BigNumAdd(const string& strNum1, const string& strNum2)
{
string strSum;
int len1 = strNum1.size();
int len2 = strNum2.size();
int bit = 0;
while (len1 > 0 || len2 > 0 || bit)
{
int tmpSum = (len1 ? strNum1[len1 - 1] - '0' : 0) + (len2 ? strNum2[len2 - 1] - '0' : 0) + bit;
strSum += tmpSum % 10 + '0';
bit = tmpSum / 10;
len1 = len1 ? len1 - 1 : 0;
len2 = len2 ? len2 - 1 : 0;
}
reverse(strSum.begin(), strSum.end());
return strSum;
}
int main()
{
int n = 0, m = 0;
vector<string>res;
cin >> n;
cin.ignore();
vector<string> input;
map<string, string> mpint;
vector<string> jiashu;
for (int i = 0; i < n; i++)
{
char c[100];
string tmp;
getline(cin, tmp, '\n');
strremov(c, tmp.c_str());
tmp = c;
input.push_back(tmp);
}
for (int i = 0; i < n; i++)
{
string::size_type position;
int len = input[i].size();
position = input[i].find("=");
string subs1, subs2, re = "0";
subs1 = input[i].substr(0, position);
subs2 = input[i].substr(position + 1, len);
if (subs2.find("+") != subs2.npos) //有+
{
vector<string> jiashu;
splitstring(subs2, jiashu, "+");
int jslen = jiashu.size();
for (int j = 0; j < jslen; j++)
{
//加数不是数字 ,到map中找对应的val string
if (!isnum(jiashu[j][0]))
{
map<string, string>::iterator it = mpint.find(jiashu[j]);
if (it == mpint.end())
{
cout << "NA";
return 0;
}
else {
re = BigNumAdd(re, it->second);
}
}
else
{
re = BigNumAdd(re, jiashu[j]);
}
if (j == jslen - 1){
mpint.insert(pair<string, string>(subs1, re));
res.push_back(re);
}
}
}
//没有+
else
{
if (isnum(input[i][position + 1]))
{
mpint.insert(pair<string, string>(subs1, subs2));
res.push_back(subs2);
}
else if (!isnum(input[i][position + 1]))
{
map<string, string>::iterator it = mpint.find(subs2);
if (it == mpint.end())
{
cout << "NA";
return 0;
}
else
{
mpint.insert(pair<string, string>(subs1, it->second));
res.push_back(subs2);
}
}
}
}
cout << res[n - 1];
system("pause");
return 0;
}
/*
输入1:
4
xx =111111111
yy= xx+111111111
zz=xx+yy+111111111
tt=xx+yy+zz+111111111
输出:888888888
输入1:
4
xx =111111111
yy= xx+111111111
zz=xx+yy+vv
tt=xx+yy+zz+111111111
输出:NA
*/
#华为##笔试题目##C/C++#


