月度归档:2015年01月

二进制文件读取(C语言)

二进制文件读取

描述:使用isprint函数, 实现字符的打印功能
头文件:#include
原型:int isprint(int c);
说明:判断字符c是否可打印(包括空格)
返回值:若成功则返回其真值,否则返回0。

c语言实现代码

#include <stdio.h>
#include <ctype.h>

#define LEN 16

int main(void)
{
	int 		n;
	unsigned long	count = 0;
	unsigned char 	buf[LEN];
	FILE		*fp;
	char 		fname[64];

	printf("please enter a file name: ");
	scanf("%s", fname);

	if ((fp = fopen(fname, "rb")) == NULL) {
		puts("\afile open failed");
	} else {
		while ((n = fread(buf, 1, LEN, fp)) > 0) {
			int i;

			printf("%08lX   ", count);

			for (i = 0; i < n; i++)
				printf("%02X ", (unsigned)buf[i]);

			if (n < LEN)
				for (i = n; i < LEN; i++) 
					printf("   ");

			printf("   ");
			for (i = 0; i < n; i++)
				putchar(isprint(buf[i]) ? buf[i] : '.');

			putchar('\n');

			count += LEN;
		}
		fclose(fp);
	}

	return 0;
}


继续阅读

将文件输入信息按身高排列

文件信息

将下列信息保持为hw.dat文件

Aiba 160 59.3
Kurata 162 51.6
Masaki 182 76.5
Tanaka 170 60.7
Tsuji 175 83.9
Washio 175 72.5

将该文件与下列c代码文件放在同一个文件目录内,编译下列c代码,生产可执行文件,执行文件排序显示

#include <stdio.h>

typedef struct gstudent {
	char 	name[128];
	double 	height;
	double 	weight;
} student;

int get_num(FILE *file);
void swap(student *pa, student *pb);
void bubble(student data[], int len);

int main(void)
{
	FILE *fp;

	fp = fopen("hw.dat", "r");
	double 	hsum = 0.0;
	double 	wsum = 0.0;
	int 	num = get_num(fp);
	student stu[num];
	
	if ((fp = fopen("hw.dat", "r")) == NULL) {
		puts("\afile open failed");
	} else {
		int i = 0;
		while (fscanf(fp, "%s%lf%lf", stu[i].name, 
			&stu[i].height, &stu[i].weight) == 3)
			i++;

		bubble(stu, num);

		for (i = 0; i < num; i++) {
			printf("%-10s %5.1f %5.1f\n", stu[i].name, 
				stu[i].height, stu[i].weight);	
			hsum += stu[i].height;
			wsum += stu[i].weight;
		}
		puts("---------------------------");
		printf("average    %5.1f %5.1f\n", hsum / num, wsum / num);
		fclose(fp);
	}

	return 0;
}

int get_num(FILE *fp)
{
	int cnt = 0;
	char name[128];
	double height, weight;
	while (fscanf(fp, "%s%lf%lf", name, &height, &weight) == 3)
		cnt++;

	return cnt;
}

void swap(student *pa, student *pb)
{
	student tmp = *pa;
	*pa = *pb;
	*pb = tmp;

	return;
}

void bubble(student data[], int len)
{
	while (len > 0) {
		int i;
		for (i = 0; i < len - 1; i++)
			if (data[i].height > data[i + 1].height)
				swap(&data[i], &data[i+1]);
		len = i;
	}

	return;
}

继续阅读

字符串转数字(C语言)

字符串转数字

c语言的字符串转数字函数: atoi() 将字符串转化为整形, atol() 将字符串转化为长整形, atol() 将字符串转化为双精度浮点数。所有这些函数定义在 头文件中,下面是自定义实现代码

字符串转化为数字(C代码实现)

#include <stdio.h>

int strtoi(const char *nptr);
long strtol(const char *nptr);
double strtof(const char *nptr);

int main(void)
{
	char chi[128];
	char chl[128];
	char chf[128];

	puts("please enter thress numbers");
	printf("please enter a integer string: ");
	scanf("%s", chi);
	printf("please enter a long string: ");
	scanf("%s", chl);
	printf("please enter a float real string: ");
	scanf("%s", chf);

	printf("the integer \"%s\" is %d\n", chi, strtoi(chi));
	printf("the long \"%s\" is %ld\n", chl, strtol(chl));
	printf("the real \"%s\" is %f\n", chf, strtof(chf));

	return 0;
}

int strtoi(const char *nptr)
{
	int result = 0;
	int signal = 1;

	if ((*nptr >= '0' && *nptr <= '9') || *nptr == '-' || *nptr == '+') {
		if (*nptr == '+' || *nptr == '-') {
			if (*nptr == '-')
				signal = -1;
			nptr++;
		}
	} else {
		return 0;

	}

	while (*nptr >= '0' && *nptr <= '9')
		result = result * 10 + (*nptr++ - '0');

	return result * signal;
}

