//arrayone.cpp---small arrays of integers
#include <iostream>
int main1()
{
using namespace std;
int yams[3];
yams[0] = 7;
yams[1] = 8;
yams[2] = 6;
int yamcosts[3] = {20,30,5};
// yamcosts[3]= { 20, 30, 5 };这种初始化方式不行
cout << "Total yams = " << yams[0] + yams[1] + yams[2] << endl;
cout << "The package with " << yams[1] << " yams costs "
<< 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 << "\nSize of yams array = " << sizeof yams << " bytes.\n";
cout << "Size of one element = " << sizeof yams[0] << " bytes.\n";
字符串拼接///
cout << "I'd give my right arm to be" " a great violinist.\n";
cout << "I'd give my right arm to be a great violinist.\n";
cout << "I'd give my right ar"
"m to be a great violinist.\n";
/
system("pause");
return 0;
}
///
程序清单4.2 string.cpp///
//string.cpp---storing strings in an array
#include <iostream>
#include<cstring>
int main2()
{
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 characters of my name: ";
cout << name2 << endl;
system("pause");
return 0;
}
///程序清单4.3//
/insert1.cpp----reading more than one string///
/
#include<iostream>
int main3()
{
using namespace std;
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];
cout << "Enter your name:\n";
cin >> name;
cout << "Enter your favorite dessert:\n";
cin >> dessert;
cout << "I have some delicious " << dessert;
cout << " for you," << name << ".\n";
system("pause");
return 0;
}
/
///程序清单4.4//
//insert2.cpp---reading more than one word with getline
#include <iostream>
int main4()
{
using namespace std;
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];
cout << "Enter your name:\n";
cin.getline(name, ArSize);
cout << "Enter your favorite dessert:\n";
cin.getline(dessert, ArSize);
cout << "I have some delicious " << dessert;
cout << " for you," << name << ".\n";
system("pause");
return 0;
}
///
///程序清单4,5///
///insert3.cpp---reading more than one word with get() & get()
#include <iostream>
int main5()
{
using namespace std;
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];
cout << "Enter your name:\n";
cin.get(name, ArSize).get(); //第二个get()将换行符读取掉,方便后面一行的读取。
cout << "Enter your favorite dessert:\n";
cin.get(dessert, ArSize).get();
cout << "I have some delicious " << dessert;
cout << " for you," << name << ",\n";
system("pause");
return 0;
}
/
//程序清单4.6//
numstr.cpp---following number input with line input///
#include <iostream>
int main6()
{
using namespace std;
cout << "What year was your house built?\n";
int year;
cin >> year;
// cin.get();
cout << "What is its street address?\n";
char address[80];
cin.getline(address, 80);
cout << "Year built: " << year << endl;
cout << "Address: " << address << endl;
cout << "Done!\n";
system("pause");
return 0;
}
///
//程序清单4.7///
strtype1.cpp---using the C++ string class/
#include <iostream>
#include <string>
int main7()
{
using namespace std;
char charr1[20];
char charr2[20] = "jaguar";
string str1;
string str2 = "panther";
cout << "Enter a kind of feline: ";
cin >> charr1;
cout << "Enter another kind of feline: ";
cin >> str1;
cout << "Here are some felines:\n";
cout << charr1 << " " << charr2 << " "
<< str1 << " " << str2 << endl;
cout << "The third letter in " << charr2 << " is"
<< charr2[2] << endl;
cout << "The third letter in " << str2 << " is"
<< str2[2] << endl;
system("pause");
return 0;
}
//C++ 11字符串初始化
//char first_date[] = { "Le Chapon Dodu" };
//char second_date[] {"The Elegant Plate"};
//string third_date = { "The Bread bowl" };
//string fourth_date{ "Hank's Fine Eats" };
/
/程序清单4.8
///strtype2.cpp---assigning,adding,and appending///
#include <iostream>
#include <string>
int main8()
{
using namespace std;
string s1 = "penguin";
string s2, s3;
cout << "You can assign one string object to another:s2=s1\n";
s2 = s1;
cout << "s1: " << s1 << ", s2: " << s2 << endl;
cout << "You can assign a C-style string to a string object.\n";
cout << "s2 = \"buzzard\"\n";
s2 = "buzzard";
cout << "s2: " << s2 << endl;
cout << "You can concatenate strings: s3 = s1 + s2\n";
s3 = s1 + s2;
cout << "s1 += s2 yields s1 = " << s1 << endl;
s2 += "for a day";
cout << "s2 += \"for a day\"yields s2 =" << s2 << endl;
system("pause");
return 0;
}
/
///程序清单4.9
strtype3.cpp----more string class feature///
#include <iostream>
#include <string>
#include <cstring>
int main()
{
using namespace std;
char charr1[20];
char charr2[20] = "jaguar";
string str1;
string str2 = "panther";
str1 = str2;
strcpy(charr1, charr2);
str1 += str2;
strcat(charr1, " juice");
int len1 = str1.size();
int len2 = strlen(charr1);
cout << "The string " << str1 << " contains "
<< len1 << " characters.\n";
cout << "The string " << charr1 << " contains "
<< len2 << " characters.\n";
system("pause");
return 0;
}