明解C语言入门篇练习题第十三章

练习13-1

#include <stdio.h>
int main(void)
{
	FILE *fp;
	char file_name[128];
	printf("file name:");
	scanf("%s",file_name);
	fp = fopen(file_name, "r");//相对路径是跟源代码一样的路径
	if (fp == NULL)
		printf("\a无法打开文件\"%s\"。\n",file_name);
	else {
		printf("\a成功打开了文件\"%s\"。\n",file_name);
		fclose(fp);		
	}
	return 0;
}
练习13-2
#include <stdio.h>
int main(void)
{
	FILE *fp;
	char file_name[128];
	printf("file name:");
	scanf("%s",file_name);
	fp = fopen(file_name, "w");//相对路径是跟源代码一样的路径
	if (fp == NULL)
		printf("\a无法打开文件\"%s\"。\n",file_name);
	else {
		printf("\a成功打开了文件\"%s\"。\n",file_name);
		fclose(fp);								
	}
	return 0;
}
练习13-3
#include <stdio.h>
#define NUM 6
typedef struct 
{
	char   name[100];			
	double height;				
	double weight;				
}Student;

void swap_Student(Student *x, Student *y)
{
	Student temp = *x;
	*x = *y;
	*y = temp;
}

void sort_by_height(Student a[], int n)
{
	int i, j;
	for (i = 0; i < n - 1; i++) 
	{
		for (j = n - 1; j > i; j--)
			if (a[j - 1].height > a[j].height)
			{
				swap_Student(&a[j - 1], &a[j]);
			}
	}
}

int main(void)
{
	FILE   *fp;
	int    ninzu = 0;			
	double hsum = 0.0;			
	double wsum = 0.0;			
	Student std[NUM];

	if ((fp = fopen("hw.dat", "r")) == NULL)				
		printf("\a文件打开失败。\n");
	else 
	{
		while (fscanf(fp, "%s%lf%lf", std[ninzu].name, &(std[ninzu].height), &(std[ninzu].weight)) == 3) 
		{
			hsum += std[ninzu].height;
			wsum += std[ninzu].weight;
			ninzu++;
		}
		sort_by_height(std, NUM);
		for (ninzu = 0; ninzu<NUM; ninzu++)
			printf("%-10s %5.1f %5.1f\n", std[ninzu].name, std[ninzu].height, std[ninzu].weight);
		printf("----------------------\n");
		printf("平均       %5.1f %5.1f\n", hsum / ninzu, wsum / ninzu);
		fclose(fp);											
	}
	return 0;
}
练习13-4
#include <stdio.h>
int main(void)
{
	FILE   *fp;
	char   name[100];		
	double height, weight;		
	fp = fopen("read_from_keyboard.txt","w");//file created in the same path as source code path
	puts("input name(char[]) height(double) weight(double)");
	puts("input invalid value to end input");
	while (scanf("%s%lf%lf", name, &height, &weight) == 3) 
	{
		fprintf(fp,"%-10s %5.1f %5.1f\n", name, height, weight);
	}
	fclose(fp);
	printf("done!");
	return 0;
}
练习13-5
#include <time.h>
#include <stdio.h>
char data_file[] = "datetime.dat";				
char kimochi[128];

void get_data(void)
{
	FILE *fp;
	if ((fp = fopen(data_file, "r")) == NULL)			
	{
		printf("本程序第一次运行。\n");
		printf("当前的心情:");
		scanf("%s",kimochi);
	}
	else 
	{
		int year, month, day, h, m, s;
		fscanf(fp, "%d%d%d%d%d%d", &year, &month, &day, &h, &m, &s);
		printf("上一次运行是在%d年%d月%d日%d时%d分%d秒。\n",year, month, day, h, m, s);
		fscanf(fp, "%s", kimochi);
		printf("上一次心情:%s",kimochi);
		printf("当前的心情:");
		scanf("%s", kimochi);
		fclose(fp);										
	}
}

