我正在尝试为我的C类编写一个程序,该程序可以跟踪在银行中的存款。它为您提供一个菜单,其中包含以下选项:输入存款,显示所有存款的总和,显示从最高到最低的存款(使用冒泡排序),显示平均存款,显示最低的存款,然后选择退出选项。据我所知,输入,求和和退出选项工作正常,但其他三个选项已损坏。当您选择它们时,无论您对数组进行了什么输入,其作用都好像它们都等于零。这是我到目前为止所拥有的:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int sortCount, sortCount2, sortCount3, swap;// variables for sort
int depositCount = 0, sumCount, lowestCount;
int averageCount, avgSum = 0, avg; //variables for average
char switchInput = 0;//menu input
double deposits[100] = { 0 }, sum = 0, average;
do
{
printf("BANKING MAIN MENU\n\n");
printf("[M]ake a new deposit\n");
printf("[S]um of all deposits\n");
printf("[D]eposits will be displayed from highest to lowest\n");
printf("[A]verage of all deposits\n");
printf("[L]owest deposit will be displayed\n");
printf("[Q]uit\n\n");
printf("Please enter selection:\n\n");
scanf(" %c", &switchInput);
switch (switchInput)
{
case 'm': case 'M'://Deposit screen
printf("\nPlease enter deposit:\n\n");
scanf("%lf", &deposits[depositCount++]);//deposit input
;
for (sortCount = 0; sortCount < depositCount; sortCount++)//Should sort the array highest to lowest
for (sortCount2 = 0; sortCount2 < depositCount - sortCount - 1; sortCount2++)
if (deposits[sortCount] < deposits[sortCount+1])
{
swap = deposits[sortCount];
deposits[sortCount] = deposits[sortCount+1];
deposits[sortCount+1] = swap;
}
break;
case 's': case 'S'://Total of deposits screen
for (sumCount = 0; sumCount < depositCount; sumCount++)//depositCount should have it only use parts of the array where there are inputs.
sum = sum + deposits[sumCount];
printf("\nYour total deposits equal $%.2lf\n\n", sum);
break;
case 'd': case 'D'://Highest to lowest screen
for (sortCount3 = 0; sortCount3 < depositCount; sortCount3++)//depositCount should have it only use parts of the array where there are inputs.
{
printf("$%d \n", deposits[sortCount3]);
}
break;
case 'a': case 'A'://Average screen
for (sumCount = 0; sumCount < depositCount; sumCount++) //depositCount should have it only use parts of the array where there are inputs.
{
avgSum = avgSum + deposits[sumCount];
avg = avgSum / depositCount;
}
printf("Your average deposit is $%.2lf.\n", avg);
break;
case 'l': case 'L'://Lowest screen
printf("The lowest deposit is $%.2lf.\n", deposits[depositCount]);//If the numbers are ordered from highest to lowest, the then entry in the array at the position equal to the number of deposits should be the lowest
break;
case 'q': case 'Q'://quit screen
printf("\nThank you for using our bank!\n\n");
system("pause");
return 0;
break;
default ://invalid option
printf("\nInvalid selection!\n\n");
}
} while (switchInput != 'q'||'Q');
}
答案 0 :(得分:2)
气泡排序未在c中正确排序数组
在
silly [In]: silly['priors_inc'] = silly[['thing', 'cond']].where(silly['cond'] == 1).groupby('thing').cumsum() - 1 [Out]: silly thing cond priors priors_inc 0 a 1 0 0.0 1 b 2 0 NaN 2 a 1 1 1.0 3 c 2 0 NaN 4 b 1 0 0.0 5 c 2 0 NaN 6 c 1 0 0.0 7 a 2 2 NaN 8 a 1 2 2.0 9 b 2 1 NaN 10 c 1 1 1.0 11 a 2 3 NaN
for (sortCount = 0; sortCount < depositCount; sortCount++)//Should sort the array highest to lowest
for (sortCount2 = 0; sortCount2 < depositCount - sortCount - 1; sortCount2++)
if (deposits[sortCount] < deposits[sortCount+1])
{
swap = deposits[sortCount];
deposits[sortCount] = deposits[sortCount+1];
deposits[sortCount+1] = swap;
}
在内部 for 中是未使用的,您可以始终独立地执行相同的操作。此外,您在最后一个索引之后走了1个索引
在S.O上有很多气泡排序的实现。我让你搜索并更正
交换必须为 double
在
sortCount2
除法必须在求和之后 进行,每次都不要,所以
case 'a': case 'A'://Average screen
for (sumCount = 0; sumCount < depositCount; sumCount++) //depositCount should have it only use parts of the array where there are inputs.
{
avgSum = avgSum + deposits[sumCount];
avg = avgSum / depositCount;
}
和 avgSum 和 avg 必须为 double
case 'a': case 'A'://Average screen for (sumCount = 0; sumCount < depositCount; sumCount++) //depositCount should have it only use parts of the array where there are inputs. { avgSum = avgSum + deposits[sumCount]; } avg = avgSum / depositCount;
必须
while (switchInput != 'q'||'Q');