首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何跟踪某个循环运行的金额并将值存储到数组中

如何跟踪某个循环运行的金额并将值存储到数组中
EN

Stack Overflow用户
提问于 2017-10-02 15:26:33
回答 2查看 66关注 0票数 0

我是一个编写代码和准备作业的新手,在这个任务中我遇到了一些困难。我只想让你们帮我找出我所面临的一个问题上的错误。在粘贴我的代码之前,我将概述一下必须完成的工作。用户必须创建无人机,在创建一架无人机之后,程序应该返回主菜单,存储第一架无人机的详细信息,如果请求进入另一架无人机,他/她应该被允许这样做,直到无人机数量达到10架为止。只允许10架无人机,我们必须使用数组来存储每架无人机的值。我无法使程序计数10,并使用数组来存储它们。在我在这里粘贴的代码中,我正在尝试2,但我也无法做到这一点。请帮帮我..。

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct drone_t{
    char name[30];
    float top_s;
    float acc;
};

void printing (struct drone_t dron[2]);

int main()
{
    int a, i;
    char nam;
    struct drone_t drone[2];
    printf("Welcome to the drone travel time calculator\n");
    printf("1. Create Drone\n2. Calculate Time\n3. Exit\n");
    scanf("%d", &i);
    if (i == 1)
    {
        for (a=1; a < 3; a++)
        {
            printf("What is the name of the drone?\n");
            scanf("%s", drone[a].name);
            printf("What is the top speed of the drone? (kmph)\n");
            scanf("%f", &drone[a].top_s);
            printf("What is the acceleration of the drone? (mpsps)\n");
            scanf("%f", &drone[a].acc);
            printing(drone);
        }
    }
    else if (i == 2)
    {
        printing(drone);
    }
}

void printing (struct drone_t dron[2])
{
    int a;
    for (a=1; a < 3; a++)
    {
    printf("Name is: %s\n", dron[a].name);
    }
    //return 0;
}

预期产出如下:

代码语言:javascript
复制
Welcome to the drone travel time calculator
1. Create Drone
2. Calculate Time
3. Exit
1
What is the name of the drone?
Jayne
What is the top speed of the drone? (kmph)
12
what is the acceleration of the drone? (mpsps)
12
1. Create Drone
2. Calculate Time
3. Exit
1
What is the name of the drone?
JayneW
What is the top speed of the drone? (kmph)
12
what is the acceleration of the drone? (mpsps)
12
1. Create Drone
2. Calculate Time
3. Exit
2
Select a drone:
1. Jayne
2. JayneW

编辑:

@coderredoc

这是完整的代码。

代码语言:javascript
复制
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

struct drone_t{
    char name[30];
    float top_s;
    float acc;
};

void show_menu();
void do_create(int dronesCreated);

#define MAXDRONES 10

int main()
{
    int dronesCreated = 0;
    int i;
    while(1)
    {
        show_menu();
        if (i == 1)
        {
            if(dronesCreated<=MAXDRONES-1)
            {
                dronesCreated++;
                do_create(dronesCreated);
            }
            else
            {
                printf("error: cannot create more drones");
            }
        }
        else
        {
            break;
        }
    }
}

void show_menu()
{
    int i;
    printf("1. Create Drone\n2. Calculate Time\n3. Exit\n");
    scanf("%d", &i);
}

void do_create(int dronesCreated)
{
    char name[10];
    int b, c;
    int count = dronesCreated;
    struct drone_t drone[10];
    for (b=0; b <= count; b++)
    {
        printf("What is the name of the drone?\n");
        scanf("%s", drone[b].name);
        printf("What is the top speed of the drone? (kmph)\n");
        scanf("%f", &drone[b].top_s);
        printf("What is the acceleration of the drone? (mpsps)\n");
        scanf("%f", &drone[b].acc);
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-02 15:34:23

据我所知,你被困在这部分。

如何继续获取输入?

为此,您需要编写带有propr退出条件的while(1){..},如break等。

剩下的工作可以归结为有多少架无人机?并访问和存储它们。

存储一个结构数组就足够了。对于无人机的计数,您可以使用单独的变量。访问它们只不过是数组访问。

你的向导的伪码:

代码语言:javascript
复制
#define MAXDRONES 10
int dronesCreated =0;
while(1){
  show _menu();
  if( option is DRONE_CREATE){
     if(dronesCreated<=MAXDRONES-1){         
         do_create();//increment NUM_OF_DRONES here.
         dronesCreated++;
     }
     else 
       print appropriate message.
  else if(option is PRINT)
     print()
  else
     break;
}

从直观的角度来理解程序的设计是很简单的。

您需要一个循环,因为您需要不断地请求用户的选项。这就是为什么循环。

怎么停在10架无人机上?

您保留一个变量,并检查何时给出DRONE_CREATE选项,是否创建了10个无人机。如果是,则不要调用该函数,否则请调用该函数。

以执行为中心的办法:

现在,当您使用无人机的全局结构数组时,您需要使用这样的计量器变量。

int numberOfDrones = 0;然后使用上面所示的控件。

如何打印和扫描无人机?

仅仅是输入和输出它们的方式。

代码语言:javascript
复制
scanf("%s",drone[a].name);
print("%s,%d",drone[a].name,drone[a].age);

所有这些都是同时发生的:

代码语言:javascript
复制
    for(int i=0;i<MAXDRONES;i++){
         printf("What is the name of the drone?\n");
        scanf("%s", drone[a].name);
        printf("What is the top speed of the drone? (kmph)\n");
        scanf("%f", &drone[a].top_s);
        printf("What is the acceleration of the drone? (mpsps)\n");
        scanf("%f", &drone[a].acc);
        printing(drone);
    }
票数 0
EN

Stack Overflow用户

发布于 2017-10-02 16:29:22

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DRONES 10
struct drone_t{
    char name[30];
    float top_s;
    float acc;
};

void printing (struct drone_t dron[]); //no need to mention size while prototyping

int main()
{
    unsigned short cnt,choice;
    //char nam; //uneccessary variable
    struct drone_t drone[DRONES];
    printf("Welcome to the drone travel time calculator\n");

    do{
        printf("1. Create Drone\n2. Calculate Time\n3. Exit\n:\t");
        scanf("%d", &choice);
        switch(choice)
        {
            case 1:
                for (cnt=0; cnt < DRONES; cnt++)
                {
                    printf("What is the name of the drone?\n");
                    scanf("%s",drone[cnt].name);
                    printf("What is the top speed of the drone?(kmph)\n");
                    scanf("%f", &drone[cnt].top_s);
                    printf("What is the acceleration of the drone?(mpsps)\n");
                    scanf("%f", &drone[cnt].acc);
                }
                printing(drone);//put this statement outside loop
                break;
            case 2:
                printing(drone);
                break;
            case 3:
                exit(EXIT_SUCCESS);
            default:
                printf("choose correct option :D");
                break;
        }
    }while(choice!=3);   //if explicitly using exit case then not needed
}

void printing (struct drone_t dron[10])
{
    unsigned short cnt;
    for (cnt=0; cnt < DRONES; cnt++)
    {
        printf("Name is: %s\n", dron[cnt].name);
    }
}

注意:不要使用for(cnt=1;;) --它可能会导致像错位错误这样的问题,所以更好地用于(cnt=0;;)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46528644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档