我应该输入名称,股票数量,购买价格和当前价格,然后计算购买总额,当前总额和利润。然后,程序应输出名称,购买总额,当前总额和利润。
输入输入后,我一直收到错误或崩溃:
(点击放大)。
这是我到目前为止所做的:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void load(char *name, float *share, float *buyprice, float *currprice)
{
printf("Enter stock name");
gets(name);
printf("Enter share, buyprice, currprice");
scanf("%f %f %f", &*share, &*buyprice, &*currprice);
}
void calc(float share, float buyprice, float currprice, float *buytotal, float *currtotal, float *profit)
{
*buytotal = share * buyprice;
*currtotal = share * currprice;
*profit = *currtotal - *buytotal;
}
void output(char name, float profit, float buytotal, float currtotal)
{
printf("%s\n", name);
printf("buy total %f\n", buytotal);
printf("current total %f\n", currtotal);
printf("profit %f\n", profit);
}
void main()
{
char name [25];
float share, buyprice, currprice, buytotal, currtotal, profit;
load(name, &share, &buyprice, &currprice);
calc(share, buyprice, currprice, &buytotal, &currtotal, &profit);
output(*name, buytotal, currtotal, profit);
fflush(stdin);
load(name, &share, &buyprice, &currprice);
calc(share, buyprice, currprice, &buytotal, &currtotal, &profit);
output(*name, buytotal, currtotal, profit);
fflush(stdin);
load(name, &share, &buyprice, &currprice);
calc(share, buyprice, currprice, &buytotal, &currtotal, &profit);
output(*name, buytotal, currtotal, profit);
system("pause");
}
答案 0 :(得分:2)
您有一些错误,可以尝试一下:
void load(char *name, float *share, float *buyprice, float *currprice)
{
printf("Enter stock name");
gets(name);
printf("Enter share, buyprice, currprice");
scanf("%f %f %f", share, buyprice, currprice); // changes: removed &*
}
void calc(float share, float buyprice, float currprice, float *buytotal, float *currtotal, float *profit)
{
*buytotal = share * buyprice;
*currtotal = share * currprice;
*profit = *currtotal - *buytotal;
}
void output(char *name, float profit, float buytotal, float currtotal) // changes: char name to char *name
{
printf("%s\n", name);
printf("buy total %f\n", buytotal);
printf("current total %f\n", currtotal);
printf("profit %f\n", profit);
}
int main(void) //changed to int main(void)
{
char name [25];
float share, buyprice, currprice, buytotal, currtotal, profit;
load(name, &share, &buyprice, &currprice);
calc(share, buyprice, currprice, &buytotal, &currtotal, &profit);
output(name, buytotal, currtotal, profit); //changed *name to name
fflush(stdin);
load(name, &share, &buyprice, &currprice);
calc(share, buyprice, currprice, &buytotal, &currtotal, &profit);
output(name, buytotal, currtotal, profit); //changed *name to name
fflush(stdin);
load(name, &share, &buyprice, &currprice);
calc(share, buyprice, currprice, &buytotal, &currtotal, &profit);
output(name, buytotal, currtotal, profit); //changed *name to name
return 0; //Added this line
}