苏嵌学习日志2

学习日志

姓名:于慧 日期:2018.7.10

 

 

今日学习任务

 

复习C语言知识点:

1. 数组

2. 函数

 

今日任务完成情况

 

(详细说明本日任务是否按计划完成,开发的代码量)

1.数组初始化举例:

#include <stdio.h>

int b[10]; //未初始化的全局变量都是0

int main()

{

// int a[5]//未初始化的局部变量 是随机值

// int a[5] ={1,2,3,4,5};//全部元素初始化

// int a[5] ={1,2};//部分元素初始化

// int a[] = {1,2,3}; //数组长度是3

int a[10]= {0};//对第一个元素初始化为0,其它未初始化元素都为0

int i;

for(i=0; i< sizeof(a) /sizeof(a[0]); i++)

{

printf("%d", a[i]);

}

printf("\n");

while(1);

return o;

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2.数组地址

printf("%p\n",&a[0]);//数组首元素地址

printf("%p\n",a);//数组名,同时也是数组首元素地址

printf("%p\n",&a); //数组的地址

    

printf("%p\n",a+1);//步长不一样 代表一个元素

printf("%p\n",&a+1);//代表一个数组 4*10 字节

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

3. 冒泡排序基本代码

#include <stdio.h>

#define SIZE 10

int main()

{

int a[SIZE]= {0}

int i,j,tmp;

for(i =0; i <SIZE; i++)

{

scanf("%d", &a[i]);

}

for(i=0; i < SIZE-1; i++)

{

for(j=0; j < SIZE-i-1; j++)

{

if(a[j] > a[j+1])

{

tmp = a[j];

    a[j] = a[j+1];

    a[j+1] = tmp;

}

}

}

for(i=0; i< SIZE; i++)

{

printf("%d", &a[i]);

}

printf("\n");

while (1);

return 0;

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

4. 用数组输出字符

#include <stdio.h>

int main()

{

int i;

// char a[10] = { 'h','e','l','l','o','w','o','r','l','d'}

char a[20] = "helloworld";

a[10]= '!';

for(i=0; i<11; i++)

{

printf("%c",a[i]);

}

printf("\n");

while(1);

return o;

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

5. 简单函数举例

#include<stdio.h>

void print() //没有返回值,没有形参

{

printf("helloworld!\n");

}

 

int main()

{

print();//自定义函数

while(1);

return 0;

}

5.int get_number() //有一个整形的返回值,没有形参

{

return 2;

}

int add(int x, int y)//形参个数和实参保持一致,类型一致,名字可以不同

{

int result;

result = x + y;

return result;

}

int main()

{

int num, a=1,b=2;

print();

num= get_number(); //定义一个变量接返回值

printf("num = %d\n", num);

num = add(a,b); //ab实参

printf("num= %d\n",num);

printf("add: %p\n",add);

while(1);

return 0;

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

6. 将冒泡排序用函数表示:

#include <stdio.h>

#define SIZE 10

void GetArray(int a[])

{

int i;

for(i =0; i <SIZE; i++)

{

scanf("%d", &a[i]);

}

}

 

void sort(int a[])

{

int i,j,tmp;

for(i=0; i < SIZE-1; i++)

{

for(j=0; j < SIZE-i-1; j++)

{

if(a[j] > a[j+1])

{

tmp = a[j];

    a[j] = a[j+1];

    a[j+1] = tmp;

}

}

}

}

 

void print(int a[])

{

int i;

for(i=0; i< SIZE; i++)

{

printf("%d", &a[i]);

}

printf("\n");

}

 

int main()

{

int a[SIZE]= {0}

    GetArray(a);

    sort(a);

print(a);

 

while (1);

return 0;

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

7. 地址传递(用法及原因)

#include <stdio.h>

void swap(int *x, int *y)

{

int tmp;

tmp = *x;

*x = *y;

*y = tmp;

}

 

int main()

{

int a =1,b=2;

 

swap( &a, &b);// 地址传递(当涉及修改内存里面的值时)

printf("a = %d,b = %d\n",a,b);

while(1);

return 0;

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

8. extern :声明一个外部变量

在一文件中使用另一个文件的变量:

#include <stdio.h>

int num =100;//定义一个全局变量

void print()

int main

{

print();

while(1);

return 0;

}

 

 

#include <stdio.h>

extern int num;//声明一个外部变量

            //声明不需要分配空间  定义需要分配空间

void print()

{

print("num =%d\n",nuum);

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

9. Static

1) static int num =100 // 修饰全局变量,改变变量的作用域,只能在当前文件中被使用

2) static print(); //  修饰函数,改变函数的作用域,只能在当前文件中被使用

3) 修饰局部变量,改变变量的生命周期直到程序运行结束才被释放

(原因:存放的地方不一样 不加static修饰(普通局部变量)存放在栈(内存的一种)上面; 加上static修饰(静态变量),存放在数据段)

#include <stdio.h>

 

void add()

{

static int num =0; //修饰局部变量,改变变量的生命周期直到程序运行结束才被释放

/*没有static时,输出为1,加上static后输出为12345*/

num++;

printf("num= %d\n",num);

}

 

int main()

{

int i;

 

for(i=0; i<5; i++)

{

add();

}

while(1);

return 0;

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

10. 宏定义

// 宏函数(有参 无参)

#define PRI printf("hellowodld!\n");

#define PRINT(S) PRINTF("%S\n",s);

#define SQR(x) (x)*(x)  // 预处理 只做简单的替换

 

int main()

{

int a=1,b=2;

PRI;  // 无参宏函数

PRINT("hello!!!!!!!"); //有参的宏函数

 

    printf("%d\n",SQR(a+b));

while(1);

return 0;

}

 

 

 

今日开发中出现的问题汇总

 

1. 二维数组:

思考:

数组a[3]a[4],哪个不能表示a[1][1]

*(a[1]+1)

*(&a[1][1])

(*(a+1))[1]

*(a+5)

 

 

今日未解决问题

 

 

 

 

今日开发收获

 

1. c语言不允许对数组的大小作动态定义,即数组的大小不依赖于程序运行中变量的值

2. 数组的长度,C语言中必须定义数组的长度

3. 数组在内存空间是连续的,每一个元素占4个字节,取第一个字节作为地址,数组取第一个元素的地址

4. 函数调用过程:

1)通过函数名找到函数的入口地址(函数名就是地址)

2)给形参分配空间、

3)传值(实参传给形参)(值传递、地址传递)

4)执行函数体

5)返回

