结构:比较结构内部数组中的字符串 - C

时间:2016-07-29 02:55:31

标签: c arrays string struct strcmp

无法理解作业。我们在课堂上教的方式与我们实验室教练的任务不同,所以我很困惑。

我们获得了一个程序,我们必须填写一个功能来完成该程序。它包括一个包含4个元素的结构。我们创建的函数是一个接收2个数组的函数,这些数组在已经完成的单独函数中初始化,以及两个数组的长度。他们从输入文件中获取此信息,并且已经扫描到程序中。因此,如果两个电影标题相同,则需要读取该函数。如果是,则返回较高的一个。如果有多组电影相同,则会增加两部较昂贵的电影。

从逻辑上讲,我觉得我有正确的想法,我觉得这应该有效,但当然不是。您比较的电影列表位于两个单独的数组中,因此我应该将一个数组中的标题与另一个数组的标题进行比较,然后遍历数组直到达到该长度。

Full instructions if needed

我的代码:创建的函数是getProfit,并且不能更改任何参数或其余代码。这就是我发现它令人困惑的原因,因为我们不能像我们学到的那样去做。

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

typedef struct {
    char title[100];
    int runTime;
    int idtag;
    double salePrice;
} DVD;

double getProfit(DVD* list1, int len1, DVD* list2, int len2) ;
double max(double a, double b);
DVD* get(FILE* ifp, int len);

int main() {

    FILE* ifp = fopen("dvd.in", "r");
    int numCases, loop;
    fscanf(ifp, "%d", &numCases);

    // Go through each case.
    for (loop=0; loop<numCases; loop++) {

        // Read in movies.
        int len1,len2;
        fscanf(ifp, "%d", &len1);
        DVD* myMovies = get(ifp, len1);
        fscanf(ifp, "%d", &len2);
        DVD* yourMovies = get(ifp, len2);

        // Output sale price of all common movies.
       printf("%.2f\n", getProfit(myMovies, len1, yourMovies, len2));
        // Bookkeeping...
        free(myMovies);
        free(yourMovies);
    }

    fclose(ifp);
    return 0;
}

// Pre-condition: list1 is an array of length len1, list2 is an array
//                of length len2. No title appears in either list more
//                than once.
// Post-condition: Returns the sum of the sale prices of the common
//                 DVDs in both lists. Two DVDs are considered the
//                 same if their titles are identical.
double getProfit(DVD* list1, int len1, DVD* list2, int len2) {
    int i, j;
    double sales = 0;
    for(i = 0; i < len1; i++){
        for(j = 0; j < len2; j++){
            if(strcmp(list1[i].title, list2[j].title)){
                if(list1[i].salePrice < list2[j].salePrice)
                    sales += list2[j].salePrice;
                else if(list1[i].salePrice > list2[j].salePrice)
                	sales += list1[i].salePrice;
            }
        }
    }

    return sales;
}

// Returns the larger of a and b.
double max(double a, double b) {
    if (a > b) return a;
    return b;
}

// Reads in len DVDs from ifp and returns those store in a dynamically allocated array.
DVD* get(FILE* ifp, int len) {
    DVD* res = malloc(sizeof(DVD)*len);
    int i;
    for (i=0; i<len; i++)
        fscanf(ifp, "%s%d%d%lf", res[i].title, &(res[i].runTime), &(res[i].idtag), &(res[i].salePrice));
    return res;
}

输入文件:

2 
3 
BackToTheFuture 108 1234567 15.99 
Gremlins 120 13232423 9.99 
IndependenceDay 135 2312412 12.99 
2 
Gremlins 123 13232425 13.99 
MonstersInc 95 44232122 15.99 
5 
a 100 1234213 14.99 
b 101 1245332 7.99 
c 102 3431533 12.50 
d 103 3431434 19.99
e 104 4314133 16.47 
4 
f 120 5314443 11111111.38 
e 104 5134343 16.48 
d 110 3431414 14.99 
c 105 1234145 14.50

输出应返回的内容:

13.99
50.97

我的输出是什么:

61.96
55555738.73

我知道我的strcmp线不起作用,我的意思是显而易见的。我最初拥有它strcmp(list[i].title, list2[j].title == 0)但程序在它开始之前崩溃了。

没有人需要给我一个直接的答案,一些指导会很好。谢谢你的阅读。

0 个答案:

没有答案
相关问题