#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<conio.h>
//用戶資訊
typedef struct tagPerson
{
char szUsername[20];
char szPassword[7];
char szAccoutNumber[20];
float fMoney;
}Person;
typedef struct tagNode
{
Person per;//資料域
struct tagNode* pNext;//指標域
}Node;
Node* g_pHead=NULL;//鏈表頭結點
//開戶
int CreateAccout();
int main()
{
system("color 2F");
printf("********************************************\n");
printf("\t\t\t* *\n");
printf("\t\t\t* 帝國銀行ATM *\n");
printf("\t\t\t* *\n");
printf("\t\t\t*******************************\n");
printf("\t\t\t* *\n");
printf("\t\t\t* 1.開戶 *\n");
printf("\t\t\t* 2.登錄 *\n");
printf("\t\t\t* 3.前臺資訊查詢中心 *\n");
printf("\t\t\t* 4.請選擇你的需求 *\n");
printf("\t\t\t* *\n");
printf("\t\t\t*******************************\n");
char ch = getch();
switch(ch)
{
case '1': CreateAccout();
break;
case '2':
break;
case'3':
break;
case'4':
break;
}
while(1);
return 0;
}
int CreateAccout()
{
printf("請輸入你的姓名\n");
char szUsername[20];
scanf("%s",szUsername);
printf("請設定你的銀行卡密碼\n");
char szPassword[7];
scanf("%s",szPassword);
printf("請再次設定你的銀行卡密碼\n");
char szRePassword[7];
scanf("%s",szRePassword);
if(0!=strcmp(szPassword,szRePassword))
{
printf("兩次輸入密碼不一致.\n");
return 0;
}
//生成銀行賬號
char szAccoutNum[20];
srand((unsigned int)time(NULL));
sprintf(szAccoutNum,"%d%d%d%d%d%d",(rand()%9000)+1000,(rand()%9000)+1000,(rand()%9000)+1000,(rand()%9000)+1000,(rand()%9000)+1000,rand()%10,rand()%10); //格式化一個字串
Node* p = g_pHead;
while(g_pHead!=NULL&&p->pNext!=NULL)
{
p = p->pNext;
}
//泛型指標void*
Node* pNewNode=(Node*)malloc(sizeof(Node));
strcmp(pNewNode->per.szUsername,szUsername);
strcmp(pNewNode->per.szPassword,szPassword);
strcmp(pNewNode->per.szAccoutNumber,szAccoutNum);
pNewNode->per.fMoney=0.0f;
//添加到尾節點后面
if(g_pHead == NULL)
{
g_pHead =pNewNode;
}
else
{
p->pNext=pNewNode;
}
printf("你的賬戶資訊如下:\n");
printf("\t姓名:%s\n",pNewNode->per.szUsername);
printf("\t卡號:%s\n",pNewNode->per.szAccoutNumber);
printf("\t余額:%0.2f\n",pNewNode->per.fMoney);
printf("恭喜,賬號申請成功!請登錄\n");
return 1;
}

為啥后面有亂碼?
uj5u.com熱心網友回復:
你直接用printf("\t姓名:%s\n",szUsername);
試試看,如果輸出正常,說明Node* pNewNode=(Node*)malloc(sizeof(Node));申請的記憶體不干凈。
在之后使用 memset 將該區域清零試試看。
memset( pNewNode,0,sizeof(Node));
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/22399.html
標籤:基礎類
上一篇:拜托了
下一篇:c++建構式問題
