美文网首页
写一个实现功能的程序---小型数据库

写一个实现功能的程序---小型数据库

作者: mfdalf | 来源:发表于2021-01-01 15:06 被阅读0次

写一个程序实现以下命令行功能
foo [-h] [-pfs] [-i filename] [-o filename]
USAGE EXAMPLES:
$ ./foo -h
Usage: foo [-h] [-pfs] [-i filename] [-o filename]
-h Show the help message.
-p Only report on tests which passed.
-f Only report on tests which failed.
-s Only report summary.
-i [filename] Use the specified file as the input data.
-o [filename] Output the summary data to the specified file.
除了有-i参数将从文件获得数据,其他正常情况都需要从命令行输入参数,格式大概是:
Enter a test suite name: suite1
Enter a test name: test1
Enter a test summary for "suite1/test1": Test information for suite1/test1.
Enter a test result for "suite1/test1": PASS
Add another test: y
如此循环下去,直到你敲了n退出。
===========================

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "unistd.h"
#include "stdlib.h"
 
#define LEN sizeof(struct testcase)
 
struct testcase {
char sname[20];
char tname[20];
int  pass;
int  fail;
char summary[100];
struct testcase *next;
};
void help ();
void free1 ();
void failitem(struct testcase *);
void pasitem(struct testcase *);
void summsary(struct testcase *);
int wfile(struct testcase *);
void modifyitem(struct testcase *);
struct testcase *delitem(struct testcase *);
void finditem(struct testcase *);
 
void failitem(struct testcase *head ) {
struct testcase *p1;
 
p1=head;
while (p1!=NULL) {
    if (p1->fail==1) {
        printf ("fail case name:%s\n",p1->sname);
    }
    p1=p1->next;
    }
}
void passitem(struct testcase *head ) {
    struct testcase *p1;
 
    p1=head;
    while (p1!=NULL) {
        if (p1->pass==1) {
          printf ("pass case name:%s\n",p1->sname);
    }
    p1=p1->next;
    }
}
 
void modifyitem(struct testcase *head) {
    struct testcase *p1;
    int flag=0;
    char sname[10];
    char summary[100];
 
    printf("please enter modify item name:\n");
    memset (sname,0x00,10);
    scanf("%s",sname);
 
    printf("please enter modify item summary:\n");
    memset (summary,0x00,100);
    scanf("%s",summary);
 
    p1=head;
    while (p1!=NULL) {
        if (!strcmp(p1->sname,sname)) {
            memset (p1->summary,0x00,100);
            strcpy(p1->summary,summary);
            flag=1;
            break;
        }
    p1=p1->next;
    }
 
    if (flag==0) 
        printf ("there is no the item\n");
    }
 
    struct testcase* delitem(struct testcase *head) {
    struct testcase *p1,*p2;
    int flag=0;
    char sname[10];
 
    printf("please enter delete item name:\n");
    memset (sname,0x00,10);
    scanf("%s",sname);
 
    p1=head;
    // del head node
    if (!strcmp(p1->sname,sname)) {
        p1=p1->next;
        free(head);
        head=p1;
 
        return head;
    } 
    p2=p1;
    p1=p1->next; 
    //del from the second node
    while (p1!=NULL) {
        if (!strcmp(p1->sname,sname)) {
            p2->next=p1->next;
            flag=1;
            break;
        }
        p2=p1; 
        p1=p1->next;
    }
 
    if (flag==0) 
        printf ("there is no the item\n");
 
    return head;
}
 
void finditem(struct testcase *head) {
    struct testcase *p1;
    char sname[10];
 
    printf("please enter find item name:\n");
    memset (sname,0x00,10);
    scanf("%s",sname);
 
    p1=head;
    while (p1!=NULL) {
        if (!strcmp(p1->sname,sname)) {
            printf("sanme is %s---summary is %s \n",p1->sname,p1->summary);
        }
        p1=p1->next;
        }
    }
 
void summary1(struct testcase *head ) {
    struct testcase *p1;
 
    p1=head;
    while (p1!=NULL) {
        printf ("case name:%s--summary is %s\n",p1->sname,p1->summary);
        p1=p1->next;
    }
}
 
