b站网课学习
ATM机
#include<stdio.h>
int pass();//验证密码
void work();//业务驱动
void showMenu();//显示菜单
double openSaveMoney();//查询余额
void drawMoney();//取款
void saveMoney();//存款
void changePass();//修改密码
double openSaveMoney();//打开银行余额文件
void saveBalance(double Balance);//打开有密码的文件
void SavePassWord(int iPass);//保存修改密码
int main()
{
printf("-------欢迎来到CSDN银行---------\n");
if(Pass())
{
work();
}
return 0;
}
/*---------验证密码----------*/
int pass()
{
int try=3;
int iPass;
do
{
}
printf("请输入密码:\n");
scanf("%d",&ipass);
if(try>0)
{
if(ipass!=openPassWord())
{
printf("密码输入错误%d次,您还有%d次机会:\n",4-try,try-1);
printf("请重新输入密码:\n");
try--;
}
else
break;
}
else
{
printf("密码有误,退出系统\n");
exit(0);
}
while(try);
return(try);
/*------------业务驱动---------*/
void work()
{
int iChoice;//业务选项
do
{
showMenu();//显示菜单
printf("请输入选项0~4\n");
scanf("%d",&iChoice);
switch(iChoice)
{
case 1://查询
printf("您的余额还有:%.2lf\n",openSaveMoney());
break;
case 2://存款
saveMoney();
break;
case 3://取款
drakMoney();
break;
case 0://退出
break;
}
}while(iChoice);
}
/*---------显示菜单----------*/
void showMenu()
{
printf("---------------\n");
printf("1.查询\n");
printf("2.存款\n");
printf("3.取款\n");
printf("4.改密码\n");
printf("0.退出\n");
printf("-----------------\n");
}
/*----------取款------------*/
void drawMoney()
{
double Balance;//银行账户余额
double getMoney;//取款金额
printf("请输入取款金额:\n");
scanf("%lf",&getMoney);
Balance = openSaveMoney();//取出余额数目
if(Balance<getMoney)//取款数目大于余额
{
printf("存款余额不足\n");
exit(0);
}
else
{
Balance=Balance-getMoney;//银行账户余额
printf("已取款%.2lf\n账户余额为:%.2lf\n",getMoney,Balance);
}
saveBalance(Balance);//保存存款后的金额
}
/*----------------存款---------------*/
void saveMoney()
{
double iMoney;//存款金额
double saveMoney;//账户金额
printf("请输入存款\n");
scanf("%lf",&iMoney);
saveMoney = openSaveMoney();
saveMoney +=iMoney;
printf("您已经成功存入金额:%lf,\n账户余额为:%lf\n",iMoney,saveMoney);
saveBalance(saveMoney);//保存存款金额
}
/*---------------修改密码--------------*/
void changePass()
{
int Newpass1;//校验密码1
int NewPass2;//校验密码2
printf("请输入新设定密码:\n");
scanf("%d",&NewPass1);
printf("请重新输入新设定密码\n");
scanf("%d",&Newpass2);
if(NewPass1==Newpass2)
{
printf("密码设置成功\n");
SavePassWord(NewPass);
}
else
{
printf("密码前后不一致\n");
}
}
/*-----------打开银行余额文件-----------*/
double openSaveMoney()
{
FILE *fp;
double Balance;
if((fp=fopen("DrawMoney.txt","r"))==NULL)
{
printf("Cannot file open\n");
exit(0);
}
fscanf(fp,"%lf",&Balance);
fclose(fp);
return(Balance);
}
/*------------保存余额----------*/
void saveBalance(double Balance)
{
FILE*fp;
if((fp=fopen("DrawMoney.txt","w"))==NULL)
{
printf("Cannot file open\n");
exit(0);
}
fprintf(fp,"%lf",Balance);
fclose(fp);
}
/*-----------打开文件密码----------*/
int openPassWord()
{
FILE*fp;
int ipass;
if((fp=fopen("Password.txt","r"))==NULL)
{
printf("Cannot file open!\n");
exit(0);
}
fscanf(fp,"%d",&iPass);
fclose(fp);
return iPass;
}
/*-----------保存修改密码----------*/
void SavePassWord(int iPass)
{
FILE *fp;
if((fp=fopen("PassWord.txt","w"))==NULL)
{
printf("Cannot file open!\n");
exit(0);
}
fprintf(fp,"%d",ipass);
fclose(fp);
}
跟着学的(有瑕疵),涉及到文件的读取,指针
图书管理系统
尝试实训案列(未完成)
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define N 20
struct book /*定义学生结构体*/
{
long num; //编号
char name[20]; //书名
char author[20]; //作者
char press[20]; //出版社
int score[3]; //评分:豆瓣、当当、亚马逊
int sum; //总分
int ave; //平均分
}
struct book b[N];
int t=1,pl=0,count=0;
void enter();
void input();
void display();
void add();
void del();
void modify();
void preside();
void find();
void output();
void load();
void fprint();
int main()
{
int password;
int i;
printf("\n\n\t\t欢迎进入图书信息管理系统; /*初始界面 输入验证密码*/\n");
for(i=1;i<=3;i++)
{
printf("\n\n\t\t\t请输入密码:");
scanf("%d",&password);
if(password==123456)
{
enter();
}
printf("error!please input again!");
}
printf("密码输入三次有误!系统锁定!");
return 0;
}
void enter() /*进入主菜单函数*/
{
int choice,w;
char k;
system("cls");
printf("\t\t\t图书信息管理系统\n\n");
printf("\t\t1. 录入.\t2. 显示.\t3. 增项.\t4. 删项.\n\t\t5. 修改.\t6. 统计.\t7. 查找.\t8.退出.\n")
printf("\n输入序号执行相应功能");
scanf("%d",&choice);
switch(choice)
{
case 1:
input();
break;
case 2:
display();
break;
case 3:
add();
break;
case 4:
del();
break;
case 5:
modify();
break;
case 6:
preside();
break;
case 7:
find();
break;
case 8:
output();
break;
default:
w=1;
}
if(w==1)
{
printf("输入有误,按任意键重新选择.\n");
k=getchar();
getch();
enter();
}
}
void input() /*学生信息录入的函数*/
{
int x,i;
char w,r[20],o;
struct book*p;
system("cls");
load();
t=count+1;
p=b+t-1;
printf("\t\t\t图书信息的录入.\n");
do
{
pl++;
printf("请输入第%d本图书的信息.\n\n",t);
printf("编号:\n");
scanf("%ld",&p->num);
printf("书名:\n");
scanf("%s",&p->name);
printf("\n");
printf("作者:\n");
gets(r);
gets(&->author);
scanf("%s",&p->author);
printf("\n");
printf("出版社:\n");
scanf("%s",&p->press);
printf("\n");
printf("三大平台评分:1.豆瓣 2.当当.3.亚马逊.\n");
for(i=0;i<3;i++)
scanf("%d",&p->score[i]);
p->sum=p-score[0]+p->score[1]+p->score[2];
p->ave=p->sum/3;
printf("\n\n是否继续输入图书信息,如果是请按y;不是请按n.\n");
o=getchar();
w=getchar();
if(w=='y'||w=='Y')
{
t++;
x=1;
p++;
}
else
{
x=0;
fprint();
}
}
while(x==1);
if(w=='n'||w=='N')
printf("按任意键返回.\n");
getch();
enter();
}
void display() /*显示学生信息*/
{
int i,j;
system("cls");
load();
printf("\t\t\t图书信息显示.\n\n");
t=count+1;
for(j=0;j<t-1;j++)
{
printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n\n","编号","书名","作者","出版社","豆瓣","当当","亚马逊","平均分","总分");
printf("ld\t%s\t%s\t%s\t",b[j].num, b[j].name, b[j].author,b[j].press")
for(i=0;i<3;i++)
printf("%d\t",b[j].score[i]);
printf("%d\t",b[j].ave);
printf("%d\t",b[j].sum);
printf("\n\n");
}
printf("\n按任意键返回.\n");
getch();
enter();
}
void add()
{
int i,x;
char a,w;
struct book*p;
load();
t=count+1;
p=b+t-1;
p=b+t;
system("cls");
printf("\n\t\t\t增加图书信息.\n\n",t);
do
{
printf("请输入第%d图书的信息.\n\n",t);
printf("编号:\n");
scanf("%ld",&p->num);
printf("\n");
printf("书名:\n");
scanf("%s",p->name);
printf("\n");
printf("作者:\n");
scanf("%s",p->author);
printf("\n");
printf("出版社:\n");
scanf("%s",p->press);
printf("\n");
printf("三大平台评分:1.豆瓣 2.当当 3.亚马逊.\n");
for(i=0;i<3;i++)
scanf("%d",&p->score[i]);
for(i=0;i<3;i++)
p->sum=p->score[0]+p->score[1]+p->score[2];
p->ave=p->sum/3;
a=getchar();
printf("\n是否要继续输入图书信息,如果是请按y;不是请按n.\n\n");
w=getchar();
p++;
t++;
if(w=='y'||w=='Y')
{
x=1;
}
if(w=='n'||w=='N')
{
x=0;
fprint();
break;
}
}
while(x==1);
if(x==0)
{
printf("\n");
printf("按任意键返回.\n");
getch();
enter();
}
void del() /*学生信息的删除*/
{
long num;
int z,j;
int r=t;
char c,x;
struct("cls");
load();
t=count+1;
p=b+t-1;
printf("\n\t\t\t删图书信息\n");
printf("请输入要删除图书的编号:\n\n");
scanf("%ld",&num);
for(p=b;p<b+t;p++)
if(p->num==num)
break;
else
r--;
if(r==0)
{
printf("你要删除的图书不存在,请按任意键返回.\n");
getch();
enter();
}
printf("\n你要删除的图书为:\n");
printf(%-8s%-8s%-8s%-8s%-8s%-8s%-8s%-8s%-8s\n\n","编号","书名","作者","出版社","豆瓣 ","当当","亚马逊","平均分","总分");
printf("%-8ld%-8s%-8s%-8s",p->num,p->name,p->author,p->press);
for(j=0;j<3;j++)
printf("%-8d",p->score[j]);
printf("%-8d",p->ave);
printf("-8d",p->sum);
printf("\n你确定要删除吗?如是请输入y;不是请输入n.\n");
x=getchar();
c=getchar();
if(c=='y'||c=='Y')
{
for(p=b;p<b+t;p++)
*p=*(p+1);
t--;
fprint();
printf("系统已为你删除信息,请按任意键返回.\n");
getch();
enter();
}
if(c=='n'||c=='N')
printf("系统已为你删除信息,请按任意键返回.\n");
getch();
enter();
}
if(c=='n'||c=='N')
{
printf("删除信息取消!按任意键返回.\n");
getch();
enter();
}
}
void modify() /*学生信息的修改*/
{
long j;
int i,z,r=t;
char c,d,e,f,q,u,l,v,m,k,h1,h2,h3,h4;
struct book *p;
system("cls");
load();
t=count+1;
p=b+t-1;
}
b站三子棋
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int menu()
{
printf("*********\n");
printf("1.Start\n");
printf("0.Quit");
printf("*********\n");
printf("please select\n");
int choice=0;
scanf("%d",&choice);
return choice;
}
#define MAX_ROW 3
#define MAX_COL 3
char chess_board[MAX_ROW][MAX_COL];
void Init()
{
for(int row=0;row<MAX_ROW;row++)
{
for(int col=0;col<MAX_COL;col++)
{
chess_board[row][col]='';
}
}
}
void Print()
{
for(int row=0;row<MAX_ROW;row++)
{
printf("|%c|%c|%c|\n",chess_board[row][0],chess_board[row][0],chess_board[row][1],chess_board[row][2]);
if (row!=MAX_ROW-1)
{
31| printf("|---|---|---|\n");
}
}
}
void PlayerMove()
{
printf("玩家落子!\n");
while(1)
{
printf("请输入一组坐标(row,col):");
int row=0;
int col=0;
scanf("%d %d",&row,&col);
if(row>=MAX_ROW||row<0||col>=MAX_COL||col<0)
{
printf("Waring! Waring! Waring!");
continue;
}
if(chess_board[row][col]!='')
{
printf("您输入的位置已被占用!\n");
continue;
}
chess_board[row][col]='x';
break;
}
}
void ComputerMove()
{
while(1)
{
int row=rand()%MAX_ROW;
int col=rand()%MAX_COL;
if(chess_board[row][col]!='')
{
continue;
}
chess_board[row][col]='o';
break;
}
}
int IsFull()
{
for(int row=0;row<MAX_ROW;row++)
{
for(int col=0;col<MAX_COL;col++)
{
if(chess_board[row][col]=='')
{
return 0;
}
}
}
return 1;
}
//返回X表示玩家胜;返回O表示电脑胜;返回H表示和棋;返回' '表示未分出胜负,不是和棋,继续进行
char CheckGameover(){79| //?
}
指针的学习
#include<stdio.h>
int a[3][3]={1,2,3,4,5,6,7,8,9};
int *pa[3]={a[0],a[1],a[2]};
int *p=a[0];
main()
{
int i;
printf("使用数组a输出结果:\n");
for(i=0;i<3;i++)
printf("%d,%d,%d\n",a[i][2-i],*a[i],*(*(a+i)+i));
printf("使用指针数组pa输出结果:\n");
for(i=0;i<3;i++)
printf("%d,%d,%d\n",*pa[i],p[i],*(p+i));
}
#include<stdio.h>
void swap(int *p1,int *p2)
{
int temp;
temp=*p1; *p1=*p2; *p2=temp; //交换指针指向变量的值
}
int main()
{
int a,b,c;
int *pa, *pb, *pc;
printf("请输入a b c:\n");
scanf("%d %d %d",&a,&b,&c);
pa=&a;
pb=&b;
pc=&c;
if (*pa>*pb)
swap(pa,pb); //调用交换函数
if(*pb>*pc)
swap(pb,pc);
if (*pa>*pc)
swap(pa,pc);
printf("swaped:\n");
printf("a=%d,b=%d,c=%d\n",*pa, *pb, *pc);
}
#include<stdio.h>
int sort(int x[], int n)
{int i,j,k, t;
for(i=0; i<n-1; i++)
{k=i;
for(j=i+1; j<n; j++)
if (x[j]>x[k]) k=j;
if (k!=i) {t=x[i]; x[i]=x[k]; x[k]=t;}
}
}
int main()
{int *p, i, a[10];
p=a;
printf("请输入要排序的数字:\n");
for(i=0; i<10; i++)
scanf("%d",p++); /*将10个整数读入a数组中*/
p=a;
sort(p,10); /*调用sort函数对数组进行排序*/
printf("排序后结果:\n");
for(p=a,i=0;i<10;i++)
{printf("%3d",*p); p++;} /*输出排序后的数组*/
}
#include "stdio.h"
#define MAXSIZE 100
main()
{ char str[MAXSIZE], *p;
int n;
printf("请输入:\n");
for(n=0;n< MAXSIZE-1;n++)
{
str[n]=getchar();
if(str[n]=='$')
break;
}
str[n]='\0';
p=str;
printf("输出为");
while(*p)
putchar( *p++);
}