c语言程序设计的水电费问题,c语言程序设计(周信东版)的综合程序设计如下:编写程序,从键盘输入某楼6家住户某月的水电消耗及水费和电饵/span>...

满意答案

02ae427d08e371d7e90d5b995e828d6d.png

zwakmh14

2013.05.31

02ae427d08e371d7e90d5b995e828d6d.png

采纳率:41%    等级:12

已帮助:6060人

#include

#include

#include

int main()

{

char user[4];

char waterNum[4];

char elecNum[4];

char inlinebuf[32];

char outlinebuf[32];

char pbuf[256];

FILE *fp1;

FILE *fp2;

int WaterNum=0;

int ElecNum=0;

int length =0;

int m=0;

int n=0;

int weishuflag=0;

int flagnum=0;

int i=0;

int j=0;

float waterPrice=0.0;

float elecPrice=0.0;

if((fp1=fopen("./input.dat","r"))==NULL)

{

printf("open input data error!\n");

return -1;

}

if((fp2=fopen("./change.dat","w"))==NULL)

{

printf("open input data error!\n");

return -1;

}

fseek(fp1,0,SEEK_END);

length = ftell(fp1);

fseek(fp1,0,SEEK_SET);

memset(pbuf,0,sizeof(pbuf));

printf("the input data length == %d\n",length);

fread(pbuf,length,1,fp1);

fseek(fp2, 0, SEEK_SET);

sprintf(outlinebuf,"住户\t\t水费\t\t电费\n");

fputs(outlinebuf, fp2);

while(length--)

{

if(pbuf[m]!='\n')

{

inlinebuf[n]=pbuf[m];

n++;

}

else

{

j = 0;

inlinebuf[n++]='\t'; //为处理每一行的最后一个数据(即用电量),需加入一个tab或空格键

inlinebuf[n] = '\0';//一行数据结束

while( inlinebuf[j] != '\0')

{

switch(flagnum)

{

case 0: //用户

if(inlinebuf[j] != ' ' && inlinebuf[j] != '\t')

{

user[i]=inlinebuf[j];

i++;

}

else

{

user[i]='\0';

flagnum++;//表示之前获取的是住户,下两个数据分别获取用水量与用电量

i=0;

}

break;

case 1: //用水量

if(inlinebuf[j] != ' ' && inlinebuf[j] != '\t')

{

waterNum[i]=inlinebuf[j];

i++;

}

else

{

waterNum[i]='\0';

weishuflag=i;//表示当前数据的位数,住户、用水量、用电量均限制为三位数以内

flagnum++;//表示下个数据获取的是用电量

if(weishuflag == 1)

{

WaterNum = waterNum[0] - 48;

}

else if(weishuflag == 2)

{

WaterNum = (waterNum[0] - 48)*10 + (waterNum[1] - 48);

}

else

{

WaterNum = (waterNum[0] - 48)*100 + (waterNum[1] - 48)*10 + (waterNum[2] - 48);

}

i=0;

weishuflag = 0;

}

break;

case 2: //用电量

if(inlinebuf[j] != ' ' && inlinebuf[j] != '\t')

{

elecNum[i]=inlinebuf[j];

i++;

}

else

{

elecNum[i]='\0';

weishuflag=i;//表示当前数据的位数,住户、用水量、用电量均限制为三位数以内

flagnum = 0;//表示一行三个数据获取完毕

if(weishuflag == 1)

{

ElecNum = elecNum[0] - 48;

}

else if(weishuflag == 2)

{

ElecNum = (elecNum[0] - 48)*10 + (elecNum[1] - 48);

}

else

{

ElecNum = (elecNum[0] - 48)*100 + (elecNum[1] - 48)*10 + (elecNum[2] - 48);

}

i=0;

weishuflag = 0;

}

break;

default:

break;

}

j++;

}

waterPrice = (float)WaterNum * 1.5;

elecPrice = (float)ElecNum * 0.5;

printf("The user == %s,waterNum == %d,elecNum == %d\n",user,WaterNum,ElecNum);//打印用户,用水量,用电量

printf("The user == %s,waterPrice == %.1f,elecPrice == %.1f\n\n",user,waterPrice,elecPrice);//打印用户,水费,电费

sprintf(outlinebuf,"%3s \t\t%4.1f \t\t%4.1f\n",user,waterPrice,elecPrice);

fputs(outlinebuf, fp2);//写入每一行数据(数据格式为 住户 + 水费 + 电费)

memset(outlinebuf,0,sizeof(outlinebuf));

memset(inlinebuf,0,sizeof(inlinebuf));

n=0; //一行数据处理结束,开始新的一行

}

m++;

}

fclose(fp1);

fclose(fp2);

return 0;

}

input.dat(每一行数据分别为住户 用水量 用电量,以空格或tab键隔开,且每行数据必须以回车结尾)

1015150

201490

3014120

401378

501460

6016105

charge.dat (输出文件格式如下)

住户水费电费

101 7.5 75.0

201 6.0 45.0

301 6.0 60.0

401 4.5 39.0

501 6.0 30.0

601 9.0 52.5

69分享举报

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值