新手提个问的啦~~
时间: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;
}
代码在下面。麻烦各位帮忙看看哈~~
//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
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28