青蛙(frog)杯第一屆棒球比賽開賽啦。 你現在是一名記分員,輸入一個字串陣列(比賽記錄情況),按如下規則計分: 1. 如果該字串是數字:代表當輪比賽的得分情況。 2. 如果該字串是“+”:
阿新 • • 發佈:2019-01-04
#include "stdafx.h"
#include<stack>
#include<vector>
#include<iostream>
using namespace std;
int frogPoints(char *str,int length)
{
stack <int>stk;
int sumScore=0;//總分
int temp;//暫時儲存數字型字元的整型變數
for(int i=0;i<length;i++)
{
//if (str[i] == 0 || str[i] == 1 || str[i] == 2 || str[i] == 3 || str[i] == 4 || str[i] == 5 || str[i] == 6 || str[i] == 7 || str[i] == 8 || str[i] == 9)
if (str[i] >= '0' && str[i] <= '9')
{
temp = str[i] - '0';
//如果字元為一個數字,則轉為整型加入棧中
stk.push(temp);
}
else if (str[i] == 'C')
{
if (!stk.empty())
{
stk.pop();
}
else
cout << "輸入格式有誤!";
}
else if (str[i] == 'D')
{
if (!stk.empty())
{
temp = stk.top() * 2;
stk.push(temp);
}
else
cout << "輸入格式有誤!";
}
else if (str[i] == '+')
{
if (!stk.empty())
{
int temp1;
temp1 = stk.top();
stk.pop();
temp =temp1 + stk.top();
stk.push(temp1);
stk.push(temp);
}
else
cout << "輸入格式有誤!";
} }
while (!stk.empty())
{
sumScore += stk.top();
stk.pop();
//cout << sumScore;
}
return sumScore;
}
int main()
{
//char str[6] = { '5', '2', 'C', 'D', '+', '\0' };
//char str[]; vector<char> arr;
char j;
cin >> j;
arr.push_back(j);
while ((cin.get()) != '\n')
{
char k;
cin >> k;
arr.push_back(k);
}
cout << " round: ";
for (int i = 0; i < arr.size(); i++)
{
cout << i << " ";
}
cout << endl;
cout << "str[round]:";
for (int i = 0; i < arr.size(); i++)
{
cout << arr[i] << " ";
}
int length = arr.size();
char *str = new char(length);
memcpy(str, &arr[0], arr.size() * sizeof(char));
cout << endl;
cout << "總分=" << frogPoints(str,length);
return 0;
}