农行笔试题记录(二)计算表达式的值并输出,表达式由若干个数字和运算符(只包含加号和减号)构成

原题即为题目所示,题中给出的函数声明中形参为string类型,但平时较习惯使用C风格字符串,整个题目的完整代码如下所示:(测试用例没有单独写,直接在主函数中进行了测试)

#include
#include
#include
#include
using namespace std;
int analysis(string str)
{
	const char* expression = str.c_str();//string类型转换成c风格字符串
	int param_len = strlen(expression);//字符串长度
	const char *p = expression;
	char strnum[1024];//存储数字(字符型)
	char oper[1024];//存储运算符
	int splnum[1024];//存储数字
	memset(strnum, 0, sizeof(strnum));
	memset(oper, 0, sizeof(oper));
	int n = 0;
	int m = 0;
	int num = 0;
	for (int i = 0; i

 

你可能感兴趣的:(C++,数据结构,算法)