打印未排序数组的唯一元素的C程序

打印未排序数组的唯一元素的C程序,c,C,基本上,我试图得到5个独特的草药清单(在本例中),包括股票号码,价格等 在为我的阵列获得1个列表后,我想使用一个函数来显示库存小于5的列表及其唯一代码(1,2,3…),如果没有打印“没有任何草药消耗”,并将其存储在文本文件中 我的问题是如何做我在第2段中描述的事情。在案例2中调用一个函数,该函数将显示库存少于5种的所有草药,并将它们保存在文本文件中。 非常感谢您的帮助 #include <stdio.h> #include <string.h> #define user

基本上,我试图得到5个独特的草药清单(在本例中),包括股票号码,价格等

在为我的阵列获得1个列表后,我想使用一个函数来显示库存小于5的列表及其唯一代码(1,2,3…),如果没有打印“没有任何草药消耗”,并将其存储在文本文件中


我的问题是如何做我在第2段中描述的事情。在案例2中调用一个函数,该函数将显示库存少于5种的所有草药,并将它们保存在文本文件中。

非常感谢您的帮助

#include <stdio.h>
#include <string.h>

#define user "kaiti"
#define pass "123pass!"
#define herbs 5

**void stock(int i, int code[herbs], int quantity[herbs])
{   
    for (i=0;i<herbs;i++){
    if(quantity[i]<5)
        printf("%d \t %d",code[i], quantity[i]);        
}
}**


int main()
{
    float price[herbs];
    char username[20], password[20];
    int option, i, code[herbs], quantity[herbs], consumption[herbs], choice;
//-----LOGIN PHASE-----
printf("Please Enter username:");
scanf("%s",username);

printf("\nPlease enter password:");
scanf("%s",password);

while(strcmp(username, user) != 0  || strcmp(password, pass) != 0)
 {
printf("Wrong username or password try again!\n"); 
printf("\nPlease Enter username:");
scanf("%s",&username);

printf("\nPlease enter password:");
scanf("%s",&password);
}

    printf("\nCorrect username and password.\n\nWelcome Mrs.Kaiti!\n\nWhat would you like to do?");
//-----END OF LOGIN PHASE-----

printf("\n(1)Register Herbs.\n**(2)Display herbs with stock less than 5 and store them in a text file.**\n(3)See the recomended daily dosage of a herb.\n(4)Add or deduct quantity of a herb.\n(5)Sell a herb.\nChoice: ");
scanf("%d",&option);

switch( option )
{
    case 1:

        for(i=0;i<herbs;i++){

        printf("Enter herb code: ");
        scanf("%d",&code[i]);
        printf("Enter herb quantity: ");
        scanf("%d",&quantity[i]);
        printf("Enter recomended daily consumption: ");
        scanf("%d",&consumption[i]);
        printf("Enter price: ");
        scanf("%f",&price[i]);
        printf("Add another herb?\n(1)Yes.\n(2)Back to login screen.\n");
        scanf("%d",&choice);
        if (choice == 2)
            return main()   ;
        }

    **case 2:

        printf("The Following herbs have a quantity less than 5: \n Code\t quantity \n");
        stock(i, code, quantity);**
#包括
#包括
#定义用户“kaiti”
#定义pass“123pass!”
#定义药草5
**无效库存(整数i,整数代码[草药],整数数量[草药])
{   

对于(i=0;i您需要做的就是将
printf
替换为
fprintf
,如下所示:

void stock(int code[herbs], int quantity[herbs])
{   
    int i;
    FILE *f = fopen("herbs_file_name", "w");

    for (i = 0 ; i < herbs ; i++)
    {
        if(quantity[i] < 5)
        {
            fprintf(f, "%d \t %d\n", code[i], quantity[i]);
        }
    }
    fclose(f);
}
无效库存(整数代码[草药]、整数数量[草药])
{   
int i;
文件*f=fopen(“文件名”,“w”);
对于(i=0;i<0;i++)
{
如果(数量[i]<5)
{
fprintf(f,“%d\t%d\n”,代码[i],数量[i]);
}
}
fclose(f);
}

请注意,您不需要通过
i
——只需在本地声明即可。

您只需将
printf
替换为
fprintf
,如下所示:

void stock(int code[herbs], int quantity[herbs])
{   
    int i;
    FILE *f = fopen("herbs_file_name", "w");

    for (i = 0 ; i < herbs ; i++)
    {
        if(quantity[i] < 5)
        {
            fprintf(f, "%d \t %d\n", code[i], quantity[i]);
        }
    }
    fclose(f);
}
无效库存(整数代码[草药]、整数数量[草药])
{   
int i;
文件*f=fopen(“文件名”,“w”);
对于(i=0;i<0;i++)
{
如果(数量[i]<5)
{
fprintf(f,“%d\t%d\n”,代码[i],数量[i]);
}
}
fclose(f);
}

请注意,您不需要通过
i
——只需在本地声明即可。

您需要在
案例2
之前中断
(但您没有问任何问题)我的问题是如何执行我在第2段中描述的操作:)在案例2中调用一个函数,该函数将显示库存少于5种的所有草药,并将它们保存在一个文本文件中。您需要在
案例2
之前执行
中断操作(但是您没有问任何问题)我的问题是如何执行我在第2段中描述的操作:)在案例2中调用一个函数,该函数将显示库存少于5种的所有草药,并将它们保存在文本文件中。