本文发布于 1297 天前,最后更新于 1172 天前,其中的信息可能已经有所发展或是发生改变。
Changelog
1.0 基本完成全部代码输入(实验7除外
1.1 小幅改进代码,优化运行速度(不是
2.0 完成所以题目并改正实验八
3.0 优化布局和观感
仅供学习交流,严禁用于商业用途,请于24小时内删除
实验一 简单C++程序设计
实验学时:2学时
实验类型:验证
实验要求:必修
一、实验题目
1.编写程序,计算圆面积,半径从键盘输入。注意,输人数据前要有提示信息,便于操作。
二、程序
#include <bits/stdc++.h>
#define PI 3.1415926
using namespace std;
int main(){
double r, s;
cout << "半径 r=";
cin >> r;
s = PI * r * r;
printf ("面积 s= %.2lf", s);
}
三、运行结果
一、实验题目
2.编写程序,已知三角形的三边 a,b,c,求三角形的周长和面积。要求从键盘输入三角形的三边,输入前有提示信息,便于操作。
三角形的周长:s=a+b+c
三角形的面积:area=((s/2)(s/2-a)(s/2-b)(s/2-c))^(1/2)
二、程序
#include <bits/stdc++.h>
using namespace std;
int main() {
double s, area, a, b, c;
cout << "a= ", cin >> a;
cout << "b= ", cin >> b;
cout << "c= ", cin >> c;
s = a + b + c;
area = sqrt((s / 2) * (s / 2 - a) * (s / 2 - b) * (s / 2 - c));
cout << "s= " << s << endl;
cout << "area= " << area;
}
三、运行结果
一、实验题目
要求设计一个函数,求出一元二次方程 ax^2+bx+c=0 的根,系数 a,b,c 的值从键盘上输入,求方程根的计算公式为 x=(-b±(b^2-4ac)^(1/2))/2a,当 b^2-4ac 小于 0 时,输出“无实根!”;否则输出 x 的两个实根。
二、程序
#include "bits/stdc++.h"
using namespace std;
void Soildroot(double a, double b, double c) {
double x1, x2;
if ((pow(b, 2) - 4 * a * c) < 0) {
cout << "无实根";
return;
}
x1 = (-b + sqrt((b * b) - 4 * a * c)) / 2 * a;
x2 = (-b - sqrt((b * b) - 4 * a * c)) / 2 * a;
printf("x1= %.2lf x2= %.2lf", x1, x2);
}
int main() {
double a, b, c;
cout << "a= ", cin >> a;
cout << "b= ", cin >> b;
cout << "c= ", cin >> c;
Soildroot(a, b, c);
}
三、运行结果
实验二 循环结构程序设计
实验学时:2学时
实验类型:验证
实验要求:必修
一、实验题目
1.编写一个程序,输入一个整数,求出该整数的各位数字之和。例如,输入 1234,输出 10。
二、程序
#include <bits/stdc++.h>
using namespace std;
int main() {
int s = 0, a, n = 0;
cout << "a= ";
cin >> a;
while (a) {
n = a % 10;
s += n;
a /= 10;
}
cout << "s= " << s;
}
三、运行结果
一、实验题目
2.编写程序,求 100~200 内的所有素数
二、程序
#include <bits/stdc++.h>
using namespace std;
bool prime_num(int num) {
for (int i = 2; i * i <= num; i++) {
if (num % i ==0) return false;
}
return true;
}
int main() {
int i = 100;
while (i <= 200) {
if (prime_num(i))
cout << i << " ";
i++;
}
}
三、运行结果
一、实验题目
3.编写程序计算下列公式的值:y=x-x^3/3!+x^5/5!-x^7/7!+…,精确到第n项,其中n=10,x=2.5分别由键盘输入(答案:0.598472)
二、程序
#include <bits/stdc++.h>
using namespace std;
int main() {
double x, y, t;
int n;
cout << "x= ", cin >> x;
cout << "n= ", cin >> n;
y = x, t = x;
for (int i = 2; i <= n; i++) {
t = -t * x * x / ((2.0 * i - 1) * (2 * i - 2));
y += t;
}
cout << "y= " << y;
}
三、运行结果
实验三 数组的定义和使用
实验学时:2学时
实验类型:验证
实验要求:必修
一、实验题目
1.编写程序用筛选法求100之内的素数。
二、程序
// Created by liuruofei20030120 on 2021/12/16.
#include <bits/stdc++.h>
auto eratosthenes(int upperbound) {
std::vector<bool> flag(upperbound + 1, true);
flag[0] = flag[1] = false; //exclude 0 and 1
for (int i = 2; i * i <= upperbound; ++i) {
if (flag[i]) {
for (int j = i * i; j <= upperbound; j += i)
flag[j] = false;
}
}
return flag;
}
int main(){
std::vector<bool> flag = eratosthenes(100);
for(int i=1;i<=100;i++)
{
if(flag[i])
std::cout << i << " ";
}
}
三、运行结果
一、实验题目
2.编写程序,输出以下的杨辉三角形(输出 10 行)。
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
二、程序
#include "bits/stdc++.h"
using namespace std;
int num[11];
int tnum[11];
int main() {
int i = 1;
tnum[1] = 1;
num[1] = 1;
while (i < 12) {
int ii = 1;
while (num[ii]&&i!=1) {
cout << num[ii] << " ";
ii++;
}
cout << endl;
memcpy(tnum, num, sizeof(int) * 10);
for (int iii = 1; iii <= i; iii++) {
num[iii] = tnum[iii - 1] + tnum[iii];
}
i++;
}
}
三、运行结果
一、实验题目
3.假定在一个整数数组中,每一个元素都是不超过两位数的正整数,试编程统计该数组全部元素中数字0,1,2,3,…,9各出现多少次。
要求:
采用下列方法输入数据。
int a[]={5, 26, 98, 77,1, 27, 30};
输出结果:
0~9依次出现1 1 2 1 0 1 1 3 1 1次
二、程序
#include "bits/stdc++.h"
using namespace std;
int num[11];
int main(){
char a;
a = getchar();
while(a!=';'){
if(a<='9'&&a>='0'){
num[a-'0']++;
}
a = getchar();
}
cout << "0~9 依次出现";
for(int i;i<10;i++){
cout << num[i]<< " ";
}
cout << "次";
return 0;
}
三、运行结果
实验四 函数的定义和调用
实验学时:2学时
实验类型:验证
实验要求:必修
一、实验题目
用递归调用的方法编写,用牛顿迭代法求f(x)=0的一个实根。其中,f(x)=x^2+3x-4,迭代公式为x1=x0-f(x0)/f'(x0),x0的初值是0,当|f(x)|<10^(-5)时迭代结束(答案:1)
二、程序
#include <bits/stdc++.h>
using namespace std;
float f(float x){
return x * x + 3 * x - 4;
}
float f1(float x){
return 2 * x + 3;
}
float ff(float x) {
float y;
if (fabs(f(x)) < 1e-5)
y = x;
else
y = ff(x - f(x) / f1(x));
return y;
}
int main() {
float x1;
x1 = 0;
cout << "x=" << ff(x1) << endl;
}
三、运行结果
实验五 函数参数传递机制
实验学时:2学时
实验类型:验证
实验要求:必修
一、实验题目
编写一个函数int fun(char * str,char * substr),该函数可以统计子字符串substr在母字符串str中出现的次数,并将该次数作为函数值返回。例如,输入的母字符串为asd assasdfg asd as zx67,输入的子字符串为as,则as出现在母串中的次数应为5。要求在主函数中分别输入母串和子串,并将它们作为实际参数调用函数fun,同时将结果在主函数中输出。
二、程序
#include "bits/stdc++.h"
using namespace std;
int fun(string str, string substr) {
int ans = 0;
for (int i = 0; i < str.length(); i++)
if (str[i] == substr[0]) {
int num = 1;
while (str[i + num] == substr[num]) {
num++;
if(num == substr.length()) ans++;
}
}
return ans;
}
int main() {
string str;
string substr;
getline(cin, str);
getline(cin, substr);
int ans = fun(str, substr);
cout << ans;
}
三、运行结果
实验六 类和对象的定义与访问
实验学时:2学时
实验类型:验证
实验要求:必修
一、实验题目
试定义一个类 STR,将一个字符串 中指定位置的连续字符拼接到另一个字符串的尾部,具体要求如下:(1)私有数据成员。char*p;存放一个字符串。(2)公有成员函数。STR(char*s):构造函数。为数据成员p动态分配空间,并利用参数s初始化数据成员p.void fun(char * s,int nl, int n2): 将字符串 s从第n1(从0开始计数)个字符开始的连续n2个字符拼接到数据成员p所指向的字符串之后,形成一个新的字符串。注意,必须为p重新分配内存空间。void print():按输出示例格式输出数据成员p.~STR():析构函数,释放动态内存。(3)在主函数中完成对该类的测试。输入输出示例(下画线部分为键盘输入):字符串1:abcdefg字符串2:12345678输入起始的位置和字符个数:2 5处理后的字符串1为:abcdefg34567
二、程序
//STR.h
// Created by liuruofei20030120 on 2021/12/15.
#ifndef C_PLUS_STR_H
#define C_PLUS_STR_H
class STR {
char *p = nullptr;
public:
STR(char *s);
void fun(char *s,int n1,int n2);
void print();
~STR();
};
#endif//C_PLUS_STR_H
//STR.cpp
#include "STR.h"
#include <bits/stdc++.h>
STR::STR(char *s) {
int len = strlen(s);
this->p = new char[len + 1000];
for (int i = 0; i < len; i++) {
p[i] = s[i];
}
}
void STR::fun(char *s, int st, int len) {
int llen = strlen(p);
for (int i = 0; i < len; i++) {
p[llen + i] = s[st + i];
}
}
void STR::print() {
std::cout << "处理后的字符串1为";
std::cout << p;
}
STR::~STR() {
delete p;
p = nullptr;
}
//main.cpp
#include "bits/stdc++.h"
using namespace std;
#include "STR.h"
int main() {
char a[1000];
char b[1000];
cout << "字符串 1 ";
cin >> a;
cout << "字符串 2 ";
cin >> b;
STR aa = STR(a);
STR bb = STR(b);
cout << "输入起始位置和字符个数: ";
int c, d;
cin >> c >> d;
aa.fun(b, c, d);
cout << "处理后的字符串1为:" ;
aa.print();
}
三、运行结果
实验七 继承、派生和多态性
实验学时:2学时
实验类型:验证
实验要求:必修
一、实验题目
定义一个圆类Circle,具体要求如下:
(1)私有数据成员。
double radius:存放圆的半径
(2)公有成员函数。
Circle(double r):构造函数,设置圆的半径。
double area():获取圆的面积。
double perimeter():获取圆的周长。
定义一个圆柱类Cylinder,为圆类的派生类,具体要求如下:
(1)私有数据成员。
double height: 存放圆柱的高。
(2)公有成员函数。
Cylinder(double r, double h):构造函数,设置圆柱的半径和高。
double volume():获取圆柱的体积。
double surface():获取圆柱的表面积。
(3)在主函数中完成对该类的测试。
输入输出示例:
输入圆柱半径:3
输入圆柱高:5
输出圆柱表面积、体积:141.372 150.796
二、程序
//Circle.h
// Created by liuruofei20030120 on 2021/12/16.
#ifndef TEMP_CIRCLE_H
#define TEMP_CIRCLE_H
class Circle {
double radius;
public:
explicit Circle(double r);
[[nodiscard]] double area() const;
[[nodiscard]] double perimeter() const;
};
#endif//TEMP_CIRCLE_H
//Circle.cpp
#define Pi 3.1415926
#include "Circle.h"
Circle::Circle(double r) {
radius = r;
}
double Circle::area() const {
return radius * radius * Pi;
}
double Circle::perimeter() const {
return radius * 2.0 * Pi;
}
//Cylinder.h
#ifndef TEMP_CYLINDER_H
#define TEMP_CYLINDER_H
#include "Circle.h"
class Cylinder : public Circle {
double height;
public:
Cylinder(double r, double h);
double volume();
double surface();
};
#endif//TEMP_CYLINDER_H
//Cylinder.cpp
#include "Cylinder.h"
Cylinder::Cylinder(double r, double h) : Circle(r) {
height = h;
}
double Cylinder::volume() {
return height * area();
}
double Cylinder::surface() {
return height * perimeter() + 2.0 * area();
}
//main.cpp
#include "Cylinder.h"
#include <bits/stdc++.h>
int main() {
double r, h;
std::cout << "输入圆柱半径: ",scanf("%lf", &r);
std::cout << "输入圆柱高: ",scanf("%lf", &h);
Cylinder c = Cylinder(r, h);
printf("输出圆柱体积、表面积: %.3lf %.3lf", c.volume(), c.surface());
return 0;
}
三、运行结果
实验八 文件操作
实验学时:2学时
实验类型:验证
实验要求:必修
一、实验题目
统计“input.txt”文件中,单词个数。
二、程序
#include <bits/stdc++.h>
using namespace std;
map<string, int> IdNum;
string str;
string tstr[1008611];
int num = 0;
int ans = 0;
int main() {
freopen("/home/liuruofei20030120/C-Plus-Project/C_Plus/input.txt", "r", stdin);
while (cin >> str) {
// cout << str << " "; // 注释掉的代码是 “统计“input.txt”文件中,每个单词出现的次数” 题目的代码
// IdNum[str]++;
// tstr[num++] = str;
ans++;
}
// while (num--) {
// cout << tstr[num] << " " << IdNum[tstr[num]] << endl;
// }
cout << ans;
}
三、运行结果
实验八 (可能是题目错误的版本)
实验学时:2学时
实验类型:验证
实验要求:必修
一、实验题目
统计“input.txt”文件中,每个单词出现的次数。
二、程序
#include <bits/stdc++.h>
using namespace std;
map<string, int> IdNum;
string str;
string tstr[1008611];
int num = 0;
int main() {
freopen(“/home/liuruofei20030120/C-Plus-Project/C_Plus/input.txt”, “r”, stdin);
while (cin >> str) {
cout << str;
IdNum[str]++;
tstr[num++] = str;
}
while (num–) {
cout << tstr[num] << ” ” << IdNum[tstr[num]] << endl;
}
}
三、运行结果
总结与反馈
作为一个学过的感觉讲的很通透,有一些以前没注意到的细节都补充了,不过感觉后期题目练习不足,感觉前期稍快后期放慢会更好