版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28467367/article/details/88533895
//4.13编程练习
#include <iostream>
#include<string>
#include <vector>
#include <array>
using namespace std;
int main1()
{
char firstname[128];
char lastname[128];
char grade;
int age;
cout << "What is your first name? ";
cin.getline(firstname, 128);
cout << "What is your last name? ";
cin.getline(lastname, 128);
cout << "What letter grade do you deserve? ";
cin >> grade;
cout << "What is your age? ";
cin >> age;
cout << "Name: " << lastname << ", " << firstname << endl;
cout << "Grade: " << char(grade + 5) << endl;
cout << "Age: " << age << endl;
return 0;
}
int main2()
{
string name;
string dessert;
cout << "Enter your name:\n";
getline(cin, name);
cout << "Enter your favorite dessert:\n";
getline(cin, dessert);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
int main3 4()
{
char firstname[50];
char lastname[50];
//string lastname;
cout << "Enter your first name? ";
cin.getline(firstname, 50);
cout << "Enter your last name? ";
cin.getline(lastname, 50);
strcat(lastname, ", ");
strcat(lastname, firstname);
//strncat(lastname, firstname, 50);
cout << "Here's your infomation is a single string: " << lastname << endl;
//getline(cin, lastname);
//cout << "Here's your infomation is a single string: " << lastname << ", " << firstname << endl;
return 0;
}
int main5()
{
struct CandyBar{
char brand[20];
double weight;
int calory;
};
CandyBar snack = { "Mocha Munch", 2.3, 350 };
cout << "brand: " << snack.brand << endl;
cout << "weight: " << snack.weight << endl;
cout << "calory: " << snack.calory << endl;
return 0;
}
int main6()
{
struct CandyBar{
char brand[20];
double weight;
int calory;
};
CandyBar snack[3] = {
{ "Mocha Munch", 2.3, 350 },
{ "Xufuji",5.2,132 },
{ "Kaixin",55.2,150 }
};
for (int i = 0; i < 3; i++){
cout << "brand: " << snack[i].brand << endl;
cout << "weight: " << snack[i].weight << endl;
cout << "calory: " << snack[i].calory << endl << endl;
//cout << snack[i].brand << endl
// << snack[i].weight << endl
// <<snack[i].calory << endl;
}
return 0;
}
struct pizza{
char company[20];
double diameter;
int weight;
};
int main7()
{
pizza pie;
cout << "Enter the name of company: ";
cin.getline(pie.company, 20);
cout << "Enter the pidmeter of pizza: ";
cin >> pie.diameter;
cout << "Enter the weight of pizza: ";
cin >> pie.weight;
cout << "company: " << pie.company<< endl
<< "diameter: " << pie.diameter << endl
<< "weight: " << pie.weight << endl;
return 0;
}
int main8()
{
pizza *p=new pizza;
cout << "Enter the pidmeter of pizza: ";
(cin>>p->diameter).get();
cout << "Enter the name of company: ";
cin.getline(p->company, 20);
cout << "Enter the weight of pizza: ";
cin >> p->weight;
cout << "company: " << p->company << endl
<< "diameter: " << (*p).diameter << endl
<< "weight: " << p->weight << endl;
delete p;
return 0;
}
int main9()
{
struct CandyBar{
string brand;
double weight;
int calory;
};
CandyBar *snack = new CandyBar[3];
snack[0].brand = "Mocha Munch";
snack[0].weight = 2.3;
snack[0].calory = 350;
snack[1].brand = "Xufuji";
snack[1].weight = 5.2;
snack[1].calory = 132;
snack[2].brand = "Kaixin";
snack[2].weight = 6.2;
snack[2].calory = 150;
for (int i = 0; i < 3; i++){
cout << "brand: " << snack[i].brand << endl;
cout << "weight: " << snack[i].weight << endl;
cout << "calory: " << snack[i].calory << endl << endl;
}
delete [] snack;
return 0;
}
int main10(){
array<double, 4> time;
//double time[4]; 数组方法
cout<<"Enter three results time of runing 40 meters: "<<endl;
cin >> time[0] >> time[1] >> time[2];
time[3] = (time[0] + time[1] + time[2]) / 3;
cout << "first time: " << time[0] <<endl
<<"second time: " << time[1] << endl
<< "third time: " << time[2] << endl;
cout << "Average result: " << time[3]<<endl;
return 0;
}