gpt4 book ai didi

C 基本菜单驱动程序,无全局变量的气体计算器

转载 作者:行者123 更新时间:2023-11-30 16:34:32 37 4
gpt4 key购买 nike

作为作业的一部分,我编写了一个 Gas 成本计算器。该程序输出当前里程以及汽油消耗总额。

这是我的代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define PAUSE system("pause")
#define CLS system("cls")

// Declare variables
float currentMilage = 0.0, pricePerGallon = 0.0, MPG = 0.0, totalCost = 0.0;

// Function to calculate the total cost
void calculateTotalCost() {
totalCost=(currentMilage/MPG)*pricePerGallon;
}

//Function to read input from user
void userInput() {
printf("Enter the current milage: ");
scanf("%f", &currentMilage);

printf("Enter the price per gallon: ");
scanf("%f", &pricePerGallon);

printf("Enter the MPG: ");
scanf("%f", &MPG);
calculateTotalCost();
}

// Function to calculate the cost for trip
float calculateCost(float miles) {
float cost;
cost=(miles/MPG)*pricePerGallon;
return cost;
}

//Function to display cost
void displayCost(float cost) {
printf("Cost for trip: %.2f\n", cost);
}

//Function to update the current milage
void updateCurrentMilage(float miles) {
currentMilage = currentMilage + miles;
}

// Function to calcuate the total spend on gas
void updateTotalSpend(float cost) {
totalCost=totalCost+cost;
}

// Function takeTrip
void takeTrip() {
// Declare variables
float miles,cost;

// Read input from user
printf("Enter the miles driven: ");
scanf("%f", &miles);

// Caliculate the total cost
cost=calculateCost(miles);

// Display the cost
displayCost(cost);

// Update the current milage
updateCurrentMilage(miles);

// Update the total spend
updateTotalSpend(cost);
}

// Function to display the menu
int menu() {
int ch;

printf("\n\n");
printf("1: Take a trip\n");
printf("2: Display the current milage\n");
printf("3: Display the total spend\n");
printf("4: Exit\n");

printf("Enter the choice: ");
scanf("%d",&ch);
return ch;
}

// Function to display the current milage
void displayCurrentMilage() {
printf("The current milage is %.2f\n",currentMilage);
}

// Function to display the total spend
void displayTotalSpend() {
printf("The total spend on gas is %.2f\n",totalCost);
}

// Main class
int main() {
int ch=0;

// Read the user input
userInput();

// while start
while(ch!=4) {
ch=menu();
switch(ch) {
case 1:
takeTrip();

PAUSE;
CLS;

break;

case 2:
displayCurrentMilage();

PAUSE;
CLS;

break;

case 3:
displayTotalSpend();

PAUSE;
CLS;

break;

case 4:
break;
}
} // while end

getchar();
return 0;

} // end main

该程序运行得非常好,我刚刚意识到我不允许使用全局变量或 goto 语句。老师说我不能使用全局变量、函数原型(prototype)、调用函数传递值并通过函数返回内容。我怎样才能做到这一点?谢谢!

最佳答案

移动全局声明:

float currentMilage = 0.0, pricePerGallon = 0.0, MPG = 0.0, totalCost = 0.0;

更改:

void calculateTotalCost() { 

totalCost=(currentMilage/MPG)*pricePerGallon;
}

float calculateTotalCost(float currentMilage, float MPG, float pricePerGallon) { 

float totalCost = (currentMilage/MPG)*pricePerGallon;
return totalCost;
}

修改进行计算的函数以获取更多参数并返回值:

 void updateCurrentMilage(float miles) { 

currentMilage = currentMilage + miles;
}

喜欢:

  float updateCurrentMilage(float miles, float currentMilage) { 

float current = currentMilage + miles;
return current;
}

使用返回值更新相应的变量;

    currentMilage = updateCurrentMilage(miles,currentMilage); 

对您想要计算的所有相关数据重复此过程。

关于C 基本菜单驱动程序,无全局变量的气体计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49342268/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com