Exp 1
Exp 1
Exp 1
Program 1 : Write a program to read and translate integers into numbers and Words.
e.g. 1=ONE and One
12 = ONE TWO and Twelve
856 = EIGHT FIVE SIX and Eight Hundred Fifty Six
Generate an error if the number of digits is more than 3
Source Code:
//211119
//Shivam Deswal
#include <iostream>
#include <string>
if (n > 3) {
cout << "Length exceeds 3" << endl;
return;
}
if (n == 3) {
cout << translateInteger(str[0]) << " HUNDRED ";
if (str[1] != '0' || str[2] != '0') cout << "AND ";
}
if (n >= 2) {
if (str[n - 2] == '1') {
cout << translateTeens(str[n - 1]) << " ";
return;
} else {
cout << translateTens(str[n - 2]) << " ";
}
}
if (n >= 1 && (n == 1 || str[n - 2] != '1')) {
cout << translateInteger(str[n - 1]) << " ";
}
cout << endl;
}
int main() {
int x;
cout << "Enter an integer: ";
cin >> x;
numberToString(x);
return 0;
}
Program 2 : Write a program to convert infix notation to postfix notation.
Source Code:
//SHivam Deswal
//211119
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
while (!operatorStack.empty()) {
postfix += operatorStack.top();
operatorStack.pop();
}
return postfix;
}
int main() {
string infix = "a+b*c-(d/e+f)*g";
cout << "Infix expression: " << infix << endl;
cout << "Postfix expression: " << infixToPostfix(infix) << endl;
return 0;
}