当前位置:编程学习 > C/C++ >>

c语言编程问题

答案: 

大抵上就是这样的了,好累吖!!!你多少也把分给我吧 

源程序:

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

struct dormitory

{

       char addr[10];     //宿舍地址

       float water;  //水耗

       float wmoney;     //水费

       float electric;      //电耗

       float emoney;     //电费

       float total;    //总额

};

int n;

FILE *fp1,*fp2;

char filename1[10];

char filename2[10];

void output();

void count();

void main()

{

       int k;

       printf("\n请输入你要打开的数据存放文件名:");

       gets(filename1);

       if((fp1=fopen(filename1,"r"))==NULL)

       {

              printf("\n当前没有此通讯录文件,现在是否执行创建?:\n输入1创建,输入0退出:");

              scanf("%d",&k);

              while(k!=0&&k!=1)

              {     

                     printf("\n输入有误,请再次输入一个值:");

                     scanf("%d",&k);

              }

           if(k==1)

                     {output();}

              else

                     if(k==0)

                     {

                            printf("\n您选择了退出,现在程序即将关闭!\n");

                            exit(0);

                     }

       }

       count();

}

 

void output()

{

       struct dormitory one;

       int i;

       printf("你已经成功创建文件%s\n",filename1);

       if((fp1=fopen(filename1,"w"))==NULL)

       {

              printf("\n不能建立此类文件,程序即将关闭!\n");

              exit(0);

       }

//fprintf(fp1,"%-10.2f%-10.2f\n","1.5","0.5");

       printf("请输入你要统计宿舍信息的个数:");

       scanf("%d",&n);

       printf("现在,请输入这%d宿舍号、水耗()、电耗()\n",n);

       for(i=0;i<n;i++)

       {

              scanf("%s%f%f",one.addr,&one.water,&one.electric);

              fprintf(fp1,"%-10s%-10.2f%-10.2f\n",one.addr,one.water,one.electric);

       }

       fclose(fp1);

}

 

void count()

{

 

       int i;

       struct dormitory one,total={"total",0,0,0,0,0};

       printf("\n成功读取%s文件信息,现在开始进行统计:\n请输入1继续进行:",filename1);

       scanf("%d",&i);

       if(i==1)

       {

              printf("请输入统计数据存放文件名:");

              scanf("%s",filename2);

              if((fp2=fopen(filename2,"r"))==NULL)

//检查是否存在变量filename2的文件

              {     

                     printf("程序已经为你创建了一个%s文件\n",filename2);

              }

              if((fp1=fopen(filename1,"r"))==NULL)

              {

                     printf("\n无法打开%s文件,请检查文件是否已经创建,程序关闭!\n",filename1);

                     exit(0);

              }

              if((fp2=fopen(filename2,"w"

上一个:C语言进阶学习
下一个:C语言程序设计

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,