c语言编程问题

c语言编程问题,第1张

#include <iostream>

using namespace std;

const Pi = 31415926;

typedef struct point {

double x,y;

}point;

class Shape {

public:

    virtual double getArea() const = 0;

};

class Rectangle : public Shape {

    double length,width;

point x,y;

public:

    Rectangle(double len, double wid) : length(len), width(wid) {}

Rectangle(point p1, point p2) {

length = p2x - p1x;

width = p2y - p1y;

}

    double getArea() const { return length  width; }

};

class Circle : public Shape {

point center;

    double radius;

public:

Circle(double r) : radius(r) {}

    Circle(point cen, double r) {

center = cen;

//centerx = cenx;

//centery = ceny;

radius = r;

}

    double getArea() const { return Pi  radius  radius; }

};

int main() {

    Shape s[4];

double len = 4,wid = 5,r = 10;

point pos0 = {1,1},pos1 = {0,0},pos2 = {5,6};

    s[0] = new Circle(pos0, r);

    s[1] = new Rectangle(pos1, pos2);

s[2] = new Rectangle(len, wid);

s[3] = new Circle(r);

    for(int i = 0; i < 4; ++i)

        cout << s[i]->getArea() <<  endl;

    return 0;

}

改变函数形参的值并不会影响到实参的值这是函数的基本知识。要在函数中修改实参的值需要传递指向实参的指针作为参数然后通过指针去修改实参的值。

#include<stdioh>

typedef struct SS

{

    int x;

    int y;

}SS;

void fun(SS p)

{

    p->x=1;

    p->y=2;

}

int main()

{

    SS ss;

    fun(&ss);

    printf("%d %d",ssx,ssy);

    return 0;

}

shapeh

#ifndef __SHAPE_H

#define __SHAPE_H

#include<iostream>

class Shape {

public:

double area() {

return 0;

}

double girth() {

return 0;

}

void show() {

std::cout << "Shape Object:\n";

}

};

class Triangle:public Shape{

private:

double a,b,c;

public:

void set_side(double side_a,double side_b,double side_c) {

a=side_a,b=side_b;c=side_c;

}

double area();

double girth();

void show();

};

class Rectangle:public Shape{

private:

double a,b;

public:

void set_side(double side_a,double side_b){

a=side_a,b=side_b;

}

double area();

double girth();

void show();

};

class Circle:public Shape{

private:

double r;

public:

void set_side(double radius) {

r=radius;

}

double area();

double girth();

void show();

};

#endif

shapecpp

#include"shapeh"

#define PI 31415927

double Triangle::area(){

return 05(a+b+c);

}

double Triangle::girth(){

return a+b+c;

}

void Triangle::show() {

Shape::show();

std::cout<<"Triangle\n";

}

double Rectangle::area(){

return ab;

}

double Rectangle::girth(){

return 2(a+b);

}

void Rectangle::show() {

Shape::show();

std::cout<<"Rectangle\n";

}

double Circle::area(){

return PIrr;

}

double Circle::girth(){

return 2PIr;

}

void Circle::show() {

Shape::show();

std::cout<<"Circle\n";

}

maincpp

#include"shapeh"

#include<iostream>

using namespace std;

int main() {

Triangle tri;

Rectangle rect;

Circle cir;

double a,b,c;

cout<<"Input Triangle 3 sides:";

cin>>a>>b>>c;

triset_side(a,b,c);

trishow();

cout<<"area:"<<triarea()<<endl;

cout<<"girth:"<<trigirth()<<endl<<endl;

cout<<"Input Rectangle 2 sides:";

cin>>a>>b;

rectset_side(a,b);

rectshow();

cout<<"area:"<<rectarea()<<endl;

cout<<"girth:"<<rectgirth()<<endl<<endl;

cout<<"Input Circle 1 radius:";

cin>>a;

cirset_side(a);

cirshow();

cout<<"area:"<<cirarea()<<endl;

cout<<"girth:"<<cirgirth()<<endl<<endl;

system("pause");

}

虚函数的作用是可以通过基类的指针或者引用调到派生类的这个函数。

你上面的代码是演示虚函数的作用,不用去简便他。

你可以把这个程序中的virtual全部删除掉,然后再运行程序,观察一下两次结果的不一样,估计你就能理解虚函数的作用了。

在你程序基础上修改的

#include <stdioh>

#include <stdlibh>

typedef struct coordinate

{

int x;

int y;

}coordinate;

typedef struct

{

coordinate a;

coordinate b;

coordinate c;

coordinate d;

}quadrangle;

quadrangle LoadData(char filename, int num)

{

FILE fp;

int i;

quadrangle testdata;

if ((fp=fopen(filename, "rt"))==NULL)

{

printf("File %s can not be found!\n", filename);

exit(-1);

}

fscanf(fp, "%d\n\n", num);

testdata=(quadrangle )malloc((num)sizeof(quadrangle));

for(i=0; i<(num); i++)

{

fscanf(fp, "%d%d\n", &testdata[i]ax, &testdata[i]ay);

fscanf(fp, "%d%d\n", &testdata[i]bx, &testdata[i]by);

fscanf(fp, "%d%d\n", &testdata[i]cx, &testdata[i]cy);

fscanf(fp, "%d%d\n", &testdata[i]dx, &testdata[i]dy);

fscanf(fp, "\n");

}

fclose(fp);

return testdata;

}

/ If the input shape is a square, return 1; else return 0 /

int IsSquare(quadrangle shape)

{

coordinate v1, v2;

double len1sqr, len2sqr;

double dotProduct, crossProduct;

v1x = shape->ax - shape->bx;

v1y = shape->ay - shape->by;

v2x = shape->cx - shape->dx;

v2y = shape->cy - shape->dy;

len1sqr = v1xv1x + v1yv1y;

len2sqr = v2xv2x + v2yv2y;

if (len1sqr != len2sqr)

return 0;

crossProduct = v1xv2y - v1yv2x;

if (crossProduct==0) {

v2x = shape->ax - shape->cx;

v2y = shape->ay - shape->cy;

len2sqr = v2xv2x + v2yv2y;

dotProduct = v1xv2x + v1yv2y;

if (dotProduct == 0) {

len2sqr = v2xv2x + v2yv2y;

if (len1sqr == len2sqr)

return 1;

return 0;

}

v2x = shape->ax - shape->dx;

v2y = shape->ay - shape->dy;

dotProduct = v1xv2x + v1yv2y;

if (dotProduct == 0) {

len2sqr = v2xv2x + v2yv2y;

if (len1sqr == len2sqr)

return 1;

return 0;

}

}

dotProduct = v1xv2x + v1yv2y;

if (dotProduct == 0) {

v1x = shape->ax - shape->cx;

v1y = shape->ay - shape->cy;

v2x = shape->ax - shape->dx;

v2y = shape->ay - shape->dy;

len1sqr = v1xv1x + v1yv1y;

len2sqr = v2xv2x + v2yv2y;

dotProduct = v1xv2x + v1yv2y;

if (len1sqr==len2sqr && dotProduct==0)

return 1;

}

return 0;

}

int main()

{

int number;

int i;

int result;

quadrangle data;

data = LoadData("test2txt", &number);

for (i=0; i<number; i++)

{

result = IsSquare(&data[i]);

if (result)

printf("Yes\n");

else

printf("No\n");

}

if (data != NULL)

free(data);

return 0;

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/12176263.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存