C++ Programming for Medical Imaging- Question4

 

//  Question4-A

#include "stdafx.h"
#include <iostream>
using namespace std;



int str_len(const char *pStr)
{
    int nLen = 0;

    if (NULL != pStr)
    {
        while ((*pStr++) != '\0')
        {
            nLen++;
        }
            
    }

    return nLen;
}

// 题 A
int str_len2(const char *pStr)
{
    int nLen = 0;

    if (NULL != pStr)
    {
        while ((*pStr) != '\0')
        {
            nLen++;

            pStr++;
        }

    }

    return nLen;
}


 //  Question4-B


char * replace_by_underscore(const char *pStr)
{
    if (NULL != pStr)
    {
        int nLen = str_len(pStr);

        char *pDest = new char[nLen + 1];

        for (int i = 0; i < nLen; ++i)
        {
            char cVal = pStr[i];

            if (' ' == cVal)
            {
                cVal = '_';
            }

            pDest[i] = cVal;
        }

        pDest[nLen] = '\0';

        return pDest;
    }
    else
    {
        return NULL;
    }

}


// Question4-C
void replace_str( string &str)
{
    int i = 0;

    for (auto s : str)
    {
        if (s == ' ')
        {
            s = '_';
        }

        str[i] = s;

        ++i;
    }

}

 // Question4-D-E-F

#include "stdafx.h"
#include <iostream>
using namespace std;

// Question-D
class Shape
{

public:
	
	virtual void print_shape()
	{
		cout << "shape \n";
	};

	void print_color()
	{
		cout << m_strColour.c_str();
	};

	void setColor(string strColor)
	{
		m_strColour = strColor;
	}


protected:

	string m_strColour;

};

// Question-E
class Rectangle : public Shape
{
public:

	void print_shape()
	{
		cout << "Rectangle  \n";
	};

	

};

class Circle : public Shape
{
public:

	void print_shape()
	{
		cout << "Circle \n";
	};

};



// Question-F

// 1. 基类中用virtual关键字表明方法
// 2. 采用父类的引用和指针调用多态方法,父类对象本身不能实现多态

int main()
{
    
	Rectangle r;

	Shape& sh1{ r }; sh1.print_shape();      // Rectangle    -- 采用引用实现多态

	Shape* sh2{ &r }; sh2->print_shape();    // Rectangle    -- 采用指针实现多态

	Shape  sh3{ r }; sh3.print_shape();      // shape        -- 非多态

    return 0;
}

// output result

/*
Rectangle
Rectangle
shape
*/


	
int main()
{
    
	Shape *pShapeRectangle = new Rectangle();
	pShapeRectangle->setColor("Rectangle Color is Red ! \n");

	Shape *pShapeCircle = new Circle();
	pShapeCircle->setColor("Circle Color is Red ! \n");


	pShapeRectangle->print_shape();      // -- 采用指针实现多态

	pShapeRectangle->print_color();
	
	pShapeCircle->print_shape();

	pShapeCircle->print_color();         // -- 采用指针实现多态

    return 0;
}

// output result

/*  
Rectangle
Rectangle Color is Red !
Circle
Circle Color is Red !
*/

 // Question-G

// Question4-H.cpp : 定义控制台应用程序的入口点。
//


#include <iostream>
using namespace std;


// 基类
class Shape
{
public:
	// 提供接口框架的纯虚函数
	virtual int print_area() = 0;

	void setWidth(int w)
	{
		width = w;
	}
	void setHeight(int h)
	{
		height = h;
	}
protected:
	int width;
	int height;
};

// 派生类
class Rectangle : public Shape
{
public:
	int print_area()
	{
		return (width * height);
	}
};
class Circle : public Shape
{
public:
	int print_area()
	{
		return (width * height) / 2;
	}
};

int main(void)
{
	Rectangle Rect;
	Circle  cir;

	Rect.setWidth(5);
	Rect.setHeight(7);
	// 输出对象的面积
	cout << "Total Rectangle area: " << Rect.print_area() << endl;

	cir.setWidth(5);
	cir.setHeight(7);
	// 输出对象的面积
	cout << "Total Triangle area: " << cir.print_area() << endl;

	return 0;
}


// output result
/*
Total Rectangle area: 35
Total Triangle area: 17
*/

 

 

// Question-H

#include <iostream>
using namespace std;

template <typename T>
T max(T a,T b)
{
    if(b>a)
    {
       a=b;
    }
    
    return a;
}

int main()
{
    int i1=8,i2=5,i;

    double d1=56.9,d2=90.765,d;

    long g1=67843,g2=-456,g;

    i=max(i1,i2);

    d=max(d1,d2);

    g=max(g1,g2);

    cout <<"i_max="<<i<<endl;

    cout <<"d_max="<<d<<endl;

    cout <<"g_max="<<g<<endl;

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值