| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11903 | Rational class |
|
| 11904 | Palindrome class |
|
Description
Create a class called Rational for performing arithmetic with fraction.
-
Use integer variables to represent the private data of the class─the numerator and the denominator.
- Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction 2/4 would be stored in object as 1 in the numerator and 2 in the denominator.
- Provide public member functions that perform each of the following tasks:
a.) Adding two Rational numbers. The result should be stored in reduced form.
b.) Subtracting two Rational numbers. The result should be stored in reduced form.
c.) Multiplying two Rational numbers. The result should be stored in reduced form.
d.) Dividing two Rational numbers. The result should be stored in reduced form.
e.) Printing Rational numbers in form a/b, where a is the numerator and b is the denominator.
P.S. :
- You need to implement a private member function reduce() that will be used in your constructor and the public arithmetic member functions to derive the required reduced form.
- In reduce(), pay attention to the case that one of the numerator and denominator of a rational number is negative. In this case, the negative sign “-” should be placed at the numerator.
Note :
This problem involves three files.
- function.h: Class definition of Rational.
- function.cpp: Member-function definitions of Rational.
- main.cpp: A driver program to test your class implementation.
main.cpp
#include <iostream>
#include "function.h" // include definition of class Rational
using namespace std;
int main()
{
char s1;
int s2, s3, s4, s5;
Rational x;
while(cin >>s1>>s2>>s3>>s4>>s5)
{
if(cin.eof())
{
break;
}
Rational c(s2, s3), d(s4, s5);
if(s1 == '+')
{
x = c.addition( d ); // adds object c and d; sets the value to x
x.printRational(); // prints rational object x
cout << '\n';
}
else if(s1 == '-')
{
x = c.subtraction( d ); // subtracts object c and d
x.printRational(); // prints rational object x
cout << '\n';
}
else if(s1 == '*')
{
x = c.multiplication( d ); // multiplies object c and d
x.printRational(); // prints rational object x
cout << '\n';
}
else if(s1 == '/')
{
x = c.division( d ); // divides object c and d
x.printRational(); // prints rational object x
cout << '\n';
}
}
} // end main
function.h
#define RATIONAL_H
class Rational
{
public:
Rational( int = 0, int = 1 ); // default constructor
Rational addition( const Rational & ) const; // function addition
Rational subtraction( const Rational & ) const; // function subtraction
Rational multiplication( const Rational & ) const; // function multi.
Rational division( const Rational & ) const; // function division
void printRational () const; // print rational format
private:
int numerator; // integer numerator
int denominator; // integer denominator
void reduce();
}; // end class Rational
#endif
Input
There are five strings in each line: S1 S2 S3 S4 S5
- S1 represents an operator (+,-,*,/)
- S2 and S3 represent the numerator and denominator of the first operand, respectively.
- S4 and S5 represent the numerator and denominator of the second operand, respectively.
Input terminated by EOF.
Output
For every given operation, your program should print the corresponding result followed by a new line character.
Sample Input Download
Sample Output Download
Partial Judge Code
11903.cppPartial Judge Header
11903.hTags
Discuss
Description
Palindrome is a string that is identical to its reverse, like "LeveL" or "QAQ".
Create a class of palindrome that check whether a given string is a palindrome.
main.cpp :
// main.cpp
#include <iostream>
#include "function.h"
using namespace std;
int main()
{
char word[100001];
while(cin >> word){
PalindromeChecker p1(word);
PalindromeChecker p2;
p2 = p1;
p2.reverse();
if(p1==p2)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
function.h :
// function.h
#include <iostream>
#include <string>
using namespace std;
class PalindromeChecker
{
private:
static const int MAXWORDLENTH=100001;
char word[MAXWORDLENTH];//store the input string
public:
PalindromeChecker();
PalindromeChecker(const char*);
PalindromeChecker& operator=(const PalindromeChecker& p);
void reverse();
friend bool operator== (PalindromeChecker &p1, PalindromeChecker &p2);
};
- function.h: Class definition of PalindromeChecker.
- function.cpp: Member-function definitions of PalindromeChecker.
- main.cpp: A driver program to test your class implementation.
- Step 1. Include function.h into function.cpp and then implement your function.cpp. (You don’t need to modify function.h and main.cpp)
- Step 2. Submit the code of function.cpp into submission block.
- Step 3. Check the results and debug your program if necessary.
Input
The input consists of multiple lines, each line contains a string.
The length of each string is less than 100000. (Defined in MAXWORDLENTH)
The number of test case is less than 1000.
Output
Output "Yes" if it's a palindrome, "No" if it's not.