そこで、OpAmp データを小さなデータベース (10 個のデータ) に格納するプログラムを作成することができました。クイック ソートを実行し、スルー レートの昇順でソートするまでは、すべて問題ないようです。これを行うと、データベースの最後のデータが、入力されたデータとは関係のない一連の記号として返されます。これは、名前によるクイック ソートでは発生しません。何がうまくいかないのか誰にも分かりますか?これが私のコードです...
//File: Task1.cpp
//Title: Structured Programming in C++
//Created: 05/12/2012
//Author: Nicole ...
//ID Number: ...
//Accompanying Files: database.txt
//Description:A program which allows
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
// the format of each of the elements in the database
struct OpAmps {
char Name[20]; // the name of the op-amp (e.g. "741")
unsigned int PinCount; // the number of pins in the package
double SlewRate; // the slew rate in volts per microsecond
};
// the length of the fixed array to be used for database - must be at least one
// and no greater the maximum value allowed in an unsigned long (see the file limits.h)
#define DATABASE_MAX 10
// file used for the database
#define DATABASE_FILENAME "database.txt"
// function prototypes
void Enter(OpAmps&, unsigned long&);
void Save(OpAmps[], unsigned long);
void Load(OpAmps[], unsigned long&);
void Sort(OpAmps[], unsigned long);
void Display(OpAmps[], unsigned long);
void QuickSortName (OpAmps[], unsigned long);
void QuickSortSlewRate (OpAmps[], unsigned long);
// Control the entering, saving, loading, sorting and displaying of elements in the database
// Arguments: None
// Returns: 0 on completion
int main()
{
OpAmps OpAmp[DATABASE_MAX]; // the database
unsigned long database_length = 0; // the number of elements in the database
char UserInput;
// loop until the user wishes to exit
while (1) {
// show the menu of options
cout << endl;
cout << "Op-amp database menu" << endl;
cout << "--------------------" << endl;
cout << "1. Enter a new op-amp into the database" << endl;
cout << "2. Save the database to disk" << endl;
cout << "3. Load the database from disk" << endl;
cout << "4. Sort the database" << endl;
cout << "5. Display the database" << endl;
cout << "6. Exit from the program" << endl << endl;
// get the user's choice
cout << "Enter your option: ";
cin >> UserInput;
cout << endl;
// act on the user's input
switch(UserInput) {
case '1':
Enter(OpAmp[database_length], database_length);
break;
case '2':
Save(OpAmp, database_length);
break;
case '3':
Load(OpAmp, database_length);
break;
case '4':
Sort(OpAmp, database_length);
break;
case '5':
Display(OpAmp, database_length);
break;
case '6':
return 0;
default:
cout << "Invalid Entry" << endl << endl;
break;
}
}
}
void Enter(OpAmps& eOpAmp, unsigned long& database_length)
{
cout<<"1) Enter Data"<<endl;
if (database_length == DATABASE_MAX)
{
cout <<endl << "Database is full!!!" << endl;
}
else
{
cout << endl << "Name of OpAmp: ";
cin >> eOpAmp.Name;
cout << endl << "Number of Pins on OpAmp: ";
cin >> eOpAmp.PinCount;
cout << endl << "Slew Rate of OpAmp: ";
cin >> eOpAmp.SlewRate;
cout << endl<< "All Items Added!" << endl << "Now Save Your Data!" << endl;
database_length++;
}
}
void Save(OpAmps sOpAmp[], unsigned long database_length)
{
cout<<"2) Save Data"<<endl;
fstream output_file;
output_file.open(DATABASE_FILENAME, ios::out);
if (!output_file.good())
{
cout << "Error Loading File!!!" << endl;
return;
}
else
{
int i;
output_file << database_length<< endl<<endl;
for(i=0;i<=database_length-1;i++)
{
output_file << sOpAmp[i].Name << endl;
output_file << sOpAmp[i].PinCount<< endl;
output_file << sOpAmp[i].SlewRate;
}
cout << endl << "Data Saved" <<endl;
}
output_file.close();
}
void Load(OpAmps lOpAmp[], unsigned long& database_length)
{
cout<<"3) Load Data"<<endl;
fstream input_file;
input_file.open(DATABASE_FILENAME, ios::in);
if (!input_file.good())
{
cout << "Error Loading File!!!" << endl;
return;
}
else
{
input_file >> database_length;
for(int i=0;i<=database_length-1;i++)
{
input_file >> lOpAmp[i].Name;
input_file >> lOpAmp[i].PinCount;
input_file >> lOpAmp[i].SlewRate;
}
}
input_file.close();
}
void Sort(OpAmps qOpAmp[], unsigned long database_length)
{
cout<<"4) Sort Data"<<endl;
char UserInput;
// show the menu of options
cout << endl;
cout << "Sorting options" << endl;
cout << "---------------" << endl;
cout << "1. To sort by name" << endl;
cout << "2. To sort by slew rate" << endl;
cout << "3. No sorting" << endl << endl;
// get the user's choice of sorting operation required
cout << "Enter your option: ";
cin >> UserInput;
// act on the user's input
switch (UserInput) {
case '1':
cout <<"Sort By Name"<<endl;
QuickSortName (qOpAmp, database_length);
break;
case '2':
cout <<"Sort By Slew Rate"<<endl;
QuickSortSlewRate (qOpAmp, database_length);
break;
case '3':
cout <<"No Sort"<<endl;
break;
default:
cout <<"Invalid Entry"<< endl;
return;
break;
}
}
void QuickSortName (OpAmps nOpAmp[], unsigned long database_length)
{
OpAmps temp; // Local variable used to swap records
for(int i=0; i<database_length; i++)
{
for(int i=0; i<database_length; i++)
{
if(strcmp(nOpAmp[i].Name, nOpAmp[i+1].Name)>0)
{
temp = nOpAmp[i];
nOpAmp[i] = nOpAmp[i+1];
nOpAmp[i+1] = temp;
}
}
}
Display (nOpAmp, database_length);
}
void QuickSortSlewRate (OpAmps rOpAmp[], unsigned long database_length)
{
OpAmps temp; // Local variable used to swap records
for(int i=0; i<database_length; i++)
{
for(int i=0; i<database_length; i++)
{
if(rOpAmp[i].SlewRate > rOpAmp[i+1].SlewRate)
{
temp = rOpAmp[i];
rOpAmp[i] = rOpAmp[i+1];
rOpAmp[i+1] = temp;
}
}
}
Display (rOpAmp, database_length);
}
void Display(OpAmps dOpAmp[], unsigned long database_length)
{
cout<<"5) Display Data"<<endl;
if (database_length == 0)
{
cout<<endl<< "Database is Empty!!!" <<endl;
}
else
{
cout <<endl<<database_length<<" items are in the database" << endl;
for (unsigned long i = 0; i <= (database_length-1); i++)
{
cout << endl << "Database Entry Number: " << i+1<< endl;
cout << "Name: " << dOpAmp[i].Name <<endl;
cout << "PinCount: " << dOpAmp[i].PinCount<<endl;
cout << "Slew Rate: " << dOpAmp[i].SlewRate<< endl <<endl;
}
}
}
各データを入力したら、[保存] を選択してから次のデータを入力します。必要な数の項目を入力したら、データを読み込み、データを並べ替えます。この問題が表示されます。このバグを解決するには、どんな助けも素晴らしいでしょう!