第7章 9-10题

    xiaoxiao2021-03-25  110

    第9题

    #include<iostream>using namespace std;const int maxLen = 30;struct student{ char fullName[maxLen]; char hobby[maxLen]; int oopLevel;};int getInfo(student* pa , int lenght);void display_1(student someone);void display_2(const student* someone);void display_3(const student* allStudent , int nu);int main(){ int sizeOfClass = 0; cout<<"请输入学生人数:"; cin>>sizeOfClass; student* myClass = new student[sizeOfClass]; int nus = getInfo(myClass,sizeOfClass); for(int i = 0 ; i < nus ; ++i) { cout<<"第"<<i+1<<"个学生信息."<<endl; display_1(*(myClass+i)); display_2(myClass+i); } display_3(myClass,nus);

    delete [] myClass; return 0;}int getInfo(student* pa , int lenght){ int realSize = 0; char goEnd = 'q'; cout<<"学生信息录入,输入 'q' 退出输入.\n"; for(int i = 0 ; i < lenght ; ++i) { cin.get(); cout<<"请输入第"<<i+1<<"个学生的姓名"; cin.getline((pa+i)->fullName,maxLen); if(*((pa+i)->fullName) == goEnd) //是否结束输入 break; cout<<"请输入第"<<i+1<<"个学生的爱好."; cin.getline((pa+i)->hobby,maxLen); if(*((pa+i)->hobby) == goEnd) break; cout<<"请输入第"<<i+1<<"个学生的成绩."; if(!(cin >> ((pa+i)->oopLevel))) break; //输入不为整数也被认为结束输入 if((pa+i)->oopLevel < 0) break; //输入的成绩小于0,结束输入 ++realSize; } return realSize;}void display_1(student someone){ cout<<"姓名:"<<someone.fullName<<endl; cout<<"爱好:"<<someone.hobby<<endl; cout<<"成绩:"<<someone.oopLevel<<endl;}void display_2(const student* someone){ cout<<"姓名:"<<someone->fullName<<endl; cout<<"爱好:"<<someone->hobby<<endl; cout<<"成绩:"<<someone->oopLevel<<endl;}void display_3(const student* allStudent , int nu){ for(int i = 0 ; i < nu ; ++i) { cout<<"第"<<i+1<<"名学生信息."<<endl; cout<<"姓名:"<<(allStudent+i)->fullName<<endl; cout<<"爱好:"<<(allStudent+i)->hobby<<endl; cout<<"成绩:"<<(allStudent+i)->oopLevel<<endl; }}

    第10题

    #include<iostream>double add(double x , double y); //求和double qtc(double x, double y); //求乘积double calculate(double x , double y , double (*pt)(double , double ));int main(){ using namespace std; double X = 0; double Y = 0; cout<<"请输入2个数字"; while(cin>>X and cin>>Y) { cout<<"两数之和为"<<calculate(X,Y,add)<<endl; cout<<"两数乘积为"<<calculate(X,Y,qtc)<<endl; cout<<"请输入2个数字"; } cout<<"Bye!"; return 0;}double add(double x , double y){ return x+y;}double qtc(double x , double y){ return x*y;}double calculate(double x , double y , double (*pt)(double , double )){ return (*pt)(x,y);}

    转载请注明原文地址: https://ju.6miu.com/read-15383.html

    最新回复(0)