int wfile(struct testcase *head ) {
    struct testcase *p1;
    FILE *out;
    char outfile[10]={0};
 
    strcpy(outfile,"out.txt");
    printf ("outfile=%s\n",outfile);
 
    if ((out=fopen(outfile,"w"))==NULL)  {
        printf("cannot open outfile \n");
    return 0;
}
 
p1=head;
while (p1!=NULL) {
    fprintf (out,"%s\n",p1->sname);
    fprintf (out,"%d\n",p1->pass);
    fprintf (out,"%d\n",p1->fail);
    fprintf (out,"%s\n",p1->summary);
    p1=p1->next;
} 
    fclose (out);
    return 0;
}
  
struct testcase* rfile() {
struct testcase *p1,*head,*p2;
FILE *in;
char infile[10]={0};
char line[100]={0};
int flag=0;
int i=0;
 
strcpy(infile,"out.txt");
 
if ((in=fopen(infile,"r"))==NULL)  {
    printf("cannot open outfile \n");
    return 0;
}
 
while((fgets(line, sizeof(line), in)) != NULL) {
    if (flag==0&&(i%4)==0) {
        p2=p1=head=(struct testcase *)malloc(LEN);
    } else if (flag==1&&(i%4)==0) {
        p1=(struct testcase *)malloc(LEN);
        p2->next=p1;
        p2=p1;
    }
    p1->next=NULL;
 
    if ((i%4)==0) {
        memset (p1->sname,0x00,20);
        if (line[strlen(line)-1]=='\n')
            line[strlen(line)-1]='\0';
        strncpy (p1->sname,line,strlen(line));
        printf("sname:%s\n",p1->sname);
    } 
    if ((i%4)==1) {
        p1->pass=atoi(line);
        printf ("pass:%d\n",p1->pass);
    }
    if ((i%4)==2) {
        p1->fail=atoi(line);
        printf ("fail:%d\n",p1->fail);
    }
    if ((i%4)==3) {
        memset(p1->summary, 0, sizeof(p1->summary));
        if (line[strlen(line)-1]=='\n')
            line[strlen(line)-1]='\0';
        strncpy (p1->summary,line,strlen(line));
        printf("summary:%s\n",p1->summary);
        flag=1;
    }
 
    i++;
 }
 
fclose (in);
return (head);
}
 
struct testcase *additem(struct testcase *head) {
    char *sname,*tname,*summary,*result,yn;
    struct testcase *p1,*p2;
    int first;
    char temp;
 
    if (head==NULL) {
        p1=p2=head=(struct testcase *)malloc(LEN);
        first=0;
    } else {
        p1=head;
        while (p1->next!=NULL) {
            p1=p1->next;
        }
        p2=p1;
        p1=(struct testcase *)malloc(LEN);
        first=1;
    }
 
