第三章 程序的流程控制(四)

来源:互联网 发布:python频域转时域 编辑:程序博客网 时间:2024/05/19 13:24
第七题:
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int weight;//包裹重量
    int dist;
    const int iBase=15;
    cout<<"请输入你包裹的重量(正整数):"<<endl;
    cout<<"警告:如果不是整数,结果不正确概不负责!"<<endl;
    cin >> weight;
    cout<<"请输入邮递距离(正整数,公里,警告同上):"<<endl;
    cin >> dist;

    if(weight<=0 || dist <=0)
    {
        cout<<"包裹重量级邮递距离不能比零还少!"<<endl;
        return 1;
    }

    switch (weight/iBase)
    {
    case 0:
        cout<<"Your weight is lower than 15g, it still costs you 5 yuan."<<endl;
        break;
    case 1:
        cout<<"Your weight is between 15 and 29, it costs you 5 yuan."<<endl;
        break;
    case 2:
        cout<<"Your weight is between 30 and 44, it costs you 9 yuan."<<endl;
        break;
    case 3:
        cout<<"Your weight is between 45 and 59, it costs you 12 yuan."<<endl;
        break;
    case 4:
        cout<<"Your weight is: "<<weight<<endl;
        cout<<"Your mail distance is: "<<dist<<endl;
        cout<<"So your fee is: "<<14+dist/1000<<" yuan."<<endl;
        break;
    case 5:
        cout<<"Your weight is: "<<weight<<endl;
        cout<<"Your mail distance is: "<<dist<<endl;
        cout<<"So your fee is: "<<15+dist*2/1000<<" yuan."<<endl;
        break;
    default:
        cout<<"Your incoming data is error."<<endl;
    }
    return 0;
}

简单测试用例:
第一类:
weight: 0, dist: 0
weight: 50, dist: 0
weight: 0, dist 2000

第二类:正好压线的数据,比如weight为15,30,45...,略

第三类:介于各种正常范围之间,以及weight超过75

PS:本书的题目都没有标准答案,简单的测试用例是用来determine结果是对是错的好方法。

第八题:
#include<iostream>
#include<string>
using namespace std;
int main()
{
    double PI=1;
    double base=3;
    bool neg = false;//determine the sign
   
    for(double result=0;(result=1/base)>=1e-8;base+=2)
        if(neg){
                PI+=result;
                neg=false;
        }
        else{
                PI-=result;
                neg=true;
        }
    PI=PI*4;
    cout<<"The pi is: "<<PI<<endl;
    return 0;
}

第九题:
#include<iostream>
#include<string>
using namespace std;

int main()
{
    for(int triValue=101,triArr[3],temp;triValue<1000;triValue++){

        temp=triValue;
        for(int i=0;i<3;++i){
            triArr[i]=temp%10;
            temp=temp/10;
        }

        temp=0;//get temp's value to zero, prepare for next use
        for (int i=0;i<3;++i){
            triArr[i]=triArr[i]*triArr[i]*triArr[i];
            temp+=triArr[i];
        }

        if(temp==triValue)
            cout<< triValue <<endl;
    }       
    return 0;
}

第十题:用辗转向除法
#include<iostream>
#include<string>
using namespace std;

int main()
{
    cout<<"Please input two int value"<<endl;
    int a,b;
    cin >> a >> b;

    if(a<b)
        swap(a,b);
    int temp;
    while(b!=0)
    {
        temp=a%b;
        a=b;
        b=temp;
    }
    cout<<"最小公倍数是"<<a<<endl;
    return 0;
}

第十一题:
#include<iostream>
#include<string>
using namespace std;

int main()
{
    cout<<' ';
    for(int i=1;i<10;++i)
        cout<<"    "<<i;
    cout<<endl;
    for(int i=1;i<10;++i){
        cout<<i<<"    ";
        for(int j=1,temp=0;j<10;j++){
            temp+=i;
            cout.width(5);
            cout<<std::left<<temp;
        }
        cout<<endl;
    }
    return 0;
}
注:这里需要用到一些格式化输入的技巧,HOHO。说实话,我也是做这题的时候现查的,不然我也不知道cout咋格式化输出。

第12题
i=0;
while(i<max_length){
    if(input_line[i]=='?')  quest_count++;
    i++;
}

第13题
第一图和第二图都可以用循环结构来表示
第三图很明显可以用选择结构来表示
原创粉丝点击