C++第一次实验

=====taoyyz小陶©版权所有=====

实验一 C++运行环境及基础语法

要求:

  1.熟悉C++的开发运行环境
  2.掌握C++的基本语法
  3.熟悉结构化程序设计

思路:

  全是有手就行的题

代码:

最简单的C++程序运行调试

1
2
3
4
5
6
#include <iostream>

void main()
{
std::cout<<"my first cpp program"<<std::endl;
}

结构化程序设计基础

用long long存放结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
int main1(){
long long n,fact=1;
cout<<"input a num:"; //这条语句有错,应怎么改?
cin>>n;
int i=1;
for(;i<=n;i++)
{
fact=fact*i;
}
cout<<n<<"!="<<fact<<endl;
cout<<i<<endl;
return 0;
}

用数组存放结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;
int main()
{
int n;
int arr[100];
int num = 1;
int temp;
int i, j, c;
cout << "输入一个n:";
cin >> n;
arr[0] = 1;
for (i = 2; i <= n; i++)
{
for (j = 1, c = 0; j <= num; j++)
{
temp = arr[j - 1] * i + c;
arr[j - 1] = temp % 10;
c = temp / 10;
}
while (c)
{
arr[++num - 1] = c % 10;
c = c / 10;
}
}
cout << n << "的阶乘为:";
for (j = num; j >= 1; j--)
{
cout << arr[j - 1];
}
cout << endl;
return 0;
}

引用和函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
void swap(int &a, int &b){
int t;
t=a; a=b; b=t;
}
int main(){
int a=1,b=6;
cout<<"before swap:\n"<<"a is:"<<a
<<" b is:"<<b<<endl;
swap(a,b);
cout<<"after swap:\n"<<"a is:"<<a
<<" b is:"<<b<<endl;
return 0;
}

函数的重载和默认参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
void print(float y,char sex)
{
cout << (int)(y + 0.5) << " years old," << (sex == 'M' ? "male" : "female") << endl;
}

void print(char sex,float y)
{
cout << (int)(y + 0.5) << " years old," << (sex == 'M' ? "male" : "female") << endl;
}

void print()
{
cout<<"0 years old,male"<<endl;
}

int main(){
float year; char sex;
//输入
cin>>year>>sex;
print(year,sex);
print(sex,year);
print(); //这里输出0 years old, male,即默认是0岁,male
return 0;
}

自己编写程序

从键盘上读入两数,比较两个数的大小,并按从小到大的次序输出。(要求使用变量引用)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
//(1)、从键盘上读入两数,比较两个数的大小,并按从小到大的次序输出。
//(要求使用变量引用)
using namespace std;

void input(int& x, int& y);
void compare(int& x, int& y);

int main()
{
int x, y;//利用变量引用输入x和y
input(x, y);//按照从小到大次序输出
compare(x, y);
return 0;
}

void input(int& x, int& y)
{
cout << "输入x以及y:";
cin >> x >> y;
}

void compare(int& x, int& y)
{
x < y ? cout << x << "," << y << endl : cout << y << "," << x << endl;
}

输出1到1000的素数,一行5个。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cmath>
#include <iomanip>
//(2)、输出1到1000的素数,一行5个。
using namespace std;
int main()
{
int count = 0;
bool isPrime;
for (int i = 2; i < 1000; i++) {
isPrime = true;
//对于每一个i(i>=2)先假定为素数
for (int inside = 2;
inside <= sqrt(i);
inside++) {
if (i % inside == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
cout<<setw(4)<<i;
if (++count % 5 == 0) {
//每5个数换一行
cout << endl;
}
}
}
}

编写一个C++风格的程序,解决百元问题:将一元人民币兑换成1、2、5分的硬币,有多少种换法?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
//(3)、编写一个C++风格的程序,解决百元问题,有多少种换法?
using namespace std;
int main()
{
int x, y, z; //1分、2分、5分的硬币数目分别用x、y、z表示
int sum = 0; //换法计数器
for (z = 0; z <= 20; z++)
for (y = 0; y <= 50; y++)
{
for (x = 0; x <= 100 - y - z; x++)
{
if (x + 2 * y + 5 * z == 100)
{ //cout << x << "," << y << "," << z << endl;
sum++;
}
}
}
cout << "有" << sum << "种换法" << endl;
}

编写一个程序,建立一个上sroot()的函数,返回其参数的二次方根。要求使用重载函数,重载3次,让其返回整数、长整数与双精度数的二次方根。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
//(4)、编写一个程序,建立一个上sroot()的函数,返回其参数的二次方根。
// 要求使用重载函数,重载3次,让其返回整数、长整数与双精度数的二次方根。
using namespace std;
double sroot(int num);
double sroot(long num);
double sroot(double num);

int main()
{
cout << sroot(10) << endl;
cout << sroot(20L) << endl;
cout << sroot(30.0) << endl;
}

double sroot(int num)
{
return sqrt(num);
}
double sroot(long num)
{
return sqrt(num);
}
double sroot(double num)
{
return sqrt(num);
}

写C++风格的程序,用二分法求解f(x)=0的根。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include<cmath>
//(5)、写C++风格的程序,用二分法求解f(x)=0的根。
using namespace std;
int main()
{
float x0, x1, x2, func, fx1, fx2;
do
{
cout << "输入范围:" << endl;
cin >> x1 >> x2;
fx1 = sin(x1);
fx2 = sin(x2);
} while (fx1 * fx2 > 0);
do
{
x0 = (x1 + x2) / 2;
func = sin(x0);
if ((func * fx1) < 0)
{
x2 = x0;
fx2 = func;
}
else
{
x1 = x0;
fx1 = func;
}
} while (fabs(func) >= 1e-5);
cout << "x = " << x0 << endl;
return 0;
}

编写一个程序,用动态分配空间的方法计算 Fibonacci数列的前20项并存储到动态分配的空间中。注:使用malloc和free来实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include <iomanip>
//(6)、编写一个程序,用动态分配空间的方法计算
// Fibonacci数列的前20项并存储到动态分配的空间中。
// 注:使用malloc和free来实现
using namespace std;

int main() {
int* p = (int*)malloc(sizeof(int) * 20);
*p = 0; //第一个数为0
*(p + 1) = 1; //第二个数为1
for (int i = 2; i < 20; ++i)
{
*(p + i) = *(p + i - 1) + *(p + i - 2);
}
for (int i = 0; i < 20; ++i)
{
cout << "第" <<setw(2)<< i + 1 << "项:";
cout <<setw(4)<< *(p + i)<<"\t\t";
if ((i + 1) % 2 == 0)
{
cout << endl;
}
}
cout << endl;
free(p);
return 0;
}

运行截图:

C-FirstExp C-FirstExp

心得:

main()函数的返回值用于判断程序是否正常结束,通常,返回0表示程序正常结束。
 在Visual Studio 2019环境下,main()返回值可定义为void类型,但根据C99标准,main()的标准定义格式为:
int main(void)
 或:
int main(int argc,char *argv[])
main()函数也可以省略return 0,编译器会自动调用exit(0)来析构栈变量。
using namespace std也可以不写,但是这时想要使用cout或者endl等位于std命名空间下的,如果不引入std命名空间,那么需要使用作用域运算符::来说明命名空间来源,例如:std::cout或者std::endl
 在老的C89编译器之前使用C语言的for循环,不能再for循环的表达式1位置定义变量,只能写在for循环之前,而新标准之后的C++中,可以在for循环里定义变量,此时的变量作用域在for循环内部。

感谢阅读💗