Skip to content

Commit

Permalink
chore: Add q5.c to cprogram directory
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaoHN committed May 30, 2024
1 parent 401dc68 commit 5f5fc7c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
1 change: 1 addition & 0 deletions trivial/cprogram/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ add_executable(test3 test3.c)
add_executable(ce2 ce2.c)
add_executable(dig_trans dig_trans.c)
add_executable(fruit fruit.c)
add_executable(q5 q5.c)
2 changes: 1 addition & 1 deletion trivial/cprogram/fruit.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
typedef struct
typedef struct fruitinfo
{
int no;
char name[20];
Expand Down
114 changes: 114 additions & 0 deletions trivial/cprogram/q5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include <stdio.h>
#include <stdlib.h>

struct point {
float x;
float y;
float z;
char color[20];
};

void load(struct point* p, int n)
{
for (int i = 0; i < n; i++)
{
scanf("%f%f%f", &p[i].x ,&p[i].y, &p[i].z);
scanf(" %s", p[i].color);
}
/*for (int i = 0; i < n; i++)
{
printf("%.2f %.2f %.2f %s\n", p[i].x, p[i].y, p[i].z, p[i].color);
}*/
}

void Print(struct point* p, int n)
{
int temp = 0;
char tem[3]; tem[0] = 'r'; tem[1] = 'e'; tem[2] = 'd';

for (int i = 0; i < n; i++)
{
temp = 0;
for (int j = 0; j < 3; j++)//检查颜色
{
if (p[i].color[j] != tem[j] || p[i].color[j] == '\0')
{
temp = 1;
break;
}
}

if (temp == 0)
{
printf("%.2f %.2f %.2f %s\n", p[i].x, p[i].y, p[i].z, p[i].color);
}
}
}

float ComputeBoundBoxArea(struct point* p, int n)
{
float x1, x2, y1, y2, z1, z2;
x1 = 1000;
for (int i = 0; i < n; i++)
{
if (x1 > p[i].x)
{
x1 = p[i].x;
}
}
x2 = -1000;
for (int i = 0; i < n; i++)
{
if (x2 < p[i].x)
{
x2 = p[i].x;
}
}
y1 = 1000;
for (int i = 0; i < n; i++)
{
if (y1 > p[i].y)
{
y1 = p[i].y;
}
}
y2 = -1000;
for (int i = 0; i < n; i++)
{
if (y2 < p[i].y)
{
y2 = p[i].y;
}
}
z1 = 1000;
for (int i = 0; i < n; i++)
{
if (z1 > p[i].z)
{
z1 = p[i].z;
}
}
z2 = -1000;
for (int i = 0; i < n; i++)
{
if (z2 < p[i].z)
{
z2 = p[i].z;
}
}
printf("%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,", x1,x2,y1,y2,z1,z2);
float sum;
sum = (x2 - x1) * (y2 - y1) * 2 + (x2 - x1) * (z2 - z1) * 2 + (z2 - z1) * (y2 - y1) * 2;
return sum;
}

int main()
{
int n;
scanf("%d", &n);
struct point p[6];
load(p, n);
Print(p, n);
printf("%.2f", ComputeBoundBoxArea(p, n));
return 0;
}

0 comments on commit 5f5fc7c

Please sign in to comment.