0% found this document useful (0 votes)
33 views

Exp 1

The document contains source code for two programs - the first program translates integers into words, and the second program converts infix notation to postfix notation. The integer translation program uses switch statements to translate each digit to its word equivalent up to 3 digits. The infix to postfix conversion program uses a stack to pop operators and append them to the output in order of precedence.

Uploaded by

211119
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Exp 1

The document contains source code for two programs - the first program translates integers into words, and the second program converts infix notation to postfix notation. The integer translation program uses switch statements to translate each digit to its word equivalent up to 3 digits. The infix to postfix conversion program uses a stack to pop operators and append them to the output in order of precedence.

Uploaded by

211119
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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