C.Primer.Plus(第六版)第六章编程练习

来源:互联网 发布:sai mac版打不开 编辑:程序博客网 时间:2024/06/10 19:56

自己写的,仅供参考。版本:Windows 10 + vc 6.0.

#include<iostream>#include<string>#include<fstream>#include<cstdlib>#include<cctype>using namespace std;/* //6.3中的函数void program_show_6_3(void){    cout<<"Please enter one of the fllowing choices:"<<endl;    cout<<"c) carnivore        p) pianist\n";    cout<<"t) tree             g) game\n";}*//* // 6.4中的函数void program_show_6_4(void){    cout<<"Benevolent Order of Programers Report\n";    cout<<"a.display by name        b.display by title\n";    cout<<"c.display by bopname     d.diaplay by preference\n";    cout<<"q.quit\n";}*/int main(){   /*  //6.1 显示输入并进行大小写转换。可使用cctype中的方法。    char ch;//  cout<<int('j')<<int('J')<<endl;//用于测试大小写ascii码值之差    while(cin.get(ch)&&ch!='@')    {        if (ch>='a'&&ch<='z')        {        //  ch-=32;            ch=toupper(ch);        }           else if(ch>='A'&&ch<='Z')        {        //  ch+=32;            ch=tolower(ch);        }        cout<<ch;    }*/  /* //6.2 输入数字到数组中并进行一些简单的判断。    const int SIZE = 10;    double  donation[SIZE],average=0;    int i=0,than_average=0;    int cin_leave=0;    cout<<"Please enter a number:";    while(i<SIZE&&cin>>donation[i])//上面的情况为正确的逻辑。先判断i值是否满足条件,再判断是否正确输入。当i为是10时,前者为假不会判断cin的情况。//  while(cin>>donation[i]&&i<SIZE)//下面的逻辑在于先判断是否有输入,但i为10时,输入数字循环判断cin>>donation为真,而i却无效,但已经输入到流中了,会导致逻辑出现混乱。    {        average+=donation[i];        i++;        if (i<=9)            cout<<"Please next number "<<i<<":";    }    if(i<=9)        cout<<"Enter a wrong type data,exit";    if(i==10)        cout<<"the array is full,end";    average=average/(i+1);    for(int j=0;j<=i;j++)    {        if(donation[j]>average)            than_average++;    }    cout<<"the array average is "<<average<<" and the array than average number have "<<than_average<<endl;//  cin>>cin_leave;  //用于判断错误逻辑下,最后输入的数据是否还在流中,结果并不在。//  cout<<cin_leave;*//*  //6.3 使用有效的字符和switch语句显示功能。    program_show_6_3();    char ch;    do    {           cin>>ch;        switch(ch)        {        case 'c':cout<<"There have a new car\n";            break;        case 'p':cout<<"Pianist\n";            break;        case 't':cout<<"A maple is a tree\n";            break;        case 'g':cout<<"I like play game\n";            break;        default:cout<<"Please enter a c,p,t,or g:";        }    }while(ch!='c'&&ch!='p'&&ch!='t'&&ch!='g');//当不满足输入条件时一直循环,直到输入一个有效的case语句。*/  /*  //6.4 使用动态数组存储内容,然后使用switch判断条件。    const int strsize=20;    struct bop    {        char fullname[strsize];        char title[strsize];        char bopname[strsize];        int preference;//0=fullname,1=title,2=bopname;    };    program_show_6_4();    int people_number=0;    cout<<"Please enter the dynamic array length:";    cin>>people_number;    bop* ps_bop = new bop[people_number];    for(int i=0;i<people_number;i++)    {           cout<<"Please enter the struct member "<<i+1<<" (total "<<people_number<<")\n";        cout<<"Please enter the man/women fullname:";        cin.get();// very important 用于消除循环外输入数组长度的回车符和循环最后面的输入偏好的回车符。        cin.getline((*(ps_bop+i)).fullname,20);        cout<<"Please enter the man/women title:";        cin.getline((*(ps_bop+i)).title,20);        cout<<"Please enter the man/women bopname:";        cin.getline((*(ps_bop+i)).bopname,20);        cout<<"Please enter the man/women preference:";        cin>>(*(ps_bop+i)).preference;      }    char key_switch;//定义类型最开始出错为int型,因为类型不匹配循环跳转到default语句,然后再次进入循环,由于输入流中仍然为错误的类型,又跳转default,以此循环。    do    {        cout<<"Enter your chioce:";        cin>>key_switch;        switch(key_switch)        {        case 'A':        case 'a':            {                for(int j=0;j<people_number;j++)                    cout<<(*(ps_bop+j)).fullname<<endl;                break;            }        case 'B':        case 'b':            {                for(int j=0;j<people_number;j++)                    cout<<(*(ps_bop+j)).title<<endl;                break;            }        case 'C':        case 'c':            {                for(int j=0;j<people_number;j++)                    cout<<(*(ps_bop+j)).bopname<<endl;                break;            }        case 'D':        case 'd':            {                for(int j=0;j<people_number;j++)                {                       if (0==(*(ps_bop+j)).preference)                        cout<<(*(ps_bop+j)).fullname<<endl;                    else if (1==(*(ps_bop+j)).preference)                        cout<<(*(ps_bop+j)).title<<endl;                    else                        cout<<(*(ps_bop+j)).bopname<<endl;                }                break;            }        case 'q':        case 'Q':            cout<<"Bye!";            break;        default:            cout<<"Please enter a matching type date.";            break;        }    }while(key_switch!='q'&&key_switch!='Q');    delete [] ps_bop;//又忘记释放内存了。*/  /*    //6.5 计算公民的税收    double salary=0;    cout<<"Please enter yor salary:";    while(cin>>salary&&salary>=0)    {        if(salary<=5000)            cout<<"your revenue is zero.\n";        else if(salary>5000&&salary<=15000)            cout<<"your revenue is "<<(salary-5000)*0.10<<endl;        else if(salary>15000&&salary<=35000)            cout<<"your revenue is "<<10000*0.10+(salary-15000)*0.15<<endl;        else             cout<<"your revenue is "<<10000*0.10+20000*0.15+(salary-35000)*0.20<<endl;        cout<<"Please enter yor salary:";    }    cout<<"Please dont enter minus or wrong type data\n";*//*  //使用动态数组存放数据,并根据捐款金额决定输出顺序。    struct donation    {        char name[20];        double amount;    };    int donation_number;    int Grand_Patron=0,Grand=0;//用于判断该类型是否有捐款者。    cout<<"Please enter the donation amount:";    cin>>donation_number;    donation* ps_do = new donation[donation_number];    for (int i=0;i<donation_number;i++)    {        cin.get();        cout<<"Please enter the donation name:";        cin.getline((*(ps_do+i)).name,20);        cout<<"Please enter the donation amount:";        cin>>(*(ps_do+i)).amount;        if((*(ps_do+i)).amount>10000)            Grand_Patron++;        else            Grand++;    }    if(Grand_Patron==0)        cout<<"The table list the Grand Patrons:\nnone\n";    else    {           cout<<"The table list the Grand Patrons:\n";        for (int j=0;j<donation_number;j++)        {            if((*(ps_do+j)).amount>10000)                cout<<(*(ps_do+j)).name<<"\t"<<(*(ps_do+j)).amount<<endl;           }    }    if(Grand==0)        cout<<"The table list the Patrons:\nnone";    else    {        cout<<"The table list the Patrons:\n";        for (int k=0;k<donation_number;k++)        {            if((*(ps_do+k)).amount<=10000)                cout<<(*(ps_do+k)).name<<"\t"<<(*(ps_do+k)).amount<<endl;        }    }    delete [] ps_do;*//*  //6.7 使用程序识别不同字母及数字开头的单词或字符。        cout<<"Please enter words(q to quit):";        char ch[20];        int vowels=0,consonants=0,other=0;//vowels元音,consonants辅音        while(strcmp(ch,"q")&&cin>>ch)//最开始写成strcpy(),恒为真且没有比较意义。        {            if(isalpha(ch[0]))            {            //  switch(ch[0])            //  {            //  case 'a':            //  case 'A':            //  case 'e':            //  case 'E':            //  case 'i':            //  case 'I':            //  case 'o':            //  case 'O':            //  case 'u':            //  case 'U':                if(ch[0]=='a'||ch[0]=='e'||ch[0]=='i'||ch[0]=='o'||ch[0]=='u')                    vowels++;            //      break;            //  default:                else                    consonants++;            //      break;            //  }               }            else                other++;        }        cout<<vowels<<" words begining with vowels.\n";        cout<<consonants<<" words begining with consonants.\n";        cout<<other<<" others.\n";*//*  //6.8 从文件中读取数据,指出其中有多少字符。    const int F_SIZE=60;    ifstream inFile;    char filename[F_SIZE];    cout<<"Please enter your file name:";    cin.get(filename,60);//运行时用户输入目标文件名    inFile.open(filename);    if(!inFile.is_open())//判断文件是否打开成功    {        cout<<"Could not open the file "<<filename<<endl;        cout<<"Program terminating.";        exit(EXIT_FAILURE);    }    char ch;    int count=0;//  inFile>>ch;//  while(inFile.good())//判断最后一次读取输入的操作是否成功。//  {//      ++count;//      inFile>>ch;//  }    while(inFile>>ch)//括号表达式结果为inFIle,但在一个需要bool值的情况下,inFIle的结果为inFile.good(),即TRUE或者false。        ++count;    if(inFile.eof())//如果是因为读取到EOF而停止读取,则表示文件已经全部读取完了,该方法只能判断是否遇到EOF。        cout<<"End of file reached.\n";    else if (inFile.fail())//判断读取类型是否匹配,该方法能判断是否遇到EOF和类型是否匹配。        cout<<"Input terminated by data mismatch.\n";    else        cout<<"Input terminated br unkown reson.\n";//上面两种都不成立的话,可能为其他未知因素导致读取失败,包括文件损坏或硬件故障等问题。    if(count==0)        cout<<"No data processed.\n";//结合上面的情况,表示为空文件或读取失败。    else         cout<<"Items read:"<<count<<endl;    inFile.close();*//*  //6.9 完成6.6,但从文件中读取数据。    struct donation    {        char name[20];        double amount;    };    ifstream inFile_6_9;    int Grand_Patron=0,Grand=0;//用于判断该类型是否有捐款者。    char filename_6_9[20];    cout<<"Please enter the file name:";    inFile_6_9.clear();//清除状态    cin.get(filename_6_9,20);//从控制窗口输入文件名而不是从文件中读取。    inFile_6_9.open(filename_6_9);    if(!inFile_6_9.is_open())//判断文件是否打开成功    {        cout<<"Could not open the file "<<filename_6_9<<endl;        cout<<"Program terminating.";        exit(EXIT_FAILURE);    }    int donation_number=0;    inFile_6_9>>donation_number;//从文件中获得数组长度    donation* ps_6_9 = new donation[donation_number];    for (int i=0;i<donation_number;i++)    {        inFile_6_9.get();//混合读取数字和字符时,必须用xx.get()消除换行符,否则读取不成功。        inFile_6_9.getline((*(ps_6_9+i)).name,20);        inFile_6_9>>(*(ps_6_9+i)).amount;        if((*(ps_6_9+i)).amount>10000)            Grand_Patron++;        else            Grand++;    }    if(inFile_6_9.eof())//如果是因为读取到EOF而停止读取,则表示文件已经全部读取完了,该方法只能判断是否遇到EOF。        cout<<"End of file reached.\n";    else if (inFile_6_9.fail())//判断读取类型是否匹配,该方法能判断是否遇到EOF和类型是否匹配。        cout<<"Input terminated by data mismatch.\n";    else        cout<<"Input terminated br unkown reson.\n";//上面两种都不成立的话,可能为其他未知因素导致读取失败,包括文件损坏或硬件故障等问题。    if(Grand_Patron==0)        cout<<"The table list the Grand Patrons:\nnone\n";    else    {           cout<<"The table list the Grand Patrons:\n";        for (int j=0;j<donation_number;j++)        {            if((*(ps_6_9+j)).amount>10000)                cout<<(*(ps_6_9+j)).name<<"\t"<<(*(ps_6_9+j)).amount<<endl;         }    }    if(Grand==0)        cout<<"The table list the Patrons:\nnone";    else    {        cout<<"The table list the Patrons:\n";        for (int k=0;k<donation_number;k++)        {            if((*(ps_6_9+k)).amount<=10000)                cout<<(*(ps_6_9+k)).name<<"\t"<<(*(ps_6_9+k)).amount<<endl;        }    }    delete [] ps_6_9;*/    return 0;}
0 0