C++ 如何在C++;
对于我的CS课程介绍,我应该读一些C++ 如何在C++;,c++,C++,对于我的CS课程介绍,我应该读一些 N = 4 (0.5,1) (0.6,1.5) (0.7,2) (0.8,2.5) 并打印出来 X := [0.5, 0.6, 0.7, 0.8] Y := [1, 1.5, 2, 2.5] 到目前为止,我的代码看起来像 #include <iostream> using namespace std; int main() { int size; char c; double point; cin >> c &g
N = 4
(0.5,1) (0.6,1.5) (0.7,2) (0.8,2.5)
并打印出来
X := [0.5, 0.6, 0.7, 0.8]
Y := [1, 1.5, 2, 2.5]
到目前为止,我的代码看起来像
#include <iostream>
using namespace std;
int main()
{
int size;
char c;
double point;
cin >> c >> c >> size;
int N = size*2;
double *A = new double[N];
for(int i = 0; i < N; ++i)
cin >> c >> A[i];
cin >> c;
cout << A << endl;
return 0;
}
#包括
使用名称空间std;
int main()
{
整数大小;
字符c;
双点;
cin>>c>>c>>尺寸;
int N=大小*2;
双精度*A=新双精度[N];
对于(int i=0;i>c>>A[i];
cin>>c;
c> 你可以在C++中用<强>向量和<强>配对< /Stand >。你可以学习和从这个。
现在没事了,正是你想要的:
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int main ()
{
vector < pair <double, double> > v;
int N = 4;
for (int i=0; i<N; i++)
{
double x,y;
scanf("(%lf,%lf)", &x, &y);
v.push_back(pair <double, double>(x,y));
getchar();
}
cout<< "X := ["<< v[0].first;
for (int i=1; i<N; i++)
cout<<" ,"<<v[i].first;
cout<< "]"<<endl;
cout<< "Y := ["<< v[0].second;
for (int i=1; i<N; i++)
cout<<" ,"<<v[i].second;
cout<< "]"<<endl;
return 0;
}
输出:
X := [0.5, 0.6, 0.7, 0.8]
Y := [1, 1.5, 2, 2.5]
首先,我认为使用向量和对C++ STL的所有建议是完全正确的。但是,考虑到MIDI代码刚刚开始了他的编程之旅,我认为使用数组是一个很好的练习< /强>的方法。我认为你可以使用两个双数组存储关于点的信息。
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
int main()
{
int n;
char c;
cin >> c >> c >> n;
// store x, y coordinates in two separated arrays.
double *x = new double[n];
double *y = new double[n];
for (int i = 0; i < n; i++) {
// Read x, y coordiantes and ignore the parenthesis and comma.
cin >> c >> x[i] >> c >> y[i] >> c;
}
cout << "X := [";
for (int i = 0; i < n; i++) {
cout << x[i];
// Here worth beginner's attention.
if (i < n - 1) {
cout << ", ";
}
}
cout << "]" << endl;
cout << "Y := [";
for (int i = 0; i < n; i++) {
cout << y[i];
if (i < n - 1) {
cout << ", ";
}
}
cout << "]" << endl;
return 0;
}
#包括
使用名称空间std;
#包括
使用名称空间std;
int main()
{
int n;
字符c;
cin>>c>>c>>n;
//将x、y坐标存储在两个单独的数组中。
double*x=新的double[n];
双精度*y=新双精度[n];
对于(int i=0;i>x[i]>>c>>y[i]>>c;
}
我想你会明白这一点的。只要确保在每次循环迭代中输入一个完整的括号对就行了。提示:使用std::vector
而不是原始数组和new
将在以后的程序中为你省去很多麻烦。哇,非常感谢!这非常有效。也感谢你的评论(这里值得初学者注意)那个逗号会让人讨厌的。
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
int main()
{
int n;
char c;
cin >> c >> c >> n;
// store x, y coordinates in two separated arrays.
double *x = new double[n];
double *y = new double[n];
for (int i = 0; i < n; i++) {
// Read x, y coordiantes and ignore the parenthesis and comma.
cin >> c >> x[i] >> c >> y[i] >> c;
}
cout << "X := [";
for (int i = 0; i < n; i++) {
cout << x[i];
// Here worth beginner's attention.
if (i < n - 1) {
cout << ", ";
}
}
cout << "]" << endl;
cout << "Y := [";
for (int i = 0; i < n; i++) {
cout << y[i];
if (i < n - 1) {
cout << ", ";
}
}
cout << "]" << endl;
return 0;
}