yangzhixiaohuli 发表于 2023-10-17 17:49:33

求助一下

给定四种水果,分别是苹果(apple)、梨(pear)、桔子(orange)、葡萄(grape),单价分别对应为3.00元/公斤、2.50元/公斤、4.10元/公斤、10.20元/公斤。

首先在屏幕上显示以下菜单:

apple
pear
orange
grape
exit
用户可以输入编号1~4查询对应水果的单价。当连续查询次数超过5次时,程序应自动退出查询;不到5次而用户输入0即退出;输入其他编号,显示价格为0。

输入格式:
输入在一行中给出用户连续输入的若干个编号。

输出格式:
首先在屏幕上显示菜单。然后对应用户的每个输入,在一行中按格式“price = 价格”输出查询结果,其中价格保留两位小数。当用户连续查询次数超过5次、或主动输入0时,程序结束。

输入样例1:
3 -1 0 2
输出样例1:
apple
pear
orange
grape
exit
price = 4.10
price = 0.00
输入样例2:
1 2 3 3 4 4 5 6 7 8
输出样例2:
apple
pear
orange
grape
exit
price = 3.00
price = 2.50
price = 4.10
price = 4.10
price = 10.20

这个题用c或者c++怎么做

陶远航 发表于 2023-10-17 17:50:15

这个问题可以通过使用数组和循环来解决。以下是一个使用C++编写的示例代码:

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    double prices[] = {0, 3.00, 2.50, 4.10, 10.20}; // 价格数组,0位置空出来,方便后续直接使用输入的编号索引
    int count = 0; // 查询次数计数器

    // 显示菜单
    cout << \" apple\" << endl;
    cout << \" pear\" << endl;
    cout << \" orange\" << endl;
    cout << \" grape\" << endl;
    cout << \" exit\" << endl;

    while (count < 5) { // 当查询次数小于5时,继续循环
      int input;
      cin >> input; // 接收用户输入

      if (input == 0) { // 如果输入为0,退出程序
            break;
      }

      if (input < 0 || input > 4) { // 如果输入的编号不在1~4之间,价格为0
            cout << \"price = \" << fixed << setprecision(2) << 0.00 << endl;
      } else { // 否则,输出对应的价格
            cout << \"price = \" << fixed << setprecision(2) << prices << endl;
      }

      count++; // 每次查询后,计数器加1
    }

    return 0;
}


这段代码首先定义了一个包含水果价格的数组,然后显示菜单。然后进入一个循环,循环条件是查询次数小于5。在循环中,首先接收用户的输入,然后根据输入的编号输出对应的价格。如果输入的编号不在1~4之间,价格为0。每次查询后,查询次数加1。当查询次数达到5次,或者用户输入0时,程序退出。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案

isdkz 发表于 2023-10-17 17:50:48

这个问题可以使用C++来解决。我们可以使用一个循环来接收用户的输入,并根据输入来输出相应的结果。

以下是解决这个问题的代码:

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    string fruits[] = {"apple", "pear", "orange", "grape"};
    double prices[] = {3.00, 2.50, 4.10, 10.20};

    int count = 0;
    int input;
    while (true) {
      cout << " " << fruits << endl;
      cout << " " << fruits << endl;
      cout << " " << fruits << endl;
      cout << " " << fruits << endl;
      cout << " exit" << endl;

      cin >> input;

      if (input == 0 || count >= 5) {
            break;
      } else if (input >= 1 && input <= 4) {
            cout << fixed << setprecision(2) << "price = " << prices << endl;
            count++;
      } else {
            cout << "price = 0.00" << endl;
            count++;
      }
    }

    return 0;
}


首先,我们定义了一个字符串数组 fruits 来存储水果的名称,定义了一个双精度浮点数数组 prices 来存储水果的价格。

然后,我们使用一个 while 循环来接收用户的输入。在每次循环中,我们首先输出菜单,然后接收用户的输入。

如果用户输入的是0,或者连续查询次数超过5次,则跳出循环,程序结束。

如果用户输入的是1到4之间的数字,我们输出相应水果的价格,并将查询次数加1。

如果用户输入的是其他数字,则输出价格为0,并将查询次数加1。

最后,我们使用 fixed 和 setprecision 来设置输出价格的小数位数为2位。

希望这能帮到你!如果你还有其他问题,请随时提问。

Mike_python小 发表于 2023-10-17 17:51:08

问题分析:

我们需要编写一个程序,根据用户输入的水果编号查询对应水果的单价,并按照要求输出结果。主要的步骤包括:
1. 定义水果数组和价格数组;
2. 显示菜单;
3. 循环接受用户输入的编号并查询对应的价格;
4. 根据查询结果进行输出;
5. 判断退出条件。

C++代码实现:


#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    string fruits = {"apple", "pear", "orange", "grape"};
    double prices = {3.00, 2.50, 4.10, 10.20};
   
    cout << " apple\n pear\n orange\n grape\n exit" << endl;

    int count = 0;
    int num;
    while (count < 5) {
      cin >> num;
      
      if (num == 0) {
            break;
      } else if (num >= 1 && num <= 4) {
            cout << "price = " << fixed << setprecision(2) << prices << endl;
            count++;
      } else {
            cout << "price = 0.00" << endl;
      }
    }
   
    return 0;
}


解题思路:

首先,我们使用字符串数组 fruits 存储水果的名称,使用浮点数数组 prices 存储水果的单价。

然后,我们显示菜单选项。

接下来,使用一个循环来接受用户的输入。如果用户输入的是 0,则跳出循环;如果用户输入的是 1、2、3 或 4,则输出对应水果的单价,并将查询次数 count 加一;如果用户输入的不是上述几个数字,则输出价格为 0.00。

最后,程序结束。

希望能对你有所帮助!如果你还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:}
页: [1]
查看完整版本: 求助一下