2014年5月1日 星期四

視窗程式設計期中考

midTest1
#include <iostream>

using namespace std;

void sort(int *dd,int num){   // Descending
 int tmp;

 for(int i=0;i<num-1;i++){
  for(int j=i+1;j<num;j++){
   if(dd[i]<dd[j]){
                    tmp=dd[i];
                    dd[i]=dd[j];
                    dd[j]=tmp;
   }
  }
 }
}
int bsearch(int* dd, int low, int high, int tk){
    int idx;

    if(low > high) return -1;
    idx=(low+high)/2;
    if(tk==dd[idx]) return idx;
    else if(tk>dd[idx]) bsearch(dd,low,idx-1,tk);
    else bsearch(dd,idx+1,high,tk);
}
int main(){
    int num, rk, tk;
    int data[]={59,83,66,54,79,63,44,99,50,88,73,47,80,75,60,39,92,95,87,69,57};

 num=sizeof(data)/4;       // number of array element
 sort(data,num);
    rk=bsearch(data,0,20,63);
    if(rk < 0) cout << "score 63 not found!!!" << endl;
    else cout << "rank of 63 is " << bsearch(data,0,20,63)+1 << endl;
    rk=bsearch(data,0,20,53);
    if(rk < 0) cout << "score 53 not found!!!" << endl;
    else cout << "rank of 53 is " << bsearch(data,0,20,53)+1 << endl;
    rk=bsearch(data,0,20,92);
    if(rk < 0) cout << "score 92 not found!!!" << endl;
    else cout << "rank of 92 is " << bsearch(data,0,20,92)+1 << endl;
    }

midTest2
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;
int main(void)
{
   char txt[80],ch;
   string record;

   ifstream ifile("D:\\stdrec5.txt",ios::in);        // for input
   ofstream ofileA("D:\\reportA.txt",ios::out);       // for output1
   ofstream ofileB("D:\\reportB.txt",ios::out);       // for output2
   char porf=' ';
   string cls, id, name,sc[5],fnum;

   int isc[5],sum;
   while(!ifile.eof())
   {
       sum=0;
       fnum="";
       ifile.getline(txt,80,'\n');
       cout << txt << endl;
       record=txt;
       cls=record.substr(0,6);          // class
       id=record.substr(6,8);           // student identifier
       name=record.substr(14,10);         // student name
       for(int i=0;i<5;i++){              // get 5 course score & transfer to integer
            sc[i]=record.substr(24+i*3,3);
            isc[i]=atoi(sc[i].c_str());
            sum+=isc[i];
            if(isc[i]<60) fnum+="*";
      }
      if(cls.compare("DITM3A")==0){
            ofileA<<cls<<" "<<id<<" "<<name<<" "<<sc[0]<<" "<<sc[1]<<" "<<sc[2]<<" "<<sc[3]<<" "<<sc[4]<<"  Total="<<sum<<" Average="<<round(sum*10/5.0)/10.0<<"("<<fnum<<")"<<endl;
      }
      else{
            ofileB<<cls<<" "<<id<<" "<<name<<" "<<sc[0]<<" "<<sc[1]<<" "<<sc[2]<<" "<<sc[3]<<" "<<sc[4]<<"  Total="<<sum<<" Average="<<round(sum*10/5.0)/10.0<<"("<<fnum<<")"<<endl;
      }
   }
   ifile.close();
   ofileA.close();
   ofileB.close();

   return 0;
}

midTest3
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

class person{
private:
    double pound,inch,kg,m;
public:
    person(double pd, double in){
        pound=pd;
        inch=in;
        toKg();
        toM();
    }
private:
    void toKg(){
        kg=pound*0.454;
    }
    void toM(){
        m=inch*0.0254;
    }
public:
    double getKg(){
        return kg;
    }
    double getM(){
        return m;
    }
};

class BMI:person{

    public:
    BMI(double pd, double in):person(pd, in){};
    public:
        short ckBMI(){
            double bmi=getBMI();
            if(bmi<18.5) return -1;
            else if(bmi<24.0) return 0;
            else if(bmi<27.0) return 1;
            else if(bmi<30.0) return 2;
            else if(bmi<35.0) return 3;
            else return 4;
        }
        double getBMI(){
            double kg,m, bmi;
            kg=person::getKg();
            m=person::getM();
            return bmi=kg/(m*m);
        }
        char* toMessage(short f){
            switch(f){
                case -1:
                    return "體重過輕";
                    break;
                case 0:
                    return "正常";
                    break;
                case 1:
                    return "過重";
                    break;
                case 2:
                    return "輕度肥胖";
                    break;
                case 3:
                    return "中度肥胖";
                    break;
                case 4:
                    return "重度肥胖";
            }
        }
};


int main(void) {
        BMI John(200, 71);
        BMI Mary(170, 69);
        BMI Ronny(192, 65);

        cout << "John BMI is " << John.getBMI()<< " Status:" << John.toMessage(John.ckBMI())<< endl;
        cout << "Mary BMI is " << Mary.getBMI()<< " Status:" << Mary.toMessage(Mary.ckBMI())<< endl;
        cout << "Ronny BMI is " << Ronny.getBMI()<< " Status:" << Ronny.toMessage(Ronny.ckBMI())<< endl;
    }

沒有留言:

張貼留言