您知道如何使我的摘要功能正常工作吗?因为它一直给我值 0

  • 本文关键字:因为 工作 何使我 常工作 功能 c
  • 更新时间 :
  • 英文 :


所以我想在我的摘要函数中对所有价格求和,但我总是得到 0 的总值。我有 尝试将虚空更改为int,但没有运气。或者也许我以错误的方式做了。该程序是要知道电子设备的电费总额,因此它应该能够将每个设备的所有价格相加。我很感激你的帮助。 这是我的代码:

#include <stdio.h>
#include <string.h>
void countAC(int PK)
{
float durationAC;
float priceAC;
int qttAC;
printf("How long do you use it a day? (in hours)n");
scanf("%f",&durationAC);
printf("How many do you use in a day? n");
scanf("%d",&qttAC);
if (PK==1){
priceAC=durationAC*qttAC*400*1467/1000;
}
else if (PK==2){
priceAC=durationAC*qttAC*600*1467/1000;
}
else if (PK==3){
priceAC=durationAC*qttAC*840*1467/1000;
}
else if (PK==4){
priceAC=durationAC*qttAC*540*1467/1000;
}
else {
printf("wait, that's illegaln");
}
printf("AC(s) electricity cost/day : Rp %f",priceAC);
}
void AC()
{
int PK;
printf("AC Types :n");
printf("1. 1/2 PKn");
printf("2. 3/4 PKn");
printf("3. 1 PKn");
printf("4. Invertern");
printf("What types of AC do you use? ");
scanf("%d",&PK);
countAC(PK);
}
void countLamp(int LT)
{
float durationLamp;
float priceLamp;
int qttLamp;
printf("How long do you use it a day? (in hours)n");
scanf("%f",&durationLamp);
printf("How many do you use in a day? n");
scanf("%d",&qttLamp);
if (LT==1){
priceLamp=durationLamp*qttLamp*60/1000*1467;
}
else if (LT==2){
priceLamp=durationLamp*qttLamp*42/1000*1467;
}
else if (LT==3){
priceLamp=durationLamp*qttLamp*12/1000*1467;
}
else if (LT==4){
priceLamp=durationLamp*qttLamp*10/1000*1467;
}
else {
printf("wait, that's illegaln");
}
printf("Lamp(s) electricity cost/day : Rp %f",priceLamp);
}
void Lamp()
{
int LT;
printf("Lamp Types :n");
printf("1. Standardn");
printf("2. Halogenn");
printf("3. CFLn");
printf("4. LEDn");
printf("What types of Lamp do you use? ");
scanf("%d",&LT);
countLamp(LT);
}
void countFan(int FT)
{
float durationFan;
float priceFan;
int qttFan;
printf("How long do you use it a day? (in hours)n");
scanf("%f",&durationFan);
printf("How many do you use in a day? n");
scanf("%d",&qttFan);
if (FT==1){
priceFan=durationFan*qttFan*50/1000*1467;
}
else if (FT==2){
priceFan=durationFan*qttFan*30/1000*1467;
}
else {
printf("wait, that's illegaln");
}
printf("Fan(s) electricity cost/day : Rp %f",priceFan);
}
void Fan()
{
int FT;
printf("Fan Types :n");
printf("1. Large Fann");
printf("2. Small Fann");
printf("What types of Fan do you use? ");
scanf("%d",&FT);
countFan(FT);
}
void countRef(int RT)
{
float durationRef;
float priceRef;
int qttRef;
printf("How long do you use it a day? (in hours)n");
scanf("%f",&durationRef);
printf("How many do you use in a day? n");
scanf("%d",&qttRef);
if (RT==1){
priceRef=durationRef*qttRef*125/1000*1467;
}
else if (RT==2){
priceRef=durationRef*qttRef*80/1000*1467;
}
else {
printf("wait, that's illegaln");
}
printf("Refrigerator(s) electricity cost/day : Rp %f",priceRef);
}
void Ref()
{
int RT;
printf("Fan Types :n");
printf("1. Large Refrigeratorn");
printf("2. Small Refrigeratorn");
printf("What types of Refrigerator do you use? ");
scanf("%d",&RT);
countRef(RT);
}
void countTV(int TT)
{
float durationTV;
float priceTV;
int qttTV;
printf("How long do you use it a day? (in hours)n");
scanf("%f",&durationTV);
printf("How many do you use in a day? n");
scanf("%d",&qttTV);
if (TT==1){
priceTV=durationTV*qttTV*50/1000*1467;
}
else if (TT==2){
priceTV=durationTV*qttTV*110/1000*1467;
}
else {
printf("wait, that's illegaln");
}
printf("Television(s) electricity cost/day : Rp %f",priceTV);
}

