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

C++ Primer Plus 第六版 第四章编程练习记录

2017-07-04 21:17 519 查看
第4章(VS2013)
3.
#include<iostream>
#include<cstring>

int main()
{
using namespace std;

char fn[10];
cout << "Enter your first name: ";
cin >> fn;
char ln[10];
cout << "Enter your last name: ";
cin >> ln;
strcat_s(fn, ", ");
char he[20];
strcpy_s(he,fn);
strcat_s(he, ln);
cout << "Here's the information in a single string: "<<he << endl;
cin.get(); cin.get(); return 0; }

4.

#include<iostream>
#include<string>

int main()
{
using namespace std;

string str1, str2;
cout << "Enter your first name: ";
cin >> str1;
cout << "Enter your last name: ";
cin >> str2;
string str;
str = str2 + ", ";
str = str + str1;
cout << "Here's the information in a single string: " << str << endl;

cin.get();
cin.get();
return 0;
}


5.

#include<iostream>

struct CandyBar
{
char kind[20];
double weights;
int k;
};

int main()
{
using namespace std;

CandyBar snack = { "Mocha Much", 2.3, 350 };
cout << "kind: " << snack.kind << endl;
cout << "weights: " << snack.weights << endl;
cout << "calorie: " << snack.k << endl;

//cin.get();
cin.get();
return 0;
}


6.

#include<iostream>

struct CandyBar
{
char kind[20];
double weights;
int k;
};

int main()
{
using namespace std;

CandyBar snack[3] = {
{ "Mocha Much", 2.3, 350 },
{"BuLaoLin",2.0,400},
{"DaBaiTu",2.6,300}

};

for (int i = 0; i < 3; i++)
{
cout << "kind: " << snack[i].kind << endl;
cout << "weights: " << snack[i].weights << endl;
cout << "calorie: " << snack[i].k << endl;
}
//cin.get();
cin.get();
return 0;
}


7或8.

#include<iostream>

struct Piza
{
char CompName[20];
double d;
double weights;
};

int main()
{
using namespace std;

Piza* p = new Piza;

cin.getline(p->CompName,20);
cin >> (*p).d;
cin >> p->weights;
cout << "CompName: " << p->CompName << endl;
cout << "d: " << p->d << endl;
cout << "weights: " << p->weights << endl;
delete p;
cin.get();
cin.get();
return 0;
}

9.

#include<iostream>

struct CandyBar
{
char kind[20];
double weights;
int k;
};

int main()
{
using namespace std;

CandyBar* p = new CandyBar[3];

p[0] = { "Mocha Much", 2.3, 350 };
p[1] = { "BuLaoLin", 2.0, 400 };
p[2]={"DaBaiTu", 2.6, 300 };

for (int i = 0; i < 3; i++)
{
cout << "kind: " << p[i].kind << endl;
cout << "weights: " << p[i].weights << endl;
cout << "calorie: " << p[i].k << endl;
}
delete[] p;
cin.get();
return 0;
}

9.
#include<iostream>
#include<array>

int main()
{
using namespace std;

cout << "Please input thtee grades in seconds\n";
array<float, 3>a;
cin >> a[0];
cin >> a[1];
cin >> a[2];
8e73
cout << "first " << a[0]<<" s"<<endl;
cout << "second " << a[1] << " s" << endl;
cout << "thred " << a[2] << " s" << endl;
cout << "avgave: " << (a[0] + a[1] + a[2]) / 3 <<" s"<< endl;
cin.get(); cin.get(); return 0; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: