+ -
当前位置:首页 → 问答吧 → 新手提个问的啦~~

新手提个问的啦~~

时间:2011-12-11

来源:互联网

才刚开始学习C++,不会指针数组,就用循环结构写了一个给输入的n个数排序的程序,可是为什么运行会出现奇怪的错误呢?
代码在下面。麻烦各位帮忙看看哈~~

//rank all the inputs
//by ljw

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

string Value (int i, string Input) {
string s;
if (i == 1) {
int c = 0;
c = Input.find (" ", 0);
s = Input.substr(0, c);
return s;
}
else {
int a = 0;
for (int j=1; j<=i; ++j) {
a = Input.find (" ", a+1);
}
int b = 0;
for (int k=1; k<=i-1; ++k) {
b = Input.find (" ", b+1);
}
s = Input.substr(b+1, b-a-1);
return s;
}
}

int main()
{
//step1: compute the amount of all numbers
//and extract all the input by string
cout << "Please enter the numbers: ";
string Input;
string Num;
int counts = 0;
cin >> Num;
Input = Num;
while (cin >> Num) {
Num = " " + Num;
Input += Num;
counts ++;
}

//step2: extract each number and compare
//this number with other numbers
string CurrentNum;
string PastNum = 0;
int Times = 1;
string Output;
int Position = 1;
while (Times <= counts) {
for (int i = 1; i <= counts; ++i) {
CurrentNum = Value(i, Input);
for (int m = 1; m <= counts; ++m) {
PastNum = Value(m, Input);
int Current = atoi(CurrentNum.c_str());
int Past = atoi(PastNum.c_str());
if ( Current > Past ) {
Position++;
}
}
if (Position == Times) {
CurrentNum += " ";
Output += CurrentNum;
}
}

Times++;
}

//step3: output the order string

cout << Output;

return 0;
}

作者: freshman_in_program   发布时间: 2011-12-11

错误是什么?

作者: xulai1001   发布时间: 2011-12-11