其实西安交大的老师们 上课没什么趣味性,开始听着挺不习惯的。 但是老师们人真的都很好,问的问题基本都认真帮忙解答的~很热心~还是点个赞
递归还是很简单,毕竟搞了很多题目了。
内联。。讲实话 写代码的时候 基本没用过inline。。 不过可以提高程序运行效率 这主要是因为调用函数要搞个栈保存函数返回地址等等 调用结束 恢复现场 浪费空间 ,时间
而内联则是 直接把代码块复制过来 没有上述操作 所以要快一些。(不过用的很少吧? 我是基本没用过inline的。。)
函数重载。。这个真没啥好说的 注意只有返回值不一样不构成重载 就ok了吧。
作用域是没啥,基本和C一样。
C++程序运行的使用的内存区域比较有意思:
new和malloc出来的 分配在堆区。。 你不free掉 就一直驻留 在内存中 即时该变量的作用域已经结束了。
比如这个代码 输出1,2,3
int*p;
void get() {
int* a = new int[3]{1,2,3};
p = a;
//free(a);
}
int main()
{
get();
for (int i = 0; i < 3; i++)
cout << *(p+i) << " ";
cout << endl;
return 0;
}
局部变量 存放在栈区的 没啥好说的 作用域结束内存就被释放。。
全局数据区 存放全局变量和静态数据(和new,malloc出来的性质很像~)
程序代码区。。顾名思义。。
课后作业:(感觉都很简单。。非炫耀。。讲个事实)
1.编写函数求一个数组中数组元素的最大值,要求必须用递归方法解决。
#include "iostream"
#include "cstdio"
#include "cstring"
#include "queue"
using namespace std;
int getMax(int* p,int start,int end) {
if (start == end) {
return *(p + start);
}
int mid = (start + end) / 2;
return (max(getMax(p,start,mid),getMax(p, mid + 1, end)));
}
int main() {
int n;
cin >> n;
int* p = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
cin >> p[i];
}
cout << getMax(p, 0, n - 1) << endl;
return 0;
}
2.编写函数求矩形的面积和周长,由于算式非常简单,请使用内联函数方式编写,提高程序运行效率
#include "iostream"
#include "cstdio"
#include "cstring"
#include "queue"
using namespace std;
inline int Area(int w, int h) {
return w*h;
}
inline int Perimeter(int w,int h) {
return 2 * (w + h);
}
int main() {
int w, h;
cin >> w >> h;
cout << Area(w,h) << " "<<Perimeter(w,h) << endl;
return 0;
}
3.
编写重载函数来打印字符串(20分)
#include "iostream"
#include "cstdio"
#include "string"
#include "queue"
using namespace std;
void print_spaced(char s[]) {
for (int i = 0; s[i]; i++) {
if (i == 0)
cout << s[i];
else
cout << " " << s[i];
}
}
void print_spaced(string s) {
for (int i = 0; i<s.length(); i++) {
if (i == 0)
cout << s[i];
else
cout << " " << s[i];
}
}
int main() {
char* s1 = new char[101];
string s2;
cin >> s1;
cin >> s2;
print_spaced(s1);
cout << endl;
print_spaced(s2);
cout << endl;
return 0;
}
4.编写重载函数来打印字符串(20分)题目内容:
编写一组重载的排序函数,可以对两个整数、三个整数、四个整数、整数数组从大到小排序,函数名为sort,其中数组排序应使用递归的方法,另补充print函数,在一行显示排序后的数组元素。
#include "iostream"
#include "cstdio"
#include "string"
#include "queue"
using namespace std;
void sort(int &a,int &b) {
if (a < b) {
int temp = a;
a = b;
b = temp;
}
}
void sort(int &a, int &b, int &c) {
sort(a, b);
sort(a, c);
sort(b, c);
}
void sort(int &a, int &b, int &c, int &d) {
sort(a, b, c);
sort(a, b, d);
sort(a, c, d);
sort(b, c, d);
}
void merge(int* a,int left,int mid,int right) {
int* temp = new int[right-left+1];
int* p = temp;
int i, j, k;
i = left; j = mid + 1; k = 0;
while (i <= mid && j <= right) {
if (a[i] >= a[j]) {
*temp++ = a[i++];
}
else {
*temp++ = a[j++];
}
}
while (i <= mid) {
*temp++ = a[i++];
}
while (j <= right) {
*temp++ = a[j++];
}
for (i = 0, j = left; j <=right; i++, j++) {
a[j] = *(p+i);
}
free(p);
}
void mergeSort(int* a,int left,int right) {
if (left == right) {
return;
}
int mid = (left + right) / 2;
mergeSort(a, left, mid);
mergeSort(a, mid + 1, right);
merge(a,left,mid,right);
}
void sort(int* a, int n) {
mergeSort(a, 0, n-1);
}
void print(int data[],int n) {
for (int i = 0; i < n; i++) {
if (i == 0)
cout << data[i];
else
cout << " " << data[i];
}
cout << endl;
}
int main()
{
int a, b, c, d;
int data[100];
int k, n, i;
cin >> k;
switch (k)
{
case 1:
cin >> a >> b;
sort(a, b);
cout << a << " " << b << endl;
break;
case 2:
cin >> a >> b >> c;
sort(a, b, c);
cout << a << " " << b << " " << c << endl;
break;
case 3:
cin >> a >> b >> c >> d;
sort(a, b, c, d);
cout << a << " " << b << " " << c << " " << d << endl;
break;
case 4:
cin >> n;
for (i = 0; i<n; i++)
{
cin >> data[i];
}
sort(data, n);
print(data, n);
break;
}
return 0;
}
5.编写重载函数来打印字符串(20分)
题目内容:
编写函数来使一个字符串逆序输出,要求必须用递归函数。
#include "iostream"
#include "cstdio"
#include "cstring"
#include "string"
#include "queue"
using namespace std;
void Reverse(char* s,int begin,int end) {
if (begin >= end)
return;
char temp = s[begin];
s[begin] = s[end];
s[end] = temp;
Reverse(s, begin + 1, end - 1);
}
int main()
{
char s[101];
cin.getline(s,101);
Reverse(s,0,strlen(s)-1);
cout << s << endl;
return 0;
}