您的位置:首页 > 编程语言 > C语言/C++

C++简单学习(Part1_lecture 4)(函数、数字、数组、字符串)

2018-01-30 09:27 477 查看

Part 1 基本语法和使用

Lecture 4 函数、数字、数组、字符串

目录

Part 1 基本语法和使用

Lecture 4 函数数字数组字符串

目录
C 函数

数字

数组

字符串

1 C++ 函数











#include <iostream>
using namespace std;

// 函数声明
void swap(int *x, int *y);

int main ()
{
// 局部变量声明
int a = 100;
int b = 200;

cout << "交换前,a 的值:" << a << endl;
cout << "交换前,b 的值:" << b << endl;

/* 调用函数来交换值
* &a 表示指向 a 的指针,即变量 a 的地址
* &b 表示指向 b 的指针,即变量 b 的地址
*/
swap(&a, &b);

cout << "交换后,a 的值:" << a << endl;
cout << "交换后,b 的值:" << b << endl;

return 0;
}

// 函数定义
void swap(int *x, int *y)
{
int temp;
temp = *x;    /* 保存地址 x 的值 */
*x = *y;        /* 把 y 赋值给 x */
*y = temp;    /* 把 x 赋值给 y */

return;
}






#include <iostream>
using namespace std;

// 函数声明
void swap(int &x, int &y);

int main ()
{
// 局部变量声明
int a = 100;
int b = 200;

cout << "交换前,a 的值:" << a << endl;
cout << "交换前,b 的值:" << b << endl;

/* 调用函数来交换值 */
swap(a, b);

cout << "交换后,a 的值:" << a << endl;
cout << "交换后,b 的值:" << b << endl;

return 0;
}
// 函数定义
void swap(int &x, int &y)
{
int temp;
temp = x; /* 保存地址 x 的值 */
x = y;    /* 把 y 赋值给 x */
y = temp; /* 把 x 赋值给 y  */

return;
}




总结

函数传递形式参数时,指针调用,引用调用,是分别把参数的(地址、引用)复制给形式参数,该(地址、引用)用于访问调用中要用到的实际参数,修改形式参数会影响实际参数。



#include <iostream>
using namespace std;

int sum(int a, int b=20)
{
int result;

result = a + b;

return (result);
}

int main ()
{
// 局部变量声明
int a = 100;
int b = 200;
int result;

// 调用函数来添加值
result = sum(a, b);
cout << "Total value is :" << result << endl;

// 再次调用函数
result = sum(a);
cout << "Total value is :" << result << endl;

return 0;
}


运行结果:

Total value is :300

Total value is :120











2 数字

通常,当我们需要用到数字时,我们会使用原始的数据类型,如 int、short、long、float 和 double 等等。这些用于数字的数据类型,其可能的值和数值范围,我们已经在 C++ 数据类型一章中讨论过。



#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
// 数字定义
short  s = 10;
int    i = -1000;
long   l = 100000;
float  f = 230.47;
double d = 200.374;

// 数学运算
cout << "sin(d) :" << sin(d) << endl;
cout << "abs(i)  :" << abs(i) << endl;
cout << "floor(d) :" << floor(d) << endl;
cout << "sqrt(f) :" << sqrt(f) << endl;
cout << "pow( d, 2) :" << pow(d, 2) << endl;

return 0;
}


运行结果:

sin(d) :-0.634939

abs(i) :1000

floor(d) :200

sqrt(f) :15.181
f6e8
2

pow( d, 2 ) :40149.7



#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main ()
{
int i,j;

// 设置种子
srand( (unsigned)time( NULL ) );

/* 生成 10 个随机数 */
for( i = 0; i < 10; i++ )
{
// 生成实际的随机数
j= rand();
cout <<"随机数: " << j << endl;
}

return 0;
}


运行结果:

随机数: 1748144778

随机数: 630873888

随机数: 2134540646

随机数: 219404170

随机数: 902129458

随机数: 920445370