void TV()
{
int TT;
printf("TV Types :n");
printf("1. LED TVn");
printf("2. LCD TVn");
printf("What types of Television do you use? ");
scanf("%d",&TT);
countTV(TT);
}
void WM()
{
float durationWM;
float priceWM;
int qttWM;
printf("How long do you use it a day? (in hours)n");
scanf("%f",&durationWM);
printf("How many do you use in a day? n");
scanf("%d",&qttWM);
priceWM=durationWM*qttWM*245/1000*1467;
printf("Washing Machine(s) electricity cost/day : Rp %f",priceWM);
}
void Menu()
{
int menu_opt;
printf("Electronic Devices :n");
printf("1. Air Conditionern");
printf("2. Lampn");
printf("3. Fann");
printf("4. Refrigeratorn");
printf("5. Televisionn");
printf("6. Washing Machinen");
printf("Please choose your electronic device : ");
scanf("%d",&menu_opt);
if(menu_opt==1){
AC();
}
else if(menu_opt==2){
Lamp();
}
else if(menu_opt==3){
Fan();
}
else if(menu_opt==4){
Ref();
}
else if(menu_opt==5){
TV();
}
else if(menu_opt==6){
WM();
}
else {
printf("wait, that's illegaln");
}
}
void Summary(float priceAC,float priceLamp,float priceFan,float priceRef,float priceTV,float priceWM)
{
float totalprice;
totalprice=priceAC+priceLamp+priceFan+priceRef+priceTV+priceWM;
printf("AC Electricity Cost In A Day : Rp %fn",priceAC);
printf("Total Electricity Cost : Rp %fn",totalprice);
}
int main ()
{
int run=1,choice;
float priceAC,priceLamp,priceFan,priceRef,priceTV,priceWM,totalprice;
char answer[5];
char a[]="yes",b[]="no",c[]="Yes",d[]="No",e[]="YES",f[]="NO";
printf ("Hi! In this program, you can predict your electricity cost.nContinue? (type 'yes' to continue or 'no' to stop)n");
scanf ("%s", answer);
if (strcmp(a,answer)==0||strcmp(c,answer)==0||strcmp(e,answer)==0)
{
do{
Menu();
printf("nDo you want to add more device?t1.Yest2.Non");
scanf("%d",&choice);
run=choice;
}while(run==1);
Summary(priceAC,priceLamp,priceFan,priceRef,priceTV,priceWM);
}
else if (strcmp(b,answer)==0||strcmp(d,answer)==0||strcmp(f,answer)==0)
{
printf("Okay, see you later!"); 
}
else 
{
printf ("Sorry, I don't understand what you say");
}
return 0;
}

您声明了变量

float priceAC, priceLamp, priceFan, priceRef, priceTV, priceWM, totalprice;

在你的主函数中,然后总结它们调用

Summary(priceAC, priceLamp, priceFan, priceRef, priceTV, priceWM);

但它们永远不会被赋值,因为它们是局部变量。

例如,如果在countAC中计算priceAC,则它与 main 函数中的priceAC不同。您可以使用全局变量在函数之间交换这些值。

你的函数Summary(priceAC,priceLamp,priceFan,priceRef,priceTV,priceWM);打印总是 0 的原因是因为你在int main()中声明了变量:

float priceAC,priceLamp,priceFan,priceRef,priceTV,priceWM,totalprice;

但是你永远不会给它们分配一个值,所以当你打破do while循环并达到Summary你传递给它的所有参数都没有实际值。

要为Menu()中要求的设备添加Summary,您需要做的是将每个设备的值保存在变量上,并将该值发送到您的Summary()

这样,您将获得所需的输出。

希望这对您有所帮助。

相关内容

最新更新