cpp: 九九乘法表显示

 

// 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("左下角");
                    /*1*/
                    int i, j;
                    for (i = 1; i < 10; i++)
                    {
                        for (j = 1; j <= i; j++)
                        {
                            str=str+std::to_string(j) + "*" + std::to_string(i) + "=" + std::to_string(i * j);

                        }
                        str = str + "\n";
                    }
                    /*2
                 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";
                        // printf("\t\t");
                        // str.append(ss);
                  
                 }
                 str = str + "\t\n";
                // printf("\t\n");
                //System.out.println();
               
                } */
            break;
        case bottomRight:
            //System.out.println("右下角"); //1*1=1 
            /*1
            int i, j;
            for (i = 1; i < 10; i++)
            {

                for (j = 1; j < 10 - i; j++)
                {
                    str = str + "\t";
                }
                for (j = 1; j <= i; j++)
                {
                    str=str+ std::to_string(j) + "*" + std::to_string(i) + "=" + std::to_string(i * j) + "\t";
                }
                str = str + "\n";

            }
            */
            /*2*/
            for (int i = 1; i < 10; i++) {
                for (int j = 9; j >= 1; j--) {
                   /* if (i == 1 && j == 1)                    {
                        str = str + "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
                        str = str + std::to_string(j) + "*" + std::to_string(i) + "=" + std::to_string(i * j) + "\t";
                    }
                    else 
                    */
                    if (j >i) {
                        //System.out.print("");
                        str = str + "\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("左上角");  //1*1=1 
            /*1*/
            int i, int j;
            for (i = 1; i < 10; i++)
            {
                for (j = 1; j < 10; j++)
                {
                    if (j >= i) {
                        str=str+ str = str + std::to_string(j) + " * " + std::to_string(i) + " = " + std::to_string(i * j) + "\t";
                    }
                }
                str = str + "\n";
            }
            /*2 
            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("右上角");
            /** 1 */
            int i, j;
            for (i = 1; i < 10; i++)
            {
                for (j = 1; j < 10; j++)
                {
                    if (j < i)
                    {
                        str = str + "\t";
                    }
                    else
                    {
                        str=str+ str = str + std::to_string(j) + "*" + std::to_string(i) + "=" + std::to_string(i * j) + "\t";
                    }
                }
                str = str + "\n";
            }

            /*2
            
            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";
                        //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(topLeft) << endl; //bottomRight //topRight //bottomLeft


	}

  

调用:

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

  

 

 

posted @ 2023-04-11 00:26  ®Geovin Du Dream Park™  阅读(57)  评论(0)    收藏  举报