rex.fate的个人空间 https://blog.eetop.cn/1286187 [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

C++ Primer Plus 第六章 程序6.1-6.10

已有 736 次阅读| 2014-10-20 08:06 |个人分类:C++学习

程序清单6.1

//6_1
#include<iostream>
int main()
{using std::cin;
using std::cout;
using std::endl;//上述三句可以使用using namespace std;替换
char ch;
int spaces=0;
int total=0;
cin.get(ch);//获取单个字符
while(ch!='.')//在句尾.处停止
{
    if(ch==' ')//检测是否存在空格
        ++spaces;//空格数计数
    ++total;    //字符计数
    cin.get(ch);
}
    cout<<spaces<<"spaces, "<<total;
    cout<<" characters total in sentence."<<endl;
    return 0;
}

 

 

程序清单6.2

//6_2
#include<iostream>
int main()
{using namespace std;
char ch;
cout<<endl<<"Type, and i shall repeat."<<endl;
cin.get(ch);
while(ch!='.')
{
    if(ch=='\n')
        cout<<ch;//回车换行字符输入后不变
    else
        cout<<++ch;//其他字符后移一项 例c->d,空格->!
    cin.get(ch);
}
    cout<<endl<<"Please excuse the slight confusion."<<endl;
    return 0;
}

 

程序清单6.3

//6_3
#include<iostream>
const int Fave=520;  //520为my favourite number
int main()
{
    using namespace std;
    int n;
    cout<<"Enter a number in the range of 1-100 to find my favourite number:"<<endl;
    do
    {
    cin>>n;
    if(n<Fave)
    cout<<"Too low --guess again:";
    else if(n>Fave)
    cout<<"Too high --guess again:";
    else
    cout<<Fave<<" ,that is right"<<endl;
    }while(n!=Fave);
    return 0;
}

 

程序清单6.4

//6_4 Y/N只判断首字符是否是y/n 或Y/N
#include<iostream>
int main()
{
    using namespace std;
    cout<<"This program may refomat your hard disk"<<endl
        <<"and destory all your data"<<endl
        <<"DO you wish to continue?<Y/N>";
    char ch;
    cin>>ch;
    if(ch=='Y'||ch=='y')
    cout<<"you were warned!\a\a"<<endl;
    else if(ch=='N'||ch=='n')
    cout<<"A wise choice ....bye";
    else
    cout<<"That was't a y or n!Apparently you can't follow\ninstructions,so i will trash your disk anyway.\a\a\a"<<endl;
    return 0;
}

 

程序清单6.5

//6.5

#include<iostream>
const int ArSize=6;//数组容量为7;
int main()
{
    using namespace std;
    float naaq[ArSize];
    cout<<"Enter the NAAQs(New Age Awareness Questions) of"<<endl
        <<"your neighbors. Program terminates "
        <<"when you make"<<endl<<ArSize<<" entries "
        <<"or enter a negative value."<<endl;
    int i=0;
    float temp;
    cout<<"First value: ";
    cin>>temp;
    while(i<ArSize&&temp>=0)  //小于6个输入并且输入不小于0
    {
        naaq[i]=temp;
        ++i;
        if(i<ArSize)
        {
            cout<<"Next value: ";
            cin>>temp;
        }
    }
    if(i==0)
    cout<<"NO data--bye"<<endl;
    else
    {
        cout<<"Enter your NAAQ:";
        float you;
        cin>>you;
        int count=0;
        for(int j=0;j<i;j++)//循环计算大于you的数值个数
            if(naaq[j]>you)
                ++count;
        cout<<count;
        cout<<" of your neighbors have greater awareness of"<<endl
            <<"the New Age than you do."<<endl;
    }
    return 0;
}

 

程序清单6.6

