📄 chap7.lst
字号:
listing 1
struct addr {
char name[30];
char street[40];
char city[20];
char state[3];
char zip[11];
int customer_num;
};
listing 2
struct addr addr_info;
listing 3
struct addr {
char name[30];
char street[40];
char city[20];
char state[3];
char zip[11];
int customer_num;
} addr_info, binfo, cinfo;
listing 4
struct {
char name[30];
char street[40];
char city[20];
char state[3];
char zip[11];
int customer_num;
} addr_info;
listing 5
addr_info.customer_num = 88;
listing 6
printf("%d", addr_info.customer_num);
listing 7
gets(addr_info.name);
listing 8
register int t;
for(t=0; addr_info.name[t]; ++t) putchar(addr_info.name[t]);
listing 9
#include <stdio.h>
int main(void)
{
struct {
int a;
int b;
} x, y;
x.a = 10;
x.b = 20;
y = x; /* assign one structure to another */
printf("Contents of y: %d %d.", y.a, y.b);
return 0;
}
listing 10
struct addr addr_info[100];
listing 11
printf("%s", addr_info[2].zip);
listing 12
#define MAX 100
struct inv {
char item[30];
float cost;
int on_hand;
} inv_info[MAX];
listing 13
int main(void)
{
char choice;
init_list(); /* initialize the structure array */
for(;;) {
choice = menu_select();
switch(choice) {
case 1: enter();
break;
case 2: del();
break;
case 3: list();
break;
case 4: return 0;
}
}
}
listing 14
/* Initialize the structure array. */
void init_list(void)
{
register int t;
for(t=0; t<MAX; ++t) inv_info[t].item[0] = '\0';
}
listing 15
/* Input the user's selection. */
int menu_select(void)
{
char s[80];
int c;
printf("\n");
printf("1. Enter an item\n");
printf("2. Remove an item\n");
printf("3. List the inventory\n");
printf("4. Quit\n");
do {
printf("\nEnter your choice: ");
gets(s);
c = atoi(s);
} while(c<0 || c>4);
return c;
}
listing 16
/* Input the inventory information. */
void enter(void)
{
int slot;
slot = find_free();
if(slot == -1) {
printf("\nList Full");
return;
}
printf("Enter item: ");
gets(inv_info[slot].item);
printf("Enter cost: ");
scanf("%f", &inv_info[slot].cost);
printf("Enter number on hand: ");
scanf("%d%*c",&inv_info[slot].on_hand);
}
/* Return the index of the first unused array
location or -1 if no free locations exist.
*/
int find_free(void)
{
register int t;
for(t=0; inv_info[t].item[0] && t<MAX; ++t) ;
if(t == MAX) return -1; /* no slots free */
return t;
}
listing 17
/* Remove an item from the list. */
void del(void)
{
register int slot;
char s[80];
printf("enter record #: ");
gets(s);
slot = atoi(s);
if(slot >= 0 && slot < MAX) inv_info[slot].item[0] = '\0';
}
listing 18
/* Display the list on the screen. */
void list(void)
{
register int t;
for(t=0; t<MAX; ++t) {
if(inv_info[t].item[0]) {
printf("Item: %s\n", inv_info[t].item);
printf("Cost: %f\n", inv_info[t].cost);
printf("On hand: %d\n\n", inv_info[t].on_hand);
}
}
printf("\n\n");
}
listing 19
/* A simple inventory program using an array of structures */
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
struct inv {
char item[30];
float cost;
int on_hand;
} inv_info[MAX];
void init_list(void), list(void), del(void);
void enter(void);
int menu_select(void), find_free(void);
int main(void)
{
char choice;
init_list(); /* initialize the structure array */
for(;;) {
choice = menu_select();
switch(choice) {
case 1: enter();
break;
case 2: del();
break;
case 3: list();
break;
case 4: return 0;
}
}
}
/* Initialize the structure array. */
void init_list(void)
{
register int t;
for(t=0; t<MAX; ++t) inv_info[t].item[0] = '\0';
}
/* Input the user's selection. */
int menu_select(void)
{
char s[80];
int c;
printf("\n");
printf("1. Enter an item\n");
printf("2. Remove an item\n");
printf("3. List the inventory\n");
printf("4. Quit\n");
do {
printf("\nEnter your choice: ");
gets(s);
c = atoi(s);
} while(c<0 || c>4);
return c;
}
/* Input the inventory information. */
void enter(void)
{
int slot;
slot = find_free();
if(slot == -1) {
printf("\nList Full");
return;
}
printf("Enter item: ");
gets(inv_info[slot].item);
printf("Enter cost: ");
scanf("%f", &inv_info[slot].cost);
printf("Enter number on hand: ");
scanf("%d%*c", &inv_info[slot].on_hand);
}
/* Return the index of the first unused array
location or -1 if no free locations exist.
*/
int find_free(void)
{
register int t;
for(t=0; inv_info[t].item[0] && t<MAX; ++t) ;
if(t == MAX) return -1; /* no slots free */
return t;
}
/* Remove an item from the list. */
void del(void)
{
register int slot;
char s[80];
printf("enter record #: ");
gets(s);
slot = atoi(s);
if(slot >= 0 && slot < MAX) inv_info[slot].item[0] = '\0';
}
/* Display the list on the screen. */
void list(void)
{
register int t;
for(t=0; t<MAX; ++t) {
if(inv_info[t].item[0]) {
printf("Item: %s\n", inv_info[t].item);
printf("Cost: %f\n", inv_info[t].cost);
printf("On hand: %d\n\n", inv_info[t].on_hand);
}
}
printf("\n\n");
}
listing 20
struct fred {
char x;
int y;
float z;
char s[10];
} mike;
listing 21
func(mike.x); /* passes character value of x */
func2(mike.y); /* passes integer value of y */
func3(mike.z); /* passes float value of z */
func4(mike.s); /* passes address of string s */
func(mike.s[2]); /* passes character value of s[2] */
listing 22
func(&mike.x); /* passes address of character x */
func2(&mike.y); /* passes address of integer y */
func3(&mike.z); /* passes address of float z */
func4(mike.s); /* passes address of string s */
func(&mike.s[2]); /* passes address of character s[2] */
listing 23
#include <stdio.h>
/* declare a structure type */
struct struct_type {
int a, b;
char ch;
};
void f1(struct struct_type parm);
int main(void)
{
struct struct_type arg; /* declare arg */
arg.a = 1000;
f1(arg);
return 0;
}
void f1(struct struct_type parm)
{
printf("%d", parm.a);
}
listing 23
struct addr *addr_pointer;
listing 24
struct bal {
float balance;
char name[80];
} person;
struct bal *p; /* declare a structure pointer */
listing 25
p = &person;
listing 26
p->balance
listing 27
/* Display a software timer. */
#include <stdio.h>
#include <conio.h>
#define DELAY 128000
struct my_time {
int hours;
int minutes;
int seconds;
};
void update(struct my_time *t), display(struct my_time *t);
void mydelay(void);
int main(void)
{
struct my_time systime;
systime.hours = 0;
systime.minutes = 0;
systime.seconds = 0;
for(;;) {
update(&systime);
display(&systime);
if(kbhit()) return 0;
}
}
void update(struct my_time *t)
{
t->seconds++;
if(t->seconds==60) {
t->seconds = 0;
t->minutes++;
}
if(t->minutes==60) {
t->minutes = 0;
t->hours++;
}
if(t->hours==24) t->hours = 0;
mydelay();
}
void display(struct my_time *t)
{
printf("%02d:", t->hours);
printf("%02d:", t->minutes);
printf("%02d\n", t->seconds);
}
void mydelay(void)
{
long int t;
for(t=1; t<DELAY; ++t) ;
}
listing 29
if(t->hours==24) t->hours = 0;
listing 28
/* A generalized input example using structure pointers. */
#include <stdio.h>
#include <conio.h>
#include <string.h>
struct xyinput {
int x, y; /* screen location for prompt */
char message[80]; /* prompting message */
int i; /* input value */
} ;
void input_xy(struct xyinput *info);
int main(void)
{
struct xyinput mess;
mess.x = 10; mess.y = 10;
strcpy(mess.message, "Enter an integer: ");
clrscr();
input_xy(&mess);
printf("Your number squared is: %d.", mess.i*mess.i);
return 0;
}
/* Display a prompting message at the specified location
and input an integer value.
*/
void input_xy(struct xyinput *info)
{
gotoxy(info->x, info->y);
printf(info->message);
scanf("%d", &info->i);
}
listing 29
struct x {
int a[10][10]; /* 10 x 10 array of ints */
float b;
} y;
listing 30
y.a[3][7]
listing 31
struct emp {
struct addr address;
float wage;
} worker;
listing 32
worker.wage = 65000.00;
strcpy(worker.address.zip,"98765");
listing 33
struct device {
unsigned int active : 1;
unsigned int ready : 1;
unsigned int xmt_error : 1;
} dev_code;
listing 34
void wr_tape(char c)
{
while(!dev_code.ready) rd(&dev_code); /* wait */
wr_to_tape(c); /* write out byte */
while(dev_code.active) rd(&dev_code); /* wait until info is written */
if(dev_code.xmt_error) printf("Write Error");
}
listing 35
struct device {
unsigned active : 1;
unsigned ready : 1;
unsigned xmt_error : 1;
unsigned : 2;
unsigned EOT : 1;
} dev_code;
listing 36
struct emp {
struct addr address;
float pay;
unsigned lay_off:1; /* lay off or active */
unsigned hourly:1; /* hourly pay or wage */
unsigned deductions:3; /* IRS deductions */
};
listing 37
union union_type {
int i;
char ch;
} ;
listing 38
union union_type cnvt;
listing 39
cnvt.i = 10;
listing 40
union pw {
int i;
char ch[4];
};
listing 41
#include <stdio.h>
#include <stdlib>
union pw {
int i;
char ch[4];
};
int write_int(int num, FILE *fp);
int main()
{
FILE *fp;
fp = fopen("test.tmp", "w+");
if(fp==NULL) {
printf("Cannot open file.\n");
exit(1);
}
write_int(1000, fp);
fclose(fp);
return 0;
}
/* write an integer using union */
int write_int(int num, FILE *fp)
{
union pw wrd;
wrd.i = num;
putc(wrd.ch[0], fp); /* write first byte */
putc(wrd.ch[1], fp); /* write second byte */
putc(wrd.ch[2], fp); /* write third byte */
return putc(wrd.ch[3], fp); /* writes last byte */
}
listing 42
enum coin { penny, nickel, dime, quarter,
half_dollar, dollar};
enum coin money;
listing 43
money = dime;
if(money==quarter) printf("is a quarter\n");
listing 44
printf("The value of quarter is %d ", quarter);
listing 45
printf("%d %d", penny, dime);
listing 46
enum coin { penny, nickel, dime, quarter=100,
half_dollar, dollar};
listing 47
/* This will not work. */
money = dollar;
printf("%s", money);
listing 48
/* This code will not work. */
money = "penny";
listing 49
switch(money) {
case penny: printf("penny");
break;
case nickel: printf("nickel");
break;
case dime: printf("dime");
break;
case quarter: printf("quarter");
break;
case half_dollar: printf("half_dollar");
break;
case dollar: printf("dollar");
}
listing 50
char name[][12]={
"penny",
"nickel",
"dime",
"quarter",
"half_dollar",
"dollar"
};
/* ... */
printf("%s", name[money]);
listing 51
char ch;
int i;
float f;
printf("%d\n", sizeof ch);
printf("%d\n", sizeof i);
printf("%d\n", sizeof f);
printf("%d\n", sizeof(long double));
listing 52
struct s {
char ch;
int i;
float f;
} s_var;
listing 53
union u {
char ch;
int i;
float f;
} u_var;
listing 54
typedef float balance;
listing 55
balance past_due;
listing 56
typedef struct {
float due;
int over_due;
char name[40];
} client; /* here client is the new type name */
client clist[NUM_CLIENTS]; /* define array of
structures of type client */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -