- C++ Built-in Operators, Precedence and Associativity
123COM;123COM
3COM
C
O
2
2
- q2.cpp
#include
#include
using namespace std;
int main(int argc, char **argv){
int i,j;
for (i = argc - 1; i > 0; i--) {
for (j = strlen(argv[i]) - 1; j >= 0; j--) {
cout << argv[i][j];
}
cout << endl;
}
}
- q3.cpp
#include
#include
using namespace std ;
int main()
{
int num,i;
bool loopFlag,isPrime;
loopFlag = isPrime = true;
cout << "Please enter a positive integer value: ";
cin >> num;
if (num == 2)
loopFlag = false;
else {
if (num % 2 == 0){
isPrime = loopFlag = false;
}
else {
i = 3;
while (loopFlag && (i <= int(sqrt(num)))){
if (num % i == 0){
loopFlag = isPrime = false;
} else i += 2;
}
}
}
if (isPrime)
cout << num << " is a prime number" << endl;
else cout << num << " is not a prime number" << endl;
return 0 ;
}
- q4.py
from StackModule import Stack
stack = Stack()
print ('Postfix evaluation using Stack')
postfixExp = input('Enter postfix expression separated by spaces:')
for e in postfixExp.split():
if e.isdigit():
stack.push(int(e))
elif e == '+':
stack.push(int(str(stack.pop()))+int(str(stack.pop())))
elif e == '*':
stack.push(int(str(stack.pop()))*int(str(stack.pop())))
elif e == '-':
top = stack.pop()
stack.push(int(str(stack.pop()))-int(str(top)))
elif e == '/':
top = stack.pop()
stack.push(int(int(str(stack.pop()))/int(str(top))))
print ('The result is:', stack)
- Lab 4 q5.py
from StackModule import Stack
def isInt(s):
try:
int(s)
return True
except ValueError:
return False
stack = Stack()
print ('Postfix evaluation using Stack')
postfixExp = input('Enter postfix expression separated by spaces:')
for e in postfixExp.split():
if e.isInt(e):
stack.push(int(e))
elif e == '+':
stack.push(int(str(stack.pop()))+int(str(stack.pop())))
elif e == '*':
stack.push(int(str(stack.pop()))*int(str(stack.pop())))
elif e == '-':
top = stack.pop()
stack.push(int(str(stack.pop()))-int(str(top)))
elif e == '/':
top = stack.pop()
stack.push(int(int(str(stack.pop()))/int(str(top))))
elif e == '**':
top = stack.pop()
stack.push(int(int(str(stack.pop()))**int(str(top))))
print ('The result is:', stack)