
拜请C语言高手看一下代码哪里出问题了 20
代码较长,先拜谢各位了!主要疑惑在stage1,也就是读入文件数据的部分,要求是用ProjectB<input.txt代替直接输入xy格式的数据,测试用的input.tx...
代码较长,先拜谢各位了!
主要疑惑在stage 1,也就是读入文件数据的部分,要求是用
ProjectB <input.txt
代替直接输入x y格式的数据,测试用的input.txt内容是:
10.0 10.0
70.0 25.0
20.0 50.0
40.0 10.0
25.0 25.0
我觉得用的i=0有重复且可能有问题,另外不是很确定PointArray[i].x/y的指代是不是正确。。。
另一个主要疑惑是stage 3,期望是能够在输入:
ProjectB 3 <input.txt
后,(按测试数据,总共为5组数据),输入的3为(也是main (int argc)的值)存在的灯泡数量,在剩下的2点计算光强。总的计算光强的公式应该是没问题的,但是对于实现这个结果的循环是否正确有些怀疑,因为我没办法成功运行程序,无法测试这一部分。。。
/*
* ProjectB.c
* 背景是测定路灯的位置能否提供合适的亮度,路灯高8.25米,假设灯泡在路灯的直接下方(8.25米距离)的亮度是1.0,或者100%,亮度和离灯泡距离的关系是1/d^2,就是说假如8.25*2=16.5米(三角形斜边)的距离的话,亮度就变成了1/4,离路灯的地面距离是根号(16.5^2-8.25^2)=14.29。 *
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
float x;
float y;
}stPoint;
/* Assume maximum 20 rows */
stPoint PointArray[20];
/* function to calculate the light intensity */
float cal_intensity(stPoint testpoint, stPoint lightposition)
{
float ret = 8.25*8.25/(pow((lightposition.x - testpoint.x), 2.0) + pow((lightposition.y
- testpoint.y), 2.0) + pow(8.25, 2.0));
return ret;
}
int main(int argc, char *argv[])
{
int i, j, k;
int point_num=0, underlightnum;
float intensity_00, percentage;
stPoint pointtemp;
/*===stage 1===*/
/* 用ProjectB <input.txt来读入数据,假设最多不超过20组坐标 */
int i=0;
while(scanf("%f %f\n",
PointArray[i].x,
PointArray[i].y)==2){
point_num++;
}
/* return the number of total lights to use in other stages */
return point_num;
printf("Stage 1\n");
printf("===\n");
printf("%d lighting tower locations read from file\n", point_num);
/*
for(i=0; i<point_num; i++)
{
printf("%f %f\n", PointArray[i].x, PointArray[i].y);
}*/
/*stage 2=计算导入的灯位置后原点(0,0)的光强 这部分应该没问题*/
intensity_00 = 0;
pointtemp.x = 0;
pointtemp.y = 0;
for(i=0; i<point_num; i++)
{
intensity_00 += cal_intensity(pointtemp, PointArray[i]);
}
printf("\n");
printf("Stage 2\n");
printf("=\n");
printf("Lighting intensity at ( 0.0, 0.0) from 5 lighting towers: %.1f%%\n",
intensity_00*100);
/*=stage 3=*/
printf("\n");
printf("Stage 3\n");
printf("========\n");
for(i=0; i<argc; i++)
{
pointtemp.x = (float)i;
pointtemp.y = (float)j;
intensity_00 = 0;
intensity_00 += cal_intensity(pointtemp, PointArray[i]);
printf("Lighting intensity at ( %.1f, %.1f) from %d lighting towers: %.1f%%\n",
pointtemp.x, pointtemp.y, intensity_00*100, argc);
}
(stage 4、5略)
system("PAUSE");
return EXIT_SUCCESS;
} 展开
主要疑惑在stage 1,也就是读入文件数据的部分,要求是用
ProjectB <input.txt
代替直接输入x y格式的数据,测试用的input.txt内容是:
10.0 10.0
70.0 25.0
20.0 50.0
40.0 10.0
25.0 25.0
我觉得用的i=0有重复且可能有问题,另外不是很确定PointArray[i].x/y的指代是不是正确。。。
另一个主要疑惑是stage 3,期望是能够在输入:
ProjectB 3 <input.txt
后,(按测试数据,总共为5组数据),输入的3为(也是main (int argc)的值)存在的灯泡数量,在剩下的2点计算光强。总的计算光强的公式应该是没问题的,但是对于实现这个结果的循环是否正确有些怀疑,因为我没办法成功运行程序,无法测试这一部分。。。
/*
* ProjectB.c
* 背景是测定路灯的位置能否提供合适的亮度,路灯高8.25米,假设灯泡在路灯的直接下方(8.25米距离)的亮度是1.0,或者100%,亮度和离灯泡距离的关系是1/d^2,就是说假如8.25*2=16.5米(三角形斜边)的距离的话,亮度就变成了1/4,离路灯的地面距离是根号(16.5^2-8.25^2)=14.29。 *
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
float x;
float y;
}stPoint;
/* Assume maximum 20 rows */
stPoint PointArray[20];
/* function to calculate the light intensity */
float cal_intensity(stPoint testpoint, stPoint lightposition)
{
float ret = 8.25*8.25/(pow((lightposition.x - testpoint.x), 2.0) + pow((lightposition.y
- testpoint.y), 2.0) + pow(8.25, 2.0));
return ret;
}
int main(int argc, char *argv[])
{
int i, j, k;
int point_num=0, underlightnum;
float intensity_00, percentage;
stPoint pointtemp;
/*===stage 1===*/
/* 用ProjectB <input.txt来读入数据,假设最多不超过20组坐标 */
int i=0;
while(scanf("%f %f\n",
PointArray[i].x,
PointArray[i].y)==2){
point_num++;
}
/* return the number of total lights to use in other stages */
return point_num;
printf("Stage 1\n");
printf("===\n");
printf("%d lighting tower locations read from file\n", point_num);
/*
for(i=0; i<point_num; i++)
{
printf("%f %f\n", PointArray[i].x, PointArray[i].y);
}*/
/*stage 2=计算导入的灯位置后原点(0,0)的光强 这部分应该没问题*/
intensity_00 = 0;
pointtemp.x = 0;
pointtemp.y = 0;
for(i=0; i<point_num; i++)
{
intensity_00 += cal_intensity(pointtemp, PointArray[i]);
}
printf("\n");
printf("Stage 2\n");
printf("=\n");
printf("Lighting intensity at ( 0.0, 0.0) from 5 lighting towers: %.1f%%\n",
intensity_00*100);
/*=stage 3=*/
printf("\n");
printf("Stage 3\n");
printf("========\n");
for(i=0; i<argc; i++)
{
pointtemp.x = (float)i;
pointtemp.y = (float)j;
intensity_00 = 0;
intensity_00 += cal_intensity(pointtemp, PointArray[i]);
printf("Lighting intensity at ( %.1f, %.1f) from %d lighting towers: %.1f%%\n",
pointtemp.x, pointtemp.y, intensity_00*100, argc);
}
(stage 4、5略)
system("PAUSE");
return EXIT_SUCCESS;
} 展开
1个回答
展开全部
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
float x;
float y;
}stPoint;
/* Assume maximum 20 rows */
stPoint PointArray[20];
/* function to calculate the light intensity */
float cal_intensity(stPoint testpoint, stPoint lightposition)
{
float ret = ((8.25*8.25)/(pow((lightposition.x - testpoint.x), 2.0) + pow((lightposition.y- testpoint.y), 2.0) + pow(8.25, 2.0)));
return ret;
}
int main(int argc, char *argv[])
{
int j, k;
int i=0;
int point_num=0;
int underlightnum;
float intensity_00;
int percentage;
stPoint pointtemp;
/*===stage 1===*/
/* 用ProjectB <input.txt来读入数据,假设最多不超过20组坐标 */
while(scanf("%f %f\n",
PointArray[i].x,
PointArray[i].y)==2)
{
point_num++;
}
/* return the number of total lights to use in other stages */
return point_num;
printf("Stage 1\n");
printf("===\n");
printf("%d lighting tower locations read from file\n", point_num);
/*
for(i=0; i<point_num; i++)
{
printf("%f %f\n", PointArray[i].x, PointArray[i].y);
}*/
/*stage 2=计算导入的灯位置后原点(0,0)的光强 这部分应该没问题*/
intensity_00 = 0;
pointtemp.x = 0;
pointtemp.y = 0;
for(i=0; i<point_num; i++)
{
intensity_00 += cal_intensity(pointtemp, PointArray[i]);
}
printf("\n");
printf("Stage 2\n");
printf("=\n");
printf("Lighting intensity at ( 0.0, 0.0) from 5 lighting towers: %.1f%%\n",intensity_00*100);
/*=stage 3=*/
printf("\n");
printf("Stage 3\n");
printf("========\n");
for(i=0; i<argc; i++)
{
pointtemp.x = (float)i;
pointtemp.y = (float)j;
intensity_00 = 0;
intensity_00 += cal_intensity(pointtemp, PointArray[i]);
printf("Lighting intensity at ( %.1f, %.1f) from %d lighting towers: %.1f%%\n",
pointtemp.x, pointtemp.y, intensity_00*100, argc);
}
//(stage 4、5略)
system("PAUSE");
return EXIT_SUCCESS;
}
#include <math.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
float x;
float y;
}stPoint;
/* Assume maximum 20 rows */
stPoint PointArray[20];
/* function to calculate the light intensity */
float cal_intensity(stPoint testpoint, stPoint lightposition)
{
float ret = ((8.25*8.25)/(pow((lightposition.x - testpoint.x), 2.0) + pow((lightposition.y- testpoint.y), 2.0) + pow(8.25, 2.0)));
return ret;
}
int main(int argc, char *argv[])
{
int j, k;
int i=0;
int point_num=0;
int underlightnum;
float intensity_00;
int percentage;
stPoint pointtemp;
/*===stage 1===*/
/* 用ProjectB <input.txt来读入数据,假设最多不超过20组坐标 */
while(scanf("%f %f\n",
PointArray[i].x,
PointArray[i].y)==2)
{
point_num++;
}
/* return the number of total lights to use in other stages */
return point_num;
printf("Stage 1\n");
printf("===\n");
printf("%d lighting tower locations read from file\n", point_num);
/*
for(i=0; i<point_num; i++)
{
printf("%f %f\n", PointArray[i].x, PointArray[i].y);
}*/
/*stage 2=计算导入的灯位置后原点(0,0)的光强 这部分应该没问题*/
intensity_00 = 0;
pointtemp.x = 0;
pointtemp.y = 0;
for(i=0; i<point_num; i++)
{
intensity_00 += cal_intensity(pointtemp, PointArray[i]);
}
printf("\n");
printf("Stage 2\n");
printf("=\n");
printf("Lighting intensity at ( 0.0, 0.0) from 5 lighting towers: %.1f%%\n",intensity_00*100);
/*=stage 3=*/
printf("\n");
printf("Stage 3\n");
printf("========\n");
for(i=0; i<argc; i++)
{
pointtemp.x = (float)i;
pointtemp.y = (float)j;
intensity_00 = 0;
intensity_00 += cal_intensity(pointtemp, PointArray[i]);
printf("Lighting intensity at ( %.1f, %.1f) from %d lighting towers: %.1f%%\n",
pointtemp.x, pointtemp.y, intensity_00*100, argc);
}
//(stage 4、5略)
system("PAUSE");
return EXIT_SUCCESS;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询