weixin_50917758 2020-12-24 20:05 采纳率: 66.7%
浏览 35
已采纳

C语言银行转钱的小程序

void transfer(void){
	char deb[11], cre[11], tim[11];
	int amount;
	time_t t;
	struct tm *p;
	FILE *old, *new, *record;
	struct Account *account=(struct Account *)malloc(sizeof(struct Account));	
	struct Account *debit=(struct Account *)malloc(sizeof(struct Account));
	struct Account *credit=(struct Account *)malloc(sizeof(struct Account));
	struct Transaction *transaction=(struct Transaction *)malloc(sizeof(struct Transaction));
	if((old=fopen("Account.txt", "r"))==NULL){
		printf("Fail to open the file!\n");
		exit(EXIT_FAILURE);
	}
	if((new=fopen("Modify.txt", "w"))==NULL){
		printf("Fail to open the file!\n");
		exit(EXIT_FAILURE);
	}
	if((record=fopen("Transaction.txt", "a"))==NULL){
		printf("Fail to open the file!\n");
		exit(EXIT_FAILURE);
	}
	printf("Enter the debiting account number: ");
	scanf("%s", deb);
	getchar();
	printf("\n");
	while((fread(debit, sizeof(struct Account), 1, old))!=0){
		if(strcmp(debit->accountnumber, deb)==0){
			printf("Name of the account holder: %s\n", debit->name);
			printf("Id number of the account holder: %s\n", debit->id);
			printf("Balance of the account: %d\n\n", debit->balance);
			break;
		}	
	}
	printf("Enter the crediting account number: ");
	scanf("%s", cre);
	getchar();
	printf("\n");
	fseek(old, 0, SEEK_SET);
	while((fread(credit, sizeof(struct Account), 1, old))!=0){
		if(strcmp(credit->accountnumber, cre)==0){
			printf("Name of the account holder: %s\n", credit->name);
			printf("Id number of the account holder: %s\n", credit->id);
			printf("Balance of the account: %d\n\n", credit->balance);
			break;
		}
	}
	amount=(debit->balance)+1;	
	while(amount>(debit->balance)){
		printf("Enter the transfer amount: ");
		scanf("%d", &amount);
		getchar();
		if(amount>(debit->balance)){
			printf("\nTransfer amount can't be more than the balance of the debiting account.\n\n");
		}
	}
	printf("\nTransaction successful.\n\n");
	(debit->balance)-=amount;
	(credit->balance)+=amount;
	printf("Balance of the account %s is: %d\n", debit->accountnumber, debit->balance);
	printf("Balance of the account %s is: %d\n\n", credit->accountnumber, credit->balance);
	
	





	fseek(old, 0, SEEK_SET);
	while((fread(account, sizeof(struct Account), 1, old))!=0){
		if((account->accountnumber)==(debit->accountnumber)){
			account=debit;
		}
		else if((account->accountnumber)==(credit->accountnumber)){
			account=credit;
		}
		fwrite(account, sizeof(struct Account), 1, new);
	}
	fclose(old);
	fclose(new);
	if((old=fopen("Account.txt", "w"))==NULL){
		printf("Fail to open the file!\n");
		exit(EXIT_FAILURE);
	}
	if((new=fopen("Modify.txt", "r"))==NULL){
		printf("Fail to open the file!\n");
		exit(EXIT_FAILURE);
	}
	while((fread(account, sizeof(struct Account), 1, new))!=0){
		fwrite(account, sizeof(struct Account), 1, old);
	}
	fclose(old);
	fclose(new);
	free(account);
	time(&t);
	p=localtime(&t);
	sprintf(transaction->Dot, "%d/%02d/%02d",1900+(p->tm_year), 1+(p->tm_mon), p->tm_mday);
	transaction->Dot[10]='\0';
	strcpy(transaction->debitac, debit->accountnumber);
	strcpy(transaction->creditac, credit->accountnumber);
	transaction->figure=amount;
	fwrite(transaction, sizeof(struct Transaction), 1, record);
	fclose(record);
	free(debit);
	free(credit);
	free(transaction);
	printf("\n");
}

我写的这个功能,在空白的那几行之后的代码都没有运行,不知道是为什么,应该怎么改?

等于说虽然成功得到了debit和credit两个结构体,也成功修改了,但是存不回去。

  • 写回答

6条回答 默认 最新

  • bosaidongmomo 2020-12-24 21:01
    关注

    你得告诉我你怎么操作的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献