void put_data(void)
{
	FILE *fp;
	time_t current = time(NULL);/* 当前日历时间 */
	struct tm *timer = localtime(¤t);/* 分解时间*/


	if ((fp = fopen(data_file, "w")) == NULL)				
		printf("\a文件打开失败。\n");
	else {
		fprintf(fp, "%d %d %d %d %d %d\n",
			timer->tm_year + 1900, timer->tm_mon + 1, timer->tm_mday,
			timer->tm_hour, timer->tm_min, timer->tm_sec);
		fprintf(fp,"%s",kimochi);

		fclose(fp);											
	}
}

int main(void)
{
	get_data();/* 取得并显示上一次运行时的日期和时间 */
	put_data();/* 写入本次运行时的日期和时间 */
	return 0;
}
练习13-6
#include <stdio.h>
char file_name[128];		
int main(void)
{
	int ch;
	int line_num = 0;
	FILE *fp;
	printf("input filename:");
	scanf("%s",file_name);
	if ((fp = fopen(file_name, "r")) == NULL)
	{
		printf("文件打开失败。\n");
	}
	else
	{
		while ((ch = fgetc(fp)) != EOF)
		{
			if (ch == '\n')
				line_num++;
		}
		printf("file has line number:%d",line_num);
	}
	
	return 0;
}

练习13-7

#include <stdio.h>
char file_name[128];		
int main(void)
{
	int ch;
	int ch_num = 0;
	FILE *fp;
	printf("input filename:");
	scanf("%s",file_name);
	if ((fp = fopen(file_name, "r")) == NULL)
	{
		printf("文件打开失败。\n");
	}
	else
	{
		while ((ch = fgetc(fp)) != EOF)
		{
			if(ch!='\n')
				ch_num++;
		}
		printf("file has character number:%d",ch_num);
	}
	return 0;
}

练习13-8

#include <stdio.h>
int main(void)
{
	int ch;
	FILE *sfp;/* 原文件 */
	FILE *dfp;/* 目标文件 */
	char sname[FILENAME_MAX];/* 原文件名 */
	char dname[FILENAME_MAX];/* 目标文件名 */

	printf("打开原文件:");   scanf("%s", sname);
	printf("打开目标文件:");   scanf("%s", dname);

	if ((sfp = fopen(sname, "r")) == NULL)			
		printf("\a原文件打开失败。\n");
	else {
		if ((dfp = fopen(dname, "w")) == NULL)		
			printf("\a目标文件打开失败。\n");
		else {
			while ((ch = fgetc(sfp)) != EOF)
			{
				fputc(ch, dfp);//输出到目标文件
				printf("%c",ch);
			}
			fclose(dfp);						
		}
		fclose(sfp);						
	}
	return 0;
}

练习13-9

#include <stdio.h>
int main(void)
{
	int ch;
	FILE *sfp;/* 原文件 */
	FILE *dfp;/* 目标文件 */
	char sname[FILENAME_MAX];/* 原文件名 */
	char dname[FILENAME_MAX];/* 目标文件名 */

	printf("打开原文件:");   scanf("%s", sname);
	printf("打开目标文件:");   scanf("%s", dname);

	if ((sfp = fopen(sname, "r")) == NULL)			
		printf("\a原文件打开失败。\n");
	else {
		if ((dfp = fopen(dname, "w")) == NULL)		
			printf("\a目标文件打开失败。\n");
		else {
			while ((ch = fgetc(sfp)) != EOF)
			{
				if ((ch >= 'a') && (ch <= 'z'))
					ch -= 32;
				fputc(ch, dfp);//输出到目标文件
				printf("%c",ch);
			}
			fclose(dfp);						
		}
		fclose(sfp);							
	}
	return 0;
}

练习13-10

#include <stdio.h>
int main(void)
{
	int ch;
	FILE *sfp;/* 原文件 */
	FILE *dfp;/* 目标文件 */
	char sname[FILENAME_MAX];/* 原文件名 */
	char dname[FILENAME_MAX];/* 目标文件名 */

	printf("打开原文件:");   scanf("%s", sname);
	printf("打开目标文件:");   scanf("%s", dname);

	if ((sfp = fopen(sname, "r")) == NULL)			
		printf("\a原文件打开失败。\n");
	else {
		if ((dfp = fopen(dname, "w")) == NULL)		
			printf("\a目标文件打开失败。\n");
		else {
			while ((ch = fgetc(sfp)) != EOF)
			{
				if ((ch >= 'A') && (ch <= 'Z'))
					ch += 32;
				fputc(ch, dfp);//输出到目标文件
				printf("%c",ch);
			}
			fclose(dfp);						
		}
		fclose(sfp);						
	}
	return 0;
}

