第一種方式為老師說的陣列+結構體(課本上),我用的是c++,其實與c沒什麼不同(區別:cin是scanf,cout是print,new是malloc()函式),我用的全域性變數,所以不用傳參。
程式碼1:
點選檢視程式碼
#include<iostream>
#include<cstring>
using namespace std;
const int N = 1e4 + 5;
struct ss {
char name[10], str[15];
double test;
};
struct kk {
ss data[N];
int length;
}tests;
void scanfs(int);
void print();
void find1(char x[]);
void find2(int);
void cha(ss, int);
void shan(int);
int main() {
int n, h;
cout << "請輸入初始成績表人數:\n";
cin >> n;
scanfs(n);
while (true) {
cout << "請選擇您的操作:\n";
cout << "1:輸出全部成績表\n";
cout << "2:按姓名查詢\n";
cout << "3:按指定位置查詢\n";
cout << "4:在指定位置插入成績\n";
cout << "5:刪除指定位置資訊\n";
cout << "6:成績表中的人數\n";
cout << "7:退出:\n";
cin >> h;
if (h == 7) break;
if (h > 7 || h < 1) cout << "輸入錯誤!\n";
switch (h) {
case 1:print(); break;
case 2:char x[10];
cout << "請輸入您要查詢的姓名:\n";
cin >> x;
find1(x); break;
case 3:int k;
cout << "請輸入您要查詢的位置:\n";
cin >> k;
find2(k); break;
case 4:ss x1; int k1;
cout << "請按學號、姓名、成績、位置的順序輸入;\n";
cout << "學號:\n"; cin >> x1.str;
cout << "姓名:\n"; cin >> x1.name;
cout << "成績:\n"; cin >> x1.test;
cout << "插入位置\n"; cin >> k1;
cha(x1, k1); break;
case 5:int k2;
cout << "請輸入您要刪除的位置:\n";
cin >> k2;
shan(k2); break;
case 6:cout << "人數為:" << tests.length << endl;
}
cout << "----------輸入任意鍵返回----------\n";
getchar(); getchar();
}
return 0;
}
void scanfs(int n) {
cout << "請按學號、姓名、成績的順序輸入;\n";
for (int i = 1; i <= n; i++) {
printf("-----學生%d-----\n", i);
cout << "學號:\n"; cin >> tests.data[i].str;
cout << "姓名:\n"; cin >> tests.data[i].name;
cout << "成績:\n"; cin >> tests.data[i].test;
}
tests.length = n;
}
void print() {
if (tests.length == 0) {
cout << "暫為空表\n";
return;
}
printf("%15s%10s%8s\n", "學號", "姓名", "成績");
for (int i = 1; i <= tests.length; i++)
printf("%15s%10s%8.2lf\n", tests.data[i].str, tests.data[i].name, tests.data[i].test);
}
void find1(char x[]) {
for (int i = 1; i <= tests.length; i++)
if (strcmp(x, tests.data[i].name) == 0) {
printf("%15s%10s%8s\n", "學號", "姓名", "成績");
printf("%15s%10s%8.2lf\n", tests.data[i].str, tests.data[i].name, tests.data[i].test);
return;
}
printf("查無此人!\n");
}
void find2(int k) {
if (k > 0 && k <= tests.length) {
printf("%15s%10s%8s\n", "學號", "姓名", "成績");
printf("%15s%10s%8.2lf\n", tests.data[k].str, tests.data[k].name, tests.data[k].test);
return;
}
printf("查無此人!\n");
}
void cha(ss x, int k) {
if (tests.length == N) {
printf("記憶體已滿!\n");
return;
}
if (k > tests.length + 1) {
printf("插入不在成績表中,請重新選擇插入位置\n");
return;
}
tests.length++;
for (int i = tests.length; i > k; i--)
tests.data[i] = tests.data[i - 1];
tests.data[k] = x;
}
void shan(int k) {
if (k<1 || k>tests.length) {
cout << "刪除位置不在成績表內\n";
return;
}
for (int i = k; i < tests.length; i++)
tests.data[i] = tests.data[i + 1];
tests.length--;
}
第二種方式是連結串列,是上一種稍微做了改變(報告是兩種方式都要哦!)
程式碼2:
點選檢視程式碼
#include<iostream>
#include<cstring>
using namespace std;
struct ss {
char name[10], str[15];
double test;
ss* next;
};
ss* head;
int num;
void scanfs(int);
void print();
void find1(char x[]);
void find2(int);
void cha(ss, int);
void shan(int);
int main() {
int n, h;
cout << "請輸入初始成績表人數:\n";
cin >> n;
scanfs(n);
while (true) {
cout << "請選擇您的操作:\n";
cout << "1:輸出全部成績表\n";
cout << "2:按姓名查詢\n";
cout << "3:按指定位置查詢\n";
cout << "4:在指定位置插入成績\n";
cout << "5:刪除指定位置資訊\n";
cout << "6:成績表中的人數\n";
cout << "7:退出:\n";
cin >> h;
if (h == 7) break;
if (h > 7 || h < 1) cout << "輸入錯誤!\n";
switch (h) {
case 1:print(); break;
case 2:char x[10];
cout << "請輸入您要查詢的姓名:\n";
cin >> x;
find1(x); break;
case 3:int k;
cout << "請輸入您要查詢的位置:\n";
cin >> k;
find2(k); break;
case 4:ss x1; int k1;
cout << "請按學號、姓名、成績、位置的順序輸入;\n";
cout << "學號:\n"; cin >> x1.str;
cout << "姓名:\n"; cin >> x1.name;
cout << "成績:\n"; cin >> x1.test;
cout << "插入位置\n"; cin >> k1;
cha(x1, k1); break;
case 5:int k2;
cout << "請輸入您要刪除的位置:\n";
cin >> k2;
shan(k2); break;
case 6:cout << "人數為:" << num<< endl;
}
cout << "----------輸入任意鍵返回----------\n";
getchar(); getchar();
}
return 0;
}
void scanfs(int n) {
ss *te,*p;
head = new ss;
p = new ss;
head->next = nullptr;
cout << "請按學號、姓名、成績的順序輸入;\n";
for (int i = 1; i <= n; i++) {
te = new ss;
printf("-----學生%d-----\n", i);
cout << "學號:\n"; cin >> te->str;
cout << "姓名:\n"; cin >> te->name;
cout << "成績:\n"; cin >> te->test;
te->next = nullptr;
if (i == 1) head->next = te;
else p->next = te;
p = te;
}
num = n;
}
void print() {
if (head->next == nullptr) {
cout << "暫為空表\n";
return;
}
ss* te=head->next;
printf("%15s%10s%8s\n", "學號", "姓名", "成績");
while (te != nullptr) {
printf("%15s%10s%8.2lf\n", te->str, te->name, te->test);
te = te->next;
}
}
void find1(char x[]) {
ss* te = head->next;
while (te != nullptr) {
if (strcmp(x, te->name) == 0) {
printf("%15s%10s%8s\n", "學號", "姓名", "成績");
printf("%15s%10s%8.2lf\n", te->str, te->name, te->test);
return;
}
te = te->next;
}
printf("查無此人!\n");
}
void find2(int k) {
ss* te = head->next;
int p = 0;
while (te!= nullptr && p < k-1) {
te = te->next;
p++;
}
if (k > 0 && k <= num) {
printf("%15s%10s%8s\n", "學號", "姓名", "成績");
printf("%15s%10s%8.2lf\n", te->str, te->name, te->test);
return;
}
printf("查無此人!\n");
}
void cha(ss x, int k) {
ss* te,*p=head;
int p1 = 0;
if (k > num + 1) {
printf("插入不在成績表中,請重新選擇插入位置\n");
return;
}
num++;
te = &x;
while (p->next != nullptr && p1++ < k-1)
p = p->next;
te->next = p->next;
p->next = te;
}
void shan(int k) {
if (k<1 || k>num) {
cout << "刪除位置不在成績表內\n";
return;
}
ss* p = head,*te;
int p1 = 0;
while (p->next != nullptr && p1++ < k-1)
p = p->next;
te = p->next;
p->next = te->next;
delete te;
num--;
}