个人练习,仅供参考,欢迎大家提出意见,指出错误!喜欢的点个赞吧!

Practice 1

//用户输入两个整数,求出两个整数之间(包括这两个数)的所有整数的和

#include <iostream>

int main()
{
  using namespace std;
  int min = 0;
  int max = 0;
  int sum = 0;
  cout << "Enter min: " << endl;
  cin >> min;
  cout << "Enter max: " << endl;
  cin >> max;

  if (min > max) {
    int tmp = max;
    max = min;
    min = tmp;
  }

  for (int i = min; i <= max; i++) {
    sum += i;
  }

  cout << "Sum is: " << sum << endl;
  return 0;

}

Practice 2

//使用 array 和 long double 实现100的阶乘

#include <iostream>
#include <array>

int main()
{
  using namespace std;
  const int arr_size = 101;
  array<long double, arr_size> factorials;   
  // array 声明中的 arr_size 必须是常量
  factorials[0] = 1;
  factorials[1] = 1;

  for (int i = 2; i < arr_size; i++) {
    factorials[i] = i * factorials[i - 1];
  }

  for (int i = 0; i < arr_size; i++) {
    cout << factorials[i] << endl;
  }
  return 0;
}

Practice 3

//即时输出用户输入的所有数的和,输入 0 时结束

#include <iostream>

int main()
{
  using namespace std;
  double tmp;
  double sum = 0;

  cin >> tmp;

  while (tmp) {
    sum += tmp;
    cout << "Now sum is " << sum << endl;
    cin >> tmp;
  }
}

Practice 4

//计算单利和复利

#include <iostream>

int main()
{
  using namespace std;
  double daphne = 100;
  double cleo = 100;
  double daphne_profit = 10;
  double cleo_profit = 0.05;
  int year = 1;

  while (daphne >= cleo) {
    daphne += daphne_profit;
    cleo = cleo + cleo * cleo_profit;
    year ++;
  }
  cout << "Year: " << year << endl;
  cout << "Daphne: " << daphne << endl;
  cout << "Cleo: " << cleo << endl;
  return 0;
}

Practice 5

// 按月输入销量,并计算总数
#include <iostream>
#include <string>
#include <sstream>

int main()
{
  using namespace std;

  const int arr_size = 12;
  stringstream* months = new stringstream [arr_size];
  string di = "Sale num of ";
  string xl = " month";
  int sales[arr_size];

  for (int i = 0; i < arr_size; i++) {
    months[i] << di << (i + 1) << xl;
    cout << months[i].str() << endl;
    cin >> sales[i];
  }

  //字符串拼接时出现了很多问题: 
  //首先是使用 to_string() 编译报错
  //其次采用 stringstream 实现时不能直接使用字符串常量进行拼接, 一定要先声明并初始化 string 类型的字符串
  //关于字符串拼接可参考 https://blog.csdn.net/PROGRAM_anywhere/article/details/63720261

  int sum = 0;

  for (int i = 0; i < arr_size; i ++) {
    sum += sales[i];
    cout << months[i].str() << "is " << sales[i] << endl;
  }

  cout << "Sum is " << sum << endl;
  return 0;

}

Practice 6

//使用二维数组

#include <iostream>

int main()
{
  using namespace std;
  int sales[3][12] = {
    {1, 3, 4, 5, 7, 7, 7, 7, 7, 10, 11, 12},
    {1, 3, 4, 5, 7, 7, 7, 7, 7, 10, 11, 13},
    {1, 3, 4, 5, 7, 7, 7, 7, 7, 10, 11, 14},
  };

  int sum_by_year[3] = {0, 0, 0};
  int sum_all = 0;

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 12; j++) {
      sum_by_year[i] += sales[i][j];
    }
    cout << "Sum by year: " << sum_by_year[i] << endl;
    sum_all += sum_by_year[i];
  }
  cout << "Sum all: " << sum_all << endl;
  return 0;

}

Practice 7

//结构数组的创建和输入输出

#include <iostream>
#include <string>

using namespace std;

struct car
{
  string band;
  int year;
};

int main()
{
  int arr_size = 0;
  cout << "Enter numbers: " << endl;
  (cin >> arr_size).get();    //此处不加 get() 会影响下面第一次输入
  car* my_cars = new car [arr_size];

  for (int i = 0; i < arr_size; i++) {
    cout << "Enter band: " << endl;
    getline(cin, my_cars[i].band);
    cout << "Enter year: " << endl;
    (cin >> my_cars[i].year).get();
  }

  cout << "You have entered: " << endl;
  for (int i = 0; i < arr_size; i++) {
    cout << my_cars[i].band << " " << my_cars[i].year << endl;
  }
  return 0;

}

Practice 8

//输入单词,以 done 结束

#include <iostream>
#include <cstring>

int main()
{
  using namespace std;
  const int arr_size = 100;
  char words[100];
  int num = 0;

  cin >> words;

  while (strcmp(words, "done") != 0) {
    num ++;
    cin >> words;
  }

  cout << "Words: " << num << endl;
  return 0;
}

Practice 9

//使用 string 实现上题

#include <iostream>
#include <string>

int main()
{
  using namespace std;
  string words;
  int num = 0;

  cin >> words;

  while (words != "done") {
    num ++;
    cin >> words;
  }

  cout << "Words: " << num << endl;
  return 0;
}

Practice 10

//根据输入数字来输出星号

#include <iostream>

int main()
{
  using namespace std;
  int lines = 0;

  cout << "Enter lines: " << endl;
  cin >> lines;

  for (int i = 1; i <= lines; i++) {
    for (int j = 0; j < lines - i; j++) {
      cout << ".";
    }
    for (int k = 0; k < i; k++){
      cout << "*";
    }
    cout << endl;
  }
  return 0;
}

欢迎访问 Github 仓库地址


CheeReus_11
140 声望5 粉丝

学会爱人,成为大人