//6.6
#include<iostream>
const char* qualify[4]=
{
    "10,000-meter  race.\n",
    "mud tug-of-war.\n",
    "masters canoe jousting.\n",
    "pie-throw festival.\n"
};
int main()
{
    using namespace std;
    int age;
    cout<<"Enter your age in years: ";
    cin>>age;
    int index;
    if(age>17&&age<35)
        index=0;
    else if(age>=35&&age<50)
        index=1;
    else if(age>50&&age<65)
        index=2;
    else
        index=3;
    cout<<"You qualify for the "<<qualify[index];
    return 0;
}

 

程序清单6.7

//6.7
#include<iostream>
#include<climits>
bool is_int(double);
int main()
{
    using namespace std;
    double num;
    cout<<"Yo,dude!Enter an intrger value: ";
    cin>>num;
    while(!is_int(num))
    {
        cout<<"Out of range --please try again: ";
        cin>>num;
    }
    int val= int(num);
    cout<<"You've entered the integer "<<val<<"\n bye\n";
    return 0;
}

bool is_int(double x)
{
    if(x<=INT_MAX&&x>=INT_MIN)
        return true;
    else
        return false;
}

 

程序清单6.8

//6.8
#include<iostream>
#include<cctype>
int main()
{
    using namespace std;
    cout<<"Enter text for analysis,and type @"<<"to terminate input.\n";
    char ch;
    int whitespace=0;
    int digits=0;
    int chars=0;
    int punct=0;
    int thers=0;
    cin.get(ch);
    while(ch!='@")
    {
        if(isalpha(ch))//检查字符是否为字母字符
            chars++;
        else if(isspace(ch))//测试字符是否为空白,即换行符
            whitespace++;
        else if(isdigit(ch))//测试字符是否为数字字符
            digits++;
        else if(ispunct(ch))//测试字符是否是标点字符
            punct++;
        else
            others++;
        cin.get(ch);
    }
    cout<<"char"<<" letters, "
        <<whitespace<<" whitespace, "
        <<digits<<" digits, "
        <<punct<<" punctuations, "
        <<others<<" others."<<endl;
    return 0;
}

 

程序清单6.9

//6.9
#include<iostream>
int main()
{
    using namespace std;
    int a,b;
    cout<<"Enter two integers: ";
    cin>>a>>b;
    cout<<"The larger of "<<a<<" and "<<b;
    int c=a>b?a:b;//比较适合简单关系或简单表达式;
    cout<<"is "<<c<<endl;
    return 0;
}

 

程序清单6.10\

//6.10
#include<iostream>
using namespace std;
void showmenu();
void report();
void comfort();
int main()
{
    showmenu();
    int choice;//输入必须为整数,否则会无限循环
    cin>>choice;
    while(choice!=5)//输入5时循环结束
    {
       switch(choice)
       {
           case 1: cout<<"\n";
                    break;//如没有break,程序会接下去执行case 2.
            case 2: report();
                    break;
            case 3: cout<<"The boss was in all day."<<endl;
                    break;
            case 4: comfort();
                    break;
            default:cout<<"That's not a choice."<<endl;
       }
       showmenu();
       cin>>choice;
    }
    cout<<"Bye!"<<endl;
    return 0;
}

void showmenu()
{
    cout<<"Please enter 1,2,3,4,or 5:"<<endl
        <<"1)alarm           2)report"<<endl
        <<"3)alibi           4)comfort"<<endl
        <<"5)quit"<<endl;
}
void report()
{
    cout<<"It's been an excellent week for business."<<endl
        <<"Sales are up 120%.Expenses are down 35%."<<endl;
}
void comfort()
{
    cout<<"Your employees think you are the finest CEO\n"
        "in the industry.The board of directors think\n"
        "you are the finest CEO in the industry.\n";
}


点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 1

    粉丝
  • 0

    好友
  • 0

    获赞
  • 0

    评论
  • 访问数
关闭

站长推荐 上一条 /2 下一条


小黑屋| 手机版| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-11-25 07:10 , Processed in 0.026215 second(s), 18 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部