C语言家教记录(七)

C语言家教记录(七)

  • 导语
  • 字符串
    • 字面量
    • 变量
    • 读写字符串
    • 操作函数
    • 惯用法
    • 数组
  • 结构
  • 联合
  • 枚举
  • 总结与复习

导语

本次授课的内容如下:字符串,结构体、联合体、枚举

辅助教材为 《C语言程序设计现代方法(第2版)》

字符串

字面量

双引号括起来的字符序列

"to be or not to be, is a question"

字面量需要用\来延续\

printf("When you come to a fork in the road, take it. \
--Yogi Berra");//必须顶格

字面量长度为n,则存储空间为n+1,字符串也可以为空,用单独的\0存储

char*s="abc";//不能修改内容
char ch;
ch = "abc"[1];
printf('\n');//非法,只能是字面量

变量

和整数一样,也可以用数组

说明一下数据的实际存储,探讨各情况下存储用的空间

char date1[8] = "June 14";
//等价于
char date1[8] = {'J', 'u', 'n', 'e', ' ', '1', '4', '\0'};
char date2[9] = "June 14";
char date3[7] = "June 14";
char date4[] = "June 14";

char *p;
char s[121];
p=str;

读写字符串

用printf、scanf控制输入输出

char str[] = "Are we having fun yet?";
printf("%s\n", str);
printf("%.6s\n", str);//思考一下会输出什么
scanf("%s",str);

用gets、puts控制输出

char s[121];
gets(s);//不知道数据长度有风险,fgets更好
puts(s);

逐个读入

int read_line(char str[], int n)
{
 int ch, i = 0;
 while ((ch = getchar()) != '\n')
 if (i < n)
 str[i++] = ch;
 str[i] = '\0'; /* terminates string */
 return i; /* number of characters stored */
}

示例程序

int count_spaces(const char s[])
{
 int count = 0, i;
 for (i = 0; s[i] != '\0'; i++)
 if (s[i] == ' ')
 count++;
 return count;
}

操作函数

strcpy,strlen,strcat,strcmp

strcpy(str2, "abcd");
strcpy(str1, str2);
strcpy(str1, strcpy(str2, "abcd"));

int len;
len = strlen("abc"); /* len is now 3 */
len = strlen(""); /* len is now 0 */
strcpy(strl, "abc");
len = strlen(strl);

strcpy(str1, "abc");
strcat(str1, "def"); /* str1 now contains "abcdef" */
strcpy(str1, "abc");
strcpy(str2, "def");
strcat(str1, str2);

int strcmp(const char *s1, const char *s2);
if (strcmp(str1, str2) < 0)

惯用法

示例程序

size_t strlen(const char *s)
{
 const char *p = s;
 while (*s)
 s++;
 return s - p;
}

{
 char *p = s1;
 while (*p)
 p++;
 while (*p++ = *s2++)
 ;
 return s1;
}

数组

探讨存储方式区别

char planets[][8] = {"Mercury", "Venus", "Earth",
 "Mars", "Jupiter", "Saturn",
 "Uranus", "Neptune", "Pluto"};
 char *planets[] = {"Mercury", "Venus", "Earth",
 "Mars", "Jupiter", "Saturn",
 "Uranus", "Neptune", "Pluto"};

示例程序

#include 
#define NUM_PLANETS 9
int main(int argc, char *argv[])
{
 char *planets[] = {"Mercury", "Venus", "Earth",
 "Mars", "Jupiter", "Saturn",
 "Uranus", "Neptune", "Pluto"};
 int i, j;
 for (i = 1; i < argc; i++) {
 for (j = 0; j < NUM_PLANETS; j++)
 if (strcmp(argv[i], planets[j]) == 0) {
 printf("%s is planet %d\n", argv[i], j + 1);
 break;
 }
 if (j == NUM_PLANETS)
 printf("%s is not a planet\n", argv[i]);
 }
 return 0;
}

结构

struct {
 int number;
 char name[NAME_LEN+1];
 int on_hand;
} part1, part2;
//介绍存储实现,视为一整个变量,可以认为生成了一个新的类型
struct{
char stu_name[10];
int id;
int grade;
}student;

struct {
 int number;
 char name[NAME_LEN+1];
 int on_hand;
} part1 = {528, "Disk drive", 10},
 part2 = {914, "Printer cable", 5};
 //初始化,但是不推荐这么用;

//通过.运算符进行访问,或者用->
printf("Part number: %d\n", part1.number);
printf("Part name: %s\n", part1.name);
printf("Quantity on hand: %d\n", part1.on_hand);

Part1.number = 258; /* changes part1's part number */
Part1.on_hand++;

scanf("%d", &part1.on_hand);

part2 = part1;

struct { int a[10]; } a1, a2;
a1 = a2;

命名

struct part {
 int number;
 char name[NAME_LEN+1];
 int on_hand;
};//一个新的类型

struct part part1, part2;//不能直接用part

struct part {
 int number;
 char name[NAME_LEN+1];
 int on_hand;
} part1, part2;

typedef struct {
 int number;
 char name[NAME_LEN+1];
 int on_hand;
} Part;//这之后可以用Part直接命名

示例程序

struct part build_part(int number, const char * name, int on_hand)
{
 struct part p;
 p.number = number;
 strcpy (p.name, name);
 p.on_hand = on_hand;
 return p;
}
part1 = build_part(528, "Disk drive", 10);

其余部分见书

联合

解释一下存储实现

union {
 int i;
 double d;
} u;
union {
 int i;
 double d;
} u = {0};

示例程序

#define INT_KIND 0
#define DOUBLE_KIND 1
typedef struct {
 int kind; /* tag field */
 union{
 int i;
 double d;
 } u;
} Number;

n.kind = INT_KIND;
n.u.i = 82;

void print_number(Number n)
{
 if (n.kind == INT_KIND)
 printf("%d", n.u.i);
 else
 printf("%g", n.u.d);
}

枚举

#define SUIT int
#define CLUBS 0
#define DIAMONDS 1
#define HEARTS 2
#define SPADES 3
enum {CLUBS, DIAMONDS, HEARTS, SPADES} s1, s2;
//等价于
enum suit {CLUBS, DIAMONDS, HEARTS, SPADES};
enum suit s1, s2;
//等价于
typedef enum {CLUBS, DIAMONDS, HEARTS, SPADES} Suit;
Suit s1, s2;

//c89中的bool
typedef enum {FALSE, TRUE} Bool;

enum suit {CLUBS = 1, DIAMONDS = 2, HEARTS = 3, SPADES = 4};

typedef struct {
 enum {INT_KIND, DOUBLE_KIND} kind;
 union {
 int i;
 double d;
 } u;
} Number;

总结与复习

本次授课讲述第13章和第16章内容,关键点:字符串和新类型

你可能感兴趣的:(C语言家教记录,c语言,算法,数据结构)