cpp: 九九乘法表显示

发布时间 2023-04-11 00:34:47作者: ®Geovin Du Dream Park™

 

// CardinalDirection.h : 
//练习案例:九九乘法表位置  CardinalDirection
//案例描述:九九乘法表位置
//
//2023年4月5日 涂聚文 Geovin Du edit.
//



#pragma once
#ifndef CARDINALDIRECTION_H
#define CARDINALDIRECTION_H

#include <iostream>
#include<string.h>
#include<math.h>

using namespace std;

/**
 * @brief 
 * \author geovindu.
 * \date 20230-4-10
 */
namespace geovindu
{
    /**
     * @brief 位置枚举类型
     * .
     */
    enum CardinalDirection
    {
        /**
         * @brief 左上角
         */
        topLeft,
        /**
         * @brief 右上角
         */
        topRight,
        /**
         * @brief 左下角
         */
        bottomLeft,
        /**
         * @brief 右下角
         */
        bottomRight

    };


}




#endif

  

// MultiplicationTables.h : 
//练习案例:九九乘法表  MultiplicationTables
//案例描述:九九乘法表
//
//2023年4月5日 涂聚文 Geovin Du edit.
//

#pragma once
#ifndef MULTIPLLCATIONTABLES_H
#define MULTIPLLCATIONTABLES_H

#include <iostream>
#include<string.h>
#include<math.h>
#include "CardinalDirection.h"


using namespace std;

/**
 * @brief
 * \author geovindu.
 * \date 20230-4-10
 */
namespace geovindu
{
	/**
	 * @brief 九九乘法表显示.
	 */
	class MultiplicationTables
	{
	private:
		
	public:

		/**
		 * @brief 九九乘法表显示.
		 * 
		 * \param cardinalDirection
		 * \return 
		 */
		string getString(CardinalDirection cardinalDirection);


	};

}




#endif

  

// MultiplicationTables.cpp : 
//练习案例:九九乘法表  MultiplicationTables
//案例描述:九九乘法表
//
//2023年4月5日 涂聚文 Geovin Du edit.
//


#include "MultiplicationTables.h"
#include <iostream> 
#include <string> 

using namespace std;

/**
 * @brief
 * \author geovindu.
 * \date 20230-4-10
 */
namespace geovindu
{


    /**
     * @brief 九九乘法表显示.
     * 
     * \param cardinalDirection
     * \return 
     */
    string MultiplicationTables::getString(CardinalDirection cardinalDirection)
    {
        string str = "";
        //stringstream dustring;

        switch (cardinalDirection)
        {
                case bottomLeft:
                    //System.out.println("左下角");
                 for (int i = 1; i < 10; i++) {
                    for (int j = 1; j <= i; j++) {
                        //System.out.print(j+"*"+i+"="+i*j+"\t");
                         str = str + "" + std::to_string(j) + " * " + std::to_string(i) + " = "  + std::to_string(i * j);
                            // printf("%d*%d=%2d\t", j, i, i * j);           
                             //str = str + s;                  
                         str = str + "\t\t";
                        // printf("\t\t");
                        // str.append(ss);
                  
                 }
                 str = str + "\t\n";
                // printf("\t\n");
                //System.out.println();
               
            }
            break;
        case bottomRight:
            //System.out.println("右下角");
            for (int i = 1; i < 10; i++) {
                for (int j = 9; j >= 1; j--) {
                    if (j > i) {
                        //System.out.print("");
                        str = str + "\t\t";
                        
                    }
                    else {
                        //System.out.print(j+"*"+i+"="+i*j+"\t");        
                        str = str + std::to_string(j) + " * " + std::to_string(i) + " = " + std::to_string(i * j) + "\t";
                       
                    }
                }
                str = str + "\t\n";
                //System.out.println();
                cout << endl;
            }
            break;
        case topLeft:
            //System.out.println("左上角");
            for (int i = 9; i > 0; i--) {
                for (int j = 1; j <= i; j++) {
                    // System.out.print(j+"*"+i+"="+i*j+"\t");
                    str = str + std::to_string(j) + " * "  + std::to_string(i) +" = " + std::to_string(i * j) + "\t";
                  

                }
                str = str + "\t\n";
               // System.out.println();
                cout << endl;
            }
            break;
        case topRight:
            //System.out.println("右上角");
            for (int i = 9; i >= 1; i--) {
                for (int j = 9; j >= 1; j--) {
                    if (j <= i) {
                        //System.out.print(j+"*"+i+"="+i*j+"\t");
                        str = str + std::to_string(j) + " * "  + std::to_string(i) + " = " + std::to_string(i * j)+ "\t";                       

                    }
                    else {
                        str = str + "\t\t";
                        //System.out.print("\t\t");
                    }
                }
                str = str + "\t\n";
                //System.out.println();
                cout << endl;
            }
            break;
        default:
            str = "";
            break;

        }

        return  str;
    }

}

  

 

 

	/**
	 * @brief 九九乘法表显示.
	 * 
	 */
	void Geovin::DisplayMultiplicationTables()
	{
		MultiplicationTables table;
		CardinalDirection cd;
		cout << table.getString(bottomLeft) << endl;


	}

  

调用:

	//九九乘法表显示.
	geovin.DisplayMultiplicationTables();