1.(多文件程序)
tips:1.在一个项目中自定义头文件时,头文件的标题是以.h结尾,而.cpp是指源文件,且在其他同项目的文件中引用该头文件是以#include“” 2.若在以上操作中对于自定义的头文件仍显示“无法打开源文件,可试验以下方法
我的项目下新建的头文件为 my.h
找到其在硬盘上的路径,D:\C++\day1\project1\project1\hwenjian,在VS项目project1上右键属性,C/C+±>常规->附加包含目录->编辑中,把此路径添加上
#ifndef GOLF_H
#define GOLF_H
const int len = 40;
struct golf
{
char fullname[len];
int handicap;
};
void setgolf(golf & g, const char* name, int hc);
int setgolf(golf& g);
void handicap(golf& g, int hc);
void showgolf(const golf& g);
#endif
#include<iostream>
#include "golf.h"
using namespace std;
int main()
{
golf list[len];
cout << "Please enter the basic information of the member:" << endl;
int i = 0;
char full[len];
int hand;
cout << "#" << i + 1 << endl;
cin.getline(full, len);
cin >> hand;
cin.get();
setgolf(list[i], full, hand);
while (list[i].fullname != "\0" && i < 3)
{
i++;
cout << "#" << i + 1 << endl;
cin.getline(full, len);
cin >> hand;
cin.get();
setgolf(list[i], full, hand);
}
cout << "Which member would you like to correct and how to correct?";
int n, value;
cin >> n;
cin >> value;
handicap(list[n-1], value);
cout << "This is the result after correction" << endl;
showgolf(list[n-1]);
return 0;
}
```cpp
#include<iostream>
#include<cstring>
#include "golf.h"
using namespace std;
void setgolf(golf& g, const char* name, int hc)
{
strcpy_s(g.fullname, name);
g.handicap = hc;
};
int setgolf(golf& g)
{
cin.getline(g.fullname, len);
if (g.fullname == NULL)
return 0;
cin >> g.handicap;
return 1;
}
void handicap(golf& g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf& g)
{
cout << g.fullname << endl;
cout << g.handicap << endl;
}
3.(new定位运算符)
#include<iostream>
#include<cstring>
#include<new>
using namespace std;
const int len = 20;
struct chaff
{
char doss[len];
int slag;
};
void show_data(chaff & c);
int main()
{
int a[100];
chaff* pi = new chaff[2];
chaff* pd = new(pi)chaff;
cout << "Please enter the data of #1" << endl;
char d1[len], d2[len];
int s1, s2;
cin.getline(d1, len);
cin >> s1;
cin.get();
cout << "Please enter the data of #2" << endl;
cin.getline(d2, len);
cin >> s2;
cin.get();
strcpy_s(pd[0].doss, d1);
pd[0].slag = s1;
strcpy_s(pd[1].doss, d2);
pd[1].slag = s2;
for (int i = 0; i < 2; i++)
show_data(pd[i]);
delete[]pi;
}
void show_data(chaff& c)
{
cout << c.doss << endl;
cout << c.slag << endl;
}
4.(名称空间)
#ifndef SALE_H
#define SALE_H
namespace SALES
{
const int QUARTERS = 4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
void setsales(Sales& s, const double ar[], int n);
void setsales(Sales& s);
void showsales(const Sales& s);
}
#endif
#include<iostream>
#include"sale.h"
using namespace std;
int main()
{
using SALES::Sales;
Sales a, b;
SALES::setsales(a);
cout << "Please enter the non-interactive version";
cout << "please enter the data for 4:" << endl;
double ar[4];
for (int i = 0; i < 4; i++)
{
cout << "#" << i + 1;
cin >> ar[i];
}
SALES::setsales(b, ar, 4);
cout << "We can start to show the two structures";
cout << "#a" << endl;
SALES::showsales(a);
cout << "#b" << endl;
SALES::showsales(b);
}
#include<iostream>
using namespace std;
#include"sale.h"
namespace SALES
{
void setsales(Sales& s, const double ar[], int n)
{
int m = (QUARTERS < n) ? QUARTERS : n;
double sum = 0, max = ar[0], min = ar[0];
for (int i = 0; i < m; i++)
{
s.sales[i] = ar[i];
sum += ar[i];
max = (ar[i] > max ? ar[i] : max);
min = (ar[i] < min ? ar[i] : min);
}
s.average = sum / m;
s.max = max;
s.min = min;
}
void setsales(Sales& s)
{
cout << "please enter the data for 4:" << endl;
for (int i = 0; i < 4; i++)
{
cout << "#" << i + 1;
cin >> s.sales[i];
}
double sum = 0, max = s.sales[0], min = s.sales[0];
for (int i = 0; i < 4; i++)
{
sum += s.sales[i];
max = (s.sales[i] > max ? s.sales[i] : max);
min = (s.sales[i] < min ? s.sales[i] : min);
}
s.average = sum / QUARTERS;
s.max = max;
s.min = min;
}
void showsales(const Sales& s)
{
cout << "Now we are going to show the salas;" << endl;
for (int i = 0; i < QUARTERS; i++)
{
cout << s.sales[i] << " ";
}
cout << endl;
cout << "average:" << s.average << endl;
cout << "max:" << s.max << endl;
cout << "min:" << s.min << endl;
}
}
|