CPP学习笔记(自用) - 第八章编程题

2023/9/10

第八章编程题

1.

void show(const char *h, int n = 0)
{
  static int cnt = 0; // 静态声明,不会重新定义
  cnt++;
  if (n == 0)
    cout << h << endl;
  else
  {
    for (int i = 1; i <= cnt; i++)
      cout << h << endl;
  }
}

2.

typedef struct CandyBar
{
  char name[40];
  double weight;
  int heat;
} CandyBar;
void set(CandyBar &c, const char *n = "Millennium", const double w = 2.85, const int h = 350);
void set(CandyBar &c, const char *n, const double w, const int h)
{
  strcpy(c.name, n); // 用字符串拷贝
  c.weight = w;
  c.heat = h;
  return;
}
void show(const CandyBar &c)
{
  cout << "name: " << c.name << endl
       << "weight: " << c.weight << endl
       << "heat: " << c.heat << endl;
  return;
}

3.

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
string str;
static int cnt = 0;
bool f = true;
void UpperStr(string &str);
int main()
{
  while (f)
  {
    if (cnt == 0)
      cout << "Enter a string(q to quit):";
    else
      cout << "Next string(q to quit):";
    getline(cin, str);
    cnt++;
    UpperStr(str);
  }
}
void UpperStr(string &str)
{
  if (str == "q")
  {
    cout << "Bye";
    f = false;
  }
  else
    for (unsigned int i = 0; i < str.size(); i++)
      cout << (char)toupper(str[i]);
  cout << endl;
  return;
}

4.

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
struct stringy
{
  char *str;
  int ct;
};
void set(stringy &s, char *c);
void show(stringy &s, int i = 1);
void show(char *c, int i = 1);

int main()
{
  stringy beany;
  char testing[] = "Reality isn't what it used to be.";
  set(beany, testing);
  show(beany);
  show(beany, 2);
  testing[0] = 'D';
  testing[1] = 'u';
  show(testing);
  show(testing, 3);
  show("Done!");
  return 0;
}
void set(stringy &s, char *c)
{
  s.str = new char[strlen(c) + 1]; // 用new开辟空间
  strcpy(s.str, c);
}
void show(stringy &s, int i)
{
  for (int j = 1; j <= i; j++)
    cout << s.str << endl;
}
void show(char *c, int i)
{
  for (int j = 1; j <= i; j++)
    cout << c << endl;
}

5.

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <class T>
T max5(T a[]);
int main()
{
  int a[5] = {4, 2, 9, 3, 1};
  double b[5] = {14.32, 52.1, 9.5, 31.5, 98.24};
  cout << "int arr:" << max5(a) << endl;
  cout << "double arr:" << max5(b) << endl;
  return 0;
}

template <class T>
T max5(T a[])
{
  T max = a[0];
  for (int i = 1; i < 5; i++)
    if (a[i] > max)
      max = a[i];
  return max;
}

6.

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <class T>
T maxn(T a[], int n);
char *maxn(char *[], int n);
int main()
{
  int a[6] = {4, 2, 9, 3, 1, 7};
  double b[4] = {14.32, 52.1, 31.5, 98.24};
  char *c[5] = {"asdasdasdasdd", "asdsaadavc", "1145141919810", "safeeww", "mizuki"};
  cout << "int arr:" << maxn(a, 6) << endl;
  cout << "double arr:" << maxn(b, 4) << endl;
  cout << "char* arr:" << maxn(c, 5) << endl;
  return 0;
}

template <class T>
T maxn(T a[], int n)
{
  T max = a[0];
  for (int i = 1; i < n; i++)
    if (a[i] > max)
      max = a[i];
  return max;
}
char *maxn(char *c[], int n)
{
  int max = 0;
  int i;
  for (i = 1; i < n; i++)
  {
    if (strlen(c[i]) > strlen(c[max]))
      max = i;
  }
  return c[max];
}

关于第六题warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]的解决:可以把char*全改为string

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值