c实用技巧:文件统计信息和银行账户管理系统

需求

        写一个shell程序命名为wc,统计文件字符数、行数和单词数。

        写一个shell程命名为bank,完成以下功能:退出、存款、取款、添加账户、删除账户、查询账户、显示所有账户,所有信息保存在account.dat文件中,每次操作会实时更新account.dat文件。

        编写对应的makefile,使用 make编译


shell版本

        《shell实用技巧:文件统计信息和银行账户管理系统》:

       https://blog.csdn.net/qq21497936/article/details/79952413


实现截图(c与shell的差不多)

wc

        c实用技巧:文件统计信息和银行账户管理系统_第1张图片

bank

c实用技巧:文件统计信息和银行账户管理系统_第2张图片

wc的makefile

CC=gcc
wc : wc.o
	$(CC) -o wc wc.o
wc.o : wc.c
	$(CC) -c wc.c

clean:
	rm -rf wc.o wc

wc的c代码

#include 

void wc(char *path)
{
  int isWord = 0;
  int chars = 0;
  int rows = 0;
  int words = 0;
  int ch;
  FILE *fp = NULL; 
  fp = fopen(path, "r");
  if(fp == NULL)
  {
    printf("Failed to open file:%s", path);
    return;
  }
  do
  {
    ch = fgetc(fp);
    if(ch != EOF)
      chars++;
    if(ch == '\n')
      rows++;
    if((ch >= 'a' && ch <='z') || (ch >= 'A' && ch <='Z'))
    {
      isWord = 1;
    }else{
      if(isWord == 1)
        words++;
      isWord = 0;
    }
  }while(ch != EOF);
  fclose(fp);
  printf("===== file: %s =====\n", path);
  printf("字符数:%d\n", chars);
  printf("换行数:%d\n", rows);
  printf("单词数:%d\n", words);
  return;
}

int main(int argc, char *argv[])
{
  int i = 0;
  char path[32] = {0x00};
  if( argc == 1 )
  {
     printf("input a file:");
     scanf("%s", path);
     wc(path);
  }else{
     for(i = 1; i < argc; i++)
     {
         wc(argv[i]);
     }
  }
}

bank的makefile

CC=gcc
bank : bank.o
	$(CC) -o bank bank.o
bank.o : bank.c
	$(CC) -c bank.c

clean:
	rm -rf bank.o bank

bank的c代码

#include 
#include 
#include 

#define DATA_FILE ("accounts.dat")

struct Info *pAccountHead = 0;

struct Info{
  char firstName[128];
  char middleName[128];
  char lastName[128];
  char number[128];
  int balance;
  struct Info *pNext;
};

void loadInfo()
{
  struct Info *p = 0;
  struct Info *q = 0;
  struct Info *pInfo = 0;
  FILE *fp = fopen(DATA_FILE, "rb");
  if(fp == NULL)
  {
    printf("Failed to open file : %s", DATA_FILE);
    return;
  }
  pAccountHead = 0;
  do{
    struct Info *pInfo = (struct Info *)malloc(sizeof(struct Info));
    memset(pInfo, 0x00, sizeof(struct Info));
    if(5!=fscanf(fp,"%s %s %s %s %d ", pInfo->firstName, pInfo->middleName, pInfo->lastName, pInfo->number, &pInfo->balance))
    {
      free(pInfo);
      break;
    }
    if(pAccountHead == 0)
    {
      pAccountHead = pInfo;
    }else{
      p = pAccountHead;
      while(p->pNext != 0)
      {
        p = p->pNext;
      }
      p->pNext = pInfo;
      pInfo->pNext = 0;
    }
  }while(1);
  fclose(fp);
} 

void saveInfo()
{
  struct Info *p = 0;
  int result = 0;
  FILE *fp = 0;
  fp = fopen(DATA_FILE, "wb+");
  if(fp == NULL)
  {
    printf("Failed to open file : %s", DATA_FILE);
    return;
  }
  p = pAccountHead;
  while(p != 0)
  {
    fprintf(fp,"%s %s %s %s %d ", p->firstName, p->middleName, p->lastName, p->number, p->balance);
    p = p->pNext;
  }
  fclose(fp);
}

int showMenu()
{
  int number = 0;
  printf("========== 银行账户菜单 ==========");
  printf("\n");
  printf("0.退出");
  printf("\n");
  printf("1.存款");
  printf("\n");
  printf("2.取款");
  printf("\n");
  printf("3.添加账户");
  printf("\n");
  printf("4.删除账户");
  printf("\n");
  printf("5.查询账户余额");
  printf("\n");
  printf("6.打印所有账户信息");
  printf("\n");
  printf("=================================");
  printf("\n");
  printf("请输入您的选择(0-6):");
  scanf("%d", &number);
  return number;
}

void menu2()
{
  int i = 0;
  char number[128] = {0x00};
  int balance;
  struct Info *pInfo = 0;
  struct Info *p = 0;
  struct Info *q = 0;
  printf("请输入account(6位数字):");
  scanf("%s", number);
  if(strlen(number) != 6)
  {
    printf("账户必须是6位数字!!!");
    printf("\n");
    return;
  }
  for(i = 0; i < 6; i++)
  {
    if(number[i] >= '0' && number[i] <= '9')
    {
      ;
    }else{
      printf("账户必须是6位数字!!!");
      printf("\n");
      return;
    }
  }
  printf("请输入取款金额(元):");
  scanf("%d", &balance);
  q = 0;
  p = pAccountHead;
  while(p != 0)
  {
    if(strcmp(p->number, number) == 0)
    {
      if(p->balance-balance < 0)
      {
        printf("取款失败,账户余额不足!!!");
        printf("\n");
        return;
      }
      p->balance-=balance;
      printf("取款成功!!!");
      printf("\n");
      return;
    }
    q = p;
    p = p->pNext;
  }
  printf("未找到账户");
  printf("\n");
}

