foldless777 2022-10-12 13:53 采纳率: 60%
浏览 125
已结题

c++提示用户输入两个点的坐标,然后计算并输出这两个点之间的距离

提示用户输入两个点的坐标,然后计算并输出这两个点之间的距离。两点坐标为(x1,y1), (x2,y2),距离为对应坐标差的平方和,再开平方。输出的距离保留4位小数。
求解答 为什么计算结果不正确

#include <iostream>
#include<math.h>
#include<iomanip>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    cout<<"Please input the coordinates of two points:"<<endl;
    cout<<"point 1(x,y):"<<endl;
    cout<<"point 2(x,y):"<<endl;
    double x1,x2,y1,y2,distance;
    cin>>(x1,y1)>>(x2,y2);
    distance=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
    cout<<"This distance is:"<<fixed<<setprecision(4)<<distance<<endl;
    return 0;
}

  • 写回答

2条回答 默认 最新

  • _GX_ 2022-10-12 14:05
    关注
    #include <cmath>
    #include <iomanip>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char **argv) {
      double x1, x2, y1, y2, distance;
      char ch;
      cout << "Please input the coordinates of two points:" << endl;
      cout << "point 1(x,y): ";
      cin >> ch >> x1 >> ch >> y1 >> ch;
      cout << "point 2(x,y): ";
      cin >> ch >> x2 >> ch >> y2 >> ch;
      distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
      cout << "This distance is:" << fixed << setprecision(4) << distance << endl;
      return 0;
    }
    
    $ g++ -Wall main.cpp
    $ ./a.out
    Please input the coordinates of two points:
    point 1(x,y): (1,1)
    point 2(x,y): (2,2)
    This distance is: 1.4142
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月20日
  • 已采纳回答 10月12日
  • 创建了问题 10月12日

悬赏问题

  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。