#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
void add(string names[], int section[], int grade[]);
void printRecords(const string names[], const int sections[], const int grades[], int size);
void insertionSort(int numbers[], int size);
void display(const int numbers[], int size);
int main(){
//command
string command;
//add
string names[20] = { "" };
int sections[20];
int grades[20];
cout << "Enter a command (add, print, drop, sort, average, quit):\n";
cin >> command;
while (command != "quit"){
if (command == "add"){
add(names,sections, grades);
}
else if (command == "print"){
printRecords(names, sections, grades, 20);
}
else if (command == "drop"){
cout << command;
}
else if (command == "sort"){
cout << command;
}
else if (command == "average"){
cout << command;
}
else if (command == "smallest"){
cout << command;
}
else{
cout << "Invalid command.\n";
}
cin >> command;
}
cout << "Quitting the program.\n";
return 0;
}
void printRecords(const string names[], const int sections[], const int grades[], int size) {
int i;
for (i = 0; i < size; i = i + 1) {
cout << "[" << i << "]:\t";
cout << setw(20) << left << names[i];
cout << setw(5) << sections[i];
cout << setw(10) << grades[i];
cout << "\n";
}
return;
}
void insertionSort(int numbers[], int size) {
int i;
int j;
int toInsert;
// outer loop i is the element we are to insert in to the sorted portion.
// [0, i-1] is the sorted range.
for (i = 1; i < size; i = i + 1) {
toInsert = numbers[i];
j = i - 1;
while (j >= 0 && numbers[j] > toInsert) {
numbers[j + 1] = numbers[j];
j = j - 1;
}
// either j >= 0 and numbers[j] <= toInsert
// or j = -1 here
// we want to store toInsert at numbers[j+1]
numbers[j + 1] = toInsert;
}
return;
}
void add(string names[], int sections[], int grades[]){
int i = 0;
while (names[i] != "" && i < 20){
i = i + 1;
}
cin >> names[i];
cin >> sections[i];
cin >> grades[i];
return;
}
我只需要用戶在調用該函數時只輸入一組新信息。 添加功能是我最關心的。爲什麼它無休止地傾瀉?爲什麼我的功能無限循環
假設在 當前電子表格的最後一條記錄後面添加一個學生的記錄(姓名,部分和等級)。您的電子表格應該能夠容納20個學生記錄。如果 已有20條學生記錄,則您的代碼應提示「電子表格已滿。可以 不可以添加。「
在此處發佈代碼。 – alex