    while (1) {
        sname=(char *)malloc(20*sizeof(char));
        tname=(char *)malloc(20*sizeof(char));
        summary=(char *)malloc(100*sizeof(char));
        result=(char *)malloc(100*sizeof(char));
 
        memset (sname,0x00,20);
        memset (tname,0x00,20);
        memset (summary,0x00,100);
        memset (result,0x00,100);
 
        printf ("Enter a test suite name:\n");
        scanf ("%s",sname);
        printf ("Enter a test name:\n");
        scanf ("%s",tname);
        printf ("Enter a test summary for \"%s/%s\"\n",sname,tname);
        scanf ("%s",summary);
        printf ("Enter a test result for \"%s/%s\"\n",sname,tname);
        scanf ("%s",result);
 
        memset (p1->sname,0x00,20);
        memset (p1->tname,0x00,20);
        memset (p1->summary,0x00,100);

        strcpy (p1->sname,sname);
        strcpy (p1->tname,tname);
        strcpy (p1->summary,summary);
 
        free (sname);
        free (tname);
        free (summary);
        free (result);
 
        p1->pass=0;
        p1->fail=0;
        p1->next=NULL;
 
        if (!strcasecmp(result,"PASS")) {
            p1->pass=1;
        } else if (!strcasecmp(result,"fail")){
            p1->fail=1;
        } else {
            p1->fail=1;
        } 
        if (first != 0) {
            p2->next=p1;
            p2=p1;
        }
        printf ("Add another test \n");
        while((temp=getchar()) != '\n') {
            printf ("temp=%c\n",temp);
            /*scanf ("%c",&yn);*/
            yn = getchar();
 
            if (yn == 'n') break;
            first=1;
            p1=(struct testcase *)malloc(LEN);
        }
        return (head);
}
int main (int argc,char **argv) {
    struct testcase *head,*p;
    char ch;
    int ret;
 
    head=NULL;
//while ((ch = getopt(argc,argv,"afh:i:o:ps"))!=-1) 
    while (1)
    {
        help();
        ch=getchar();
        //if ch is integer ,it change to char
        switch(ch)
        {
            case 'a':
                head=additem(head);
                //flush key buffer
                fflush(stdin);
                break;
            case 'f':
                failitem(head);
                fflush(stdin);
                break;
            case 'h':
                help();
                fflush(stdin);
                break;
            case 'i':
                free1(head);
                head=rfile(); 
                fflush(stdin);
                break;
            case 'o':
                ret=wfile(head);
                fflush(stdin);
                break;
            case 'p':
                passitem(head);
                fflush(stdin);
                break;
            case 's':
                summary1(head);
                fflush(stdin);
                break;
            case 'c':
                finditem(head);
                fflush(stdin);
                break;
            case 'm':
                modifyitem(head);
                fflush(stdin);
                break;
            case 'd':
                head=delitem(head);
                fflush(stdin);
                break;
            case 'q':
                free1(head);
                fflush(stdin);
                exit (1);
            default:
                printf ("parameters are wrong\n");
                break;
        }
    }
}
void free1 (struct testcase *head) {
    struct testcase *p;
    if (head!=NULL) {
        p=head;
        while (p!=NULL) {
            p=head->next;
            free (head);
            head=p;
        }
    }
}
 
void help () {
      printf ("Usage: foo [-h] [-pfs] [-i filename] [-o filename]\n");
      printf ("-a            add item.\n");
      printf ("-h            Show the help message.\n");
      printf ("-p            Only report on tests which passed.\n");
      printf ("-f            Only report on tests which failed.\n");
      printf ("-s            Only report summary.\n");
      printf ("-i [filename] Use the specified file as the input data.\n");
      printf ("-o [filename] Output the summary data to the specified file.\n");
      printf ("-c            find item\n");
      printf ("-m            modify item\n");
      printf ("-d            delete item\n");
      printf ("-q            quit.\n");
}

相关文章

  • 写一个实现功能的程序---小型数据库

    写一个程序实现以下命令行功能foo [-h] [-pfs] [-i filename] [-o filename]...

  • java基础面试/笔试题整理(三)

    数据的隔离级别由数据库系统实现,是数据库系统本身的一个功能。我们写java程序的时候只是设定事务的隔离级别,而不是...

  • SQLite 3.32.0 发布了

    数据库SQLite发布新版本了,SQLite是使用纯C语言实现的 ,它实现了一个小型、快速、独立、高可靠性、全功能...

  • SQLite 之 INSERT OR REPLACE使用

    SQLite是一个C语言库,它实现了一个 小型, 快速, 自包含, 高可靠性, 功能齐全的 SQL 数据库引擎。S...

  • day5-作业

    一.基础 读程序,总结程序的功能: 读程序,总结程序的功能: 编程实现(for和while各写一遍):求1到100...

  • JDBC01

    1.加载驱动程序 驱动程序:即是JDBC这组接口的实现类和其他功能类 由数据库厂商提供 ...

  • 第二个程序,链接一个函数

    想要点击一个按键,运行一个程序,是很自然的想法,如何去实现呢? 写一个小程序: 这样子就实现了一个功能:点击按钮,...

  • linux进程与作业管理

    操作系统的组成 程序员写程序可以实现库调用和系统调用来实现。调用库或者系统,其实就是调用一个已经写好的功能 应用程...

  • 如何创建合理的数据库表?

    在创建数据库表之前,首先要考虑清楚以下几点: 1、程序要实现哪些功能? 规划好整体功能模块,例如我要做一个任务系统...

  • 记一次Bug排查

    一 背景 我用c写的一个小程序发现内存泄露,内存泄露的原因比较明显,程序主要的功能实现对另外一个程序生成的tcp或...

网友评论

      本文标题:写一个实现功能的程序---小型数据库

      本文链接:https://www.haomeiwen.com/subject/qvjjoktx.html