随机数: 1319072661

随机数: 257938873

随机数: 1256201101

随机数: 580322989



#include <stdlib.h>
#include <stdio.h>
#include <time.h> /*用到了time函数,所以要有这个头文件*/
#define MAX 10

int main( void)
{
int number[MAX] = {0};
int i;
srand((unsigned) time(NULL)); /*播种子*/
for(i = 0; i < MAX; i++)
{
number[i] = rand() % 100; /*产生100以内的随机整数*/
printf("%d ", number[i]);
}
printf("\n");
return 0;
}


运行结果:

30 9 83 1 67 61 72 64 88 97

3 数组





#include <iostream>
using namespace std;

#include <iomanip>
using std::setw;

int main ()
{
int n[ 10 ]; // n 是一个包含 10 个整数的数组

// 初始化数组元素
for ( int i = 0; i < 10; i++ )
{
n[ i ] = i + 100; // 设置元素 i 为 i + 100
}
cout << "Element" << setw( 13 ) << "Value" << endl;

// 输出数组中每个元素的值
for ( int j = 0; j < 10; j++ )
{
cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
}

return 0;
}










#include <iostream>
using namespace std;

int main ()
{
// 带有 5 个元素的双精度浮点型数组
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p;

p = balance;

// 输出数组中每个元素的值
cout << "使用指针的数组值 " << endl;
for ( int i = 0; i < 5; i++ )
{
cout << "*(p + " << i << ") : ";
cout << *(p + i) << endl;
}

cout << "使用 balance 作为地址的数组值 " << endl;
for ( int i = 0; i < 5; i++ )
{
cout << "*(balance + " << i << ") : ";
cout << *(balance + i) << endl;
}

return 0;
}










4 字符串



#include <iostream>

using namespace std;

int main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

cout << "Greeting message: ";
cout << greeting << endl;

return 0;
}


Greeting message: Hello



#include <iostream>
#include <cstring>

using namespace std;

int main ()
{
char str1[11] = "Hello";
char str2[11] = "World";
char str3[11];
int  len ;

// 复制 str1 到 str3
strcpy( str3, str1);
cout << "strcpy( str3, str1) : " << str3 << endl;

// 连接 str1 和 str2
strcat( str1, str2);
cout << "strcat( str1, str2): " << str1 << endl;

// 连接后,str1 的总长度
len = strlen(str1);
cout << "strlen(str1) : " << len << endl;

return 0;
}


strcpy( str3, str1) : Hello

strcat( str1, str2): HelloWorld

strlen(str1) : 10



#include <iostream>
#include <string>

using namespace std;

int main ()
{
string str1 = "Hello";
string str2 = "World";
string str3;
int  len ;

// 复制 str1 到 str3
str3 = str1;
cout << "str3 : " << str3 << endl;

// 连接 str1 和 str2
str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;

// 连接后,str3 的总长度
len = str3.size();
cout << "str3.size() :  " << len << endl;

return 0;
}


str3 : Hello

str1 + str2 : HelloWorld

str3.size() : 10



#include <iostream>
#include <string>
using namespace std;

int main()
{
//定义一个string类对象
string http = "www.runoob.com";

//打印字符串长度
cout<<http.length()<<endl;

//拼接
http.append("/C++");
cout<<http<<endl; //打印结果为:www.runoob.com/C++

//删除
int pos = http.find("/C++"); //查找"C++"在字符串中的位置
cout<<pos<<endl;
http.replace(pos, 4, "");   //从位置pos开始,之后的4个字符替换为空,即删除
cout<<http<<endl;

//找子串runoob
int first = http.find_first_of("."); //从头开始寻找字符'.'的位置
int last = http.find_last_of(".");   //从尾开始寻找字符'.'的位置
cout<<http.substr(first+1, last-first-1)<<endl; //提取"runoob"子串并打印

return 0;
}


运行结果:

14

www.runoob.com/C++

14

www.runoob.com

runoob



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 函数
相关文章推荐