书籍:C++ Primer Plus (第六版)(中文版)
工具:Dev-C++ 5.11
电脑信息:Intel(R) Xeon(R) CPU E5-2603 v3 @ 1.60GHz
系统类型:64位操作系统,基于X64的处理器 Windows10 专业版
第4章 复合类型
4.1 数组
实例4.1 arrayone.cpp
#include <iostream>
int main()
{
using namespace std;
int yams[3];
yams[0] = 7;
yams[1] = 8;
yams[2] = 6;
int yamcosts[3] = {20, 30, 5};
cout << "Total yams = ";
cout << yams[0] + yams[1] + yams[2] << endl;
cout << "The package with " << yams[1] << " yams costs ";
cout << yamcosts[1] << " cents per yam.\n";
int total = yams[0] * yamcosts[0] + yams[1] * yamcosts[1];
total = total + yams[2] * yamcosts[2];
cout << "The total yam expense is " << total << " cents.\n";
cout << "\n Size of yams array = " << sizeof yams;
cout << " bytes.\n";
cout << "Size of one element = " << sizeof yams[0];
cout << " bytes.\n";
return 0;
}
编译运行结果:
Total yams = 21
The package with 8 yams costs 30 cents per yam.
The total yam expense is 410 cents.
Size of yams array = 12 bytes.
Size of one element = 4 bytes.
--------------------------------
Process exited after 0.3744 seconds with return value 0
请按任意键继续. . .
4.2 字符串
实例4.2 string.cpp
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
const int size = 15;
char name1[size];
char name2[size] = "C++owboy";
cout << "Howdy! I'm " << name2;
cout << "! what's your name?\n";
cin >> name1;
cout << "well, " << name1 << ", your name has ";
cout << strlen(name1) << " letters and is stored\n";
cout << "in an array of " << sizeof(name1) << " bytes.\n";
cout << "your initial is " << name1[0] << ".\n";
name2[3] = '\0';
cout << "here are the first 3 letters of my name: ";
cout << name2 << endl;
return 0;
}
编译运行结果:
Howdy! I'm C++owboy! what's your name?
Basicman
well, Basicman, your name has 8 letters and is stored
in an array of 15 bytes.
your initial is B.
here are the first 3 letters of my name: C++
--------------------------------
Process exited after 33.84 seconds with return value 0
请按任意键继续. . .
实例4.3 instr1.cpp [此处为图片1]
#include <iostream>
int main()
{
using namespace std;
const int Arsize = 20;
char name[Arsize];
char dessert[Arsize];
cout << "Input your name:\n ";
cin >> name;
cout << "Input your preferred dessert:\n";
cin >> dessert;
cout << "I have some delectable " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
编译运行结果:
Enter your name:
Alistair Dreeb
Enter your favorite dessert:
I have some delicious Dreeb for you, Alistair .
--------------------------------
Process exited after 45 seconds with return value 0
请按任意键继续. . .
实例4.4?instr2.cpp
#include <iostream>
int main()
{
using namespace std;
const int Arsize = 20;
char name[Arsize];
char dessert[Arsize];
cout << "Input your name:\n ";
cin.getline(name, Arsize);
cout << "Input your preferred dessert:\n";
cin.getline(dessert, Arsize);
cout << "I have some delectable " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
编译运行结果:
Enter your name:
Dirk Hammernose
Enter your favorite dessert:
Radish Torte
I have some delicious Radish Torte for you, Dirk Hammernose .
--------------------------------
Process exited after 51.39 seconds with return value 0
请按任意键继续. . .
实例4.5?instr3.cpp
#include <iostream>
int main()
{
using namespace std;
const int Arsize = 20;
char name[Arsize];
char dessert[Arsize];
cout << "Input your name:\n ";
cin.get(name, Arsize).get();
cout << "Input your preferred dessert:\n";
cin.getline(dessert, Arsize).get();
cout << "I have some delectable " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
编译运行结果:
Enter your name:
Mai Parfait
Enter your favorite dessert:
Chocolate Mousse
I have some delicious Chocolate Mousse for you, Mai Parfait .
--------------------------------
Process exited after 67.92 seconds with return value 0
请按任意键继续. . .
实例4.6?numstr.cpp
#include <iostream>
int main()
{
using namespace std;
cout << "What year was your house built?\n";
int year;
cin >> year;
cout << "What is its street address?\n";
char address[80];
cin.getline(address, 80);
cout << "建成年份: " << year << endl;
cout << "地址: " << address << endl;
cout << "完成!\n"; // 使用数组表示法
return 0;
}
编译运行结果:
您的房屋是哪一年建成的?
1966
它的街道地址是什么?
建成年份: 1966
地址:
完成!
--------------------------------
进程在8.354秒后退出,返回值为0
请按任意键继续. . .


雷达卡


京公网安备 11010802022788号







