注:这个实验我之前由于把它想得太复杂,没有实现,相当于是失败了,后来明白过来,数据什么的都找不到了,于是重新设计和参考同学的代码后完成了代码,却没有测试。
这个实验的代码其实很简单,只需要几个结构体,读文件写文件和做一个筛选条件就行,应该主要是让我们体会一下在Linux系统下的编写C文件,shell文件和GCC的用法。
而按照我本来的设想中涉及到了数据库的建立、数据的导入,然后在MySQL中完成筛选,最后输出,一直没弄明白怎么才能实现,也算是把简单的问题复杂化了,由于数据库是刚学的新知识,所以思维被局限了。
几个结构体:
typedef struct
{
int c_custkey;
char c_mkgsegment[20];
}customer; //顾客
typedef struct
{
int o_orderkey;
int o_custkey;
char o_orderdate[10];
}orders; //订单
typedef struct
{
int l_orderkey;
double l_extendedprice;
char l_shipdate[10];
}lineitem; //商品
typedef struct
{
int l_orderkey;
char o_orderdate[10];
double l_extendedprice;
}result; //其他情况
读文件:
customer * read_data_customer() //读取customer.txt
{
FILE *fp;
char text;
customer *a = NULL;
int i=0;
a = (customer *)malloc(100 * sizeof(customer));
if ((fp = fopen("customer.txt", "r")) == NULL)//其实这个路径是错误的,读不出来,但是我没有数据,就做了这个意思,程序我也没有真正跑过,后面几个也是一样。
{
printf("1");
return NULL;
}
while (!feof(fp))
{
fscanf(fp, "%d%c%s", &a[i].c_custkey, &text, &a[i].c_mkgsegment);
printf("%d%c%s\n", a[i].c_custkey, text, a[i].c_mkgsegment);
i = i + 1;
}
fclose(fp);
return a;
}
***************************************************************************
orders *read_data_order() //读取orders.txt
{
FILE *fp;
char text1;
char text2;
int a;
int i=0;
orders *p;
p = (orders *)malloc(4000 * sizeof(orders));
if ((fp = fopen("orders.txt", "r")) == NULL)
{
return NULL;
}
while (!feof(fp))
{
fscanf(fp, "%d%c%d%c%s", &p[i].o_orderkey, &text1, &a, &text2, &p[i].o_orderdate);
printf("%d%c%d%c%s\n", p[i].o_orderkey, text1, a, text2, p[i].o_orderdate);
i = i + 1;
}
fclose(fp);
return p;
}
***************************************************************************
lineitem *read_data_lineitem() //读取lineitem.txt
{
FILE *fp;
char text1;
char text2;
int i=0;
lineitem *p;
p = (lineitem *)malloc(4000 * sizeof(lineitem));
if ((fp = fopen("lineitem.txt", "r")) == NULL)
{
return NULL;
}
while (!feof(fp))
{
fscanf(fp, "%d%c%lf%c%s", &p[i].l_orderkey, &text1, &p[i].l_extendedprice, &text, &p[i].l_shipdate);
printf("%d%c%lf%c%s\n", p[i].l_orderkey, text1, p[i].l_extendedprice, text2, p[i].l_shipdate);
i = i + 1;
}
fclose(fp);
return p;
}
筛选:
result *select(customer *cus, orders *ord, lineitem *lin,char *order_date,char *ship_date)
{
result *flag_1=NULL;
result *flag_2=NULL;
int set_1 = 0;
int set_2 = 0;
int set_3 = 0;
int a, b, c;
flag_1 = (result *)malloc(10000 * sizeof(result));
flag_2 = (result *)malloc(10000 * sizeof(result));
for (a = 0; a < 100; a++)
{
for (b = 0; b < 4000; b++)
{
for (c = 0; c < 1000; c++)
{
if ((cus[a].c_custkey == ord[b].o_custkey) && (ord[b].o_orderkey == lin[c].l_orderkey)
&& (strcmp(ord[b].o_orderdate, order_date) < 0)&&(strcmp(lin[c].l_shipdate,ship_date)>0))
{
printf("%d,%s,%lf\n", lin[c].l_orderkey, ord[b].o_orderdate, lin[c].l_extendedprice);
flag_1[set_1].l_orderkey = lin[c].l_orderkey;
strcpy(flag_1[set_1].o_orderdate, ord[b].o_orderdate);
flag_1[set_1].l_extendedprice = lin[c].l_extendedprice;
set_1 = set_1 + 1;
}
}
}
}
for (set_2 = 0; set_2 < set_1; set_2++)
{
if (set_2 == 0)
{
flag_2[set_3].l_extendedprice = flag_1[set_2].l_extendedprice;
flag_2[set_3].l_orderkey = flag_1[set_2].l_orderkey;
strcpy(flag_2[set_3].o_orderdate, flag_1[set_2].o_orderdate);
}
if (set_2 != 0 && flag_1[set_2].l_orderkey == flag_1[set_2-1].l_orderkey)
{
flag_2[set_3].l_extendedprice = flag_2[set_3].l_extendedprice + flag_1[set_2].l_extendedprice;
}
if (set_2 != 0 && flag_1[set_2].l_orderkey != flag_1[set_2 - 1].l_orderkey)
{
set_3 = set_3 + 1;
flag_2[set_3].l_extendedprice = flag_1[set_2].l_extendedprice;
flag_2[set_3].l_orderkey = flag_1[set_2].l_orderkey;
strcpy(flag_2[set_3].o_orderdate, flag_1[set_2].o_orderdate);
}
}
return flag_2;
}
整个程序如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<Windows.h>
typedef struct
{
int c_custkey;
char c_mkgsegment[20];
}customer; //顾客结体
typedef struct
{
int o_orderkey;
int o_custkey;
char o_orderdate[10];
}orders; //订单结构体
typedef struct
{
int l_orderkey;
double l_extendedprice;
char l_shipdate[10];
}lineitem; //商品信息结构体
typedef struct
{
int l_orderkey; //订单号
char o_orderdate[10]; //订货日期
double l_extendedprice; //额外价格
}result;
customer * read_data_customer() //读取customer。txt内容
{
FILE *fp;
char word;
customer *a = NULL;
int i;
i = 0;
a = (customer *)malloc(100 * sizeof(customer));
if ((fp = fopen("/home/customer.txt", "r")) == NULL)
{
printf("1");
return NULL;
}
while (!feof(fp))
{
fscanf(fp, "%d%c%s", &a[i].c_custkey, &word, &a[i].c_mkgsegment);
printf("%d%c%s\n", a[i].c_custkey, word, a[i].c_mkgsegment);
i = i + 1;
}
fclose(fp);
return a;
}
orders *read_data_order() //读取orders.txt内容
{
FILE *fp;
char word1;
char word2;
int a;
orders *p;
int i;
i = 0;
p = (orders *)malloc(4000 * sizeof(orders));
if ((fp = fopen("/home/orders.txt", "r")) == NULL)
{
return NULL;
}
while (!feof(fp))
{
fscanf(fp, "%d%c%d%c%s", &p[i].o_orderkey, &word1, &a, &word2, &p[i].o_orderdate);
printf("%d%c%d%c%s\n", p[i].o_orderkey, word1, a, word2, p[i].o_orderdate);
i = i + 1;
}
fclose(fp);
return p;
}
lineitem *read_data_lineitem() //读取lineitem.txt内容
{
FILE *fp;
char word1;
char word2;
lineitem *p;
int i;
i = 0;
p = (lineitem *)malloc(4000 * sizeof(lineitem));
if ((fp = fopen("/home/lineitem.txt", "r")) == NULL)
{
return NULL;
}
while (!feof(fp))
{
fscanf(fp, "%d%c%lf%c%s", &p[i].l_orderkey, &word1, &p[i].l_extendedprice, &word2, &p[i].l_shipdate);
printf("%d%c%lf%c%s\n", p[i].l_orderkey, word1, p[i].l_extendedprice, word2, p[i].l_shipdate);
i = i + 1;
}
fclose(fp);
return p;
}
result *select(customer *cus, orders *ord, lineitem *lin,char *order_date,char *ship_date)
{
result *flag_1=NULL;
result *flag_2=NULL;
int set_1 = 0;
int set_2 = 0;
int set_3 = 0;
int a, b, c;
flag_1 = (result *)malloc(10000 * sizeof(result));
flag_2 = (result *)malloc(10000 * sizeof(result));
for (a = 0; a < 100; a++)
{
for (b = 0; b < 4000; b++)
{
for (c = 0; c < 1000; c++)
{
if ((cus[a].c_custkey == ord[b].o_custkey) && (ord[b].o_orderkey == lin[c].l_orderkey)
&& (strcmp(ord[b].o_orderdate, order_date) < 0)&&(strcmp(lin[c].l_shipdate,ship_date)>0))
{
printf("%d,%s,%lf\n", lin[c].l_orderkey, ord[b].o_orderdate, lin[c].l_extendedprice);
flag_1[set_1].l_orderkey = lin[c].l_orderkey;
strcpy(flag_1[set_1].o_orderdate, ord[b].o_orderdate);
flag_1[set_1].l_extendedprice = lin[c].l_extendedprice;
set_1 = set_1 + 1;
}
}
}
}
for (set_2 = 0; set_2 < set_1; set_2++)
{
if (set_2 == 0)
{
flag_2[set_3].l_extendedprice = flag_1[set_2].l_extendedprice;
flag_2[set_3].l_orderkey = flag_1[set_2].l_orderkey;
strcpy(flag_2[set_3].o_orderdate, flag_1[set_2].o_orderdate);
}
if (set_2 != 0 && flag_1[set_2].l_orderkey == flag_1[set_2-1].l_orderkey)
{
flag_2[set_3].l_extendedprice = flag_2[set_3].l_extendedprice + flag_1[set_2].l_extendedprice;
}
if (set_2 != 0 && flag_1[set_2].l_orderkey != flag_1[set_2 - 1].l_orderkey)
{
set_3 = set_3 + 1;
flag_2[set_3].l_extendedprice = flag_1[set_2].l_extendedprice;
flag_2[set_3].l_orderkey = flag_1[set_2].l_orderkey;
strcpy(flag_2[set_3].o_orderdate, flag_1[set_2].o_orderdate);
}
}
return flag_2;
}
int run_shell(char *a,char*b,char*c,int d)
{
int i;
customer *cus;
orders *ord;
a = read_data_customer();
b = read_data_order();
c = read_data_lineitem();
int limit=d;
result *run=NULL;
run=select(cus,ord,lin,b,c);
printf("l_orderkey|o_orderdate|revenue\n");
for(i=0;i<limit;i++)
{
printf("%d|%s|%lf\n", flag_2[set_2].l_orderkey, flag_2[set_2].o_orderdate, flag_2[set_3].l_extendedprice);
}
return 0;
}
int main(int argc,char **argv)
{
int i;
int a;
unsigned int n=atoi(argv[4]);
for(i=1;i<limit;i++)
{
unsigned int t=atoi(argv[4*i+4];
a=run(argv[4*i+1],argv[4*i+2],argv[4*i+3],t);
}
return 0;
}
实验虽然没有做完,没有测试程序,但是对于Linux的GCC和shell文件已经有了一定的了解,在做的过程中也学到了很多和实验无关的东西,例如Linux的常用指令,终端的使用,GCC的一些特点优势和弊端也都有了了解。