学习日志
姓名:唐小玲 日期:2018.7.10
今日学习任务
|
1. 数组 2. 函数 |
今日任务完成情况
(详细说明本日任务是否按计划完成,开发的代码量) |
1. 数组的定义、用法及初始化 2. 冒泡排序部分相关程序用 #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; }
3. 二维数组相关知识点 4. 函数的定义、用法 5. 函数调用过程 1、通过函数名找到函数的入口地址(函数名就是地址) 2、给形参分配空间、 3、传值(实参传给形参)(值传递、地址传递) 4、执行函数体 5、返回 6、释放空间(栈空间)
6. 地址传递(用法及原因) #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; } 7. 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); } 8. Static: 1) static int num =100 // 修饰全局变量,改变变量的作用域,只能在当前文件中被使用 2) static print(); // 修饰函数,改变函数的作用域,只能在当前文件中被使用 3) 修饰局部变量,改变变量的生命周期直到程序运行结束才被释放 (原因:存放的地方不一样 不加static修饰(普通局部变量)存放在栈(内存的一种)上面; 加上static修饰(静态变量),存放在数据段)
|
今日开发中出现的问题汇总
|
部分C语言知识点回忆起来有点困难,需要重新学习。 |
今日未解决问题
|
无 |
今日开发收获
|
复习以及新学了C语言知识,使得自己对于C语言有了更多的理解,收获很多! |
自我评价 (是否按开发规范完成既定任务,需要改进的地方,与他人合作效果等) |
一些简单的练习能够做出,但是有一些小知识点遗忘了,不够熟练,需要多多实践练习! |
其他
|
|
作业:
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 <time.h>
#include <stdlib.h>
#define NUM 10000 //洗牌混乱度
char color[7][7] = {"空白","红桃","黑桃","草花","方片","小王","大王"}; // 1:红桃 2:黑桃 3:草花 4:方片 5:小王 6:大王
char number[]={"0A234567890JQK"}; // 0:不存内容 1:A 2:2 3:3 4:4 5:5 6:6 7:7 8:8 9:9 10:10 11:J 12:Q 13:K
//构建扑克
struct card{
unsigned int number;
unsigned int color;
};
unsigned int NUM_card=53; //剩余未发的牌数(发牌时用的变量)
struct card card[54]; //创建保存一副牌的结构体
//构建一副牌(有序)
void creat_pair_card()
{
int num=1;
int col=1;
int i=0;
for(num=1;num<=15;num++)
{
//生成大小王
if(num>=14)
{
card[i].number=num;
card[i].color=col++;
i++;
}
else
{
//生成普通牌
for(col=1;col<=4;col++)
{
card[i].number=num;
card[i].color=col;
i++;
}
}
}
}
//显示一副牌
void print_card()
{
int i;
int num,col; //点数与花色对应数组下标
for(i=0;i<54;i++)
{
col=card[i].color;
num=card[i].number;
if(col>=5)
{
printf("%s\t",color[col]);
}
else
{
if(num==10)
{
printf("%s:",color[col]);
printf("10\t");
}
else
{
printf("%s:",color[col]);
printf("%c\t",number[num]);
}
}
}
printf("\n");
}
//洗牌
void wash_card()
{
int i;
int ch1,ch2;
srand((unsigned)time(NULL));
for(i=0;i<NUM;i++) //循环次数越大越乱
{
ch1=rand()%100; //2147483647 取最后两位0-99
ch2=rand()%100;
if(ch1>53)
{
ch1=100-ch1; //随机数大于53则减小
}
if(ch2>53)
{
ch2=100-ch2;
}
if(ch1 == ch2)
{
continue;
}
//两个随机牌交换
struct card tmp=card[ch1];
card[ch1]=card[ch2];
card[ch2]=tmp;
}
}
//发牌
void send_card(struct card hand[],int num)
{
if(NUM_card<num)
{
printf("数量不足\n");
return;
}
int i;
for(i=0;i<num;i++)
{
hand[i]=card[NUM_card-i];
}
NUM_card=NUM_card-num;
}
//显示手牌
void print_hand_card(struct card hand[],int num)
{
int i;
int point,col;
for(i=0;i<num;i++)
{
col=hand[i].color;
point=hand[i].number;
if(col>=5)
{
printf("%s ",color[col]);
}
else
{
if(point==10)
{
printf("%s:",color[col]);
printf("10 ");
}
else
{
printf("%s:",color[col]);
printf("%c ",number[point]);
}
}
}
printf("\n");
}
int main(void)
{
creat_pair_card(); //创建一副牌 有序
wash_card();//洗牌
print_card();//打印
struct card hand1[17]; //创建两个结构体保存手牌17张
struct card hand2[17];
send_card(hand1,17); //发牌
send_card(hand2,17);
printf("手牌A: ");
print_hand_card(hand1,17);
printf("\n");
printf("手牌B: ");
print_hand_card(hand2,17);
printf("\n");
}