1

作为初学者,我试图以单词形式输入一位数字,但将它们排序并显示为数字。

输入时:

七三五一无

输出是

0 1 3 5 7 我怎样才能阻止 0 显示和首先填充到 num 数组中?

#include <iostream>
#include <algorithm>
#include <string>
#define n 5
using namespace std;

string words[n];
int nums[n],size;
void input(){
     cout<<"Enter in word form, the numbers to be sorted\n";
     for( int i = 0;  ; i++){
         cin >> words[i];
         if( words[i] == "nil" )
             break;
     }
     size = sizeof words/sizeof(string);
}
void convert(){
     for( int i = 0; words[i]!= "nil" ; i++ ){
          if ( words[i] == "one" )
             nums[i] = 1;
          //
          else 
               cout<<"Wrong input\n";
     }
}
void sort(){
     sort(nums, nums + size);
     for ( int i = 0; i < size; i++ )
         cout<< nums[i]<<endl;
}
int main(){
    input();
    convert();
    sort();
    system("pause");
    return 0;
}

这很好用,我在单词长度上犯了错误..为了从排序的考虑中删除元素 nil (用作哨兵),我必须将大小减小 1 。这段代码工作得很好,并且按照它的预期方式工作。

 //headers and std

string words[n];
int nums[n],size;
void input(){
     cout<<"Enter in word form, the numbers to be sorted\n";
     for( int i = 0; ; i++){
         cin >> words[i];
         if ( words[i] == "nil" )
            break;
     }
     size = sizeof words/sizeof(string) - 1;//one for nil
}
void convert(){
     for( int i = 0; words[i]!= "nil" ; i++ ){
          if ( words[i] == "one" )
              nums[i] = 1;
          else if ( words[i] ==  "two") 
           //
     }
}
void sort(){
     sort(nums, nums + size);
     for ( int i = 0; i < size; i++ )
         cout<< nums[i]<<endl;
}
int main(){
    // 
}
4

2 回答 2

1

words 是一个大小为 n 的数组,这意味着 word 的有效索引是从 0 到 n - 1。您的代码使用索引 1 到 n。所以这

string words[n];
...
for( int i = 1;  ; i++){
...
for( int i = 1; i<= n; i++ ){
      if ( words[i] == "one" )
...
for ( int i = 1; i <= n; i++ )
     cout<< nums[i]<<endl;

应该是这个

string words[n];
...
for( int i = 0;  ; i++){
...
for( int i = 0; i < n; i++ ){
      if ( words[i] == "one" )
...
for ( int i = 0; i < n; i++ )
     cout<< nums[i]<<endl;

在 C++ 中,数组从零开始

于 2013-05-03T06:58:12.640 回答
0
  1. in void input(),应该没有i++out for?而且i应该从 0 开始?
  2. 中的致命错误void convert() for( int i = 1; i<= n; i++ )。由于您将 words 定义为string[5],它应该从words[0]to开始words[4]。也是如此nums[5]

更正代码:

#include <iostream>
#include <algorithm>
#include <string>
#define n 5
using namespace std;

string words[n] = {};
int nums[n];
void input(){
     cout<<"Enter in word form, the numbers to be sorted\n";
     for( int i = 0; i<n ; i++){
         cin >> words[i];
         if( words[i] == "nil" )
             break;
         //i++;
     }
}
void convert(){
     cout<<"n = "<<n<<endl;
     for( int i = 0; i<n; i++ ){
          if ( words[i] == "one" )
             nums[i] = 1;
          else if ( words[i] ==  "two")
             nums[i] = 2;
          else if ( words[i] == "three")
             nums[i] = 3;
          else if ( words[i] == "four")
             nums[i] = 4;
          else if ( words[i] == "five")
             nums[i] = 5;
          else if ( words[i] ==  "six")
             nums[i] = 6;
          else if ( words[i] ==  "seven")
             nums[i] = 7;
          else if ( words[i] == "eight")
             nums[i] = 8;
          else if ( words[i] ==  "nine")
             nums[i] = 9;
          else if ( words[i] ==  "zero")
             nums[i] = 0;
          else
               cout<<"Wrong input\n";
     }
}
void sort(){
     sort(nums, nums + n);
     for ( int i = 0; i < n; i++ )
         cout<< nums[i]<<endl;
}
int main(){
    input();
    cout <<"after input" <<endl;
    convert();
    cout <<"after convert" <<endl;
    sort();
    system("pause");
    return 0;
}
于 2013-05-03T07:12:21.470 回答