我的程序工作得非常好,除了我希望它显示进行冒泡排序和选择所花费的时间,我确实在程序中有代码,但它总是显示0.请帮助。
#include <iostream>
#include <iomanip>
#include <vector>
#include <stdlib.h>
using namespace std;
struct student { //structure student
unsigned int StuNo;
string LastName;
string FirstName;
double Gpa;
unsigned int Age;
string Major;
string College;
};
void clearCIN(void);
int inputInt(string, int, int);
double inputDouble(string, double, double);
string inputString(string, int, int);
// Note: changed "int" to "unsigned int", changed int to size_t
bool findstunum( unsigned int, vector<student>);
void initArrays (int[], double []);
char displayMenu(void);
void addstudent(vector<student> &);
void display(vector<student>);
void bubblesort(vector<student> &);
void selectionsort(vector<student> &);
void addrandomstudents(vector<student> &);
long double startTime;
long double endTime;
int main(int argc, const char * argv[]) {
vector<student> myStudents; //Vector Student
srandom((unsigned int)time(NULL));
char userChoice;
bool keepdisplaying =true;
student tempstudent;
cout << "Welcome to our student management system\n========================================\n";
while (keepdisplaying) { //displaying menu
userChoice = displayMenu();
switch (toupper(userChoice)) {
case 'A': //If user inputs 'A'
addstudent(myStudents);
break;
//else
case 'B': //If user inputs 'B'
bubblesort(myStudents);
break;
case 'S': //If user inputs 'S'
selectionsort(myStudents);
break;
case 'L': //If user inputs 'l'
display (myStudents);
break;
case 'X': keepdisplaying = false; //if user wants to break.
break;
case 'R':
addrandomstudents(myStudents);
break;
}
}
cout << endl << "Thank You for using our student management program\n";
cout << endl << "Have a great day!\n";
//
//End the program with an exit message
//
return 0;
} //main
//
//Clear the CIN input buffer to avoid problems
//
void clearCIN(void) {
cin.clear();
cin.ignore(32768, '\n');
return;
} //clearCIN
//Input a integer value to be returned to the calling function. Also, validate the
//the minimum and maximum value of the integer input.
//
int inputInt(string promptString, int minValue, int maxValue) {
int userInput;
cout << promptString << " \n";
while (true) {
cin >> userInput;
if (!cin.fail()) {
char myChar = cin.peek();
if (myChar == '\n')
if (userInput >= minValue && userInput <= maxValue)
break;
else {
cout << "Invalid value entered. Should be between " << minValue << " and " << maxValue << " Please try again\n";
clearCIN();
}//else
else {
cout << "Invalid value entered. Should be between " << minValue << " and " << maxValue << " Please try again\n";
clearCIN();
}//else
}//if
else {
cout << "Invalid integer value entered. Please try again.\n";
clearCIN();
}//else
}
clearCIN();
return userInput;
}//inputInt
//
//Input a double value to be returned to the calling function. Also, validate the
//the minimum and maximum value of the double input.
//
double inputDouble(string promptString, double minValue, double maxValue) {
double userInput;
cout << promptString << " \n";
while (true) {
cin >> userInput;
if (!cin.fail()) {
char myChar = cin.peek();
if (myChar == '\n')
if (userInput >= minValue && userInput <= maxValue)
break;
else {
cout << "Invalid value entered. Should be between " << minValue << " and " << maxValue << " Please try again\n";
clearCIN();
}//else
else {
cout << "Invalid value entered. Should be between " << minValue << " and " << maxValue << " Please try again\n";
clearCIN();
}//else
}//if
else {
cout << "Invalid double value entered. Please try again.\n";
clearCIN();
}//else
}
clearCIN();
return userInput;
}//inputdouble
//
//Input a string to be returned to the calling function.
//
string inputString(string promptString, int minLength, int maxLength) {
string userInput;
while (true) {
cout << promptString << "\n";
getline(cin, userInput);
if (userInput.size() < minLength || userInput.size() > maxLength) {
cout << "The string you entered is too short or too long. Should be " << minLength << " to " << maxLength << " in Length\n";
cout << "Please try again\n";
}//if
else
break;
}//while
return userInput;
}//inputString
bool findstunum(unsigned int toFind, vector<student> numArray) {
for (size_t y=0; y<numArray.size(); y++) {
if (numArray[y].StuNo == toFind)
return true;
else {}
}//for
return false;
}//findstudentnum
//
//Input a char for the menu
//
char displayMenu(void) {
char userSelection = ' '; //The menu selection given by the user
bool keepAsking = true; //While this is true, the while loop will continue to operate.
while (keepAsking) {
cout << "Main Menu\n";
cout << "---------\n";
cout << "<A>dd a student\n";
cout << "<L>ist students\n";
cout << "<R>andomly generate 1000\n";
cout << "<B>ubble sort by ID\n";
cout << "<S>election sort by ID\n";
cout << "e<X>it program\n";
cin >> userSelection;
switch (toupper(userSelection)) {
case 'L':
case 'l':
case 'A':
case 'a':
case 'X':
case 'x':
case 'S':
case 's':
case 'B':
case 'b':
case 'R':
case 'r':
keepAsking = false; //Since the user has entered one of these, then we don't
break; //need to keep asking.
default:
cout << "Invalid Selection, please try again\n\n"; //Not sure this would ever happen...
}//switch
}//while
clearCIN(); //Always clear the CIN when a cin >> is done, just in case a getline will be executed.
return userSelection;
}//displaymenu
void addrandomstudents(vector<student> &students) {
for(int i = 0; i < 1000; i++) {
student tempstudent;
tempstudent.Gpa = (double)(random() % 100) / 25.0;
bool duplicateFound = false;
do {
tempstudent.StuNo = random() % 1000;
duplicateFound = false;
for(int j = 0; j < students.size(); j++) {
if(students[j].StuNo == tempstudent.StuNo) {
duplicateFound = true;
break;
}
}
} while(duplicateFound);
tempstudent.FirstName = "joe"; //inputString("Enter the student's First name:", 1, 20);
tempstudent.LastName = "Smith";// inputString("Enter the student's Last name:", 1, 20);
tempstudent.Age = 18; // inputInt("Please Enter students age", 1, 100);
tempstudent.Major = "Computer Science"; // inputString("Please Enter students major", 1, 1000);
tempstudent.College = "UCSD"; // inputString("Please Enter students College", 1, 40);
students.push_back(tempstudent);
}
cout << "Thousand Students generated" << endl;
}
void addstudent ( vector<student> &students){
cout << "New Student" << endl;
student tempstudent;
bool keepGoing = true;
while (keepGoing) {
tempstudent.StuNo = inputInt("Please enter student ID:", 1, 1000);
// keepGoing = false; //out of while
if (findstunum(tempstudent.StuNo, students))
cout << "Student number already exists, please try again" << endl;
else {
keepGoing = false;
}//else
}//while
tempstudent.Gpa = inputDouble("Please enter the student's gpa", 0, 4);
tempstudent.FirstName = inputString("Enter the student's First name:", 1, 20);
tempstudent.LastName = inputString("Enter the student's Last name:", 1, 20);
tempstudent.Age = inputInt("Please Enter students age", 4, 80);
tempstudent.Major = inputString("Please Enter students major", 1, 1000);
tempstudent.College = inputString("Please Enter students College", 1, 40);
students.push_back(tempstudent);
}
void display ( vector<student> students){
cout << endl << endl;
cout << "student Listing" << endl << endl;
cout << setw(10) << "Student ID" << setw(20) << "Student Name" << setw(16) << "Student GPA" << setw(15) << "Student Age" << setw(18) << "Student Major" << setw(25) << "Student College" << endl;
cout << setw(10) << "==========" << setw(20) << "============" << setw(16) << "===========" << setw(15) << "===========" << setw(18) << "=============" <<setw (25) << "===============" << endl;
for (int y=0; y< students.size(); y++) {
cout << fixed << setprecision(2) << setw(10) << students[y].StuNo << setw(20) << students[y].LastName + " " + students[y].FirstName << setw(16) << students[y].Gpa << setw(15) <<students[y].Age << setw(18) << students[y].Major << setw(25) << students[y].College << endl;
}
}
//
//
//BUBBLE SORT
//
//
void bubblesort (vector<student> &students){
student tempStudent;
bool changed = true;
clock_t startingTime = clock(); //Timer begins
if (students.size() == 0){
cout <<"Error, No students to sort!\n";
return; }
while (changed) {
changed = false;
for (int i=0; i<students.size()-1; i++) {
if (students[i].StuNo >
students[i+1].StuNo) {
tempStudent = students[i];
students[i] = students[i+1];
students[i+1] = tempStudent;
changed = true;
} //if
else { }
}//for
}
cout <<"Done Sorting!\n";
clock_t endTime = clock(); //Timer Ends
cout << "It took ";
cout << (endTime - startingTime)/CLOCKS_PER_SEC;
cout << " seconds"<< endl;
}
//
//
//SELECTION SORT
//
//
void selectionsort(vector<student> &students)
{ student tempstudent;
int startScan, minIndex, minValue;
startScan = 0;
clock_t startingTime = clock(); //Timer for selection Sort begins
for(startScan = 0; startScan < students.size() - 1; startScan++)
{
minIndex = startScan;
minValue = students[startScan].StuNo;
for(int index = startScan + 1; index < students.size(); index++)
{
if (students[index].StuNo < minValue)
{
minValue = students[index].StuNo;
minIndex = index;
}
}
tempstudent = students[minIndex];
students[minIndex] = students[startScan];
students[startScan] = tempstudent;
}
cout <<"Done Sorting!\n";
clock_t endTime = clock();
cout << "It took ";
cout << (endTime - startingTime) / CLOCKS_PER_SEC ; //Timer Ends
cout << " seconds"<< endl;
}
我有上面的计时器代码,它总是显示0,即使我更改程序显示10000学生而不是1000.此外它实际上不需要显示秒,它可以只显示刻度,或某种东西或那种。
感谢
答案 0 :(得分:0)
如果您使用的是C ++ 11,请使用:
#include <chrono>
#include <iostream>
int main ()
{
using namespace std::chrono;
time_t start = system_clock::to_time_t(system_clock::now());
for (auto i = 0; i < 100000000; ++i)
;
time_t difference = system_clock::to_time_t(system_clock::now()) - start;
std::cout << "Difference in seconds: " << difference;
return 0;
}
&#13;
对于纳秒,使用相同的system_clock :: now()或其他时钟(如果显示)和std :: chrono :: nanosecods类型。时间类型之间的转换使用std :: chrono :: duration_cast。
答案 1 :(得分:0)
尝试这样写:
cout << (endTime - startingTime) / (double)CLOCKS_PER_SEC ;
我认为只需不到1秒就可以endTime - startingTime = 500并将其除以1000结果为0.5但是因为它被转换为int结果为0.(double)阻止转换并显示0.5或类似的东西。