第四章 编程练习

编程练习4.1

#include <iostream>
#include <string>
int main()
{
    using namespace std;
    string firstName;
    string lastName;
    char gradeDeserve;
    int age;

    cout << "what is your first name? ";
    getline(cin, firstName);
    cout << "what is your last name? ";
    getline(cin, lastName);
    cout << "What letter grade do you deserve? ";
    cin >> gradeDeserve;
    cout << "What is your age? ";
    cin >> age;

    cout << "Name: " << lastName << ", " << firstName <<endl;
    cout << "Grade: " << (char) (gradeDeserve +1) << endl;
    cout << "Age: " << age <<endl;

    system("pause");
    return 0;
}

编程练习4.2

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string name;
    string dessert;

    cout << "Enter your name: ";
    getline(cin, name);
    cout << "Enter your favorite dessert: ";
    getline(cin, dessert);
    cout << "I have some delicious " << dessert
        << " for your, " << name << endl;

    system("pause");
    return 0;
}

编程练习4.3 & 4.4

#include <iostream>
#include <string>

int main()
{
    using namespace std;
    char firstName[20];
    char lastName[20];
    string fullName;

    cout << "Enter your first name: ";
    cin.getline(firstName,20);
    cout << "Enter your last name: ";
    cin.getline(lastName,20);

    strcat(lastName,", ");
    fullName = strcat(lastName, firstName);
    cout << "Here's the information in a single string: " << fullName << endl;

    system("pause");
    return 0;
}

编程练习4.4

#include <iostream>
#include <string>

using namespace std;

struct CandyBar {
    string brand;
    float weight;
    int calorie;
};

int main()
{
    CandyBar snack = {"Mocha Munch", 2.3, 350};

    cout << "Candy " << snack.brand << "'s weight is "
        << snack.weight << endl;
    cout << "Candy " << snack.brand << " has "
        << snack.calorie << " calorie "<< endl;

    system("pause");
    return 0;
}

编程练习4.6

#include <iostream>
#include <string>

using namespace std;

struct CandyBar {
    string brand;
    float weight;
    int calorie;
};

int main()
{
    CandyBar candy[3] = {
        {"Candy 1", 2.3, 350},
        {"Candy 2", 3.2, 530},
        {"Candy 3", 1.5, 250}
    };

    cout << "Candy " << candy[0].brand << "'s weight is "
        << candy[0].weight << endl;
    cout << "Candy " << candy[0].brand << " has "
        << candy[0].calorie << " calorie "<< endl;
     cout << "Candy " << candy[1].brand << "'s weight is "
        << candy[1].weight << endl;
    cout << "Candy " << candy[1].brand << " has "
        << candy[1].calorie << " calorie "<< endl;
     cout << "Candy " << candy[2].brand << "'s weight is "
        << candy[2].weight << endl;
    cout << "Candy " << candy[2].brand << " has "
        << candy[2].calorie << " calorie "<< endl;

    // 可以用一个show函数来挨个显示每个candy的内容
    system("pause");
    return 0;
}

编程练习4.8

#include <iostream>
#include <string>

using namespace std;

struct Pizza {
    string brand;
    float size;
    float weight;
};

int main()
{
    Pizza *pizza = new Pizza;
    cout << "Please enter the brand of pizza: ";
    getline(cin, pizza->brand);
    cout << "Please enter the size of pizza: ";
    cin >> pizza->size;
    cout << "Please enter the weight of pizza: ";
    cin >> pizza->weight;

    cout << "Pizza's brand: " << pizza->brand << endl;
    cout << "Pizza's size: " << pizza->size << endl;
    cout << "Pizza's weight: " << pizza->weight << endl;

    system("pause");
    return 0;
}

编程练习9 (下面的程序在VS2012上不能正常结束,还请路过的大神指教)

#include <iostream>  
#include <string>  
using namespace std;  
struct CandyBar  
{  
    string brand;  
    double weight;  
    int calorie;  
};  
void show(const CandyBar *candy);  
void main()  
{  
    CandyBar *candy=new CandyBar[3];
    for(int i=0;i<3;i++,candy++)  
    {  
        candy->brand="String"+i;  
        candy->weight=i;  
        candy->calorie=i*10;  
        show(candy);  
    }  

    delete []candy;  

}  

void show(const CandyBar *candy)  
{  
    cout<<"name="<<candy->brand<<" ,weight="<<candy->weight<<" ,calorie="<<candy->calorie<<endl;  
} 

编程练习10

#include <iostream>
#include <array>

using namespace std;

int main()
{
    array <float, 3> run;
    for (int i=0; i<3; i++)
    {
        cout << "Enter the runner " << i << "'s time: ";
        cin >> run[i];
    }
    float sum = 0;
    for (int i=0; i<3; i++)
    {
        cout << "the runner " << i << "'s time: ";
        cout << run[i] << " s" << endl;
        sum = sum+run[i];
    }

    cout << "average time is " << sum/3 << " s" << endl;

    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值