您的位置:首页 > 编程语言

第四章 编程练习

2016-11-09 04:09 141 查看
编程练习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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: