【发布时间】:2013-10-01 13:05:34
【问题描述】:
说实话,这是一个家庭作业。但是我已经花了 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");
}
}
感谢你们所有的cmets。
其实是一个关于“贷款计算”的问题。 要求是使用typedef、struct、指针和函数来完成程序,我对指针只有一个简单的想法。据我所知,这是为了永久解决问题。
这是完整的问题和我到目前为止所做的代码。
问:有以下要求,做一个汽车安装程序,如下面的屏幕转储:
价格
分期月数(只能选择24或36个月,请输入验证)
如果客户加入会员。
变量: - 24个月利率为10%
36个月利率为15%
如果加入会员,可一次性退还 3000 美元的总贷款
第一个月的付款将是总贷款的 20%
屏幕转储示例:
汽车价格: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...
}
经过大量 google,我尝试在函数 colecInfo 中使用 malloc
【问题讨论】:
-
你忘了问一个问题,并告诉我们究竟是什么失败以及如何失败。
-
那么什么你的代码有什么问题吗?除了不编译(我猜)。请编辑您的问题以包含完整且未经编辑的错误消息。
-
您应该处理多少辆汽车或客户?
-
几个小技巧:阅读有关数组、指针和局部变量与全局变量的更多信息。
-
“我不擅长编程,但它是一个必修模块。” - 这是我们的错?
标签: c function pointers struct