Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Exp 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

||Experiment-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>

using namespace std;

string translateInteger(char digit) {


switch (digit) {
case '0':
return "ZERO";
case '1':
return "ONE";
case '2':
return "TWO";
case '3':
return "THREE";
case '4':
return "FOUR";
case '5':
return "FIVE";
case '6':
return "SIX";
case '7':
return "SEVEN";
case '8':
return "EIGHT";
case '9':
return "NINE";
default:
return "";
}
}
string translateTens(char digit) {
switch (digit) {
case '1':
return "TEN";
case '2':
return "TWENTY";
case '3':
return "THIRTY";
case '4':
return "FORTY";
case '5':
return "FIFTY";
case '6':
return "SIXTY";
case '7':
return "SEVENTY";
case '8':
return "EIGHTY";
case '9':
return "NINETY";
default:
return "";
}
}

string translateTeens(char digit) {


switch (digit) {
case '0':
return "TEN";
case '1':
return "ELEVEN";
case '2':
return "TWELVE";
case '3':
return "THIRTEEN";
case '4':
return "FOURTEEN";
case '5':
return "FIFTEEN";
case '6':
return "SIXTEEN";
case '7':
return "SEVENTEEN";
case '8':
return "EIGHTEEN";
case '9':
return "NINETEEN";
default:
return "";
}
}
void numberToString(int x) {
string str = to_string(x);
int n = str.length();

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 == '/');
}

int precedence(char op) {


if (op == '+' || op == '-')
return 1;
if (op == '*' || op == '/')
return 2;
return 0;
}

string infixToPostfix(const string& infix) {


stack<char> operatorStack;
string postfix;

for (char c : infix) {


if (isalnum(c)) {
postfix += c;
} else if (c == '(') {
operatorStack.push(c);
} else if (c == ')') {
while (!operatorStack.empty() && operatorStack.top() != '(')
{
postfix += operatorStack.top();
operatorStack.pop();
}
operatorStack.pop(); // Discard the '('
} else if (isOperator(c)) {
while (!operatorStack.empty() &&
precedence(operatorStack.top()) >= precedence(c)) {
postfix += operatorStack.top();
operatorStack.pop();
}
operatorStack.push(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;
}

You might also like