当前位置:网站首页>程序设计综合实验三
程序设计综合实验三
2022-07-27 17:17:00 【千干】
学习教材《C程序设计》第十章关于文件的操作,完成下面作业:
文件资源见本人上传的资源。
(1)算法描述:将“Zhangsan M 24”按照字符串、字符和整形数的格式写入文本文件file.txt,然后从file.txt中读出内容,并打印。
(2)算法描述:将“hello world!”写入文本文件file.txt,然后从file.txt中读出内容,并打印。
(3)算法描述: 用文本文件保存一段英文文本。统计各字母在文本中出现的次数(区分大小写),并按英文字母序打印统计结果。
(4)现有两个文本文件 file1.txt 和 file2.txt。file1 中第一列为姓名, 第二列为英语成绩; file2 中第一列为姓名, 第二列为数学成绩。通过程序关联 file2 中与 file1 对应的姓名生成 file3.out 文件。file3.out 文件第一列为 姓名,第二列为英语成绩,第三列为数学成绩,第四列为平均成绩。
该博文仅供参考
(1)(2)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
FILE *fp;
int i,l;
char c[20]="Zhangsan M 24\n",fn[10]="file.txt",str[100];
fp=fopen(fn,"w");
fputs("Zhangsan M 24\n",fp);
l=strlen(c);
for(i=0;i<l;i++) fputc(c[i],fp);
fprintf(fp,"%d",c);
fputc('\n',fp);
fclose(fp);
fp=fopen(fn,"r");
fgets(str,100,fp);
while(!feof(fp))
{
printf("%s",str);
fgets(str,100,fp);
}
fclose(fp);
fp=fopen(fn,"a");
fputs("hello world\n",fp);
fclose(fp);
fp=fopen(fn,"r");
fgets(str,100,fp);
while(!feof(fp))
{
printf("%s",str);
fgets(str,100,fp);
}
fclose(fp);
return 0;
}
(3)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
FILE *fp;
char en[1024],fn[20]="English.txt";
int i,j,len,k;
struct letterlist
{
char letter;
int count;
}l[52],t;
fp=fopen(fn,"r");
fgets(en,1024,fp);
fclose(fp);
len=strlen(en);
l[0].letter=en[0];
l[0].count=1;
k=1;
for(i=1;i<len;i++)
{
if(en[i]>='A'&&en[i]<='Z'||en[i]>='a'&&en[i]<='z')
{
for(j=0;j<k;j++)
{
if(l[j].letter==en[i])
{
l[j].count++;
break;
}
}
if(j==k)
{
l[k].letter=en[i];
l[k].count=1;
k++;
}
}
}
for(i=0;i<k-1;i++)
{
for(j=0;j<k-1-i;j++)
{
if(l[j].letter>l[j+1].letter)
{
t=l[j];l[j]=l[j+1];l[j+1]=t;
}
}
}
for(i=0;i<k;i++)
{
printf("%c 出现的次数为 %d",l[i].letter,l[i].count);
if(i!=k-1) printf("\n");
}
return 0;
}
(4) file1.txt 和 file2.txt两个文件需要自行创建
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct file
{
char name[20];
int score;
};
int read(struct file f[],char fn[])
{
FILE *fp;
char c[30];
int i,k=0,l,m,n,s;
fp=fopen(fn,"r");
while(!feof(fp))
{
m=0;s=0;n=0;
fgets(c,30,fp);
l=strlen(c);
for(i=0;i<l;i++)
{
if(c[i]>='A'&&c[i]<='Z') m++;
if(c[i]>='a'&&c[i]<='z') m++;
if(c[i]>='0'&&c[i]<='9') n++;
}
strcpy(f[k].name,c);
for(i=0;i<l;i++)
{
if(f[k].name[i]==' '||f[k].name[i]>'0'&&f[k].name[i]<'9') f[k].name[i]='\0';
}
for(i=m+1;i<m+1+n;i++)
{
s=s*10+c[i]-'0';
}
f[k].score=s;
k++;
}
fclose(fp);
return k;
}
int main()
{
FILE *fp;
int i,j,n;
char fn1[10]="file1.txt",fn2[10]="file2.txt",fn[10]="file3.out";
struct list
{
char name[20];
int English;
int math;
float average;
}l[10];
struct file f1[10],f2[10];
n=read(f1,fn1);n=read(f2,fn2);
for(i=0;i<n;i++)
{
strcpy(l[i].name,f1[i].name);
l[i].English=f1[i].score;
for(j=0;j<n;j++)
{
if(strcmp(l[i].name,f2[j].name)==0)
{
l[i].math=f2[j].score;
}
}
l[i].average=(l[i].English+l[i].math)/2.0;
}
fp=fopen(fn,"w");
for(i=0;i<n;i++)
{
fprintf(fp,"%s %d %d %.1f",l[i].name,l[i].English,l[i].math,l[i].average);
if(i!=n-1) fputc('\n',fp);
}
fclose(fp);
return 0;
}
边栏推荐
- Intel's process roadmap for the next 10 years is exposed: 1.4nm process will be launched in 2029! How?
- 深度主动学习综述2020
- 【深度学习基础知识 - 49】Kmeans
- Hyperledger caliper is built on fabric for performance test
- The go zero singleton service uses generics to simplify the registration of handler routes
- 王牌代码静态测试工具Helix QAC 2022.2 中的新增功能(1)
- The valuation exceeds 15.6 billion yuan! Huaqin communication completed the round B financing of 1billion yuan! Qualcomm venture capital, Intel Capital led investment
- What's new in helix QAC 2022.2, the ace code static testing tool (1)
- 【深度学习基础知识 - 44】逻辑回归实现多分类的方法
- Matplotlib (basic usage)
猜你喜欢
随机推荐
链表~~~
【深度学习基础知识 - 42】逻辑回归详解
Detailed interpretation of IEC104 protocol (I) protocol structure
Map and set
Map和Set
ReferenceError: __ dirname is not defined in ES module scope
Application pool has been disabled
The valuation exceeds 15.6 billion yuan! Huaqin communication completed the round B financing of 1billion yuan! Qualcomm venture capital, Intel Capital led investment
pytorch tensor的基本函数
GestureOverlayView(手势识别2)
10.31静态路由的扩展配置
11.5.OSPF
Turn Hyper-V on and off
SystemService(系统服务)
BroadcastReceiver(广播)
Matplotlib(基本用法)
Systemservice (system service)
【日常积累 - 07】cuda多版本切换
[basic knowledge of deep learning - 42] detailed explanation of logistic regression
【深度学习基础知识 - 49】Kmeans