void menu1()
{
  int i = 0;
  char number[128] = {0x00};
  int balance;
  struct Info *pInfo = 0;
  struct Info *p = 0;
  struct Info *q = 0;
  printf("请输入account(6位数字):");
  scanf("%s", number);
  if(strlen(number) != 6)
  {
    printf("账户必须是6位数字!!!");
    printf("\n");
    return;
  }
  for(i = 0; i < 6; i++)
  {
    if(number[i] >= '0' && number[i] <= '9')
    {
      ;
    }else{
      printf("账户必须是6位数字!!!");
      printf("\n");
      return;
    }
  }
  printf("请输入存款金额(元):");
  scanf("%d", &balance);
  q = 0;
  p = pAccountHead;
  while(p != 0)
  {
    if(strcmp(p->number, number) == 0)
    {
      p->balance+=balance;
      printf("存款成功!!!");
      printf("\n");
      return;
    }
    q = p;
    p = p->pNext;
  }
  printf("未找到账户");
  printf("\n");
}

void menu3()
{ 
  int i = 0;
  char fisrtName[128] = {0x00};
  char middleName[128] = {0x00};
  char lastName[128] = {0x00};
  char number[128] = {0x00};
  struct Info *pInfo = 0;
  struct Info *p = 0;
  printf("请输入firstName:");
  scanf("%s", fisrtName);
  printf("请输入middleName:");
  scanf("%s", middleName);
  printf("请输入lastName:");
  scanf("%s", lastName);
  printf("请输入account(6位数字):");
  scanf("%s", number);
  if(strlen(number) != 6)
  {
    printf("账户必须是6位数字!!!");
    printf("\n");
    return;
  }
  for(i = 0; i < 6; i++)
  {
    if(number[i] >= '0' && number[i] <= '9')
    {
      ;
    }else{
      printf("账户必须是6位数字!!!");
      printf("\n");
      return;
    }
  }
  pInfo = (struct Info*)malloc(sizeof(struct Info));
  memset(pInfo, 0x00, sizeof(struct Info));
  memcpy(pInfo->firstName, fisrtName, strlen(fisrtName)+1);
  memcpy(pInfo->middleName, middleName, strlen(middleName)+1);
  memcpy(pInfo->lastName, lastName, strlen(lastName)+1);
  memcpy(pInfo->number, number, strlen(number)+1);
  pInfo->balance = 0;
  pInfo->pNext = 0;
  if(pAccountHead == 0)
  {
    pAccountHead = pInfo;
    return;
  }
  p = pAccountHead;
  do
  {
    if(p->pNext == 0)
    {
      p->pNext = pInfo;
      return;
    }
    p = p->pNext;
  }while(1);
}

void menu4()
{
  int i = 0;
  char number[128] = {0x00};
  struct Info *q = 0;
  struct Info *p = 0;
  printf("请输入账户(6位数字):");
  scanf("%s", number);
  if(strlen(number) != 6)
  {
    printf("账户必须是6位数字!!!");
    printf("\n");
    return;
  }
  for(i = 0; i < 6; i++)
  {
    if(number[i] >= '0' && number[i] <= '9')
    {
      ;
    }else{
      printf("账户必须是6位数字!!!");
      printf("\n");
      return;
    }
  }
  q = 0;
  p = pAccountHead;
  while(p != 0)
  {
    if(strcmp(p->number, number) == 0)
    {
      if(q == 0)
      {
        pAccountHead = p->pNext;;
        free(p);
      }else{
        q->pNext = p->pNext;
        free(p);
      }
      printf("删除账户%s成功!!!", number);
      printf("\n");
    }
    q = p;
    p = p->pNext;
  }
}

void menu5()
{
  int i = 0;
  char number[128] = {0x00};
  struct Info *q = 0;
  struct Info *p = 0;
  printf("请输入账户(6位数字):");
  scanf("%s", number);
  if(strlen(number) != 6)
  {
    printf("账户必须是6位数字!!!");
    printf("\n");
    return;
  }
  for(i = 0; i < 6; i++)
  {
    if(number[i] >= '0' && number[i] <= '9')
    {
      ;
    }else{
      printf("账户必须是6位数字!!!");
      printf("\n");
      return;
    }
  }
  q = 0;
  p = pAccountHead;
  while(p != 0)
  {
    if(strcmp(p->number, number) == 0)
    {
      printf("firstName:%s middleName:%s lastName:%s account:%s balance:%d ", p->firstName, p->middleName, p->lastName, p->number, p->balance);
      printf("\n");
      return;
    }
    q = p;
    p = p->pNext;
  }
  printf("查询账户%s失败!!!", number);
  printf("\n");
}

void menu6()
{
  struct Info *p = pAccountHead;
  if(p == 0)
  {
    printf("没有账户信息");
    printf("\n");
    return;
  }
  do{
      printf("firstName:%s middleName:%s lastName:%s account:%s balance:%d ", p->firstName, p->middleName, p->lastName, p->number, p->balance);
    printf("\n");
    p = p->pNext;
  }while(p != NULL);
}

void main(int argc, char *argv[])
{
  loadInfo();
  while(1)
  {
    switch(showMenu())
    {
      case 0:
        return;
      case 1:
        menu1();
        break;
      case 2:
        menu2();
        break;
      case 3:
        menu3();
        break;
      case 4:
        menu4();
        break;
      case 5:
        menu5();
        break;
      case 6:
        menu6();
        break;
      default:
        ;
    }
    saveInfo();
  }
}


你可能感兴趣的:(C,wc,bank,c编程)