6)释放空间(栈空间)

5. register修饰的变量不能取地址(寄存器变量)

 extern Static的用法

7预处理

8. 宏定义、宏参数

 

 

 

自我评价

(是否按开发规范完成既定任务,需要改进的地方,与他人合作效果等)

 

 

其他

 

作业: 

1. 字符数组中在指定位置插入字符串

#include "stdafx.h"

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

char query[] = "insert into babyData values(,'yuanlifu',37.5,0,22,0,55,0,10,0,12,0,23,0,now());";

void insert(char *str, char *pch, int pos) {

    int len = strlen(str);

    int nlen = strlen(pch);

    for (int i = len - 1; i >= pos; --i) {

        *(str + i + nlen) = *(str + i);

    }

    for (int n = 0; n < nlen;n++)

    *(str + pos + n) = *pch++;

    *(str + len + nlen) = 0;

}

int main() {

    char ch[] = "4558";

    puts(query);

    insert(query, ch, 28);

    puts(query);

    return 0;

}

 

2. 设计一洗牌发牌的程序

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

//card structure definition//

struct card

{

        const char *face;

        const char *suit;

};//end structure card

typedef struct card Card;//new type name for struct card

//prototypes

void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[] );

void shuffle( Card * const wDeck );

void deal( const Card * const wDeck );

int main( void )

{

        Card deck[ 52 ];//define array of Cards     

        //initialize array of pointers

        const char *face[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };

        //initialize array of pointers

        const char *suit[] = { "红桃", "方块", "黑桃", "梅花" };

        srand( time( NULL ) );        //randomize

        fillDeck( deck, face, suit );//load the deck with Cards

        shuffle( deck );//put Cards in random order

        deal( deck );//deal all 52 Cards

        return 0;

}//end main

//place strings into Card structures

void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[] )

{

        int i;

 

        for ( i = 0; i<=51; i++ )

        {

                wDeck[ i ].face = wFace[ i % 13 ];

                wDeck[ i ].suit = wSuit[ i / 13 ];

        }

}//end function fillDeck

//shuffle cards

void shuffle( Card * const wDeck )

{

        int i;

        int j;//variable to hold random value between 0 - 51

        Card temp;//define temporary structure for swapping Cards

        for ( i = 0; i <=51; i++ )

        {

                j = rand() % 52;

                temp = wDeck[ i ];

                wDeck[ i ] = wDeck[ j ];

                wDeck[ j ] = temp;

        }

}//end function shuffle

//deal cards

void deal( const Card * const wDeck )

{

        int i;

        for ( i = 0; i <= 51; i++ )

        {

            printf( "%5s %-8s%c", wDeck[ i ].face, wDeck[ i ].suit, ( i + 1 ) % 2 ? '\t' : '\n' );

        }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值