工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 1837|回复: 5

一道C++的题目

[复制链接]
发表于 2006-11-24 23:05 | 显示全部楼层 |阅读模式
A farmer wishes to build a fence around his field. Figure 1 gives an example of a field with four corners. The positions of the corners are measured as (x, y) co-ordinates relative to the farmhouse (0, 0). Write a C++ program to calculate how long the fence must be, given the number of corners and the position of each of them.

一个农民想建个维栏,Figure 1给出4个角。每个角得位置已屋子(0,0)为坐标,用C++ 程序去求维栏得长度,坐标给出如下图。





Figure 1 Farmer's Field


Part A.

A1. The programme should operate as follows:
程序应如下操作:
·        Requests the user (i.e. the farmer) to enter the number of corners and then asks for the co-ordinates of each corner relative to the farmhouse.(Use a loop and store the x and y co-ordinates of each corner of the field in separate arrays.)
·       要求(农民)输入角的数量,然后输入每个角相对屋子(0。0)的坐标。(用LOOP语言和分别储存每个角的坐标(x,y))
Displays the co-ordinates of each corner. (Use a loop.)
(陈列出每个角的坐标(用loop))
Calculates the distances between all corners. (Use the following formula to calculate the distance between any two corners i and i-1: . )
(计算每个角之间的距离(用 方程去计算每个角之间的距离))
Displays the distances between all corners. Example:
(陈列出所有每个角间的距离,例如:)
The distance between corner 1 (1,1) and corner 2 (2,2) is: 1.41
Calculates and displays the total length of the fence.
(角1(1,1)和角2(2,2)间的的是:1。41
计算和列出栏的总长度)
·        The user should be able to repeat these steps as many times as required.




A2. Test your programme. Provide sufficient evidence of testing.
(使用者应该能重复可以做这些步骤像它要求的这么多
测试你的程序)
Part B.
B1. Implement a simple option menu to make your program easier to use.
B1(运用简单的操作去做程序,可以容易的用)
B2. Improve your programme. Consider cases such as choosing an option that is not available in the menu, starting from option 2,3, etc. instead of 1, entering a negative number of corners, entering the same co-ordinates for two or more corners, entering corner co-ordinates that form a line, not a field, etc.). Provide an option to change the number of corners or their co-ordinates without exiting the menu or re-entering those co-ordinates that remain unchanged.


改善你的计划.例如认为这不是一个选择可供选择的菜单,选择由2,3,等不是一日进入负多少角落,进入同一统筹为两个或两个以上的角落,进入角落协调,形成一条线,不是一个领域,等等).提供一个选择改变一些死角或协调无退出菜单或重新进入那些协调,维持不变.

[ 本帖最后由 iptton 于 2006-12-11 18:20 编辑 ]
 楼主| 发表于 2006-11-24 23:35 | 显示全部楼层
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'corners' : unknown size
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'dist' : unknown size
回复

使用道具 举报

发表于 2006-11-24 23:38 | 显示全部楼层
DEV C++下编译成功。。。
回复

使用道具 举报

 楼主| 发表于 2006-11-25 00:11 | 显示全部楼层
  1. /*********************************
  2. program file:develop_for_quine.cpp
  3. power by David Hwong
  4. date:24/11/2006
  5. *********************************/
  6. #include<iostream>
  7. #include<cmath>
  8. #include<iomanip>
  9. using namespace std;
  10. float Distance(float x1,float x2,float y1,float y2);/*10*/
  11. int main()
  12. {   
  13.     /*int noCorners;
  14. float *corners;
  15. float dist;
  16. cout<<"Please enter the number of corners:"<<endl;
  17. cin>>noCorners;
  18. corners=new float[noCorners][2];*/
  19. int noCorners;
  20. cout<<"Please enter the number of corners:"<<endl;
  21. cin>>noCorners;
  22. float (*corners)[2]=new float[noCorners][2];
  23. float dist;
  24. float l=0;
  25. int i,j;
  26. for(i=0;i<noCorners;i++)
  27. { j=i+1;
  28.   cout<<"Please enter the co-ordinates of corner "<<j<<" relative to the farmhouse(x"<<j<<",y"<<j<<");"<<endl;
  29.   cin>>corners[0]>>corners[1];
  30.   }
  31. for(i=0;i<noCorners;i++)
  32. { j=i+1;
  33.   cout<<"The co-ordinates of corner "<<j<<" relative to the farmhouse is ("<<corners[0]<<","<<corners[1]<<");"<<endl;
  34.   }
  35. for(i=0;i<noCorners;i++)
  36. {   for(j=1;j<noCorners-i;j++)
  37.         {  dist=Distance(corners[0],corners[1],corners[(i+j)%noCorners][0],corners[(i+j)%noCorners][1]);
  38.      cout<<"The distance between corner "<<i+1<<" and corner "<<(i+j)%noCorners+1<<" is "<<dist<<";"<<endl;
  39.         if((i+j)%noCorners==i+noCorners-1||(i+j)%noCorners-i==1)
  40.                               {l=l+dist;
  41.                               /*cout<<i<<" "<<(i+j)%noCorners<<" "<<l<<endl;*/}
  42.                               }
  43. }
  44. cout<<"The total length of the fence is "<<l<<"."<<endl;
  45. cout<<"Put anykey+Enter to Esc!"<<endl;
  46. cin>>j;
  47. return 0;
  48. }
  49. float Distance(float x1,float y1,float x2,float y2)
  50. {
  51. float dx,dy;
  52. dx=(x2-x1)*(x2-x1);
  53. dy=(y2-y1)*(y2-y1);
  54. /*cout<<x1<<" "<<x2<<" "<<y1<<" "<<y2<<" "<<dx<<" "<<dy<<endl;*/
  55. float dist=sqrt(dx+dy);
  56. return dist;
  57. }
复制代码
-------------------------END-----------------------------

[ 本帖最后由 iptton 于 2006-12-11 18:17 编辑 ]
回复

使用道具 举报

发表于 2006-12-11 18:22 | 显示全部楼层
LS   只帖代码 意欲何为?

[ 本帖最后由 iptton 于 2006-12-11 23:08 编辑 ]
回复

使用道具 举报

发表于 2006-12-11 22:41 | 显示全部楼层
想加分
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-16 00:25

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表