『壹』 在c語言中如何用程序輸出邏輯表達式的值
C語言編譯系統在表示邏輯運算結果時,以數值1代表「真」,以0代表「假」。所以說邏輯表達式的值是int類型的,可以通過輸出時格式%d控制輸出,例如printf(「%d",1>2).
『貳』 用c語言編寫ATM的程序,實現開戶、存款、取款、查詢余額、轉賬的業務邏輯。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
void regist();
void login();
void quite();
void inputpassword(char mima[]);
void service();
struct bank
{
char name[20];
char password[7];
int account;
double money;
}kehu;
int find;
int a[10];
struct bank one;
FILE *fp;
void main()
{
int i;
int t=1;
for(i=0;i<100;i++)
{
printf("\t\t\t\t\t\t歡迎使用青軟ATM系統\n");
printf("\t\t\t\t\t\t正在進入主界面,請稍等");
int j;
for(j=1;j<t;j++)
{
printf(".");
}
t++;
if(t==10)
{
t=1;
}
printf("\n\t\t\t\t\t\t%d%%",i);
system("cls");
}
while(1)
{
printf("\t\t\t\t\t\t服務類型: \n");
printf("\t\t\t\t\t\t[a]: 用戶注冊\n");
printf("\t\t\t\t\t\t[b]: 用戶登錄\n");
printf("\t\t\t\t\t\t[c]: 退出系統\n");
printf("\t\t\t\t\t\t請選擇服務: ");
fflush(stdin);
char xz;
scanf("%c",&xz);
if(xz=='a'||xz=='A')
{
regist();
} else if (xz=='b'||xz=='B')
{
login();
} else if(xz=='c'||xz=='C')
{
quite();
} else
{
printf("輸入有誤,請重新輸入");
}
getch();
system("cls");
}
}
void inputpassword(char mima[])
{
int i=0;
char ch;
while(1)
{
ch=getch();
if(ch!='\r')
{
if(ch!='\b'){
mima[i]=ch;
i++;
printf("*");
}else{
if(i>0){
i--;
printf("\b \b");
}
}
}else{
break;
}
}
mima[i]='\0';
printf("\n");
}
void regist()
{
fp=fopen("atm.txt","ab+");
if(fp==NULL)
{
printf("\n\t\t\t文件打開失敗!");
return;
}
system("cls");
printf("\t\t\t現在執行的是注冊函數的使用\n");
printf("\t\t請輸入用戶名: ");
fflush(stdin);
gets(kehu.name);
char password1[7];
while(1)
{
while(1)
{
printf("\n\n\t\t請輸入密碼:");
fflush(stdin);
inputpassword(kehu.password);
int n=strlen(kehu.password);
if(n==6)
{
break;
}else
{
printf("\n\t\t密碼必須為6位!");
}
}
printf("\n\t\t請輸入正確密碼!: ");
fflush(stdin);
inputpassword(password1);
if(strcmp(kehu.password,password1)==0)
{
break;
}else{
printf("\n\n\t\t兩次密碼必須相同!");
}
}
rewind(fp);
struct bank k;
if(fread(&k,sizeof(struct bank),1,fp)==1)
{
fseek(fp,-sizeof(k),2);
fread(&k,sizeof(k),1,fp);
kehu.account=k.account+1;
}else{
kehu.account=20170001;
}
kehu.money=0;
fseek(fp,0,2);
fwrite(&kehu,sizeof(struct bank),1,fp);
fclose(fp);
printf("\n\n\t\t開戶成功! ");
printf("\n\t\t您的賬號為%d!",kehu.account);
printf("\n\t\t現在請您重新登錄!");
}
void searchmoney()
{
system("cls");
printf("您現在使用的是查詢余額功能: \n");
printf("\n\n\t\t您的余額是%0.2lf",one.money);
}
void savemoney()
{
system("cls");
double inmoney;
printf("請您選擇您要存款的金額 \n");
scanf("%lf",&inmoney);
int q;
int r=1;
for(q=0;q<100;q++)
{
int w;
for(w=1;w<r;w++)
{
printf(".");
}
r++;
if(r==10)
{
r=1;
}
printf("\n\t\t\t\t\t\t正在存款%d%%",q);
system("cls");
}
one.money=one.money+inmoney;
fseek(fp,-sizeof(one),1);
fwrite(&one,sizeof(one),1,fp);
printf("\n\n\t\t\t\t\t\t您已存款成功!");
}
void withdrawalmoney()
{
system("cls");
double outputsomemoney;
printf("請您選擇您要取款的金額 \n");
scanf("%lf",&outputsomemoney);
if(one.money<outputsomemoney){
printf("您的余額已不足,請您注意!");
}else {
int q;
int r=1;
for(q=0;q<100;q++)
{
int w;
for(w=1;w<r;w++)
{
printf(".");
}
r++;
if(r==10)
{
r=1;
}
printf("\n\t\t\t\t\t\t正在取款%d%%",q);
system("cls");
}
one.money=one.money-outputsomemoney;
fseek(fp,-sizeof(one),1);
fwrite(&one,sizeof(one),1,fp);
printf("\n\n\t\t\t\t\t\t您已取款成功!請點清鈔票!");
printf("\n\n\t\t\t\t\t\t您現在的余額為%lf",one.money);
}
}
void transfermoney()
{
system("cls");
int ifang;
int qian;
fflush(stdin);
printf("\n\n\n\t\t您現在使用的是轉賬功能");
printf("\n\t\t\t請輸入您要轉賬的賬戶:");
scanf("%d",&ifang);
int n=ftell(fp);
rewind(fp);
int flag=0;
struct bank temp;
while(fread(&temp,sizeof(temp),1,fp)==1)
{
if(temp.account==ifang)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("請輸入轉賬金額:");
scanf("%d",&qian);
if(one.money>=qian)
{
int q;
int r=1;
for(q=0;q<100;q++)
{
int w;
for(w=1;w<r;w++)
{
printf(".");
}
r++;
if(r==10)
{
r=1;
}
printf("\n\t\t\t\t\t\t正在轉賬,請稍後!%d%%",q);
system("cls");
}
temp.money=temp.money+qian;
fseek(fp,-sizeof(temp),1);
fwrite(&temp,sizeof(temp),1,fp);
one.money=one.money-qian;
fseek(fp,n-sizeof(one),0);
fwrite(&one,sizeof(one),1,fp);
printf("\n\t\t\t\t轉賬成功!");
}else{
printf("\n\t\t\t\t您的余額已不足!");
}
}
}
void xiugai(){
system("cls");
printf("\n\n\t\t 現在進行的是修改密碼功能\n");
char oldpassword[7];
char newpassword[7];
char newpassword1[7];
int i;
for(i=0;i<3;i++){
printf("\n\t\t\t 請輸入舊密碼:\n");
inputpassword(oldpassword);
if(strcmp(oldpassword,one.password)==0){
printf("\n\t\t\t 輸入成功!\n");
break;
}else{
printf("\n\t\t\t 密碼輸入有誤,請重新輸入!\n");
}
}
if(i<3){
while(1){
printf("\n\t\t\t 請輸入您的新密碼:\n");
inputpassword(newpassword);
printf("\n\t\t\t 請輸入您的確認密碼:\n ");
inputpassword(newpassword1);
if(strcmp(newpassword,newpassword1)==0){
strcpy(one.password,newpassword);
fseek(fp,-sizeof(one),1);
fwrite(&one,sizeof(one),1,fp);
printf("\n\t\t\t 密碼修改成功!");
break;
}else{
printf("\n\t\t\t 兩次密碼輸入不一致!");
}
}
}else{
printf("\n\t\t\t 密碼輸入錯誤!");
}
}
int zhuxiaozhanghao()
{
system("cls");
int zhuxiaoxitong;
char sf;
printf("你要注銷的賬號是%d",one.account);
printf("你是否要對此賬號進行注銷?\n\n\t\t請您選擇:注銷(Y)or不注銷(N):");
fflush(stdin);
scanf("%c",&sf);
if(sf=='y'||sf=='Y')
{
printf("正在為您注銷!\n",one.account);
zhuxiaoxitong=1;
}else{
printf("不注銷系統!\n",one.account);
}
return zhuxiaoxitong;
}
void service()
{
while(1){
system("cls");
printf("\n\n\n\t\t\t\t\t\t現在是服務系統,本系統有以下服務");
printf("\n\t\t\t\t\t\t[a] 查詢余額\n");
printf("\n\t\t\t\t\t\t[b] 存款服務\n");
printf("\n\t\t\t\t\t\t[c] 轉賬服務\n");
printf("\n\t\t\t\t\t\t[d] 取款服務\n");
printf("\n\t\t\t\t\t\t[e] 修改密碼\n");
printf("\n\t\t\t\t\t\t[f] 注銷 \n");
printf("\n\t\t\t\t\t\t[g] 退出系統\n");
char e;
printf("\n\t\t\t\t\t\t您要選擇的服務是:");
fflush(stdin);
scanf("%c",&e);
switch(e)
{ case'A':
case'a': searchmoney() ;break;
case'B':
case'b': savemoney() ;break;
case'C':
case'c': transfermoney() ;break;
case'D':
case'd': withdrawalmoney() ;break;
case'E':
case'e': xiugai() ;break;
case'F':
case'f': {int zhuxiaoxitong=zhuxiaozhanghao();{if(zhuxiaoxitong==1) return;}break;}
case'G':
case'g': quite();break;
}
printf("\n\n\n\t\t\t\t按任意鍵繼續......\n");
getch();
}
}
void login()
{
fp=fopen("atm.txt","rb+");
if(fp==NULL)
{
printf("\n\n\n\t\t\t\t文件打開失敗!");
return;
}
system("cls");
printf("\n\t\t\t\t\t\t現在執行的是登錄函數的使用\n");
int zhanghao;
printf("\n\t\t\t\t\t\t請輸入賬號:");
scanf("%d",&zhanghao);
int flag=0;
while(fread(&one,sizeof(one),1,fp)==1)
{
if(zhanghao==one.account){
flag=1;
break;
}
}
char password2[7];
if(flag==1){
int h;
for(h=0;h<3;h++){
printf("\n\t\t\t\t\t\t請輸入密碼:");
fflush(stdin);
inputpassword(password2);
if(strcmp(password2,one.password)==0)
{
printf("\n\t\t\t\t\t\t登陸成功!");
service();
break;
}else{
printf("密碼不正確!");
}
}
if(h==3){
printf("\n\t\t\t您的密碼三次輸入有誤,返回");
}
}else{
printf("無此賬號!");
}
fclose(fp);
}
void quite()
{
system("cls");
printf("\t\t\t現在執行的是退出函數的使用\n");
exit(0);
}
『叄』 C語言中邏輯運算怎麼做啊
&&邏輯與,2和8都大於0所以,2&&8為真,==是邏輯等於運算符,1==1為真,為真在c語言中用1表示,假用0表示。