c - 结构库存和数据文件

标签 c file struct stocks

这是我应该做的:

  1. 创建结构体 stock - 名称[20]、股份、buyprice、currprice、buycost、currcost、利润。
  2. 从键盘加载 - 名称、股票、购买价格、当前价格、当前成本、利润、购买成本。
  3. 按利润从高到低排序
  4. 计算所有股票的总利润
  5. 打印有多少股票赚钱、亏损和收支平衡。

这是我到目前为止所拥有的:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define size 2
#include<stdlib.h>
struct stock
{
    char name[20];
    int shares;
    float buyp, currp, buycost, currcost, profit;
};
void load(struct stock s[], int n)
{
    int i;
    for (i = 0; i<n; i++)
    {
        printf("Enter name:");
        gets(s[i].name);
        fflush(stdin);
        printf("Enter shares:");
        scanf("%d", &s[i].shares);
        printf("Enter buyprice");
        scanf("%f", &s[i].buyp);
        printf("Enter current price");
        scanf("%f", &s[i].currp);

        s[i].currcost = s[i].shares * s[i].currp;
        s[i].buycost = s[i].shares * s[i].buyp;
        s[i].profit = s[i].currcost - s[i].buycost;


    }
}

void print(struct stock s[], int n)
{
        int ne,p,z;
        ne = p = z =0;
    for (int i=0;i<n;i++)
    {
        if(s[i].profit<0)
            ne++;
        if (s[i].profit>0)
            p++;
        if(s[i].profit==0)
            z++;
        printf("Amount of stocks made money:%d\n",p);
        printf("Amount of stocks lost money:%d\n",ne);
        printf("Amount of stocks broke even:%d\n",z);
        printf("The current cost is:%f\n",s[i].currcost);
        printf("The profit is:%f\n",s[i].profit);
        printf("The buycost is:%f\n",s[i].buycost);
    }
}
void totprofit(struct stock s[], int n)

{
    int count = 0,  i;
    float totprofit = 0.0;
    for (i = 0; i < n; i++)
    {
        totprofit +=s[i].profit;
        printf("Total profit is:%f\n",totprofit);
        }
    }

void sort(struct stock s[], int n)
{
    int i; int j;
    stock t;
    for (i = 0; i<n - 1; i++)
        for (j = 0; j<n - 1; j++)
            if (s[j].profit<s[j + 1].profit)
            {
                t = s[j]; s[j] = s[j + 1]; s[j + 1] = t;
            }
}

void main()
{
    stock s[size];
    load(s, size);
    print(s,size);
    totprofit(s, size);
    sort(s,size);
    system("pause");
}

我的问题是,当它编译时,它工作得很好,直到再次询问名称(因为程序运行了两次)。为什么?

Enter name:
Apple
Enter shares:
5
Enter buyprice:
3
Enter current price:
4
Enter name:
Enter shares:
3
Enter buyprice:
4
Enter current price:
5
Amount of stocks made money:1
Amount of stocks lost money:0
Amount of stocks broke even:0
The current cost is:20.000000
The profit is:5.000000
The buycost is:15.000000
Amount of stocks made money:2
Amount of stocks lost money:0
Amount of stocks broke even:0
The current cost is:15.000000
The profit is:3.000000
The buycost is:12.000000
Total profit is:5.000000
Total profit is:8.000000
Press any key to continue . . .

最佳答案

您的函数 void totprofit 与变量 float totprofit 具有相同的名称。

至少更改其中一项。

至于第7步,我不确定你在问什么。

关于c - 结构库存和数据文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30089728/

相关文章:

c - C的树库

c - 段错误错误: While reversing a string

php - 强制用户下载 .txt (php, file.txt)

go - 在 Golang 中返回一个新的 Struct 实例

c# - 我应该使用结构而不是类来在 C# 中保存仅字符串数据吗?

c - 你如何在 C 中定义一个接受结构指针参数的宏?

c - 用 c 解析 csv

c - 如何用 C 语言编写过滤器程序?

iPhone - 从网络服务器获取文件的最后修改日期

iOS:如何备份项目?