2

老实说,这是一个家庭作业。但是我已经花了 2 天,没有得到任何结果。我不擅长编程,但它是一个强制性模块。

问题需要先用typedef构建一个struct,然后用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、指针和函数来完成程序,我对指针只有一个简单的想法。据我所知,这是为了永久解决问题。

这是完整的问题和我到目前为止所做的代码。

问:有以下要求,做一个汽车安装程序,如下面的屏幕转储:

  1. 价格

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

  3. 如果客户加入会员。

变量: - 24 个月的利率为 10%

  • 36个月利率为15%

  • 如果加入会员,可一次性退还 3000 美元的总贷款

  • 第一个月还款额为总贷款的20%

示例屏幕转储:

车价:30000

分期付款的首选月份:36

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

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

打印详细信息:

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

贷款总额: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

4

2 回答 2

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

于 2013-10-01T13:08:25.213 回答
0

您在函数中获得了 Car*。使用它,即

void collecInfo(Car *p){ //Pre-define in question, can't change
    printf("Price of Car : ");
    scanf("%d",&(p->price));
    ...
于 2013-10-01T13:12:50.800 回答