我想用c编写一个程序,该程序管理具有3个属性的银行账户结构数组: 1 -账户号, 2 -账户余额和 3 -帐户所有者。
我想写一个函数:
这是我到目前为止的代码:
#include <stdio.h>
struct bancaccount {
int num;
double balance;
char owner;
};
void maintext(); //prototype of the function that print the main text
void addaccount();
void main(void) {
struct bankaccount account[50];
maintext();
char ch;
ch = getchar();
if (ch == 'a') {
addaccount();
maintext();
}
else if (ch == 'b') {
printf("Result: show");
}
else {
printf("another key was pressed ");
}
}
void maintext() {
printf("tap one of this keys : \n"
" a : to add a new account \n"
" b : to see all accounts\n");
}
void addaccount(struct comptebanc num) {
num++; //this seems not possible it gives an error, what should i do insteed
num = num;
printf("owner name : ");
scanf("%s", account.owner);
print("balance : ");
scanf("%lf", account.balance);
printf("\n\n");
printf("Num : %d\n", account.num);
printf("Prop : %s\n", account.owner);
printf("Solde : %lf\n", account.balance);
}
如何为每个新帐户分配一个号码? 以及如何在结构数组中保存新元素? 谢谢您的帮助。
我还是一个初学者,所以我确定我在基本面方面犯了一些错误
答案 0 :(得分:1)
这是基于您的代码的另一个可行的解决方案。 但是在此示例中,我使用了 scanf_s 函数而不是 scanf 。
#include <stdio.h>
struct bancaccount {
int num;
double balance;
char owner[64];
};
struct bancaccount accounts[10];
int account_id = 0;
void maintext(void)
{
printf("tap one of this keys : \r\n"
" a : to add a new account \r\n"
" b : to see all accounts\r\n");
}
void add_account(void)
{
accounts[account_id].num = account_id + 1;
printf("owner name : ");
scanf_s("%63s", &accounts[account_id].owner, 64);
printf("balance : ");
scanf_s("%lf", &accounts[account_id].balance);
printf("\r\n\r\n");
printf("Num : %d\r\n", accounts[account_id].num);
printf("Prop : %s\r\n", accounts[account_id].owner);
printf("Solde : %lf\r\n", accounts[account_id].balance);
account_id++;
}
void print_accounts(void)
{
int i;
for (i = 0; i < account_id; i++) {
printf("Num : %d\r\n", accounts[i].num);
printf("Prop : %s\r\n", accounts[i].owner);
printf("Solde : %lf\r\n", accounts[i].balance);
printf("\r\n");
}
}
int main(int argc, char *argv[])
{
char c;
maintext();
while (1) {
c = getchar();
switch (c) {
case 'a':
add_account();
getchar(); /* consume '\n' */
break;
case 'b':
print_accounts();
getchar(); /* consume '\n' */
break;
case 'q':
return 0;
default:
printf("another key was pressed\r\n");
}
}
return 0;
}
答案 1 :(得分:1)
这是您想如何解决的问题。
#include <stdio.h>
typedef struct {
int num;
double balance;
char owner[15];
} bankaccount;
int main() {
int numAllocated = 0; // Number of accounts allocated
bankaccount accounts[50];
int ch;
printf("\"a\": to add new account\n\"b\": to print all accounts\n");
void addAccount (bankaccount *, int);
void printAccounts (bankaccount *, int);
for ( ;; ) {
ch = getchar();
if (ch == '\n' ) continue;
else if (ch == 'a') {
addAccount(accounts, numAllocated);
numAllocated++;
}
else if (ch == 'b') printAccounts(accounts, numAllocated);
else break;
printf("\"a\": to add new account\n\"b\": to print all accounts\n");
}
}
void addAccount (bankaccount *account, int num) {
account[num].num = num + 1;
printf("Owner's name: ");
scanf("%s", account[num].owner);
printf("Balance: ");
scanf("%lf", &account[num].balance);
}
void printAccounts (bankaccount *accounts, int num) {
if (num > 0) {
for (int i = 0; i < num; i++) {
printf("\n%-10s%d\n", "Acc#:", accounts[i].num);
printf("%-10s%s\n", "Name:", accounts[i].owner);
printf("%-10s%.2f\n", "Balance:", accounts[i].balance);
}
}
else printf("No account allocated yet\n");
}
我查看了您的代码示例。尽管您可能会通过阅读有关动态内存,指针和可能的链接列表的信息来使结构变好,从而使此问题变得更好。但是这里是开始,希望您可以从这里开始!
答案 2 :(得分:0)
动态数组标记相当混乱,因为您只有静态的。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int num;
double balance;
char *owner;
} bancaccount;
size_t Naccounts = 0;
bancaccount *accounts = NULL;
bancaccount addaccount(int num, double ballance, char *owner)
{
bancaccount *tmp = realloc(accounts, ++Naccounts * sizeof(*tmp));
if(tmp)
{
accounts = tmp;
tmp[Naccounts - 1].owner = malloc(strlen(owner) + 1);
if([Naccounts - 1].owner)
{
strcpy(tmp[Naccounts - 1].owner, owner);
tmp[Naccounts - 1].balance = ballance;
tmp[Naccounts - 1].num = num;
}
}
return tmp;
}