如何使用point和typedef结构的函数

时间:2013-10-01 13:05:34

标签: c function pointers struct

老实说,这是一个功课。但是我花了2天时间没有结果。我不擅长编程,但它是一个强制性模块。

使用typedef构建结构所需的问题,然后使用struct编写一个收集信息的函数并使用指针方法。

以下是代码,仅包括结构和功能部分,以及我自己想要做的其他事情。请相信我,我尽力做到这一点

//Assignment.cpp

#include <stdio.h> //Pre-define in question, can't change
#include <ctype.h> //Pre-define in question, can't change
#include <stdlib.h> //Pre-define in question, can't change

//Requirement: Use typedef to create a struct called Car

typedef struct Car{
    float price;
    unsigned int numofmonth;
    unsigned char member;
}Car;

Car *p
struct Car customer1;
struct Car *p=&customer1;

//Function phototype
void collectInfo(Car *p); //pre-define in question, can't change
void printInfo(Car p); //Pre-define in question, can't change //Once the Collect info part done, I will do it myself


int main(){ //Pre-define in question, can't change
    Car customer1; //Pre-define in question, can't change
    collectInfo(&customer1); //Pre-define in question, can't change
    printInfo(customer1); //Pre-define in question, can't change

    system("pause"); //Pre-define in question, can't change

    return 0; //Pre-define in question, can't change
}

//Function defintions // The problem I want to ask, how to make it work? Thanks
void collecInfo(Car *p){ //Pre-define in question, can't change
    for(int i = 0; i < 3;i++){
    printf("Price of Car : ");
    scanf("%d",&customer1[i].price);
    printf("Perferred Months for Installment : ");
    scanf("%d",&customer1[i].numofmonth);
    printf("Perferred Months for Installment : ");
    scanf("%c",&customer1[i].member);

    printf("\n");
    }
}

感谢您的所有评论。

实际上,这是一个关于“贷款计算”的问题。 要求是使用typedef,struct,pointer和function来完成程序,我只是对指针有一个简单的想法。据我所知,这是永久性的。

这是完整的问题和我迄今为止所做的代码。

问:有以下要求,并为screendump下面的汽车分期付款做一个程序:

  1. 分期付款的月数(只能选择24或36个月,请输入验证)

  2. 如果客户加入会员。

  3. 变量: - 24个月利率将为10%

    • 36个月的利率将是15%

    • 如果加入会员,总贷款一次性退还$ 3000

    • 首月付款将占贷款总额的20%

    示例screendump:

    汽车价格:30000

    分期付款的首选月份:36

    您是否加入我们的会员(y / n):y

    =================

    打印详细信息:

    =================

    贷款总额:31500

    第一个月付款:6300

    每月付款:700

    以下是我现在仍在做的代码

    //Assignment.cpp
    
    #include <stdio.h> //Pre-define in question, can't change
    #include <ctype.h> //Pre-define in question, can't change
    #include <stdlib.h> //Pre-define in question, can't change
    
    //Use typedef to create a struct called Car
    
    typedef struct Car{
        float price;
        unsigned int numofmonth;
        unsigned char member;
           }Car;
    
    Car *p
    struct Car customer1;
    struct Car *p=&customer1;
    
    //Function phototype
    void collectInfo(Car *p); //pre-define in question, can't change
    void printInfo(Car p); //Pre-define in question, can't change
    
    int main(){ //Pre-define in question, can't change
        Car customer1; //Pre-define in question, can't change
        collectInfo(&customer1); //Pre-define in question, can't change
        printInfo(customer1); //Pre-define in question, can't change
    
        system("pause"); //Pre-define in question, can't change
    
        return 0; //Pre-define in question, can't change
    }
    
    //Function defintions
    void collecInfo(Car *p){ //Pre-define in question, can't change
        int interest;
        int lumpsum;
    
        printf("Price of Car : ");
        scanf("%f",&(p->price));
    
        //check if the installment is 24 or 36
        printf("Perferred Months for Installment : ");
        scanf("%u",&(p->numofmonth));
        if(p->numofmonth == 24)
            interest=0.1;
        else if(p->numofmonth == 36)
            interest=0.15;
        else
            printf("Sorry, we only accept 24 or 36 months installment");
    
        printf("Are you our member (y/n) : ");
        scanf("%u",(p->member));
    
        //check if the member = y or n
        if(p->member == 'y')
        lumpsum=-3000;
        else if(p->member == 'n')
            lumpsum=0;
        else 
            printf("Please only input 'y' or 'n'");
    
        printf("\n");
    
    }
    //Show result on screen, still doing, have problem to display result of pointer...
    void printInfo(Car p){
        printf("Price of the Car: %.2f\n", customer1.price);
        printf("Preferred Months for Installment : %u\n", customer1.numofmonth);
        printf("Are you our member (y/n) : %u\n", customer1.member);
        printf("========================================\n");
        printf("See the installment details below\n");
        printf("========================================\n\n");
    
        float total;
        total= // still doing, have problem to display result of pointer...
    }
    

    经过很多谷歌,我试图在函数collecInfo中使用malloc

2 个答案:

答案 0 :(得分:0)

不应该这样: scanf("%d",&customer1[i].numofmonth);

scanf("%u",&customer1[i].numofmonth);

还有:

scanf("%d",&customer1[i].price);

scanf("%f",&customer1[i].price);

格式说明符应与变量类型匹配。 unsigned int使用%ufloat使用%f

答案 1 :(得分:0)

你在这个功能中得到了一辆Car *。使用它,即

void collecInfo(Car *p){ //Pre-define in question, can't change
    printf("Price of Car : ");
    scanf("%d",&(p->price));
    ...