C++ primer plus学习

#include
#include <stdio.h>
#include
#include
#include
using namespace std;
#pragma warning(disable:4996)
int num_count=0;
void print_word(const char * word,const int &num=0)//8.1
{
const char * word1 = {0};
word1 = word;//练习字符串之间的赋值
if (num == 0)
{
num_count++;
cout << “The word: “;
printf(”%s”, word1);//字符串的C语言打印方式
cout<< " num_count: " << num_count<<endl;
}
else {
for (int i = 0; i < num_count; i++)
{
cout << word<<endl;
}
}
}
struct CandyBar //8.2
{
const char * Grand;
double candy_weight;
int candy_hot;
};
void set_CandyBar(CandyBar &candy,const char Grand=“Millennium Munch”,double candy_weight=2.85,int candy_hot=350)//8.2
{
candy.candy_hot = candy_hot;
candy.candy_weight = candy_weight;
candy.Grand=Grand;
}
void show_CandyBar(const CandyBar & candy) //8.2
{
cout << "Candy Grand: "<<candy.Grand << endl;
cout << "Candy Weight: " << candy.candy_weight << endl;
cout << "Candy Hot: " << candy.candy_hot << endl;
}
void change_string(string & word)//8.3
{
int string_num = word.size();
/char * big_word={};
char * big_word = new char[string_num];//char类型的输出
for(int i=0;i<string_num;i++)
big_word[i]=toupper(word[i]);
for (int i = 0; i<string_num; i++)
cout << big_word[i];//直接输出指针会发生内存泄漏,需要一个一个输出。
delete big_word;
/
for (int i = 0; i<string_num; i++)
word[i] = toupper(word[i]);//使用这个函数时,无法把每个值赋给另外的string类,原因待议。
}
struct stringy { //8.4
char str;
int ct;
};
void set(stringy &beany, char * testing)//8.4
{
int num;
num = strlen(testing);
    beany.str =new char[num+1];//这里很关键,并没有包括\0字符,所以需要加1
strcpy(beany.str, testing);
beany.ct = num;
}
void show(const stringy &beany, int times=1)//8.4
{
for (int i = 0; i < times; i++)
{
cout << "Beany.str: " << beany.str<<endl;
cout << "Beany.ct: " << beany.ct << endl;
}
}
void show(const char * testing, int times = 1)//8.4
{
for (int i = 0; i < times; i++)
{
cout << "testing: " << testing << endl;
}
}
template
T max5(T array[],int num=5) //8.5
{
T max = array[0];
for (int i = 1; i < num; i++)
{
if (array[i] > max)
{
max = array[i];
}
}
return max;
}
template T maxn(T array[], int num = 5) //8.6
{
T max = array[0];
for (int i = 1; i < num; i++)
{
if (array[i] > max)
{
max = array[i];
}
}
return max;
}
template <> const char
maxn<const char
>(const char * array[], int num) //8.6
{//具体化的类型是const int 故这里需要const int;
int array_max_len= strlen(array[num-1]),long_num=0;
for(int i=num-1;i>=0;i–)

if (strlen(array[i]) >= array_max_len)
{
array_max_len = strlen(array[i]);
long_num = i;
}
}
return array[long_num];
}
template
T SumArray(T arr[], int n) //8.7
{
int sum=0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
return sum;
}
template
T SumArray(T *arr[], int n) //8.7
{
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += *arr[i];
}
return sum;
}
void main()
{

/for (int i = 0; i < 3; i++)
{
print_word(“Hello!”);
}
print_word(“Hello!world”, 3);
///8.1

/CandyBar candy;
set_CandyBar(candy);
show_CandyBar(candy);
/ //8.2

/string input_string; //8.3
cout << "Enter a string (q to quit): ";
do{ 
if (!(getline(cin,input_string))) break;
if (input_string == “q”) break;
change_string(input_string);
cout << input_string << endl;
cout << "Next string(q to quit): ";
} while (input_string != “q”);
cout << “Bye.” << endl;
/

/stringy beany;     //8.4
char testing[] = “Raeality isn’t what it used to be.”;
set(beany, testing);
show(beany);
cout << endl;
show(beany, 2);
cout << endl;
delete beany.str;
testing[0] = ‘D’;
testing[1] = ‘u’;
show(testing);
cout << endl;
show(testing, 3);
cout << endl;
show(“Done!”);
/

/int array1[5] = { 12,15,78,20,35 };  //8.5
int array1_max = 0;
double array2_max = 0;
double array2[5] = { 22.2,52.3,52.4,89.5,79.6 };
array1_max=max5(array1);
array2_max=max5(array2);
cout << "Array1_max: " << array1_max << endl;
cout << "Array2_max: " << array2_max << endl;
/

/int array1[6] = { 12,15,78,20,35,50 }; //8.6
int array1_max = 0;
double array2_max = 0;
const char * array3_long = {};
double array2[4] = { 22.2,52.3,52.4,89.5 };
const char * array3[5] = { “Hello!”,“OK!”,“HEllo!”,“Good”,“Bad” };
array1_max=maxn(array1);
array2_max=maxn(array2);
array3_long=maxn(array3,5);
cout << "Array1_max: " << array1_max << endl;
cout << "Array2_max: " << array2_max << endl; 
cout << "Array3_long: " << array3_long << endl;
/

struct debts { //8.7
char name[50];
double amount;
};
int things[6] = { 13,31,103,301,310,130 };
struct debts mr_E[3] = {
{“Ima Wolfe”,2400.0},
{“Ura Foxe”, 1300.0},
{“Iby Stout”,1800.0}
};
double *pd[3];
for (int i = 0; i < 3; i++)
{
pd[i] = &mr_E[i].amount;
}
cout << “Suming Mr.E’s counts of things:\n”;
cout<<SumArray(things, 6)<<endl;
cout << “Suming Mr.E’s debts:\n”;
cout << SumArray(pd, 3) << endl;
system(“pause”);
————————————————
版权声明:本文为CSDN博主「Ba_ball」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sinat_35412848/article/details/79816615

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值