c++ Primer Plus(习题9.1)
/*golf.h习题9.1头文件*/
#pragma once
#ifndef GOLF_H
#define GOLF_H //习惯的用法
const int Len = 40; //存储名字的长度
//golf结构
struct golf
{
char fullname[Len];
int handicap;
};
/* g引用一个golfj结构 */
/* 提供输入,把信息输入到g引用的结构 */
void setgolf(golf&g, const char*name, int hc);
/* 函数的重载,如果用户输入名字,返回1,否则返回0*/
int setgolf(golf &g);
/* 重新设置g引用结构的handlip值 */
void handicap(golf &g, int hc);
/* 打印结构,没啥好说 */
void showgolf(const golf &g);
#endif // !GOLF_H
/*9.1提供头文件函数定义的文件*/
#include"golf.h"
#include<cstring>
#include<iostream>
/*结构的赋值*/
void setgolf(golf &g, const char*name, int hc)
{
strcpy(g.fullname,name); //读数据进入结构
g.handicap = hc;
}
int setgolf(golf &g)
{
if (strcmp(g.fullname, "")==0) //与空字符比较,看是否为空行,别忘了它的返回值
return 0;
return 1;
}
void handicap(golf &g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf &g)
{
std::cout << "The struct contains: \n"
<<"Full name: "<< g.fullname << std::endl
<<"Handip: "<<g.handicap << std::endl;
}
/*主程序,需包含golf.h头文件,且和golf.cpp一起编译*/
/*程序缺陷1: 当用户不想输入名字,确还要输入成绩和修改时=值*/
#include<iostream>
#include"golf.h"
const int MAX = 3;
int main()
{
using namespace std;
golf input[MAX];
char temp[Len]; //临时数组
int lin; //临时存储的成绩
int count=0; //因为出题人的函数要求,好像只能这样传递参数
while(count<MAX)
{
cout << "Please input your fullname: ";
cin.getline(temp, Len);
cout << "And your handicap: ";
cin >> lin;
setgolf(input[count], temp, lin);
cout << "If want change prior Value,give your value,don't put 0:"; //如果用户想改,则可以输入数字修改
if ((std::cin >> lin).get())
handicap(input[count], lin);
if (setgolf(input[count]) == 0) //判断用户是否输入空行
break;
count++;
}
for(int i=0;i<count;i++)
{
showgolf(input[i]);
}
return 0;
}
c++ Primer Plus(习题9.2)
/*这题修改程序清单9.9,要求用string代替字符数组*/
/*这题有两种传递string类的方法*/
#include<iostream>
#include<string>
using namespace std;
void strcount(string &str); //传递的是string类的引用,也可以这样
int main() /* void strcount(const char str[]) */
{
string input;
string end = "";
cout << "Enter a line: ";
getline(cin, input);
while (input!=end)
{
strcount(input); //string不可以直接换换换为char*,需要用一个类方法input.c_str()
cout << "Enter the next line(empty line to exit):";
getline(cin, input);
}
cout << "Bye!\n";
return 0;
}
void strcount(string &str)
{
static int total; //
int count = 0;
cout << "\"" << str << "\"" << "contains: ";
count = str.size(); //用了string类的一个成员方法,
total += count;
cout << count << " characters\n"
<< total << " characters total!\n";
}
c++ Primer Plus(习题9.3)
/*把两个结构放在一个缓存区中*/
/*可以用常规的new,也可以用定位的new*/
#include<iostream>
#include<cstring>
const int max = 2;
const int BUF = 512;
char buffer[BUF];
struct chaff
{
char dross[20];
int slag;
};
int main()
{
using namespace std;
chaff *p = new chaff[max];
chaff *p2 = new(buffer)chaff[max];
for (int i = 0; i < max; i++)
{
p[i] = { "i am so hangsome!",88 };
p2[i]= { "your are so dity!",66 };//{ "your are so beautiful!",66};
}
cout << "Now,The output\n";
cout << "Dross:\t\tSlag:\n";
for (int i = 0; i < max; i++)
{
cout << hex;
cout << p[i].dross << " at " << p + i <<"\tUsing usual new"<< endl
<< p[i].slag << " at " << p + i << "\tUsing usual new\n";
cout << p2[i].dross << " at " << p2 + i <<"\tUsing posion new\n"
<< p2[i].slag << " at " << p2 + i << endl;
}
delete[]p;
return 0;
}
c++ Primer Plus(习题9.4)
/*9.4的头文件*/
#pragma once
#ifndef NAMESPACE_H
#define NAMESPACE_H
namespace SALES
{
const int QUARTERS = 4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
//名称空间的各个函数
//拷贝double ar[]的值给g所引用的结构,n为拷贝个数(不能大于3)
void setSales(Sales &s, const double ar[], int n);
//计算s引用结构的平均值,最大值,最小值
void setSales(Sales &s);
//显示结构
void showSales(const Sales &s);
#endif // !NAMESPACE_H
}
/*9.4题目头文件定义*/
#include<iostream>
#include"namespace.h"
namespace SALES
{
void setSales(Sales &s, const double ar[], int n)
{
int i;
for (i = 0; i < n; i++)
s.sales[i] = ar[i];
}
void setSales(Sales &s)
{
int n = sizeof(s.sales) / sizeof(s.sales[0]);
double sum =0;
for (int i = 0; i < n; i++)
{
if (s.sales[i] > s.max)
s.max = s.sales[i];
else if (s.sales[i] < s.min)
s.min = s.sales[i];
sum += s.sales[i];
}
s.average = sum / n;
}
void showSales(const Sales &s)
{
std::cout << "\nHere the Sales result: \n";
int n = sizeof(s.sales) / sizeof(s.sales[0]);
for (int i = 0; i < n; i++)
std::cout << s.sales[i] << " ";
std::cout << "\nAverage: " << s.average << std::endl
<< "Max: " << s.max << std::endl
<< "Min: " << s.min << std::endl;
}
}
/*这题是拷贝一个double类型的数组里的值给sales结构*/
/*需要和9.4定义一起编译,且包含头文件namespace.h*/
#include<iostream>
#include"namespace.h"
int main()
{
using namespace SALES;
Sales tem;
const double test[QUARTERS] = {66.1,55.1,30.8,22.22};
tem = { }; //对结构内容进行空的初始化,不然后面指定拷贝个数不为4容易出错
setSales(tem,test, 2);
setSales(tem);
std::cout << "Here the result,Source array:\n";
for (int i = 0; i < QUARTERS; i++)
std::cout <<test[i] <<" ";
showSales(tem);
return 0;
}
只用于本人记录学习记录,无其他用意