日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++基础学习(04)--(函数、数字、数组、字符串)

發(fā)布時間:2023/12/13 c/c++ 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++基础学习(04)--(函数、数字、数组、字符串) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 目錄
    • 1.函數
    • 2.數字
    • 3.字符串
    • 4.數組

目錄

1.函數




#include <iostream> #include <limits>using namespace std;void swap(int *x , int *y);int main(){int a = 100 , b=200;cout<<"交換前:"<<"a is :"<<a<<"\t"<<"b is :"<<b<<endl;swap(&a , &b);cout<<"交換后:"<<"a is :"<<a<<"\t"<<"b is :"<<b<<endl;return 0; }void swap(int *x , int *y){int temp = 0;temp = *y;*y = *x;*x = temp; }

#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.1812
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 10int 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> #include <cstring>using namespace std;int main () {char str1[11] = "Hello";char str2[11] = "World";char str3[11];int len ;// 復制 str1 到 str3strcpy( str3, str1);cout << "strcpy( str3, str1) : " << str3 << endl;// 連接 str1 和 str2strcat( 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 到 str3str3 = str1;cout << "str3 : " << str3 << endl;// 連接 str1 和 str2str3 = 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

4.數組


#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; }




總結

以上是生活随笔為你收集整理的c++基础学习(04)--(函数、数字、数组、字符串)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。