2507 - I2P(II)2022_Yang_hw3 Scoreboard

Time

2022/04/08 21:30:00 2022/04/22 13:20:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
10998 Stack
11413 Rational Class
13485 String Calculator(class)

10998 - Stack   

Description

A stack is an abstract data type that serves as a collection of elements, where a node can be added to a stack and removed from a stack only at its top. Two principal operations can be used to manipulate a stack: push, which adds an element at the top, and pop, which removes the element at the top of the collection.

Let’s see how the stack data structure can be realized in C++.We have an approach to implement stack: linked list. Thus, we define a class as follows:

class List_stack {

    public:

        List_stack();

        ~List_stack();

        void push(const int &);

        void pop();

        void print();

    private:

        ListNode *head;

        ListNode *tail;

};

where List_stack implements the stack data structure

 

REQUIREMENTS:

Implement the constructor, destructor, push(), pop() and print() member functions of List_stack classes.

Note:

1.This problem involves three files.

  • function.h: Class definitions.
  • function.cpp: Member-function definitions.
  • main.cpp: A driver program to test your class implementation.

You will be provided with main.cpp and function.h, and asked to implement function.cpp.

function.h

main.cpp

2.For OJ submission:

       Step 1. Submit only your function.cpp into the submission block.

       Step 2. Check the results and debug your program if necessary.

Input

There are three kinds of commands:

  • “push integerA” represents adding an element with int value A at the top of the stack.
  • “pop “ represents removing the element at the top of the stack.
  • “print” represents showing the current content of the stack.

Each command is followed by a new line character.

Input terminated by EOF.

Output

The output should consist of the current state of the stack.

When the stack is empty, you don’t need to print anything except a new line character.

Sample Input  Download

Sample Output  Download

Partial Judge Code

10998.cpp

Partial Judge Header

10998.h

Tags




Discuss




11413 - Rational 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:
    1. Adding two Rational numbers. The result should be stored in reduced form.
    2. Multiplying two Rational numbers. The result should be stored in reduced form.
    3. Printing Rational numbers in form a/b, where a is the numerator and b is the denominator.
  • You need to implement a private member function reduce() that will be used in your constructor and the about 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.
***You can use our gcd() to implement your reduce().
 
Note:
1. 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.
You will be provided with function.h and main.cpp, and asked to implement function.cpp.
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
        }
        else if(s1 == '*')
        {
            x = c.multiplication( d ); // multiplies object c and d
            x.printRational(); // prints rational object x
        }
    }
}

int gcd(int a, int b){
    return (b == 0) ? a : gcd(b, a % b);
}
function.h
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
    public:
        Rational( int = 0, int = 1 ); // default constructor
        Rational addition( const Rational & ) const; // function addition
        Rational multiplication( const Rational & ) const; // function multi.
        void printRational () const; // print rational format

    private:
        int numerator; // integer numerator
        int denominator; // integer denominator
        void reduce();
}; // end class Rational

int gcd(int a, int b);
#endif
2. For OJ submission:
  • 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

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

11413.cpp

Partial Judge Header

11413.h

Tags




Discuss




13485 - String Calculator(class)   

Description

In this problem, you need to finish a string calculator.

The calculator has three string operators:

  • a + b: Concatenate the string b after the string a.
  • a - b: If string b has appeared in string a, delete the first appearance of b in a, otherwise the result is "error".
  • a / b: The number of occurrences of string b in string a.

Note: You are suggested to practice the usage of C++ string class (https://www.cplusplus.com/reference/string/string/).

  • function std::operator+: Concatenate strings (https://www.cplusplus.com/reference/string/string/operator+/)
  • public member function std::string::find: Find content in string (https://www.cplusplus.com/reference/string/string/find/)
  • public member function std::string::substr: Generate substring (https://www.cplusplus.com/reference/string/string/substr/)
  • etc.

Input

The first line contains an integer N

The following N lines, each line contains two string a b.

 

testcases:

(6/6) 1 <= N <= 100, 1 <= |a|, |b| <= 100

Output

The output contains lines.

For each a b, output ((a - b)+b)/b

Sample Input  Download

Sample Output  Download

Partial Judge Code

13485.cpp

Partial Judge Header

13485.h

Tags




Discuss