//
动态存储分配
int
i,
*
pi;
float
i,
*
pf;
pi
=
(
int
*
)malloc(
sizeof
(
int
));
pf
=
(
float
*
)malloc(
sizeof
(
float
));
//
free(pi);
free(pf);
//
以上的等价代码
if
(
!
(pi
=
(
int
*
)malloc(
sizeof
(
int
))))
||
!
(pf
=
(
float
*
)malloc(
sizeof
(
float
)))
{
//
printf error
exit(EXIT_FAILURE);
}
//
if
//
因为malloc在程序中经常出现 所以使用宏.
#define
MALLOC(p, s)\
if
(
!
((p)
=
malloc(s)))
{\
/**/
/*
printf error
*/
\
exit(EXIT_FAILURE);\
}
//
现在我们可以用以下的命令来调用malloc
MALLOC(pi,
sizeof
(
int
));
MALLOC(pf,
sizeof
(
float
));
/**/
//////////////////////////////////////////
//
Example1
//
begin-----------Hanoi.cpp : 定义控制台应用程序的入口点。
/**/
/////////////////////////////////////////////
/
#include
"
stdafx.h
"
#include
<
iostream
>
static
int
times
=
0
;
void
move(
char
beginning,
int
num,
char
end);
void
hanoi(
int
n,
char
A,
char
B,
char
C
/**/
/*
, int i
*/
);
int
_tmain(
int
argc, _TCHAR
*
argv[])
{
int
number;
scanf(
"
%d
"
,
&
number);
hanoi(number,
'
x
'
,
'
y
'
,
'
z
'
);
scanf(
"
%d
"
,
&
number);
return
0
;
}
void
move(
char
beginning,
int
num,
char
end)
{
times
++
;
printf(
"
%d : Number %d From %c to %c\n
"
, times, num, beginning, end);
}
void
hanoi(
int
n,
char
A,
char
B,
char
C
/**/
/*
, int i
*/
)
{
if
(n
==
1
)
move(A,
1
, C);
else
{
hanoi(n
-
1
, A, C, B
/**/
/*
, i
*/
);
move(A, n, C);
/**/
/*
i++;
*/
hanoi(n
-
1
, B, A, C
/**/
/*
, i
*/
);
}
//
else
}
/**/
/////////////////////////////////////////////
/
//
Make2dArray
/**/
//////////////////////////////////////////
//
int
**
make2dArray(
int
rows,
int
cols)
{
int
**
x, i;
MALLOC(x, rows
*
sizeof
(
*
x));
for
(i
=
0
; i
<
rows; i
++
)
MALLOC(x[i], cols
*
sizeof
(
**
x));
return
x;
}
//
use
int
**
myArray;
myArray
=
make2dArray(
5
,
10
);
//
structure
typedef
struct
{
enum
tagField
{female, male}
sex;
union
{
int
children;
int
beard;
}
u;
}
sexType;
typedef
struct
{
char
name[
10
];
int
age;
float
salary;
sexType sexInfo;
}
humanBeing;
humanBeing person1, person2;
person1.sexInfo.sex
=
male;
person2.sexInfo.u.beard
=
FALSE;
/**/
///////////////////////////////////////////////////
/
//
fread fopen--1
/**/
////////////////////////////////////////////////
//
#include
"
stdafx.h
"
#include
<
stdio.h
>
#include
<
conio.h
>
#include
<
stdlib.h
>
int
_tmain(
int
argc, _TCHAR
*
argv[])
{
int
ch;
FILE
*
fp;
char
fname[
50
];
printf(
"
input a filename :
"
);
scanf(
"
%s
"
, fname);
fp
=
fopen(fname,
"
r
"
);
if
(
!
fp)
{
printf(
"
error!
"
);
exit(
1
);
}
//
if
while
((ch
=
getc(fp))
!=
EOF)
putchar(ch);
fclose(fp);
getch();
return
0
;
}
/**/
/////////////////////////////////////////////
//
//
beginning---2-----fileopen fread
/**/
/////////////////////////////////////////////
/
#include
"
stdafx.h
"
#include
<
stdio.h
>
#include
<
conio.h
>
#include
<
stdlib.h
>
int
_tmain(
int
argc, _TCHAR
*
argv[])
{
int
**
data;
FILE
*
fp;
fp
=
fopen(
"
h.txt
"
,
"
r
"
);
int
row, col, num;
if
(
!
feof(fp))
{
fscanf(fp,
"
%d
"
,
&
row);
fscanf(fp,
"
%d
"
,
&
col);
fscanf(fp,
"
%d
"
,
&
num);
}
//
if
while
(
!
feof(fp))
{
}
//
while
fclose(fp);
printf(
"
%d, %d, %d
"
, row, col, num);
getch();
return
0
;
}
/**/
///////////////////////////////////////////////////
/
//
example 2
//
beginning------------------transpose!!!
/**/
///////////////////////////////////////////////////
//
#include
"
stdafx.h
"
#include
<
stdio.h
>
#include
<
conio.h
>
#include
<
stdlib.h
>
typedef
struct
{
int
col;
int
row;
int
value;
}
term;
void
transpose(term a[], term b[]);
void
printMatrix(term a[]);
int
_tmain(
int
argc, _TCHAR
*
argv[])
{
term
*
pterm;
FILE
*
fp;
fp
=
fopen(
"
h.txt
"
,
"
r
"
);
int
i
=
1
;
int
row, col, num;
if
(
!
feof(fp))
{
fscanf(fp,
"
%d
"
,
&
col);
fscanf(fp,
"
%d
"
,
&
row);
fscanf(fp,
"
%d
"
,
&
num);
}
//
if
pterm
=
(term
*
)malloc(
sizeof
(term)
*
(num
+
1
));
pterm[
0
].col
=
col;
pterm[
0
].row
=
row;
pterm[
0
].value
=
num;
while
(
!
feof(fp))
{
fscanf(fp,
"
%d
"
,
&
pterm[i].row);
fscanf(fp,
"
%d
"
,
&
pterm[i].col);
fscanf(fp,
"
%d
"
,
&
pterm[i].value);
i
++
;
}
//
while
fclose(fp);
printf(
"
A:\n
"
);
printMatrix(pterm);
term
*
ptermB
=
(term
*
)malloc(
sizeof
(term)
*
(num
+
1
));
transpose(pterm, ptermB);
printf(
"
B:\n
"
);
printMatrix(ptermB);
getch();
free(pterm);
free(ptermB);
return
0
;
}
void
printMatrix(term a[])
{
for
(
int
i
=
0
; i
<=
a[
0
].value; i
++
)
{
printf(
"
(%d, %d) %d\n
"
, a[i].row, a[i].col, a[i].value);
}
//
for
}
void
transpose(term a[], term b[])
{
int
n, i, j, currentb;
n
=
a[
0
].value;
b[
0
].row
=
a[
0
].col;
b[
0
].col
=
a[
0
].row;
b[
0
].value
=
n;
if
(n
>
0
)
{
currentb
=
1
;
for
(i
=
0
; i
<
a[
0
].col; i
++
)
for
(j
=
1
; j
<=
n; j
++
)
if
(a[j].col
==
i)
{
b[currentb].row
=
a[j].col;
b[currentb].col
=
a[j].row;
b[currentb].value
=
a[j].value;
currentb
++
;
}
//
if
}
//
if
}
/**/
//////////////////////////////
/
//
fastTranspose
/**/
/////////////////////////////////
#define
MAX_COL 101
void
fastTranspose(term a[], term b[])
{
int
len[MAX_COL], beginning[MAX_COL];
int
i, n, currenta, k;
n
=
a[
0
].value;
b[
0
].row
=
a[
0
].col;
b[
0
].col
=
a[
0
].row;
b[
0
].value
=
n;
for
(i
=
0
; i
<
n ; i
++
)
len[i]
=
0
;
for
(i
=
1
; i
<=
n; i
++
)
len[a[i].col]
++
;
beginning[
0
]
=
1
;
for
(i
=
0
; i
<
n; i
++
)
beginning[i
+
1
]
=
beginning[i]
+
len[i];
for
(currenta
=
1
; currenta
<=
n; currenta
++
)
{
k
=
beginning[a[currenta].col]
++
;
b[k].row
=
a[currenta].col;
b[k].col
=
a[currenta].row;
b[k].value
=
a[currenta].value;
}
//
for
}
/**/
///////////////////////////////////////
//
list define
/**/
///////////////////////////////////////
/
typedef
struct
listNode
*
listPointer;
struct
listNode
{
char
data[
4
];
listPointer link;
}
;
#define
MALLOC(p, s)\
if
(
!
(p)
=
malloc(s))
{ exit(EXIT_FAILURE); }
listPointer first
=
NULL;
MALLOC(first,
sizeof
(
*
first));
free(first);
/**/
////////////////////////
//
//
listxi.h
/**/
//////////////////////////////
//
#ifndef LISTXI_H
#define
LISTXI_H
typedef
struct
listNode
*
listPointer;
struct
listNode
{
char
data;
listPointer link;
}
;
void
printList(listPointer first);
void
insert(listPointer
*
first, listPointer x,
int
dat);
listPointer create2();
//
listPointer insertFollow(listPointer *head, );
void
printList(listPointer first)
{
printf(
"
The list contains:
"
);
while
(first)
{
printf(
"
%4d
"
, first
->
data);
first
=
first
->
link;
}
//
while
printf(
"
\n
"
);
}
void
insert(listPointer
*
first, listPointer x,
int
dat)
{
listPointer temp;
temp
=
(listPointer) malloc(
sizeof
(
*
temp));
temp
->
data
=
dat;
if
(
*
first)
{
temp
->
link
=
x
->
link;
x
->
link
=
temp;
}
//
if
else
{
temp
->
link
=
NULL;
*
first
=
temp;
}
//
else
}
listPointer create2()
{
//
create a linked list with two nodes
listPointer first, second;
first
=
(listPointer) malloc(
sizeof
(
*
first));
second
=
(listPointer) malloc(
sizeof
(
*
second));
second
->
link
=
NULL;
first
->
link
=
second;
first
->
data
=
1
;
second
->
data
=
2
;
return
first;
}
#endif
/**/
///////////////////////////////////////
//
//
/**/
//////////////////////////////////////////
posted on 2010-11-26 12:49
xiwrong 阅读(217)
评论(0) 编辑 收藏 引用