Microsoft Interview Question
Software Engineer in Tests#include<stdio.h>
int cal(char *in)
{
int a,b;
char op;
sscanf(in,"%d%c%d",&a,&op,&b);
switch(op)
{
case '*':
return a*b;
case '-':
return a-b;
case '+':
return a+b;
case '/':
if(b!=0)
return a/b;
else
return -1;
}
return;
}
main()
{
char x[100];
scanf("%s",x);
printf("%d",cal(x));
}
@anonymous
wondering if you would be interested in knowing some test cases to break your code.
Your code fials for these sample testcases:
# -123-5 (operand in front)
# -1234+_+ 123 (having multiple operands and/or spaces)
# 123+NULL (missing arguements - decision on how to handle this case)
here are few things I would keep in mind while working on this problem. Basic functionality is partially implement by @anonymous.
we should ensure that <operand1><number><operand2><number> format is kept intact. where operand1 can be null. Only after this we should resolve the expression.
m_string::cacluation_string(const string & s) {
if (s.empty() || s.length() < 3) {
return "";
}
if ((s[0] != '-') && (s[0] < '0') && (s[0] > '9')) {
return "";
}
string oper1, oper2;
for(int i = 1; i < s.length(); i++) {
if (s[i] == '+' || s[i] == '-') {
oper1 = s.substr(0, i);
oper2 = s.substr(i);
break;
}
}
int iOper1, iOper2;
std::istringstream iss(oper1);
iss >> iOper1;
std::istringstream iss2(oper2);
iss2 >> iOper2;
string sret = "";
std::stringstream out;
out << iOper1 + iOper2;
sret = out.str();
cout<<" --- "<<sret<<endl;
return sret;
}
m_string::cacluation_string(const string & s) {
if (s.empty() || s.length() < 3) {
return "";
}
if ((s[0] != '-') && (s[0] < '0') && (s[0] > '9')) {
return "";
}
string oper1, oper2;
for(int i = 1; i < s.length(); i++) {
if (s[i] == '+' || s[i] == '-') {
oper1 = s.substr(0, i);
oper2 = s.substr(i);
break;
}
}
int iOper1, iOper2;
std::istringstream iss(oper1);
iss >> iOper1;
std::istringstream iss2(oper2);
iss2 >> iOper2;
string sret = "";
std::stringstream out;
out << iOper1 + iOper2;
sret = out.str();
cout<<" --- "<<sret<<endl;
return sret;
}
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int calculate(char *in)
{
stringstream ss;
ss << in;
int x, y;
char op;
ss >> x >> op >> y;
switch(op)
{
case '+':
return x + y;
case '-':
return x - y;
case '*':
return x * y;
case '/':
return x / y;
case '%':
return x%y;
default:
return 0;
}
}
int main()
{
cout << "Result : " << calculate("-1*0") << endl;
}
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int calculate(char *in)
{
stringstream ss;
ss << in;
int x, y;
char op;
ss >> x >> op >> y;
cout << x << endl;
cout << op << endl;
cout << y << endl;
switch(op)
{
case '+':
return x + y;
case '-':
return x - y;
case '*':
return x * y;
case '/':
return x / y;
case '%':
return x%y;
default:
return 0;
}
}
int main()
{
cout << "Result : " << calculate("-1*0") << endl;
}
1. Convert given expression to either postfix or prefix.
2. Use a stack and push operands until you get a operator.
3. Fetch top 2 operands from stack.
4. Calculate result using the operator.
5. Push the result to stack again.
6. Repeat steps 2 to 5 till the last character in prefix/postfix expression.
if both are negative numbers then
- Anonymous February 07, 2009-2--3
-2+-3ll be the input right..
and pls tell which teamasked u this Q