import java.util.Scanner;
public class Random {
public void shuru(){
int s=0;
int j=0;
int t=0;
int m=0;
int p=0;
int o=0;
int r=0;
int h=0;
double g=0;
Scanner sc=new Scanner(System.in);
System.out.print("长:");
String l1=sc.next();
System.out.print("宽:");
String l2=sc.next();
System.out.print("能量:");
String pow=sc.next();
System.out.print("速率:");
String sp=sc.next();
System.out.print("节点数:");
String n=sc.next();
String[] Array=new String[Integer.parseInt(n)];
int[] a=new int[Integer.parseInt(n)];
int[] b=new int[Integer.parseInt(n)];
int[] c=new int[Integer.parseInt(n)];
int[] d=new int[Integer.parseInt(n)];
int[] e=new int[Integer.parseInt(n)];
for(int i=0;i<Integer.parseInt(n);i++){
a[i]=0;
d[i]=0;
b[i]=Integer.parseInt(n);
int x=(int)(Math.random()*Integer.parseInt(l1));
int y=(int)(Math.random()*Integer.parseInt(l2));
int z=(int)(Math.random()*100);
e[i]=(int)(Math.random()*100);
Array[i]=x+","+y;
c[i]=z;
if(i==0){
Array[0]=x+","+y;
}else{
while(p==0){
for(int q=0;q<i;q++){
if(Array[q].equals(Array[i])){
o=o+1;
}
}
if(o>=1){
p=0;
x=(int)(Math.random()*Integer.parseInt(l1));
y=(int)(Math.random()*Integer.parseInt(l2));
Array[i]=x+","+y;
}else{
p=1;
Array[i]=x+","+y;
}
o=0;
}
p=0;
}
}
for(int i=0;i<Integer.parseInt(n);i++){
System.out.println(i+"("+Array[i]+")"+","+c[i]+","+e[i]);
}
Scanner sr=new Scanner(System.in);
System.out.print("起始节点ID:");
String l3=sr.next();
System.out.print("目标节点ID:");
String l4=sr.next();
suanfa sk=new suanfa();
s=Integer.parseInt(l3);
while(s!=Integer.parseInt(l4)){
double dis=sk.dis(Array[s], Array[Integer.parseInt(l4)]);
a[s]=1;
for(j=0;j<Integer.parseInt(n);j++){
if(a[j]==0&&d[j]==0){
Double temp=sk.dis(Array[s], Array[j]);
if(temp<=dis&&temp>=0){
dis=temp;
r=j;
}
}
}
if(c[r]<Integer.parseInt(pow)){
d[r]=1;
}else{
s=r;
g=g+dis;
b[t]=s;
t=t+1;
a[s]=1;
}
}
System.out.println(l3+"("+Array[Integer.parseInt(l3)]+")");
for(m=0;m<Integer.parseInt(n);m++){
if(b[m]!=Integer.parseInt(n)){
h=h+1;
System.out.println(b[m]+"("+Array[b[m]]+")");
}
}
System.out.println("经过"+h+"跳");
System.out.println(g/Double.parseDouble(sp)+"秒");
}
public static void main(String[] arg){
Random ck=new Random();
ck.shuru();
}
}
package TG;
import java.util.Scanner;
public class suanfa {
public Double dis(String s,String d){
int x1,x2,y1,y2=0;
int i=s.indexOf(",");
x1=Integer.parseInt(s.substring(0,i));
s=s.replace(s.substring(0,i+1),"");
y1=Integer.parseInt(s);
int j=d.indexOf(",");
x2=Integer.parseInt(d.substring(0,j));
d=d.replace(d.substring(0,j+1),"");
Double l=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return l;
}
public static void main(String[] arg){
suanfa ck=new suanfa();
System.out.println(ck.dis("45,0","50,0"));
}
}
25 个解决方案
#1
楼主是找人做作业来了?
#2
任务是要求用C++写 我不会C++.
#3
不会啊 还顶吧
#4
up
#5
路过~
#6
顶
#7
up~~
#8
看不懂
#9
文字说明下
#10
语法转换倒是简单,就是不知道这些JAVA里的类库对应在C++里用什么。
#11
路过~
#12
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
namespace TG{
class suanfa
{
public:
double dis(string s,string d)
{
int x1=0;
int x2=0;
int y1=0;
int y2=0;
int i=s.find(",");
x1=atoi(s.substr(0,i).c_str());
y1=atoi(s.substr(i+1,s.length()-i-1).c_str());;
int j=d.find(",");
x2=atoi(d.substr(0,j).c_str());
y2=atoi(d.substr(i+1,d.length()-i-1).c_str());;
double l=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return l;
}
};
}
using namespace TG;
int main()
{
suanfa* ck=new suanfa();
cout<<ck->dis("45,0","50,0")<<endl;;
}
#13
以后问问题贴码得像这位仁兄,用标准缩进模式,别人也好看些.
#14
好累。
#15
太长了,不好看
#16
帮顶··
#17
楼主贴的JAVA,代码
有个类的名是小写开头的,这点不太规范
不过楼主的JAVA类名,2个都是拼音的,这是好习惯
就是说,要么全是拼音的类名,要么全是英文的,
不过推荐使用英文的
先贴一个C++的代码,因为JAVA里类里只有方法,所以没有必要写类
个人意见
有个类的名是小写开头的,这点不太规范
不过楼主的JAVA类名,2个都是拼音的,这是好习惯
就是说,要么全是拼音的类名,要么全是英文的,
不过推荐使用英文的
先贴一个C++的代码,因为JAVA里类里只有方法,所以没有必要写类
个人意见
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
double dis(string s, string d)
{
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
int i = s.find(",");
x1 = atoi(s.substr(0, i).c_str());
y1 = atoi(s.substr(i, s.length()).c_str());
cout << " i = " << i << endl;
cout << " x1 = " << x1 << endl;
cout << " y1 = " << y1 << endl;
int j = d.find(",");
x2 = atoi(d.substr(0, i).c_str());
y2 = atoi(d.substr(i + 1, d.length()).c_str());
cout << " j = " << j << endl;
cout << " x2 = " << x2 << endl;
cout << " y2 = " << y2 << endl;
double result = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return result;
}
int main(int argc, char *argv[])
{
double result = dis("45,0", "50,0");
cout << " result is : " << result << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
#18
原来JAVA代码里的Scanner就是起到输入的作用啊,JAVA的代码是写在两个类文件里的,其中suanfa类里的main函数只起到一个测试作用,没什么用吧。
我把他们合并在一起,虽然也是两个类,但是只有一个C++文件,楼主直接贴进去运行就可以了,我跟原来的JAVA运行做了对比,应该是没有什么问题。
代码里也没写什么注释,楼主有不明白的地方再问吧。
翻译的很幸苦啊,楼主给加点分啊!
我把他们合并在一起,虽然也是两个类,但是只有一个C++文件,楼主直接贴进去运行就可以了,我跟原来的JAVA运行做了对比,应该是没有什么问题。
代码里也没写什么注释,楼主有不明白的地方再问吧。
翻译的很幸苦啊,楼主给加点分啊!
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
class suanfa
{
public:
double dis(string s, string d)
{
int x1, x2, y1, y2 = 0;
int i = s.find_first_of(",");
x1 = atoi(s.substr(0, i).c_str());
s = s.replace(0, i+1, "");
y1 = atoi(s.c_str());
int j = d.find_first_of(",");
x2 = atoi(d.substr(0, j).c_str());
d = d.replace(0, j+1, "");
double l = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return l;
}
};
class Random
{
public:
string itos(int i) //int转换为string
{
stringstream s;
s<<i;
return s.str();
}
void shuru()
{
int i;
int s = 0;
int j = 0;
int t = 0;
int m = 0;
int p = 0;
int o = 0;
int r = 0;
int h = 0;
double g = 0;
string l1, l2, pow, sp, n;
cout<<"长:";
cin>>l1;
cout<<"宽:";
cin>>l2;
cout<<"能量:";
cin>>pow;
cout<<"速率:";
cin>>sp;
cout<<"节点数:";
cin>>n;
int array_num;
array_num = atoi(n.c_str()); //将n转换为int
vector<string> Array(array_num); //定义string数组
vector<int> a(array_num);
vector<int> b(array_num);
vector<int> c(array_num);
vector<int> d(array_num);
vector<int> e(array_num);
srand(time(0)); //设置随机数种子
for (i=0; i<array_num; i++)
{
a[i] = 0;
d[i] = 0;
b[i] = array_num;
int x = (int)((rand() % 100)/100.0 * atoi(l1.c_str()));
int y = (int)((rand() % 100)/100.0 * atoi(l2.c_str()));
int z = (int)((rand() % 100)/100.0 * 100);
e[i] = (int)((rand() % 100)/100.0 * 100);
Array[i] = itos(x) + "," + itos(y);
c[i] = z;
if (i == 0)
{
Array[0] = itos(x) + "," + itos(y);
}
else
{
while (p == 0)
{
for (int q=0; q<i; q++)
{
if (Array[q] == Array[i])
o++;
}
if (o >= 1)
{
p = 0;
x = (int)((rand() % 100)/100.0 * atoi(l1.c_str()));
y = (int)((rand() % 100)/100.0 * atoi(l2.c_str()));
Array[i] = itos(x) + "," + itos(y);
}
else
{
p = 1;
Array[i] = itos(x) + "," + itos(y);
}
o = 0;
}
p = 0;
}
}
for (i=0; i<array_num; i++)
{
cout<<i<<"("<<Array[i]<<"),"<<c[i]<<","<<e[i]<<endl;
}
string l3, l4;
cout<<"起始节点ID:";
cin>>l3;
cout<<"目标节点ID:";
cin>>l4;
suanfa sf;
s = atoi(l3.c_str());
int temp_l4 = atoi(l4.c_str());
while (s != temp_l4)
{
double dis = sf.dis(Array[s], Array[temp_l4]);
a[s] = 1;
for (j = 0; j<array_num; j++)
{
if (a[j] == 0 && d[j] == 0)
{
double temp = sf.dis(Array[s], Array[j]);
if (temp <= dis && temp >= 0)
{
dis = temp;
r = j;
}
}
}
if (c[r] < atoi(pow.c_str()))
d[r] = 1;
else
{
s = r;
g += dis;
b[t] = s;
t++;
a[s] = 1;
}
}
cout<<l3<<"("<<Array[atoi(l3.c_str())]<<")"<<endl;
for (m =0; m<array_num; m++)
{
if (b[m] != array_num)
{
h++;
cout<<b[m]<<"("<<Array[b[m]]<<")"<<endl;
}
}
cout<<"经过"<<h<<"跳"<<endl;
cout<<g/(double)atoi(sp.c_str())<<"秒"<<endl;
}
};
void main()
{
Random ck;
ck.shuru();
}
#19
18L大哥 太谢谢了。基本上都能看懂。我还想问下 C++里将输出的结果在一个目录下以.txt形式保存下来该怎么做
再次感谢这位大哥 稍后补上分分
再次感谢这位大哥 稍后补上分分
#20
up!
#21
我也来一个
呵
呵
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
using namespace std;
namespace TG {
double dis(string s, string d)
{
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
int i = s.find(",");
x1 = atoi(s.substr(0, i).c_str());
y1 = atoi(s.substr(i, s.length()).c_str());
cout << " i = " << i << endl;
cout << " x1 = " << x1 << endl;
cout << " y1 = " << y1 << endl;
int j = d.find(",");
x2 = atoi(d.substr(0, i).c_str());
y2 = atoi(d.substr(i + 1, d.length()).c_str());
cout << " j = " << j << endl;
cout << " x2 = " << x2 << endl;
cout << " y2 = " << y2 << endl;
double result = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return result;
}
void shuru()
{
int s = 0;
int j = 0;
int t = 0;
int m = 0;
int p = 0;
int o = 0;
int r = 0;
int h = 0;
double g = 0;
string longStr, widthStr, pow, sp, n;
cout << " 长: ";
cin >> widthStr;
cout << " 宽: ";
cin >> longStr;
cout << " 能量: ";
cin >> pow;
cout << " 速率: ";
cin >> sp;
cout << " 节点数: ";
cin >> n;
const int array_size = atoi(n.c_str());
string temp_array[array_size];
int a[array_size], b[array_size], c[array_size], d[array_size], e[array_size];
int ix = atoi(longStr.c_str());
int iy = atoi(widthStr.c_str());
for (int i = 0; i != array_size; ++i)
{
a[i] = 0;
d[i] = 0;
b[i] = array_size;
int x = rand() * ix;
int y = rand() * iy;
int z = rand() * 100;
e[i] = z;
c[i] = z;
ostringstream oss;
oss << x << "," << y;
temp_array[i] = oss.str();
while (p == 0)
{
for (int m = 0; m != i; ++m)
{
if (temp_array[m] == temp_array[i])
{
++o;
}
}
if (o < 1)
{
p = 1;
x = rand() * atoi(longStr.c_str());
y = rand() * atoi(widthStr.c_str());
oss << x << "," << y;
temp_array[i] = oss.str();
}
o = 0;
}
p = 0;
}
for (int n = 0; n != array_size; ++n) {
cout << n << "(" << temp_array[n] << ")" << "," << c[n] << "," << e[n] << endl;
}
cout << "起始节点ID:" << endl;
string l3;
cin >> l3;
cout << "目标节点ID:" << endl;
string l4;
cin >> l4;
s = atoi(l3.c_str());
while (s != atoi(l4.c_str())) {
double dis = TG::dis(temp_array[s], temp_array[atoi(l4.c_str())]);
a[s] = 1;
for (j = 0; j != atoi(n.c_str()); ++j)
{
if (a[j] == 0 && d[j] == 0) {
double temp = TG::dis(temp_array[s], temp_array[j]);
if (temp <= dis && temp >= 0) {
dis = temp;
r = j;
}
}
}
if (c[r] < atoi(pow.c_str())) {
d[r] = 1;
} else {
s = r;
g = g + dis;
b[t] = s;
t = t + 1;
a[s] = 1;
}
}
cout << l3 << "(" << temp_array[atoi(l3.c_str())] << ")" << endl;
for (m = 0; m < atoi(n.c_str()); ++m) {
if (b[m] != atoi(n.c_str())) {
h = h + 1;
cout << b[m] << "(" << temp_array[b[m]] << ")" << endl;
}
}
cout << "经过" << h << "跳" << endl;
cout << g / atof(sp.c_str()) << "秒" << endl;
}
}
int main(int argc, char *argv[])
{
TG::shuru();
double result = TG::dis("45,0", "50,0");
cout << " result is : " << result << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
#22
学习
#23
都是牛人,我学习
#24
只需要在程序的main函数里加上
freopen("filename.txt", "w", stdout);就可以了,第一个参数是要输出到的文件名,第二个是w表示只写属性,第三个表示是输出。
对应的如果要从某个文件中读入,只要加上
freopen("filename.txt", "r", stdin);参数都能看懂吧。当然读和写的文件都要放在当前工程目录的同一个文件夹下。写入的文件不用存在,程序会自动创建。
接下来一切输入输出都跟平时一样写就可以了,不需要写什么麻烦的文件操作。
#25
学习!
#1
楼主是找人做作业来了?
#2
任务是要求用C++写 我不会C++.
#3
不会啊 还顶吧
#4
up
#5
路过~
#6
顶
#7
up~~
#8
看不懂
#9
文字说明下
#10
语法转换倒是简单,就是不知道这些JAVA里的类库对应在C++里用什么。
#11
路过~
#12
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
namespace TG{
class suanfa
{
public:
double dis(string s,string d)
{
int x1=0;
int x2=0;
int y1=0;
int y2=0;
int i=s.find(",");
x1=atoi(s.substr(0,i).c_str());
y1=atoi(s.substr(i+1,s.length()-i-1).c_str());;
int j=d.find(",");
x2=atoi(d.substr(0,j).c_str());
y2=atoi(d.substr(i+1,d.length()-i-1).c_str());;
double l=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return l;
}
};
}
using namespace TG;
int main()
{
suanfa* ck=new suanfa();
cout<<ck->dis("45,0","50,0")<<endl;;
}
#13
以后问问题贴码得像这位仁兄,用标准缩进模式,别人也好看些.
#14
好累。
#15
太长了,不好看
#16
帮顶··
#17
楼主贴的JAVA,代码
有个类的名是小写开头的,这点不太规范
不过楼主的JAVA类名,2个都是拼音的,这是好习惯
就是说,要么全是拼音的类名,要么全是英文的,
不过推荐使用英文的
先贴一个C++的代码,因为JAVA里类里只有方法,所以没有必要写类
个人意见
有个类的名是小写开头的,这点不太规范
不过楼主的JAVA类名,2个都是拼音的,这是好习惯
就是说,要么全是拼音的类名,要么全是英文的,
不过推荐使用英文的
先贴一个C++的代码,因为JAVA里类里只有方法,所以没有必要写类
个人意见
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
double dis(string s, string d)
{
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
int i = s.find(",");
x1 = atoi(s.substr(0, i).c_str());
y1 = atoi(s.substr(i, s.length()).c_str());
cout << " i = " << i << endl;
cout << " x1 = " << x1 << endl;
cout << " y1 = " << y1 << endl;
int j = d.find(",");
x2 = atoi(d.substr(0, i).c_str());
y2 = atoi(d.substr(i + 1, d.length()).c_str());
cout << " j = " << j << endl;
cout << " x2 = " << x2 << endl;
cout << " y2 = " << y2 << endl;
double result = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return result;
}
int main(int argc, char *argv[])
{
double result = dis("45,0", "50,0");
cout << " result is : " << result << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
#18
原来JAVA代码里的Scanner就是起到输入的作用啊,JAVA的代码是写在两个类文件里的,其中suanfa类里的main函数只起到一个测试作用,没什么用吧。
我把他们合并在一起,虽然也是两个类,但是只有一个C++文件,楼主直接贴进去运行就可以了,我跟原来的JAVA运行做了对比,应该是没有什么问题。
代码里也没写什么注释,楼主有不明白的地方再问吧。
翻译的很幸苦啊,楼主给加点分啊!
我把他们合并在一起,虽然也是两个类,但是只有一个C++文件,楼主直接贴进去运行就可以了,我跟原来的JAVA运行做了对比,应该是没有什么问题。
代码里也没写什么注释,楼主有不明白的地方再问吧。
翻译的很幸苦啊,楼主给加点分啊!
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
class suanfa
{
public:
double dis(string s, string d)
{
int x1, x2, y1, y2 = 0;
int i = s.find_first_of(",");
x1 = atoi(s.substr(0, i).c_str());
s = s.replace(0, i+1, "");
y1 = atoi(s.c_str());
int j = d.find_first_of(",");
x2 = atoi(d.substr(0, j).c_str());
d = d.replace(0, j+1, "");
double l = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return l;
}
};
class Random
{
public:
string itos(int i) //int转换为string
{
stringstream s;
s<<i;
return s.str();
}
void shuru()
{
int i;
int s = 0;
int j = 0;
int t = 0;
int m = 0;
int p = 0;
int o = 0;
int r = 0;
int h = 0;
double g = 0;
string l1, l2, pow, sp, n;
cout<<"长:";
cin>>l1;
cout<<"宽:";
cin>>l2;
cout<<"能量:";
cin>>pow;
cout<<"速率:";
cin>>sp;
cout<<"节点数:";
cin>>n;
int array_num;
array_num = atoi(n.c_str()); //将n转换为int
vector<string> Array(array_num); //定义string数组
vector<int> a(array_num);
vector<int> b(array_num);
vector<int> c(array_num);
vector<int> d(array_num);
vector<int> e(array_num);
srand(time(0)); //设置随机数种子
for (i=0; i<array_num; i++)
{
a[i] = 0;
d[i] = 0;
b[i] = array_num;
int x = (int)((rand() % 100)/100.0 * atoi(l1.c_str()));
int y = (int)((rand() % 100)/100.0 * atoi(l2.c_str()));
int z = (int)((rand() % 100)/100.0 * 100);
e[i] = (int)((rand() % 100)/100.0 * 100);
Array[i] = itos(x) + "," + itos(y);
c[i] = z;
if (i == 0)
{
Array[0] = itos(x) + "," + itos(y);
}
else
{
while (p == 0)
{
for (int q=0; q<i; q++)
{
if (Array[q] == Array[i])
o++;
}
if (o >= 1)
{
p = 0;
x = (int)((rand() % 100)/100.0 * atoi(l1.c_str()));
y = (int)((rand() % 100)/100.0 * atoi(l2.c_str()));
Array[i] = itos(x) + "," + itos(y);
}
else
{
p = 1;
Array[i] = itos(x) + "," + itos(y);
}
o = 0;
}
p = 0;
}
}
for (i=0; i<array_num; i++)
{
cout<<i<<"("<<Array[i]<<"),"<<c[i]<<","<<e[i]<<endl;
}
string l3, l4;
cout<<"起始节点ID:";
cin>>l3;
cout<<"目标节点ID:";
cin>>l4;
suanfa sf;
s = atoi(l3.c_str());
int temp_l4 = atoi(l4.c_str());
while (s != temp_l4)
{
double dis = sf.dis(Array[s], Array[temp_l4]);
a[s] = 1;
for (j = 0; j<array_num; j++)
{
if (a[j] == 0 && d[j] == 0)
{
double temp = sf.dis(Array[s], Array[j]);
if (temp <= dis && temp >= 0)
{
dis = temp;
r = j;
}
}
}
if (c[r] < atoi(pow.c_str()))
d[r] = 1;
else
{
s = r;
g += dis;
b[t] = s;
t++;
a[s] = 1;
}
}
cout<<l3<<"("<<Array[atoi(l3.c_str())]<<")"<<endl;
for (m =0; m<array_num; m++)
{
if (b[m] != array_num)
{
h++;
cout<<b[m]<<"("<<Array[b[m]]<<")"<<endl;
}
}
cout<<"经过"<<h<<"跳"<<endl;
cout<<g/(double)atoi(sp.c_str())<<"秒"<<endl;
}
};
void main()
{
Random ck;
ck.shuru();
}
#19
18L大哥 太谢谢了。基本上都能看懂。我还想问下 C++里将输出的结果在一个目录下以.txt形式保存下来该怎么做
再次感谢这位大哥 稍后补上分分
再次感谢这位大哥 稍后补上分分
#20
up!
#21
我也来一个
呵
呵
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
using namespace std;
namespace TG {
double dis(string s, string d)
{
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
int i = s.find(",");
x1 = atoi(s.substr(0, i).c_str());
y1 = atoi(s.substr(i, s.length()).c_str());
cout << " i = " << i << endl;
cout << " x1 = " << x1 << endl;
cout << " y1 = " << y1 << endl;
int j = d.find(",");
x2 = atoi(d.substr(0, i).c_str());
y2 = atoi(d.substr(i + 1, d.length()).c_str());
cout << " j = " << j << endl;
cout << " x2 = " << x2 << endl;
cout << " y2 = " << y2 << endl;
double result = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return result;
}
void shuru()
{
int s = 0;
int j = 0;
int t = 0;
int m = 0;
int p = 0;
int o = 0;
int r = 0;
int h = 0;
double g = 0;
string longStr, widthStr, pow, sp, n;
cout << " 长: ";
cin >> widthStr;
cout << " 宽: ";
cin >> longStr;
cout << " 能量: ";
cin >> pow;
cout << " 速率: ";
cin >> sp;
cout << " 节点数: ";
cin >> n;
const int array_size = atoi(n.c_str());
string temp_array[array_size];
int a[array_size], b[array_size], c[array_size], d[array_size], e[array_size];
int ix = atoi(longStr.c_str());
int iy = atoi(widthStr.c_str());
for (int i = 0; i != array_size; ++i)
{
a[i] = 0;
d[i] = 0;
b[i] = array_size;
int x = rand() * ix;
int y = rand() * iy;
int z = rand() * 100;
e[i] = z;
c[i] = z;
ostringstream oss;
oss << x << "," << y;
temp_array[i] = oss.str();
while (p == 0)
{
for (int m = 0; m != i; ++m)
{
if (temp_array[m] == temp_array[i])
{
++o;
}
}
if (o < 1)
{
p = 1;
x = rand() * atoi(longStr.c_str());
y = rand() * atoi(widthStr.c_str());
oss << x << "," << y;
temp_array[i] = oss.str();
}
o = 0;
}
p = 0;
}
for (int n = 0; n != array_size; ++n) {
cout << n << "(" << temp_array[n] << ")" << "," << c[n] << "," << e[n] << endl;
}
cout << "起始节点ID:" << endl;
string l3;
cin >> l3;
cout << "目标节点ID:" << endl;
string l4;
cin >> l4;
s = atoi(l3.c_str());
while (s != atoi(l4.c_str())) {
double dis = TG::dis(temp_array[s], temp_array[atoi(l4.c_str())]);
a[s] = 1;
for (j = 0; j != atoi(n.c_str()); ++j)
{
if (a[j] == 0 && d[j] == 0) {
double temp = TG::dis(temp_array[s], temp_array[j]);
if (temp <= dis && temp >= 0) {
dis = temp;
r = j;
}
}
}
if (c[r] < atoi(pow.c_str())) {
d[r] = 1;
} else {
s = r;
g = g + dis;
b[t] = s;
t = t + 1;
a[s] = 1;
}
}
cout << l3 << "(" << temp_array[atoi(l3.c_str())] << ")" << endl;
for (m = 0; m < atoi(n.c_str()); ++m) {
if (b[m] != atoi(n.c_str())) {
h = h + 1;
cout << b[m] << "(" << temp_array[b[m]] << ")" << endl;
}
}
cout << "经过" << h << "跳" << endl;
cout << g / atof(sp.c_str()) << "秒" << endl;
}
}
int main(int argc, char *argv[])
{
TG::shuru();
double result = TG::dis("45,0", "50,0");
cout << " result is : " << result << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
#22
学习
#23
都是牛人,我学习
#24
只需要在程序的main函数里加上
freopen("filename.txt", "w", stdout);就可以了,第一个参数是要输出到的文件名,第二个是w表示只写属性,第三个表示是输出。
对应的如果要从某个文件中读入,只要加上
freopen("filename.txt", "r", stdin);参数都能看懂吧。当然读和写的文件都要放在当前工程目录的同一个文件夹下。写入的文件不用存在,程序会自动创建。
接下来一切输入输出都跟平时一样写就可以了,不需要写什么麻烦的文件操作。
#25
学习!