C++ 在从文件中获取数据时使用 strcpy 和 strcmp 按字母顺序对数组进行排序?
C++ Sorting an Array in Alphabetical order using strcpy and strcmp while taking data from a file?
我几乎可以正常工作了,但是,它将以大写字母开头的姓氏与不以大写字母开头的姓氏分开。
示例文件
LastName FirstName DaysofRental BalanceDue
史密斯·乔 15 岁 100.50
约翰 10 岁 95.20
安德森保罗 30 20.00
O'Donell Miriam 10 24.30
福斯特山姆 30 岁 15.00
Zom Pete 10 20.00
模拟寒冷 100 30
斯密蒂克里斯 200 200
徐康纳 1 200
阿尼洛史蒂夫 0 0
"Sorted" 文件正在输出什么
LastName FirstName DaysofRental BalanceDue
安德森·保罗 30 岁 $20.00
约翰 10 美元 95.20 美元
福斯特山姆 30 岁 $15.00
模拟寒冷 100 $30.00
O'Donell Miriam 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;
}
存储前将所有名称转换为大写。
使用 if (strcmpi(last[count2], mname) < 0);
进行比较,而不是 if (strcmp(last[count2], mname) ==-1);
strcmpi()
功能与 strcmp()
相同,但不区分大小写。
如果ifstream in
打开失败,还要添加exit(1)
。
我几乎可以正常工作了,但是,它将以大写字母开头的姓氏与不以大写字母开头的姓氏分开。
示例文件
LastName FirstName DaysofRental BalanceDue
史密斯·乔 15 岁 100.50
约翰 10 岁 95.20
安德森保罗 30 20.00
O'Donell Miriam 10 24.30
福斯特山姆 30 岁 15.00
Zom Pete 10 20.00
模拟寒冷 100 30
斯密蒂克里斯 200 200
徐康纳 1 200
阿尼洛史蒂夫 0 0
"Sorted" 文件正在输出什么
LastName FirstName DaysofRental BalanceDue
安德森·保罗 30 岁 $20.00
约翰 10 美元 95.20 美元
福斯特山姆 30 岁 $15.00
模拟寒冷 100 $30.00
O'Donell Miriam 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;
}
存储前将所有名称转换为大写。
使用 if (strcmpi(last[count2], mname) < 0);
进行比较,而不是 if (strcmp(last[count2], mname) ==-1);
strcmpi()
功能与 strcmp()
相同,但不区分大小写。
如果ifstream in
打开失败,还要添加exit(1)
。