【C语言程序】在银行取款,需要用户输入密码,在密码输入正确后才能取款,正确密码为574824(strcmp()函数判断密码一致性)

题出自-------------------------零基础学C语言

#include<stdio.h>
#include<string.h>
int main()
{
	char answer[20] = { "574824" };
	char user[20];
	puts("请输入取款密码:");
	gets_s(user);
	while (strcmp(user, answer) != 0)
	{
		puts("请再次输入取款密码:");
		gets_s(user);
	}
	puts("密码正确,请输入取款金额!");
	return 0;
}

好的,以下是一个简单的用C语言编写的ATM取款程序,包含转账、查询、修改密码、登录、取款功能: ```c #include <stdio.h> #include <string.h> // 定义账户结构体,包含账户名、密码、余额 struct Account { char name[20]; char password[20]; float balance; }; // 定义账户数组,最多存储10个账户 struct Account accounts[10]; // 定义当前登录账户的索引 int currentAccountIndex = -1; // 定义函数,用于添加账户 void addAccount(char name[], char password[], float balance) { struct Account account; strcpy(account.name, name); strcpy(account.password, password); account.balance = balance; accounts[currentAccountIndex + 1] = account; currentAccountIndex++; printf("账户创建成功!\n"); } // 定义函数,用于查询账户 struct Account* findAccount(char name[], char password[]) { for (int i = 0; i <= currentAccountIndex; i++) { if (strcmp(accounts[i].name, name) == 0 && strcmp(accounts[i].password, password) == 0) { return &accounts[i]; } } return NULL; } // 定义函数,用于转账 void transfer(struct Account* from, struct Account* to, float amount) { if (from->balance >= amount) { from->balance -= amount; to->balance += amount; printf("转账成功!\n"); } else { printf("对不起,余额不足!\n"); } } // 定义函数,用于修改密码 void changePassword(struct Account* account, char newPassword[]) { strcpy(account->password, newPassword); printf("密码修改成功!\n"); } int main() { // 初始化两个账户 addAccount("张三", "123456", 10000); addAccount("李四", "123456", 20000); while (1) { // 判断是否已登录 if (currentAccountIndex == -1) { printf("欢迎使用ATM取款机,请先登录!\n"); char name[20]; char password[20]; printf("请输入用户名:"); scanf("%s", name); printf("请输入密码:"); scanf("%s", password); struct Account* account = findAccount(name, password); if (account != NULL) { printf("登录成功!\n"); currentAccountIndex = account - accounts; } else { printf("对不起,用户名或密码错误,请重新输入!\n"); } } else { printf("请选择操作:\n"); printf("1. 查询余额\n"); printf("2. 修改密码\n"); printf("3. 取款\n"); printf("4. 转账\n"); printf("5. 退出登录\n"); int choice; scanf("%d", &choice); switch (choice) { case 1: printf("您的当前余额为:%.2f元。\n", accounts[currentAccountIndex].balance); break; case 2: char newPassword[20]; printf("请输入密码:"); scanf("%s", newPassword); changePassword(&accounts[currentAccountIndex], newPassword); break; case 3: float amount; printf("请输入取款金额:"); scanf("%f", &amount); if (amount > accounts[currentAccountIndex].balance) { printf("对不起,余额不足!\n"); } else { accounts[currentAccountIndex].balance -= amount; printf("取款成功!\n"); printf("您的当前余额为:%.2f元。\n", accounts[currentAccountIndex].balance); } break; case 4: char toName[20]; float transferAmount; printf("请输入转账账户名:"); scanf("%s", toName); printf("请输入转账金额:"); scanf("%f", &transferAmount); struct Account* toAccount = findAccount(toName, ""); if (toAccount == NULL) { printf("对不起,账户不存在!\n"); } else { transfer(&accounts[currentAccountIndex], toAccount, transferAmount); } break; case 5: currentAccountIndex = -1; printf("已退出登录。\n"); break; default: printf("无效的选择,请重新输入!\n"); } } } return 0; } ``` 这个程序中,我们定义了一个账户结构体,包含账户名、密码、余额。我们使用账户数组来存储多个账户,最多存储10个账户。除此之外,我们还定义了一个变量 `currentAccountIndex`,用于存储当前登录账户的索引。 程序中包含以下几个函数: - `addAccount()`:添加账户 - `findAccount()`:查询账户 - `transfer()`:转账 - `changePassword()`:修改密码 在主函数中,我们首先初始化两个账户,并进入一个无限循环。在循环中,如果用户未登录,则程序会提示用户输入用户名和密码,进行登录操作。如果用户已登录,则程序会提示用户选择操作,并根据用户的选择执行相应的操作。 在程序中,我们还使用了 `switch` 语句来处理用户的选择,使程序更加清晰易懂。同时,我们还对输入进行了简单的校验,以确保程序的稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

comelyboy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值