問題描述:
學(xué)校正在做畢設(shè)項(xiàng)目,每名學(xué)生帶領(lǐng)五名學(xué)生,總共有三位老師。需求如下:設(shè)計(jì)學(xué)生和老師的結(jié)構(gòu)體,其中在老師的結(jié)構(gòu)體中,有老師的姓名和一個(gè)存放學(xué)生的數(shù)組作為成員有性命,考試分?jǐn)?shù),創(chuàng)建數(shù)組存放三名老師,通過函數(shù)給每個(gè)老師及所帶的學(xué)生賦值,最終打印出老師數(shù)據(jù)以及老師所帶的學(xué)生數(shù)據(jù)。
分析問題:
teacher1->>student1,student2,student3,student4,student5
teacher2->>student1,student2,student3,student4,student5
teacher3->>student1,student2,student3,student4,student5
實(shí)例代碼:
#include#include#includeusing namespace std;
struct student
{string name;
int score;
};
struct teacher {string name;
struct student sarray[5];
};
void allocatepace(struct teacher tarray[],int len) {string nameseed = "ABCDE";
for (int i = 0; i< 3; i++) {tarray[i].name = "teacher_";
tarray[i].name += nameseed[i];
for (int j = 0; j< 5; j++) { tarray[i].sarray[j].name = "student_";
tarray[i].sarray[j].name += nameseed[j];
int random = rand() % 40 + 60;
tarray[i].sarray[j].score = random;
}
}
}
void printfout(struct teacher tarray[],int len ) {for (int i = 0; i< 3; i++) {cout<< "老師姓名:"<< tarray[i].name<< endl;
for (int j = 0; j< 5; j++) { cout<< "\t學(xué)生姓名:"<< tarray[i].sarray[j].name<< "考試分?jǐn)?shù):"<< tarray[i].sarray[j].score<< endl;
}
}
}
int main() {struct teacher tarray[3];
//設(shè)立時(shí)間戳,實(shí)現(xiàn)真正的隨機(jī)數(shù)生成
srand((unsigned int)time(NULL));
int len = sizeof(tarray) / sizeof(tarray[0]);
allocatepace(tarray,len);
printfout(tarray, len);
system("pause");
return 0;
}
輸出結(jié)果:
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