問題描述:
學(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ù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
當(dāng)前名稱:C++考試結(jié)構(gòu)體案例試題-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://muchs.cn/article38/csgisp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、域名注冊(cè)、關(guān)鍵詞優(yōu)化、App開發(fā)、網(wǎng)站維護(hù)、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容