简体   繁体   English

如何在point和typedef结构中使用函数

[英]how to use function with point and typedef struct

Be honest, it's a homework. 老实说,这是一项家庭作业。 However I've spend 2 days and got no result. 但是我花了2天,没有结果。 I'm not good at programming but its a mandatory module. 我不太擅长编程,但它是必修模块。

The question required to build a struct with typedef, then write a function for collect information with the struct and use pointer method. 使用typedef构建结构,然后编写一个用于使用struct收集信息并使用指针方法的函数。

Below is the code, only including the struct and function part, for other things I still want do myself. 下面是代码,仅包含结构和函数部分,用于我仍然想自己做的其他事情。 Please trust me, I've try my best to do it 请相信我,我已经尽力了

//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");
    }
}

Thanks for all your comments. 谢谢你们的评论。

Actually, it's a question about "loan calculation". 实际上,这是关于“贷款计算”的问题。 The requirement is use typedef, struct, pointer and function to complete the program and I just have simple idea about pointer. 要求使用typedef,struct,pointer和function来完成程序,而我对指针的想法很简单。 As I know it is for point to address permanently. 据我所知,这是永久解决的重点。

Here is the full question and the code I've done until now. 这是完整的问题以及我到目前为止所做的代码。

Q: have below requirement and do a program for car instalment like below screendump: 问:满足以下要求,并执行以下屏幕转储所示的汽车安装程序:

  1. price 价钱

  2. number of months for instalment (can only choose 24 or 36 months, please do input validation) 分期付款的月数(只能选择24或36个月,请进行输入验证)

  3. if the customer joined member. 如果客户加入会员。

Variable: - 24 months interest rate will be 10% 变量:-24个月的利率将为10%

  • 36 months interest rate will be 15% 36个月利率将为15%

  • if joined member, have a lump sum $3000 refund on total loan 如果加入会员,可一次性付清全部贷款$ 3000

  • First month payment will be 20% of total loan 首月付款将占贷款总额的20%

Sample screendump: 屏幕转储样本:

Price of car: 30000 汽车价格:30000

Preferred months for instalment: 36 分期付款的首选月份:36

Had you join our member (y/n): y 您是否已加入我们的会员(y / n):y

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

Print out detail: 打印出详细信息:

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

Total loan: 31500 总贷款:31500

First Month payment: 6300 首月付款:6300

Monthly Payment: 700 每月付款:700

And below is the code I still doing now 下面是我现在仍在执行的代码

//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...
}

After lot of google, I trying to use malloc in the function collecInfo 经过大量的google,我尝试在collecInfo函数中使用malloc

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

be

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

And also: 并且:

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

be

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

The format specifier should match the type of variable. 格式说明符应与变量类型匹配。 unsigned int used %u and float uses %f . unsigned int使用%ufloat使用%f

You are getting a Car* in the function. 您正在该功能中获得Car *。 Use that, ie 使用它,即

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM