C ++ Сортировка массива в алфавитном порядке с использованием strcpy и strcmp при получении данных из файла? - PullRequest
0 голосов
/ 05 мая 2018

У меня почти все работает, но разделяет фамилии, начинающиеся с заглавных букв, и те, которые не имеют.

Файл примера

LastName FirstName DaysofRental BalanceDue

Смит Джо 15 100.50

Доу Джон 10 95,20

Андерсон Пол 30 20,00

О'Донелл Мириам 10 24.30

Фостер Сэм 30 15,00

Зом Пит 10 20,00

Мок чилли 100 30

Смитти Крис 200 200

Сюй Конор 1 200

Анило Стив 0 0

Какой "отсортированный" файл выводится

LastName FirstName DaysofRental BalanceDue

Андерсон Пол 30 $ 20,00

Доу Джон 10 $ 95,20

Фостер Сэм 30 $ 15,00

Mock Chilly 100 $ 30,00

О'Донелл Мириам 10 $ 24.30

Смит Джо 15 $ 100,50

Зом Пит 10 $ 20,00

Анило Стив 0 $ 0,00
Смитти Крис 200 $ 200,00
Сюй Конор 1 200,00

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <fstream>
#include <string.h>  
using namespace std;
const int STRINGSIZE = 30;
const int LISTSIZE  = 10;
const int HEADSIZE = 4;

typedef char STRING30[STRINGSIZE];
typedef STRING30 NAMES[LISTSIZE];

int main(int argc, char** argv)
{
ofstream outfile;
outfile.open("sorted.txt");

int count, 
    count2,
    mindex;

int rdays[LISTSIZE],
    hrdays;

double baldue[LISTSIZE],
       totalbaldue, 
       hbaldue, 
       tempnum;

NAMES first, 
      last, 
      header;

STRING30 mname, 
         tempname;  

ifstream in;
in.open("invoice1_test1.txt");

//Input Section

if(in.is_open())
{
    in >> header[0]
       >> header[1] 
       >> header[2] 
       >> header[3]; 

    int count = 0;

    while(!in.eof())
    {
        in >> last [count] 
           >> first[count]
           >> rdays[count]
           >> baldue[count];
        count++;
    }
    in.close();     
}

else 

{
    cout << "File failed to open" << endl;
}

    for(count = 0; count < LISTSIZE; count++)
{
    mindex = count;
    strcpy(mname, last[count]);
    for(count2 = count; count2 < LISTSIZE; count2++)
    {
        if(strcmp(last[count2], mname) == -1)
        {
            mindex = count2;
            strcpy(mname, last[count2]);
        }
    }

    strcpy(tempname, last[count]);
    strcpy(last[count], mname);
    strcpy(last[mindex], tempname);

    strcpy(tempname, first[count]);
    strcpy(first[count], first[mindex]);
    strcpy(first[mindex], tempname);

    tempnum = rdays[count];
    rdays[count]= rdays[mindex];
    rdays[mindex]= tempnum;

    tempnum = baldue[count];
    baldue[count] = baldue[mindex];
    baldue[mindex] = tempnum;
}
outfile << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);

outfile << left << setw(20) << header[0] << setw(20) << header[1] << setw(20) << header[2] << setw(20) << header[3] << endl;

for(int count = 0; count<LISTSIZE; count++)
{
    outfile << left << setw(20) << last[count] << setw(20) << first[count] << setw(20) << rdays[count] << "$" << setw(20) << baldue[count] << endl;
}

Ответы [ 2 ]

0 голосов
/ 05 мая 2018

Используйте if (strcmpi(last[count2], mname) < 0); для сравнения вместо if (strcmp(last[count2], mname) ==-1);

strcmpi() работает так же, как strcmp(), но регистр не учитывается.

Также добавьте exit(1), если ifstream in не открывается.

0 голосов
/ 05 мая 2018

Преобразование всех имен в верхний регистр перед сохранением.

...