long strtol(const char *nptr)
{
	long result = 0;
	int signal = 1;

	if ((*nptr >= '0' && *nptr <= '9') || *nptr == '-' || *nptr == '+') {
		if (*nptr == '-' || *nptr == '+') {
			if (*nptr == '-')
				signal = -1;
			nptr++;
		}
	} else {
		return 0;
	}

	while (*nptr >= '0' && *nptr <= '9')
		result = 10 * result + (*nptr++ - '0');

	return result * signal;
}

double strtof(const char *nptr)
{
	long pos = 0;
	double val = 1.0;
	double dec = 0.0;
	int signal = 1;

	if ((*nptr >= '0' && *nptr <= '9') || *nptr == '-' || *nptr == '+') {
		if (*nptr == '-' || *nptr == '+') {
			if (*nptr == '-')
				signal = -1;
			nptr++;
		}
	} else {
		return 0.0;
	}

	while (*nptr >= '0' && *nptr <= '9')
		pos = 10 * pos + (*nptr++ - '0');

	if (*nptr == '.')
		nptr++;
	else
		dec = 0.0;

	while (*nptr >= '0' &&  *nptr <= '9') {
		val /= 10.0;
		dec += val * (*nptr - '0');
		nptr++;
	}

	return signal * (pos + dec);
}

删除字符串中的数字(C语言)

删除字符串中的数字函数

要点:一个遍历每一个字符, 另一个遍历非数字,然后将一次将非数字字符依次进行赋值

看代码更简单

#include <stdio.h>

void del_digit(char str[]);

int main(void)
{
        char str[128];

        printf("please enter a string:  ");
        scanf("%s", str);

        del_digit(str);

        printf("the delete digit string: %s\n", str);

        return 0;
}

void del_digit(char str[])
{
        int i = 0;
        int j = 0;

        while (str[i])
                if (str[i] >= '0' && str[i] <= '9')
                        i++;
                else
                        str[j++] = str[i++];

        str[j] = '\0';

        return;
}

矩阵的乘法(C语言)

矩阵的乘法

只有矩阵1的列数等于矩阵2的行数时, 才有矩阵1与矩阵2的乘积.

当矩阵A[m,n] B[n, p]时: AB[m, p]
A矩阵为m行, n列
B矩阵为n行, p列

A与B的乘积AB则有m行, p列元素

矩阵的乘积, C代码

#include <stdio.h>

#define M 2
#define N 3
#define P 2

void mul(const int ma[M][N], const int mb[N][P], int mc[M][P]);

int main(void)
{
	int i, j;

	int ma[M][N] = {{1, 2, 3}, {4, 5, 6}};
	int mb[N][P] = {{5, 6}, {4, 5}, {3, 1}};
	int mc[M][P] = {{0}};

	mul(ma, mb, mc);

	for (i = 0; i < M; i++) {
		for (j = 0; j < P; j++)	
			printf("mc[%d][%d] = %d  ", i, j, mc[i][j]);
		putchar('\n');
	}

	return 0;
}

void mul(const int ma[M][N], const int mb[N][P], int mc[M][P])
{
	int i, j, k;

	for (i = 0; i < M; i++) {
		for (j = 0; j < P; j++) {
			mc[i][j] = 0;
			for (k = 0; k < N; k++)
				mc[i][j] += ma[i][k] * mb[k][j];
		}
	}

	return;
}

10000以内质数算法(C语言)

质数计算 第一版

判断一个n自然数能否被2, 3, … n-1 整除. 如果它不能被任意一个数整除, 该自然数就是质数.

例如判断13是否是质数

则需要判断13能否被下面任意一个数整除即可
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
因为13无法被其中任意一个数整除, 所有13是一个质数.

接下来判断12是否是质数

判断2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
以为12已经可以被2整除, 所有后面的数不需要继续计算.

10000以内的质数, C语言v1

#include <stdio.h>

int main(void)
{
	int i, no;
	unsigned long counter = 0;    /* 计算乘除的次数 */

	for (no = 2; no <= 10000; no++) {
		for (i = 2; i < no; i++) {
			counter++;
			if (no % i == 0)
				break;
		}
		if (no == i)    // 质数判断方法
			printf("%d\n", no);
	}

	printf("the number of nultiplication and division: %lu\n", counter); // 5775223

	return 0;
}

质数计算 第二版

在程序1中大于2的倍数都不是质数, 比如 4, 6, 8, …

10000以内的质数, C语言v2

#include <stdio.h>

int main(void)
{
	int i, no;
	unsigned long counter = 0;    /* 计算乘除的次数 */

	no = 2;
	printf("%d\n", no++);

	for (; no <= 10000; no += 2) {
		for (i = 2; i < no; i++) {
			counter++;
			if (no % i == 0)
				break;
		}
		if (no == i)    // 质数判断方法
			printf("%d\n", no);
	}

	printf("the number of multiplication and division: %lu\n", counter);    // 5770224

	return 0;
}

与第一版相比, 乘除次数几乎一致, 因为每一次循环都跳过了除以2这个数字

继续阅读