练习13-11

#include <stdio.h>
int main(void)
{
	FILE *fp;
	double haha[10] = {1.1,
					   2.2,
					   3.3,
					   4.4,
					   5.5,
					   6.6,
					   7.7,
					   8.8,
					   9.9,
					  10.10};
	int i;
	for(i=0;i<10;i++)
	{
		printf("从数组haha得到的元素haha[%d]为%23.21f。\n", i,haha[i]);
	}
	return 0;
}

练习13-12

#include <time.h>
#include <stdio.h>
char data_file[] = "datetime.dat";
void get_data(void)
{
	FILE *fp;
	if ((fp = fopen(data_file, "r")) == NULL)
		printf("本程序第一次运行。\n");
	else {
		int year, month, day, h, m, s;
		fscanf(fp, "%d%d%d%d%d%d", &year, &month, &day, &h, &m, &s);
		printf("上一次运行是在%d年%d月%d日%d时%d分%d秒。\n",
			year, month, day, h, m, s);
		fclose(fp);										
	}
}
/*--- 写入本次运行时的日期和时间 ---*/
void put_data(void)
{
	FILE *fp;
	time_t current = time(NULL);				/* 当前日历时间 */
	struct tm *timer = localtime(¤t);		/* 分解时间*/
	if ((fp = fopen(data_file, "w")) == NULL)				/* 打开文件 */
	{
		printf("\a文件打开失败。\n");
	}
	else 
	{
		fwrite(&timer, sizeof(struct tm), 1, fp);//以二进制格式写入
		fclose(fp);											
	}
}

int main(void)
{
	get_data();/* 取得并显示上一次运行时的日期和时间 */
	put_data();/* 写入本次运行时的日期和时间 */
	return 0;
}

练习13-13

#include <stdio.h>
int main(void)
{
	int n;
	int ch;
	FILE *sfp;						/* 原文件 */
	FILE *dfp;						/* 目标文件 */
	char sname[FILENAME_MAX];		/* 原文件名 */
	char dname[FILENAME_MAX];		/* 目标文件名 */

	printf("打开原文件:");   scanf("%s", sname);
	printf("打开目标文件:");   scanf("%s", dname);
	if ((sfp = fopen(sname, "rb")) == NULL)			
		printf("\a原文件打开失败。\n");
	else 
	{
		if ((dfp = fopen(dname, "wb")) == NULL)		
			printf("\a目标文件打开失败。\n");
		else 
		{
			while (n = fread(&ch, sizeof(int), 1, sfp) > 0)
			{
				fwrite(&ch, sizeof(int), 1, dfp);
			}
			fclose(dfp);						
		}
		fclose(sfp);							
		printf("done!\n");
	}
	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语言 第3版 入门篇》的第六章包括两个练习题,分别是练习6-3和练习6-6。练习6-3是一个计算输入数的立方的程序,代码如下: #include<stdio.h> int cube(int x) { return x*x*x; } int main(void) { int a ; printf("请输入A的值:"); scanf("%d",&a); printf("该值的立方为:%d",cube(a)); return 0; } 练习6-6是一个根据输入的n值,按照次数发出响铃的程序,代码如下: #include<stdio.h> void alert(int ch, int n) { putchar(ch); } int main(void) { int a, i; printf("请输入n值:"); scanf("%d",&a); for(i=1;i<=a;i++) { alert('\a',i); } return 0; }<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [明解C语言(第3版)入门篇.[日]柴田望洋(带详细书签).pdf 【半高清】](https://download.csdn.net/download/kxjrzyk/11049496)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [明解C语言入门篇)第六章](https://blog.csdn.net/a237664181a/article/details/89043758)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值