Rewrite the following functions and predicates in C++, using recursion if possib
ID: 3937788 • Letter: R
Question
Rewrite the following functions and predicates in C++, using recursion if possible.
Functions and predicates on lists. my_first([H|_]) = H. my_last([X]) = X. my_last([_|T]) = my_last(T). contains([E|_], E) => true. contains([_|T], E) => contains(T, E). find_first_of(L, E) = find_first_of(L, E, 1). find_first_of([E|_], E, I) = I. find_first_of([_|T], E, I) = find_first_of(T, E, I+1). find_first_of([], _E, _I) = -1. find_last_of(L, E) = find_last_of(L, E, 1, -1). find_last_of([]PreI) = PreI. find_last_of([E|T], E, I, _PreI) = find_last_of(T, E, I+1, I). find_last_of([_|T], E, I, PreI) = find_last_of(T, E, I+1, PreI). % the kth_elm(L, K) is the same as L[K] kth_elm([E|_], 1) = E. kth_elm([_|T], K) = kth_elm(T, K-1). my_len([]) = 0. my_len([_|T]) = my_len(T)+1. my_reverse([]) = []. my_reverse([H|T]) = my_reverse(T) ++ [H], main => test_arith, test_list. test_arith => X = succ(succ(succ(0))), % 3 Y = succ (succ(0))y % 2 printf("%w + %w = %w ", X, Y, add(X, Y)), printf("%w - %w = %w ", X, Y, sub(X, Y)), printf("%w * %w = %w ", X, Y, mul(X, Y)), printf("%w/%w = %w ", X, Y, my_div(X, Y)), printf("%w %% %vi = %w ", X, Y, my_rem(X, Y)), printf("%w^%w = %w ", X, Y, exp(X, Y)), printf("&w;! = %w ", X, fact(X)), printf("gcd(%w, %w) = &w; ", X, Y, my_gcd(X, Y)), printf("fib(%w) = %w ", X, fib(X)). test_list => L = [a, b, c, a, b], writef("first(%w) = %w ", L, my_first(L)), writef("last(%w) = %w ", L, my_last(L)), if contains(L, a) then writef("%w contains %w. ", L, a) end, writef("find_first_of(%w, %w) = %w ", L, b, find_first_of(L, b)), writef("find_last_of(%w, %w) = %w ", L, b, find_last_of(L, b)), writef("kth(%w, %w) = %w ", L, 3, kth_elm(L, 3)), writef("len(%w) = %w ", L, my_len(L)), writef("reverse(%w) = %w ", L, my_reverse(L)).Explanation / Answer
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
class rtnl
{
int nmr;
int dnmm;
public:
void getdata()
{
cout<<" enter the numerator part of the rtnl no.";
cin>>nmr;
cout<<" enter the denominator part of the rtnl no.";
cin>>dnmm;
}
void oprtr+(rtnl);
void oprtr-(rtnl);
void oprtr *(rtnl);
void oprtr /(rtnl);
};
void rtnl ::oprtr+(rtnl c1)
{
rtnl tmp;
tmp.nmr=(nmr*c1.dnmm)+(c1.nmr*dnmm);
tmp.dnmm=dnmm*c1.dnmm;
cout<<" rational no. after addition";
cout<<" numerator="<<tmp.nmr<<" denominator ="<<tmp.dnmm;
}
void rtnl ::oprtr -(rtnl c1)
{
rtnl tmp;
tmp.nmr=(nmr*c1.dnmm)-(c1.nmr*dnmm);
tmp.dnmm=dnmm*c1.dnmm;
cout<<" rtnl no. after subtraction";
cout<<" numerator="<<tmp.nmr<," denominator ="<<tmp.dnmm;
}
void rtnl ::oprtr (rtnl c1)
{
rtnl tmp;
tmp.nmr=nmr*c1.nmr;
tmp.dnmm=dnmm*c1.dnmm;
cout<<" rtnl no. after multiplication";
cout <<" numerator="<tmp.nmr<<" denominator ="<< tmp.dnmm;
}
void rtnl :: oprtr /(rtnl c1)
{
rtnl tmp;
tmp.nmr= nmr*c1.dnmm;
tmp.dnmm=c1.nmr*dnmm;
cout<<" rtnl no. after dividation";
cout <<" numerator="<<tmp.nmr<<" denominator ="<<tmp.dnmm;
}
void main()
{
clrscr();
rtnl c1, c2;
int n;
do
{
cout<<" 1.Input data for rtnl no. ";
cout<<" 2. Addition of rtnl no. ";
cout<<" 3. Subtraction of rtnl no. ";
cout<<" 4. Multiplication of rtnl no.";
cout<< 5. Division of rtnl no. ";
cout<<" 6. Quit";
cout<<" Enter your choice";
cin>>n;
switch(n)
{
case 1:
cout<<endl<<" enter the data for first rtnl no.";
c1.getdata();
cout<<endl<<" enter the data for second rtnl no. ";
c2.getdata ();
clrscr();
break;
case 2;
c1+c2;
getch();
clrscr();
break;
case 3;
c1-c2;
getch();
clrscr();
case 4:
c1*c2;
getch();
clrscr();
break;
case 5:
c1/c2;
getch();
clrscr();
break;
case 6:
exit(1);
break;
}
} while (n!=6);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.