- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个程序可以计算多边形的面积和周长。程序还会确认面积和周长的计算结果是否与预期结果相同。
我不明白发生了什么,但确认面积和周长是否与预期相同的验证部分无法正常工作。
例如,我现在测试并在所有情况下都得到“数组相同”,这是正确的,因为我在 txt 文件中给出了正确的坐标以匹配每个预期结果。但是我稍后或在不同的机器上进行了测试,我也在 Linux 中进行了测试,在某些情况下会出现“Arrays are diferent”。在不更改坐标文件或代码的情况下发生这种情况真的很奇怪。
你明白为什么会出现这个奇怪的问题吗?
这是文本文件,带有正确的坐标以达到预期的结果:
1.0 2.5 5.1 5.8 5.9 0.7
1.2 4.1 5.1 5.8 6.8 1.9 2.9 0.2
1.7 4.9 5.1 5.8 7.0 2.8 4.8 0.1 1.5 1.4
2.1 5.3 5.1 5.8 7.0 3.5 5.9 0.7 2.9 0.2 1.0 2.5
2.5 5.6 5.1 5.8 6.9 3.9 6.5 1.3 4.2 0.0 1.8 1.0 1.0 3.4
2.8 5.7 5.1 5.8 6.7 4.2 6.8 1.9 5.2 0.3 2.9 0.2 1.3 1.8 1.2 4.1
程序:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
enum {x, y};
typedef struct triangle {
double v1[2];
double v2[2];
double v3[2];
} triangle;
double area(triangle a);
double perimeter(double *vertices, int size);
double side(double *p1, double *p2);
char result[256];
char expected[] = "11.7715.65"; //triangle regular
char expected1[] = "18.1017.02"; //quadrilateral regular
char expected2[] = "21.3317.60"; //pentagon regular
char expected3[] = "23.5518.07"; //hexagon regular
char expected4[] = "24.8018.29"; //heptagon regular
char expected5[] = "25.1418.26"; //octagon regular
int main()
{
int idx;
int triangles;
int index;
int xycount;
double xy;
double triangle_area;
double polygon_area;
double perim;
double polygon_vertices[50] = {0.0};
triangle a;
FILE* data;
char line[256];
char* token;
if ((data = fopen("test.txt", "r")) == NULL) {
fprintf(stderr, "can't open data file\n");
exit (EXIT_FAILURE);
}
while (fgets(line, sizeof (line), data)){
xycount = 0;
polygon_area = 0;
line[strlen(line) - 1] = 0;
token = strtok(line, " ");
while (token != NULL){
xy = atof(token);
token = strtok(NULL, " ");
polygon_vertices[xycount++] = xy;
}
idx = 0;
triangles = (xycount / 2) - 2;
for (index = 2, idx = 0;idx < triangles;index += 2, ++idx){
a.v1[x] = polygon_vertices[0];
a.v1[y] = polygon_vertices[1];
a.v2[x] = polygon_vertices[index + 0];
a.v2[y] = polygon_vertices[index + 1];
a.v3[x] = polygon_vertices[index + 2];
a.v3[y] = polygon_vertices[index + 3];
triangle_area = area(a);
polygon_area += triangle_area;
}
printf("area=%.2f\t", polygon_area);
perim = perimeter(polygon_vertices, xycount);
printf("perimeter=%.2f\n", perim);
sprintf(result, "%.2f%.2f", polygon_area, perim);
if(strcmp(result,expected) == 0) {
printf("Arrays are the same\n");
}
else if(strcmp(result,expected1) == 0){
printf("Arrays are the same");
}
else if(strcmp(result,expected2) == 0){
printf("Arrays are the same");
}
else if(strcmp(result,expected3) == 0){
printf("Arrays are the same");
}
else if(strcmp(result,expected4) == 0){
printf("Arrays are the same");
}
else if(strcmp(result,expected5) == 0){
printf("Arrays are the same");
}
else {
printf("Arrays are the different");
}
}
fclose(data);
return 0;
}
/* calculate triangle area with Heron's formula */
double area(triangle a)
{
double s1, s2, s3, S, area;
s1 = side(a.v1, a.v2);
s2 = side(a.v2, a.v3);
s3 = side(a.v3, a.v1);
S = (s1 + s2 + s3) / 2;
area = sqrt(S*(S - s1)*(S - s2)*(S - s3));
return area;
}
/* calculate polygon perimeter */
double perimeter(double *vertices, int size)
{
int idx, jdx;
double p1[2], p2[2], pfirst[2], plast[2];
double perimeter;
perimeter = 0.0;
/* 1st vertex of the polygon */
pfirst[x] = vertices[0];
pfirst[y] = vertices[1];
/* last vertex of polygon */
plast[x] = vertices[size-2];
plast[y] = vertices[size-1];
/* calculate perimeter minus last side */
for(idx = 0; idx <= size-3; idx += 2)
{
for(jdx = 0; jdx < 4; ++jdx)
{
p1[x] = vertices[idx];
p1[y] = vertices[idx+1];
p2[x] = vertices[idx+2];
p2[y] = vertices[idx+3];
}
perimeter += side(p1, p2);
}
/* add last side */
perimeter += side(plast, pfirst);
return perimeter;
}
/* calculate length of side */
double side(double *p1, double *p2)
{
double s1, s2, s3;
s1 = (p1[x] - p2[x]);
s2 = (p1[y] - p2[y]);
s3 = (s1 * s1) + (s2 * s2);
return sqrt(s3);
}
最佳答案
on a diferente machine, I tested in Linux also, and in some cases it appears "Arrays are diferent".
例如考虑第一个 triangle .它的面积是 11.775,在 expected
中你有 11.77
可以比较。现在,在不同的实现中,舍入误差可能导致结果略低于或高于 11.775,而 sprintf(result, "%.2f ...
可以产生 11.77
或 11.78
,因此 strcmp
在一种情况下导致“相同”,在另一种情况下导致“不同”。
关于c - 奇怪的问题 : Getting different calculation results of the area and perimeter of a polyngn (on different machines and on different times),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41154443/
你好,我有一个关于 d3 的性质的问题,我认为这是关于 d3 的非常深入的细节。 据我了解, d3 中的变量声明,如 var svg = d3.select('boby').append('svg'
如 this question 中所述,java.awt.geom.Area的equals方法定义为 public boolean equals(Area other) 而不是覆盖 Object 中的
我希望红色区域始终适合内容,以便下方区域(评论部分)始终紧随其后而不是下方。 在 chrome 中,它有效,但在 Firefox 中无效(见图片)。 我认为通过添加 grid-template-row
我想在曲线下填充一小块区域。但是,带状几何图形将分布的两个“部分”连接起来。 library(tidyverse) density(rnorm(1000, 0, 1)) %$% data.fram
我在 chrome 中得到一个奇怪的行为,它在空白 IE 之后创建正方形 Price: 123234 但这毕竟不是网站上的所有空格,只是在我得到两个字符串如 Price: 然后在我的代码中添加价格的情
我有一个小问题,我不认为我想做的事情可以只用纯 CSS 来实现,但我想我还是要问。 基本上,我有一个 DIV,其中包含一个超链接元素,该元素的大小小于其父 DIV。所以实际上我在一个正方形中有一个正方
我正在尝试将元素放置到一个简单的 9x9 网格中,但是当我尝试将元素放置在左下角或右下角时,它们并没有停留在那里,而是在它们应该放置的位置上方的一个方框内结束。 Here's a JSFiddle s
我一直在阅读 CSS Grid tutorial在 CSS Tricks 中,但一个基本方面让我有点困惑。 似乎有两种方法可以决定一个网格元素跨越多少个单元格: grid-template-area使
我试图实现这个 great blog Gavin Simpson 使用从 cancensus 包下载的数据发布,但在尝试评估 gam 时出现以下错误: Error in smooth.construc
基本上,每当我发送文本区域消息来填写电子邮件正文时,该文本区域的名称总是会继续。因此,例如,如果我在文本区域中输入“Hello World”,然后按发送,我的电子邮件应用程序将打开,正文显示:“mes
PyTorch 函数 torch.nn.functional.interpolate包含多种上采样模式,例如:nearest , linear , bilinear , bicubic , trili
我正在尝试使用 jQuery 根据另一个区域的高度扩展一个区域。但是,第一个区域是动态内容,所以我不能像在 JS 代码中那样设置固定值(第 6 行,高度为 175px)。 我这里有一个例子:https
我在 MATLAB 中创建一个图形,然后对图形的背景进行着色以突出显示区域。这方面的一个例子如下: clc; clear all; hFig = figure; y = [0:0.1:2*pi]; x
我读了很多关于这个问题的资料,但我想不通。 路由和 ASP .NET MVC 的一切都非常简单,但我仍然坚持这一点。 问题是我正在尝试使用这种形式对给定的 url 进行 GET: {区域}/{ Con
题目地址: https://leetcode.com/problems/rectangle-area/description/ 题目描述: Find the total area covered
我在ScrollViewer中有一个Canvas。 Canvas 的尺寸为600x600,而ScrollViewer 400x400。如果我滚动到右侧,则看不到Canvas左侧的200pxl。因此,我
考虑一个圆。现在考虑从圆心向右绘制的圆半径。现在想象半径绕圆心旋转,旋转时扫过一个区域。我的问题是:当半径从 0 度旋转到围绕圆的任何选定的度数时,我想使用 iPhone 的动画技术用与圆的背景区域不
我想知道是否有人知道为什么 IB 在奇怪的 Nib 上到处都有莫名其妙的高亮区域.. 下面是一个例子: 我的意思是我用红色标记的区域内的亮区... 分割 View 下方没有 View ,没有与之对应的
我是 IOS 应用程序开发的新手,目前正在学习 Auto Layout。 有时,当我添加约束时,“安全区域”会丢失。 我正在尝试为我的 StackView 添加约束,其中 0、0、0、0 用于相对于“
我添加了 UIButton类型为 UIButtonTypeInfoDark到一个 View 和它的触摸区域是巨大的。我知道 Apple 推荐 44px,但在这种情况下,它要大一些。我为 View 设置
我是一名优秀的程序员,十分优秀!