#include <iostream> // cin, cout
using namespace std;
int main(void)
{
char c[80];
int i, sum=0;
cin.getline(c,80);
for(i=0; c[i]; i++) // c[i] != '\0'
if('0'<=c[i] && c[i]<='9') sum += c[i]-'0';
cout<< "Sum of digits = " << sum << endl;
getchar();
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我理解一切都接受了总和+ = c [i] - '0'; 我删除了"-'0",它没有给我正确的答案.为什么是这样?
package PJ2;
import java.util.*;
public class SimpleLispExpressionEvaluator
{
// Current input Lisp expression
private String inputExpr;
// Main expression stack & current operation stack, see algorithm in evaluate()
private Stack<Object> exprStack;
private Stack<Double> currentOpStack;
// default constructor
// set inputExpr to ""
// create stack objects
public SimpleLispExpressionEvaluator()
{
// add statements
inputExpr = "";
exprStack = new Stack<Object>();
currentOpStack = new Stack<Double>();
}
// default constructor
// set inputExpr to inputExpression
// create stack objects
public SimpleLispExpressionEvaluator(String inputExpression)
{ …Run Code Online (Sandbox Code Playgroud)