你们要的C语言实例大全-深入提高篇二
目录
实例56 fputc()和fgetc()
convert.c
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
void main()
{
FILE *fp;
char str[100];
int i;
if((fp=fopen("file.txt", "w"))==NULL)
{
printf("无法打开文件\n");
exit(0);
}
printf("请输入一个字符串:\n");
gets(str);
/* 将字符串中的小写字符转换成大写字符,直到遇到"."为止 */
for(i=0; str[i]!='.'; i++)
{
if(str[i]>='a' && str[i]<='z')
str[i] = str[i] - 32;
fputc(str[i], fp); /* 将转换后的字符存入文件 */
}
fclose(fp);
fp = fopen("file.txt", "r");
for(i=0; str[i]!='.'; i++)
str[i] = fgetc(fp);
printf("%s\n", str);
fclose(fp);
}
运行
trust100@ubuntu:~/test/clanguage$ ./a.out
请输入一个字符串:
shilasgjtg.
SHILASGJTG.
实例57 函数rewind()
rewind.c
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
void main()
{
char str[80];
FILE *fp; /* 定义一个文件类型的指针 */
/* 以写的方式打开文件test */
if((fp=fopen("test.txt", "w"))==NULL)
{
printf("Cannot open file.\n");
exit(0);
}
do{
printf("Please enter a string: \n");
gets(str);
strcat(str, "\n"); /* 增加一个新行 */
fputs(str, fp);
} while(*str!='\n');
/* 从文件中读出字符串,并将它们显示出来 */
rewind(fp); /* 重置文件指针 */
while(!feof(fp))
{
fgets(str, 79, fp);
printf(str);
}
fclose(fp);
}
运行
trust100@ubuntu:~/test/clanguage$ cat test.txt
lashdidsagh
idhsig
hjiheiw
asg
实例58 fread()和fwrite()
fread.c
# include <stdio.h>
# include <stdlib.h>
void main()
{
FILE *fp;
int i = 156;
long l = 9701076L;
double d = 3.456;
if((fp=fopen("test", "w"))==NULL)
{
printf("不能打开文件.\n");
exit(0);
}
fwrite(&i, sizeof(int), 1, fp);
fwrite(&l, sizeof(long), 1, fp);
fwrite(&d, sizeof(double), 1, fp);
rewind(fp);
fread(&i, sizeof(int), 1, fp);
fread(&l, sizeof(long), 1, fp);
fread(&d, sizeof(double), 1, fp);
printf("i = %d\n", i);
printf("l = %ld\n", l);
printf("d = %f\n", d);
fclose(fp);
}
运行
trust100@ubuntu:~/test/clanguage$ ./a.out
i = 156
l = 9701076
d = 3.456000
实例59 fprintf()和fscanf()
fprintf.c
# include <stdio.h>
# include <io.h>
# include <stdlib.h>
void main()
{
FILE *fp;
char str[80];
int i;
if((fp=fopen("test", "w"))==NULL)
{
printf("不能打开文件.\n");
exit(0);
}
printf("Please enter a string and a number: \n");
fscanf(stdin, "%s %d", str, &i); /* 参数stdin表示从键盘读入 */
fprintf(fp, "%s %d", str, i);
fclose(fp);
if((fp=fopen("test", "r"))==NULL)
{
printf("不能打开文件.\n");
exit(0);
}
fscanf(fp, "%s %d", str, &i);
fprintf(stdout, "%s %d\n", str, i); /* 参数stdout表示写向屏幕 */
fclose(fp);
}
运行
trust100@ubuntu:~/test/clanguage$ ./a.out
Please enter a string and a number:
sahig 12
sahig 12
trust100@ubuntu:~/test/clanguage$ cat test
sahig 12trust100@ubuntu:~/test/clanguage$
实例60 随机存取
fseek.c
# include <stdio.h>
# include <stdlib.h>
void main(int argc, char *argv[])
{
FILE *fp;
if(argc!=3)
{
printf("缺少字节位置,无法进行操作。\n");
exit(0);
}
if((fp=fopen(argv[1], "rb"))==NULL)
{
printf("无法打开文件。\n");
exit(0);
}
if(fseek(fp, atol(argv[2]), 0))
{
printf("寻找出现错误。\n");
exit(0);
}
printf("在%ld处的字符是%c。\n", atol(argv[2]), getc(fp));
fclose(fp);
}
运行
trust100@ubuntu:~/test/clanguage$ ./a.out test 2
在2处的字符是h。
实例61 错误处理
ferror.c
# include <stdio.h>
# include <stdlib.h>
# define TAB_NUM 8 /* 定义应换的空格数 */
# define IN 0
# define OUT 1
void error(int e)
{
if(e == IN)
printf("输入错误。\n");
else
printf("输出错误。\n");
exit(1); /* 跳出程序 */
}
/* 为使用该程序,应该指定命令行中的输入和输出出文件名 */
void main(int argc, char *argv[])
{
FILE *in, *out;
int tab, i;
char ch;
if(argc != 3)
{
printf("用法出错。\n");
exit(1);
}
if((out=fopen(argv[1], "wb"))==NULL)
{
printf("不能打开输入文件%s。\n", argv[1]);
exit(1);
}
if((out=fopen(argv[2], "wb"))==NULL)
{
printf("不能打开输出文件%s。\n", argv[2]);
exit(1);
}
tab = 0;
do{
ch = getc(in);
if(ferror(in))
error(IN);
/* 如果发现制表符,则输出相同数目的空格符 */
if(ch == '\t')
{
for(i=tab; i<8; i++)
{
putc(' ', out);
if(ferror(out))
error(OUT);
}
tab = 0;
}
else
{
putc(ch, out);
if(ferror(out))
error(OUT);
tab++;
if(tab==TAB_NUM)
tab = 0;
if(ch=='\n'||ch=='\r')
tab = 0;
}
} while(!feof(in));
fclose(in);
fclose(out);
}
实例62 综合实例
commu.c
# include <stdio.h>
# include <stdlib.h>
# define MAX 100
struct addr
{
char name[20];
char street[40];
char city[20];
char state[4];
unsigned long zip;
} addr_list[MAX];
void init_list(void);
void enter(void);
void dele(void);
void list(void);
void save(void);
void load(void);
int menu_select(void);
int find_free(void);
void main()
{
char choice;
init_list();
for( ; ; )
{
choice = menu_select();
switch(choice)
{
case 1: enter();
break;
case 2: dele();
break;
case 3: list();
break;
case 4: save();
break;
case 5: load();
break;
case 6: exit(0);
}
}
}
void init_list(void)
{
register int t;
for(t=0; t<MAX; t++)
addr_list[t].name[0] = '\0';
}
void enter(void)
{
int slot;
char str[80];
slot = find_free();
if(slot == -1)
printf("\nList Full");
printf("Enter name: ");
gets(addr_list[slot].name);
printf("Enter street: ");
gets(addr_list[slot].street);
printf("Enter city: ");
gets(addr_list[slot].city);
printf("Enter state: ");
gets(addr_list[slot].state);
printf("Enter zip: ");
gets(str);
addr_list[slot].zip = strtoul(str, '\0', 10);
}
void dele(void)
{
register int slot;
char str[80];
printf("Enter record #: ");
gets(str);
slot = atoi(str);
if(slot>=0 && slot<MAX)
addr_list[slot].name[0] = '\0';
}
void list(void)
{
register int t;
for(t=0; t<MAX; t++)
{
if(addr_list[t].name[0])
{
printf("%s\n", addr_list[t].name);
printf("%s\n", addr_list[t].street);
printf("%s\n", addr_list[t].city);
printf("%s\n", addr_list[t].state);
printf("%s\n\n", addr_list[t].zip);
}
}
printf("\n\n");
}
void save(void)
{
FILE *fp;
register int i;
if((fp=fopen("maillist", "wb"))==NULL)
printf("Cannot open file.\n");
for(i=0; i<MAX; i++)
if(*addr_list[i].name)
if(fwrite(&addr_list[i], sizeof(struct addr), 1, fp)!=1)
printf("File write error.\n");
fclose(fp);
}
void load(void)
{
FILE *fp;
register int i;
if((fp=fopen("maillist", "rb"))==NULL)
printf("Cannot open file.\n");
init_list();
for(i=0; i<MAX; i++)
if(fread(&addr_list[i], sizeof(struct addr), 1, fp)!=1)
{
if(feof(fp))
break;
printf("File read error.\n");
}
fclose(fp);
}
int menu_select(void)
{
char str[80];
int c;
printf("1. Enter a name\n");
printf("2. Delete a name\n");
printf("3. List the file\n");
printf("4. Save the file\n");
printf("5. Load the file\n");
printf("6. Quit\n");
do{
printf("\nEnter your choice: ");
gets(str);
c = atoi(str);
} while(c<0 || c>6);
return c;
}
int find_free(void)
{
register int t;
for(t=0; addr_list[t].name[0]&&t<MAX; t++);
if(t==MAX)
return -1;
return t;
}
运行
trust100@ubuntu:~/test/clanguage$ ./a.out
1. Enter a name
2. Delete a name
3. List the file
4. Save the file
5. Load the file
6. Quit
Enter your choice: 1
Enter name: gll
Enter street: s
Enter city: china
Enter state: sx
Enter zip: z
1. Enter a name
2. Delete a name
3. List the file
4. Save the file
5. Load the file
6. Quit
Enter your choice: 6
实例63 动态分配函数
dynamic.c
# include <stdio.h>
# include <stdlib.h>
# define NUM 10
int main()
{
char *str[NUM]; /* 定义一个字符性的指针数组 */
int t;
/* 为数组中的每个指针分配内存 */
for(t=0; t<NUM; t++)
{
if((str[t]=(char *)malloc(128))==NULL)
{
printf("Allocation Error.\n");
exit(1);
}
/* 在分配的内存中存放字符串 */
printf("Enter string %d: ", t);
gets(str[t]);
}
/* 释放内存 */
for(t=0; t<NUM; t++)
free(str[t]);
/* 由于主函数有返回值,故返回0值 */
return 0;
}
运行
trust100@ubuntu:~/test/clanguage$ ./a.out
Enter string 0: s
Enter string 1: f
Enter string 2: g
Enter string 3: y
Enter string 4: h
Enter string 5: r
Enter string 6: h
Enter string 7: c
Enter string 8: a
Enter string 9: x
实例64 常用时间函数
time.c
# include <time.h>
# include <stdio.h>
int main()
{
struct tm *local;
time_t tm;
tm = time(NULL);
local = localtime(&tm);
printf("Local time and date: %s\n", asctime(local));
local = gmtime(&tm);
printf("UTC time and date: %s\n", asctime(local));
return 0;
}
运行‘
trust100@ubuntu:~/test/clanguage$ ./a.out
Local time and date: Sun Mar 8 08:44:08 2020
UTC time and date: Sun Mar 8 15:44:08 2020
’
实例65 转换函数
convert.c
# include <stdlib.h>
# include <stdio.h>
int main()
{
char num1[80], num2[80];
double sum1;
int sum2;
long sum3;
printf("Enter first: ");
gets(num1);
printf("Enter second: ");
gets(num2);
sum1 = atof(num1)+atof(num2);
printf("The sum is: %f\n", sum1);
printf("Enter three: ");
gets(num1);
printf("Enter four: ");
gets(num2);
sum2 = atoi(num1)+atoi(num2);
printf("The sum is: %d\n", sum2);
printf("Enter five: ");
gets(num1);
printf("Enter six: ");
gets(num2);
sum3 = atol(num1)+atol(num2);
printf("The sum is: %ld\n", sum3);
return 0;
}
运行
trust100@ubuntu:~/test/clanguage$ ./a.out
Enter first: 12
Enter second: 45
The sum is: 57.000000
Enter three: 77
Enter four: 98
The sum is: 175
Enter five: 90
Enter six: 111
The sum is: 201
实例66 查找函数
search.c
# include <stdio.h>
# include <stdlib.h>
# include <ctype.h>
char *alpha = "abcdefghijklmnopqrstuvwxyz";
int comp(const void *ch, const void *s);
int main()
{
char ch;
char *p;
printf("Enter a character: ");
ch = getchar();
ch = tolower(ch); /* 将变元ch转换成小写字符 */
p = (char *)bsearch(&ch, alpha, 26, 1, comp);
if(p)
printf("%c is in alphabet\n", *p);
else
printf("is not in alphabet\n");
return 0;
}
int comp(const void *ch, const void *s)
{
return *(char *)ch -*(char *)s;
}
运行
trust100@ubuntu:~/test/clanguage$ ./a.out
Enter a character: i
i is in alphabet
实例67 跳转函数
jump.c
# include <setjmp.h>
# include <stdio.h>
jmp_buf ebuf; /* 类型在<setjmp.h>中定义 */
void fun(void);
int main()
{
int i;
printf("1 ");
i = setjmp(ebuf); /* 第一次调用时返回值为零 */
if(i == 0)
{
fun();
printf("This will not be printed.");
}
printf("%d\n", i);
return 0;
}
void fun(void)
{
printf("3 ");
longjmp(ebuf, 5); /* 跳转到执行setjmp()的地方,但此时函数setjmp()返回值为3 */
}
运行
trust100@ubuntu:~/test/clanguage$ ./a.out
1 3 5
实例68 排序函数
sort.c
# include <stdlib.h>
# include <stdio.h>
int num[12] = {
14, 5, 9, 7, 6, 0, 91, 4, 1, 3, 2, 8
};
int comp(const void *, const void *);
int main()
{
int i;
printf("Original array: ");
for(i=0; i<12; i++)
printf("%d ", num[i]);
printf("\n");
qsort(num, 12, sizeof(int), comp);
printf("Sorted array: ");
for(i=0; i<12; i++)
printf("%d ", num[i]);
printf("\n");
return 0;
}
int comp(const void *i, const void *j)
{
return *(int *)i - *(int *)j;
}
运行
trust100@ubuntu:~/test/clanguage$ ./a.out
Original array: 14 5 9 7 6 0 91 4 1 3 2 8
Sorted array: 0 1 2 3 4 5 6 7 8 9 14 91
实例69 伪随机数生成
rand.c
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
/* 利用系统时间来寻找随机数,并将前十个随机数显示出来 */
int main()
{
long time1;
int i, time2;
/* 获得正确的日历时间 */
time1 = time(NULL); /* 返回系统的当前日历时间 */
printf("%ld\n", time1);
time2 = (unsigned)time1/2;
printf("%ld\n", time2);
/* 以系统时间为参数,为即将生成的伪随机数序列设置起点 */
srand(time2);
/* 生成十个伪随机数序列 */
for(i=0; i<10; i++)
printf("%d ", rand());
printf("\n");
return 0;
}
运行
trust100@ubuntu:~/test/clanguage$ ./a.out
1583682555
791841277
937882791 822067410 1112968171 837771753 1392239192 870112025 1562303515 1412078305 1705961954 752498124
实例70 可变数目变元
change.c
# include <stdio.h>
# include <stdarg.h>
double sum_series(int num, ...);
int main()
{
double d;
d = sum_series(4, 0.5, 0.25, 0.125, 0.0625);
printf("Sum of series is %f.\n", d);
return 0;
}
double sum_series(int num, ...)
{
double sum = 0.0, t;
va_list argptr;
/* 初始化argptr */
va_start(argptr, num);
/* 计算序列之和 */
for( ; num; num--)
{
t = va_arg(argptr, double);
sum = sum + t;
}
va_end(argptr);
return sum;
}
运行
trust100@ubuntu:~/test/clanguage$ ./a.out
Sum of series is 0.937500.