package comefounderpubutil;
import javautilArrayList;
import javautilVector;
/
字符串处理工具类
@author xiewanzhi
/
public class StringFunction {
public static String convertFromStringArrayToStringBySymbol(String[] stringArray, char symbol) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < stringArraylength; i++) {
sbappend(stringArray[i]);
sbappend(symbol);
}
return sbtoString();
}
public static String[] convertFromStringToStringArrayBySymbol(String string, String symbol) {
Vector stringVector = convertFromStringToStringVectorBySymbol(string, symbol);
String[] stringArray = new String[stringVectorsize()];
for (int i = 0; i < stringVectorsize(); i++)
stringArray[i] = (String) (stringVectorelementAt(i));
return stringArray;
}
public static String[] convertFromStringToStringArrayBySymbolNO(String string, String symbol) {
Vector stringVector = convertFromStringToStringVectorBySymbolNO(string, symbol);
String[] stringArray = new String[stringVectorsize()];
for (int i = 0; i < stringVectorsize(); i++)
stringArray[i] = (String) (stringVectorelementAt(i));
return stringArray;
}
public static Vector convertFromStringToStringVectorBySymbol(String string, String symbol) {
Vector stringVector = new Vector();
String[] array = stringsplit(symbol);
for (int i = 0; i < arraylength; i++) {
stringVectoraddElement(array[i]);
}
return stringVector;
}
public static Vector convertFromStringToStringVectorBySymbolNO(String string, String symbol) {
Vector stringVector = new Vector();
String[] array = stringsplit(symbol);
for (int i = 0; i < arraylength; i++) {
stringVectoraddElement(array[i]);
}
return stringVector;
}
public static String convertFromStringVectorToStringBySymbol(Vector stringVector, char symbol) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < stringVectorsize(); i++) {
sbappend(stringVectorelementAt(i));
sbappend(symbol);
}
return sbtoString();
}
public static String fillString(String psStr, char psC, int psLen) {
if (psStrlength() > psLen) {
return psStrsubstring(0, psLen);
} else {
char[] vcTemp = new char[psLen];
for (int i = 0; i < psLen; i++) {
vcTemp[i] = psC;
}
String vsTemp = new String(vcTemp);
String vsResult = psStrconcat(vsTemp);
return vsResultsubstring(0, psLen);
}
}
public static String formatValue(double value, int sep, char ch, int round) {
return "";
}
public static String GB2Uni(String original) {
if (original != null) {
try {
return new String(originalgetBytes("GBK"), "ISO8859_1");
} catch (Exception e) {
eprintStackTrace();
return null;
}
} else
return null;
}
public static String Uni2GB(String original) {
if (original != null) {
try {
return new String(originalgetBytes("ISO8859_1"), "GBK");
} catch (Exception e) {
eprintStackTrace();
return null;
}
} else
return null;
}
public static String generateID(String pre, int value, int num) {
char chs[] = new char[num];
for (int i = 0; i < num; i++) {
chs[num - i - 1] = (char) (48 + value % 10);
value = value / 10;
}
return pretrim() + new String(chs);
}
public static String getTempStr() {
return StringvalueOf(SystemcurrentTimeMillis());
}
public static boolean isIncludeString(String psStr, String psS) {
int viPos = psStrindexOf(psS);
return viPos >= 0;
}
public static String replaceString(String psStr, String psS, String psD) {
int viPos = psStrindexOf(psS);
if (viPos < 0)
return psStr;
int viLength = psSlength();
StringBuffer vsValue = new StringBuffer();
while (viPos >= 0) {
vsValueappend(psStrsubstring(0, viPos));
vsValueappend(psD);
psStr = psStrsubstring(viPos + viLength);
viPos = psStrindexOf(psS);
}
vsValueappend(psStr);
return vsValuetoString();
}
public static String[] splitStringByToken(String vsStr, String symbol) {
String vsString[] = { "", "" };
int viPos1;
viPos1 = vsStrindexOf(symbol);
if (viPos1 < 0) {
vsString[0] = vsStr;
vsString[1] = "";
return vsString;
}
vsString[0] = vsStrsubstring(0, viPos1);
vsString[1] = vsStrsubstring(viPos1 + symbollength(), vsStrlength());
return vsString;
}
public static Vector convertFromStringToStringVectorByString(String string, String symbol) {
Vector stringVector = new Vector();
String vsStr = stringtrim();
String vsTemp = null;
String[] st = null;
while ((!vsStrequals("")) && isIncludeString(vsStr, symbol)) {
st = splitStringByToken(vsStr, symbol);
vsTemp = st[0]trim();
if (!vsTempequals(""))
stringVectoraddElement(vsTemp);
vsStr = st[1]trim();
}
if (!vsStrequals(""))
stringVectoraddElement(vsStr);
return stringVector;
}
public static String getTempStr(int length) {
String str = StringvalueOf(SystemcurrentTimeMillis());
return strsubstring(strlength() - length);
}
public static Vector convertFromStringToStringVectorByStringWithNull(String string, String symbol) {
Vector stringVector = new Vector();
String vsStr = stringtrim();
String vsTemp = null;
String[] st = null;
while ((!vsStrequals("")) && isIncludeString(vsStr, symbol)) {
st = splitStringByToken(vsStr, symbol);
vsTemp = st[0]trim();
stringVectoraddElement(vsTemp);
vsStr = st[1]trim();
}
if (!vsStrequals(""))
stringVectoraddElement(vsStr);
return stringVector;
}
public static String DelZeroForBM(String Bm, int Js, String Struct) {
int i, Len, Length;
Len = Bmlength();
Length = GetStructLength(Struct, Js);
if (Len >= Length) {
Bm = Bmsubstring(0, Length);
}
return Bm;
}
public static String FillZeroForBM(String Bm, String Struct) {
int i, Len, Length;
Len = Bmlength();
Length = GetStructLength(Struct, 0);
for (i = 0; i < Length - Len; i++) {
Bm += "0";
}
return Bm;
}
public static int getJsByCodingStruct(String code, String struct) {
int codeLength = codelength();
int structLength = structlength();
int tempLength = 0;
for (int js = 1; js <= structLength; js++) {
tempLength = GetStructLength(struct, js);
if (codeLength == tempLength)
return js;
}
return 0;
}
public static int GetStructLength(String Struct, int JS) {
int i, Length = 0;
String sub;
Integer ii;
if (JS == 0)
JS = Structtrim()length();
for (i = 0; i < JS; i++) {
sub = Structsubstring(i, i + 1)trim();
ii = Integerdecode("0x" + sub);
// Length += Integer parseInt(sub);
Length += iiintValue();
}
return Length;
}
public static String GetSubBMfromBM(String BM, String Struct, int SJS) {
String Res = "";
int Len;
Len = GetStructLength(Struct, SJS);
if (BMlength() >= Len)
Res = BMsubstring(0, Len);
return Res;
}
public static String FillZeroFromBegin(int num, int leng) {
return FillTagFromBegin(StringvalueOf(num), leng, "0");
}
public static String FillTagFromBegin(String num, int leng, String Tag) {
String Res = "", tempStr1 = "", tempStr2 = "";
tempStr1 = StringvalueOf(num);
for (int i = 0; i < (leng - tempStr1length()); i++) {
tempStr2 += Tag;
}
Res = tempStr2 + tempStr1;
return Res;
}
public static String ClearBMZero(String BM, String BMStruct) {
return ClearBMTag(BM, BMStruct, "0");
}
public static String ClearBMTag(String BM, String BMStruct, String Tag) {
int JSLen = BMStructlength();
String bm = "", ZERO, Tmp;
int Len;
for (int i = 1; i <= JSLen; i++) {
Tmp = BMStructsubstring(i - 1, i);
Len = IntegerparseInt(Tmp);
ZERO = RepeatChar(Tag, Len);
if (!BMstartsWith(ZERO)) {
bm += BMsubstring(0, Len);
BM = BMsubstring(Len);
} else
break;
}
return bm;
}
public static String RepeatChar(String c, int len) {
String Res = "";
for (int i = 0; i < len; i++) {
Res += c;
}
return Res;
}
/
key1=value1;key2=value2;key3=value3
@param inputString String
@param hashMap Map
/
public static void String2HashMap(String inputString, javautilMap hashMap) {
String[] keyValues = StringFunctionconvertFromStringToStringArrayBySymbolNO(inputString, ";");
String[] keyValue;
String key, value;
for (int i = 0; i < keyValueslength; i++) {
if (!""equals(keyValues[i])) {
keyValue = StringFunctionconvertFromStringToStringArrayBySymbolNO(keyValues[i], "=");
if (keyValuelength < 2)
continue;
key = keyValue[0];
value = "";
for (int k = 1; k < keyValuelength; k++) {
value += keyValue[k];
}
hashMapput(key, value);
}
}
}
/
@param hashMap
Map
@return String
/
public static String HashMap2String(javautilMap hashMap) {
Object[] keys = hashMapkeySet()toArray();
String key, value, keyValue = "";
for (int i = 0; i < keyslength; i++) {
key = (String) keys[i];
value = (String) hashMapget(key);
keyValue += key + "=" + value + ";";
}
if (keyValuelength() > 0) {
keyValue = keyValuesubstring(0, keyValuelength() - 1);
}
return keyValue;
}
/
@param value
String
@param mark
String
@return String[]
/
public static String[] split(String value, String mark) {
int index = valueindexOf(mark);
javautilList list = new ArrayList();
while (index >= 0) {
listadd(valuesubstring(0, index));
value = valuesubstring(index + marklength());
index = valueindexOf(mark);
}
listadd(value);
String[] rtn = new String[listsize()];
Systemarraycopy(listtoArray(), 0, rtn, 0, rtnlength);
return rtn;
}
public static void main(String[] args) {
String value = "a;;;; ";
String[] rtn = split(value, ";;");
Systemoutprintln(rtn);
}
}
例如
int main()
{
int n;//这个变量可以在main函数范围内部使用
void qu_kuan()
{
int q;//这个变量只能在qu_kuan函数内部使用
}
void cha_xun()
{
int m;//这个变量只能在cha_xun函数内部使用
}
}
如果你自定义函数在main内部,那跨函数使用的变量就全部定义在main中
如果你自定义的函数不在main内部,在main外部,而又没有跨文件,那变量就定义在main函数的上面,如
int n;
int main()
{
}
void cha_xun()
{
//这里和main函数都能使用n
}
下面的是我自己写的一个, 里面很多细节都没有进行细致的处理, 只是粗略的实现了基本的功能
后面有我的测试数据, 希望能有帮助
#include <stdioh>
#include <stdlibh>
#include <stringh>
typedef struct _account
{
char UID;
char pwd;
int balance;
char UName;
}ACCOUNT, PACCOUNT;
void InitAccount(PACCOUNT pA); // 初始化账户
void showAccount(ACCOUNT A, bool flag); // 显示账户信息, flag表示是否显示全部信息 如果是true则需要输入用户密码
bool deposite(PACCOUNT pA); // 存钱, 内部需要密码验证并输入金额
bool withDraw(PACCOUNT pA); // 取钱, 内部需要密码验证并输入金额
bool transfer(PACCOUNT pA, PACCOUNT pB); // 转账, 需要密码验证, 并输入金额
bool conduct(PACCOUNT pA, int chose, PACCOUNT pB); // 处理, 就是根据菜单项处理用户的 *** 作选择
void modifyPwd(PACCOUNT pA); // 更改用户密码
bool Authentication(PACCOUNT pA); // 密码认证, 3次机会输入密码
void memFree(PACCOUNT pA, PACCOUNT pB); // 在堆上分配的内存的释放
int main(void)
{
// 建立两个账户, 分别是 *** 作账户和接受转账的账户
PACCOUNT pMainAcc = (PACCOUNT)malloc(sizeof(ACCOUNT));
PACCOUNT pAssistAcc = (PACCOUNT)malloc(sizeof(ACCOUNT));
// 初始化两个账户的信息
InitAccount(pMainAcc);
InitAccount(pAssistAcc);
// 进行菜单控制, 提供用户 *** 作
int chose = -1;
while(chose != 0)
{
printf("\n1 存钱\t2 取钱\t3 转账\t4 更改密码\t5 显示账户信息\t0退出\n");
scanf("%d", &chose);
conduct(pMainAcc, chose, pAssistAcc);
}
return 0;
}
bool conduct(PACCOUNT pA, int chose, PACCOUNT pB)
{
bool rtnflag = true;
switch(chose)
{
case 1:
if(!deposite(pA))
printf(" *** 作失败!");
else
printf(" *** 作成功!");
break;
case 2:
if(!withDraw(pA))
printf(" *** 作失败!");
else
printf(" *** 作成功!");
break;
case 3:
if(!transfer(pA, pB))
printf(" *** 作失败!");
else
printf(" *** 作成功!");
break;
case 4:
modifyPwd(pA);
break;
case 5:
showAccount(pA, true);
break;
case 0:
rtnflag = false;
memFree(pA, pB);
break;
}
return rtnflag;
}
void InitAccount(PACCOUNT pA)
{
printf("请初始化账户名, 密码, 姓名, 账户余额\n");
pA->UID = (char )malloc(sizeof(char)20);
pA->pwd = (char )malloc(sizeof(char)20);
pA->UName = (char )malloc(sizeof(char)20);
gets(pA->UID);
gets(pA->pwd);
gets(pA->UName);
scanf("%d", &pA->balance);
getchar();
return ;
}
void showAccount(ACCOUNT A, bool flag)
{
if(flag)
{
int i = 0;
getchar();
char tmpPwd = (char )malloc(sizeof(char)20);
while(strcmp(tmpPwd, Apwd))
{
printf("请输入账户%s的密码:\n", AUID);
gets(tmpPwd);
if(++i > 3)
{
printf("对不起, 密码输入错误!只能显示部分信息!\n");
showAccount(A, false);
free(tmpPwd);
return ;
}
}
printf("账户信息如下:\n账户名\t账户密码\t账户余额\t姓名\n");
printf("%6s\t%8s%8d\t%8\ts\n", AUID, Apwd, Abalance, AUName);
free(tmpPwd);
}
else
{
printf("账户信息如下:\n账户名\t账户余额\t姓名\n");
printf("%6s\t%8d\t%4s\n", AUID, Abalance, AUName);
}
return ;
}
bool deposite(PACCOUNT pA)
{
if(!Authentication(pA))
return false;
int val = 0;
printf("请输入金额:\n");
scanf("%d", &val);
pA->balance += val;
return true;
}
bool withDraw(PACCOUNT pA)
{
if(!Authentication(pA))
return false;
printf("请输入金额");
int val = 0;
scanf("%d", &val);
if(pA->balance >= val)
{
pA->balance -= val;
}
else
{
printf("对不起, 余额不足!");
return false;
}
return true;
}
bool transfer(PACCOUNT pA, PACCOUNT pB)
{
if(!Authentication(pA))
return false;
printf("请输入金额");
int val = 0;
scanf("%d", &val);
if(pA->balance >= val)
{
pA->balance -= val;
pB->balance += val;
}
else
{
printf("对不起, 余额不足!");
return false;
}
return true;
}
void modifyPwd(PACCOUNT pA)
{
if(Authentication(pA))
{
printf("请输入新的密码!");
free(pA->pwd);
pA->pwd = (char )malloc(sizeof(char)20);
gets(pA->pwd);
}
else
{
printf("对不起, 您没有权限进行密码修改!");
}
}
bool Authentication(PACCOUNT pA)
{
getchar();
int i = 0;
char tmpPwd = (char )malloc(sizeof(char)20);
while(strcmp(tmpPwd, pA->pwd))
{
printf("请输入%s的密码, 3次机会:\n", pA->UID);
gets(tmpPwd);
if(++i == 3)
{
return false;
}
}
return true;
}
void memFree(PACCOUNT pA, PACCOUNT pB)
{
free(pA);
free(pB);
return ;
}
/
运行环境: VC60
请初始化账户名, 密码, 姓名, 账户余额
wed
qweasd
wednesday
800
请初始化账户名, 密码, 姓名, 账户余额
hu
sad
huni
200
1 存钱 2 取钱 3 转账 4 更改密码 5 显示账户信息 0退出
1
请输入wed的密码, 3次机会:
qwe
请输入wed的密码, 3次机会:
qweasd
请输入金额:
54
*** 作成功!
1 存钱 2 取钱 3 转账 4 更改密码 5 显示账户信息 0退出
5
请输入账户wed的密码:
qwe
请输入账户wed的密码:
qweasd
账户信息如下:
账户名 账户密码 账户余额 姓名
wed qweasd 854 s
1 存钱 2 取钱 3 转账 4 更改密码 5 显示账户信息 0退出
4
请输入wed的密码, 3次机会:
123
请输入wed的密码, 3次机会:
qweasd
请输入新的密码!123qwe
1 存钱 2 取钱 3 转账 4 更改密码 5 显示账户信息 0退出
1
请输入wed的密码, 3次机会:
qweasd
请输入wed的密码, 3次机会:
123qwe
请输入金额:
43
*** 作成功!
1 存钱 2 取钱 3 转账 4 更改密码 5 显示账户信息 0退出
5
请输入账户wed的密码:
123qwe
账户信息如下:
账户名 账户密码 账户余额 姓名
wed 123qwe 897 s
1 存钱 2 取钱 3 转账 4 更改密码 5 显示账户信息 0退出
0
Press any key to continue
/
/
要求:使用字符用户界面。当输入给定的卡号和密码(初始卡号和密码为123456)时,系统能登录ATM柜员机系统,用户可以按照以下规则进行:
1、查询余额:初始余额为10000元
2、ATM取款:每次取款金额为100的倍数,总额不超过5000元,支取金额不允许透支。
3、ATM存款:不能出现负存款。
4、修改密码:新密码长度不小于6位,不允许出现6位完全相同的情况,只有旧密码正确,新密码符合要求,且两次输入相同的情况下才可以成功修改密码。
(卡号密码余额放到文件中)
/
public class ATM {
private Account acc;
private File dataFile;
private FileWriter fw;
private BufferedWriter bw;
private String filePath = "/datatxt";
public ATM() {
thisacc = new Account();
try {
thisdataFile = new File(thisfilePath);
if (!thisdataFileexists()) {
thisdataFilecreateNewFile();
}
thisfw = new FileWriter(thisfilePath);
thisbw = new BufferedWriter(thisfw);
} catch (IOException io) {
Systemerrprintln("Cannot open file");
ioprintStackTrace();
} catch (Exception e) {
eprintStackTrace();
}
}
public static void main(String[] args) {
new ATM()interact();
}
public void interact() {
BufferedReader br = new BufferedReader(new InputStreamReader(Systemin));
Systemoutprintln("Account #: ");
String temp = brreadLine();
Systemoutprintln("Password: ");
String temp2 = brreadLine();
if (!thisaccisValid(LongparseLong(temptrim()), temp2trim()) {
Systemerrprintln("Wrong password");
return;
}
Systemoutprintln("1 Account Inquery");
Systemoutprintln("2 Withdraw");
Systemoutprintln("3 Deposit");
Systemoutprintln("4 Change Password");
Systemoutprintln("5 Export to File");
Systemoutprintln("0 Exit");
int c = 100;
while (c != 0) {
String str = brreadLine();
try {
int c = IntegerparseInt(strtrim());
} catch (NumberFormatException nfe) {
Systemerrprintln("Invalid choice");
continue;
}
switch (c) {
case 0:
Systemoutprintln("Thank you");
break;
case 1:
Systemoutprintln("Balance: " + thisaccbalanceInquery());
break;
case 2:
Systemoutprintln("How much ");
String temp = brreadLine();
try {
long ammount = LongparseLong(temptrim());
thisaccwithdraw(ammount);
break;
} catch (NumberFormatException nfe) {
Systemerrprintln("Invalid amount");
continue;
}
case 3:
Systemoutprintln("How much ");
String temp = brreadLine();
try {
long ammount = LongparseLong(temptrim());
thisaccdeposit(ammount);
break;
} catch (NumberFormatException nfe) {
Systemerrprintln("Invalid amount");
continue;
}
case 4:
Systemoutprintln("Old password: ");
String temp = brreadLine();
Systemoutprintln("New password: ");
String temp2 = brreadLine();
thisaccchangePassword(temp, temp2);
break;
case 5:
thisbwwrite(thisacctoString());
break;
default:
break;
}
}
}
}
class Account {
private long accNo = 123456;
private String pass = "123456";
private long balance = 10000;
public Account() {
}
public boolean isValid(long accNo, String pass) {
return (thisaccNo == accNo) && (passequals(thispass));
}
public void changePassword(String oldPass, String password) {
if (!oldPassequals(thispass)) {
Systemerrprintln("Wrong password");
return;
}
if (passwordlength < 6) {
Systemerrprintln("Password too short");
return;
}
if (passwordequals(thispass)) {
Systemerrprintln("Password cannot be the same");
return;
}
thispass = password;
}
public long balanceInquery() {
return thisbalance;
}
public void withdraw(long amount) {
if (amount > 5000 || amount < 0) {
Systemerrprintln("Withdraw limit: $0-$5000");
return;
}
if ((amount % 100) != 0) {
Systemerrprintln("The amount has to be a product of 100");
return;
}
long newBalance = thisbalance - amount;
if (newBalance < 0) {
Systemerrprintln("Not enough money in the account");
return;
}
thisbalance = newBalance;
}
public void deposit(long amount) {
if (amount < 0) {
Systemerrprintln("Cannot deposit negative amount");
return;
}
thisbalance += amount;
}
public String toString() {
return ("Account #: " + thisaccNo + "\n" + "Password: " + thispass + "\n" + "Balance: " + thisbalance);
}
}
请采纳答案,支持我一下。
以上就是关于用C语言写一个类似于ATM机的程序,有开户(姓名,密码),存款,取款,查询余额,转账的功能,全部的内容,包括:用C语言写一个类似于ATM机的程序,有开户(姓名,密码),存款,取款,查询余额,转账的功能,、c语言初学,写了一个ATM机 *** 作程序,遇到一个小问题 用VC6写一个简单的ATM机 *** 作的程序,遇、编写一个关于“ATM系统”c语言程序。 要求,1功能:存钱,取钱,转账,修改密码,只要现实中有的都得有,等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)