字符串数组转换为数字并排序

时间:2013-05-03 06:53:48

标签: c++ debugging

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

输入时:

7 三 五 一 零

输出

0 1 3 五 7 如何在显示和停止在num数组中首先停止0?

#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(){
    // 
}

2 个答案:

答案 0 :(得分:1)

words是一个大小为n的数组,这意味着单词的有效索引从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 ++数组中,从开始。

答案 1 :(得分:0)

  1. void input()中,i++中应该没有fori应该从0开始?
  2. void convert() for( int i = 1; i<= n; i++ )中的致命错误。由于您将字词定义为string[5],因此应从words[0]开始到words[4]nums[5]
  3. 也是

    更正码:

    #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;
    }