跪求c语言高手解答、、、
编程环境dev-c++作业1:有两个磁盘文件“A”和“B”,各存放一行字母,今要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件“C”中去。再从该文件中读入...
编程环境dev-c++
作业1:有两个磁盘文件“A”和“B”,各存放一行字母,今要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件“C”中去。再从该文件中读入这些数据,将其中的小写字母转换成大写字母后在显示器上输出。
作业2:先建立一个磁盘文件“emploee”,内存放职工的数据。每个职工的数据包括职工姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度。今要求将职工名、工资的信息单独抽出来另建一个简明的职工工资文件。从 “职工工资文件”中删去一个职工的数据,再存回原文件。 展开
作业1:有两个磁盘文件“A”和“B”,各存放一行字母,今要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件“C”中去。再从该文件中读入这些数据,将其中的小写字母转换成大写字母后在显示器上输出。
作业2:先建立一个磁盘文件“emploee”,内存放职工的数据。每个职工的数据包括职工姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度。今要求将职工名、工资的信息单独抽出来另建一个简明的职工工资文件。从 “职工工资文件”中删去一个职工的数据,再存回原文件。 展开
1个回答
展开全部
作业1:
#include<stdio.h>
#include<stdlib.h>
main()
{
FILE *fp;
int i,j,k;
char c[50],t,ch;
if((fp=fopen("A.txt","r"))==NULL)
{
printf("can't open the file\n");
exit(0);
}
printf("File A:");
for(i=0;(ch=fgetc(fp))!=EOF;i++)
{
c[i]=ch;
putchar(c[i]);
}
fclose(fp);
if((fp=fopen("B.txt","r"))==NULL)
{
printf("\ncan't open the file");
exit(0);
}
printf("\nFile B:");
for(i=i;(ch=fgetc(fp))!=EOF;i++)
{
c[i]=ch;
putchar(c[i]);
}
fclose(fp);
k=i;
for(i=0;i<k;i++)
for(j=i;j<k;j++)
if(c[i]>c[j])
{
t=c[i];
c[i]=c[j];
c[j]=t;
}
printf("\nFile C:");
fp=fopen("C.txt","w");
for(i=0;i<k;i++)
{
fputc(c[i],fp);
putchar(c[i]);
}
putchar(10);
fclose(fp);
getch();
}
作业2:
(1)先建立磁盘文件employee
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 4
struct employee
{
char name[10];
int num;
char sex;
int age;
char addr[20];
int salary;
char health[8];
char class[10];
}em[N];
struct emp
{
char name[10];
int salary;
}em_case[N];
void main()
{
int i,j;
FILE *fp;
printf("姓名 工号 性别 年龄 住址 工资 健康 文化\n");
for(i=0;i<N;i++)
scanf("%s %d %c %d %s %d %s %s",em[i].name,&em[i].num,&em[i].sex,&em[i].age,em[i].addr,&em[i].salary,em[i].health,em[i].class);
if((fp=fopen("employee.txt","w"))==NULL)
{
printf("can not open the file\n");
exit(0);
}
for(i=0;i<N;i++)
if(fwrite(&em[i],sizeof(struct employee),1,fp)!=1)
printf("error\n");
fclose(fp);
if((fp=fopen("employee.txt","r"))==NULL)
{
printf("can not open the file\n");
exit(0);
}
printf("\nname NO. sex age addr salary health class\n");
for(i=0;fread(&em[i],sizeof(struct employee),1,fp)!=0;i++)
{
printf("%4s%6d%4c%6d%8s%6d%8s%10s\n",em[i].name,em[i].num,em[i].sex,em[i].age,em[i].addr,em[i].salary,em[i].health,em[i].class);
strcpy(em_case[i].name,em[i].name);
em_case[i].salary=em[i].salary;
}
fclose(fp);
printf("\nname salary\n");
if((fp=fopen("emp_salary.txt","wb"))==NULL)
{
printf("can not open the file\n");
exit(0);
}
for(i=0;i<N;i++)
{
if(fwrite(&em_case[i],sizeof(struct emp),1,fp)!=1)
printf("error\n");
printf("%4s%6d\n",em_case[i].name,em_case[i].salary);
}
fclose(fp);
getch();
}
(2)再从employee文件中读取数据
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct employee
{
char name[10];
int salary;
}emp[4];
main()
{
FILE *fp;
int i,j,k,flag=1;
char name[10];
int salary;
if((fp=fopen("emp_salary.txt","rb"))==NULL)
{
printf("can not open file\n");
exit(0);
}
printf("original data:\n");
for(i=0;fread(&emp[i],sizeof(struct employee),1,fp)!=0;i++)
printf("%4s%6d\n",emp[i].name,emp[i].salary);
fclose(fp);
k=i;
printf("Input name deleted:");
scanf("%s",name);
for(i=0;i<k;i++)
if(strcmp(name,emp[i].name)==0)
{
for(j=i;j<k-1;j++)
{
strcpy(emp[j].name,emp[j+1].name);
emp[j].salary=emp[j+1].salary;
}
flag=0;
}
if(flag==0) k=k-1;
else printf("\nNot found!");
printf("\nNow,the content of file:\n");
fp=fopen("emp_salary.txt","wb");
for(i=0;i<k;i++)
fwrite(&emp[i],sizeof(struct employee),1,fp);
fclose(fp);
fp=fopen("emp_salary.txt","rb");
for(i=0;fread(&emp[i],sizeof(struct employee),1,fp)!=0;i++)
printf("%4s%6d\n",emp[i].name,emp[i].salary);
fclose(fp);
getch();
}
#include<stdio.h>
#include<stdlib.h>
main()
{
FILE *fp;
int i,j,k;
char c[50],t,ch;
if((fp=fopen("A.txt","r"))==NULL)
{
printf("can't open the file\n");
exit(0);
}
printf("File A:");
for(i=0;(ch=fgetc(fp))!=EOF;i++)
{
c[i]=ch;
putchar(c[i]);
}
fclose(fp);
if((fp=fopen("B.txt","r"))==NULL)
{
printf("\ncan't open the file");
exit(0);
}
printf("\nFile B:");
for(i=i;(ch=fgetc(fp))!=EOF;i++)
{
c[i]=ch;
putchar(c[i]);
}
fclose(fp);
k=i;
for(i=0;i<k;i++)
for(j=i;j<k;j++)
if(c[i]>c[j])
{
t=c[i];
c[i]=c[j];
c[j]=t;
}
printf("\nFile C:");
fp=fopen("C.txt","w");
for(i=0;i<k;i++)
{
fputc(c[i],fp);
putchar(c[i]);
}
putchar(10);
fclose(fp);
getch();
}
作业2:
(1)先建立磁盘文件employee
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 4
struct employee
{
char name[10];
int num;
char sex;
int age;
char addr[20];
int salary;
char health[8];
char class[10];
}em[N];
struct emp
{
char name[10];
int salary;
}em_case[N];
void main()
{
int i,j;
FILE *fp;
printf("姓名 工号 性别 年龄 住址 工资 健康 文化\n");
for(i=0;i<N;i++)
scanf("%s %d %c %d %s %d %s %s",em[i].name,&em[i].num,&em[i].sex,&em[i].age,em[i].addr,&em[i].salary,em[i].health,em[i].class);
if((fp=fopen("employee.txt","w"))==NULL)
{
printf("can not open the file\n");
exit(0);
}
for(i=0;i<N;i++)
if(fwrite(&em[i],sizeof(struct employee),1,fp)!=1)
printf("error\n");
fclose(fp);
if((fp=fopen("employee.txt","r"))==NULL)
{
printf("can not open the file\n");
exit(0);
}
printf("\nname NO. sex age addr salary health class\n");
for(i=0;fread(&em[i],sizeof(struct employee),1,fp)!=0;i++)
{
printf("%4s%6d%4c%6d%8s%6d%8s%10s\n",em[i].name,em[i].num,em[i].sex,em[i].age,em[i].addr,em[i].salary,em[i].health,em[i].class);
strcpy(em_case[i].name,em[i].name);
em_case[i].salary=em[i].salary;
}
fclose(fp);
printf("\nname salary\n");
if((fp=fopen("emp_salary.txt","wb"))==NULL)
{
printf("can not open the file\n");
exit(0);
}
for(i=0;i<N;i++)
{
if(fwrite(&em_case[i],sizeof(struct emp),1,fp)!=1)
printf("error\n");
printf("%4s%6d\n",em_case[i].name,em_case[i].salary);
}
fclose(fp);
getch();
}
(2)再从employee文件中读取数据
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct employee
{
char name[10];
int salary;
}emp[4];
main()
{
FILE *fp;
int i,j,k,flag=1;
char name[10];
int salary;
if((fp=fopen("emp_salary.txt","rb"))==NULL)
{
printf("can not open file\n");
exit(0);
}
printf("original data:\n");
for(i=0;fread(&emp[i],sizeof(struct employee),1,fp)!=0;i++)
printf("%4s%6d\n",emp[i].name,emp[i].salary);
fclose(fp);
k=i;
printf("Input name deleted:");
scanf("%s",name);
for(i=0;i<k;i++)
if(strcmp(name,emp[i].name)==0)
{
for(j=i;j<k-1;j++)
{
strcpy(emp[j].name,emp[j+1].name);
emp[j].salary=emp[j+1].salary;
}
flag=0;
}
if(flag==0) k=k-1;
else printf("\nNot found!");
printf("\nNow,the content of file:\n");
fp=fopen("emp_salary.txt","wb");
for(i=0;i<k;i++)
fwrite(&emp[i],sizeof(struct employee),1,fp);
fclose(fp);
fp=fopen("emp_salary.txt","rb");
for(i=0;fread(&emp[i],sizeof(struct employee),1,fp)!=0;i++)
printf("%4s%6d\n",emp[i].name,emp[i].salary);
fclose(fp);
getch();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询