高分用C语言做个游程编码程序,下面我解释具体意思,高手来
答案:1 悬赏:60 手机版
解决时间 2021-03-29 19:49
- 提问者网友:捧腹剧
- 2021-03-29 13:08
高分用C语言做个游程编码程序,下面我解释具体意思,高手来
最佳答案
- 五星知识达人网友:封刀令
- 2021-03-29 13:58
#include
#include
#include
#include
#define MAXLINE 1024
#define DIM ","
typedef struct STRLINK
{
int v;
int t;
struct STRLINK *next;
} LINK;
static LINK *head = NULL;
static int freelink()
{
LINK *tmp = NULL;
if (head==NULL) return 0;
while (head->next) {
tmp = head->next;
free(head);
head = tmp;
}
free(head);
return 0;
}
static int inode(int v)
{
LINK *tmp, *newlink;
tmp = head;
newlink = (LINK*)malloc(sizeof(LINK));
if (newlink==NULL) return -1;
newlink->v = v;
newlink->t = 1;
newlink->next = NULL;
if (tmp==NULL) {
head = newlink;
return 0;
}
while (tmp->next!=NULL) {
if (tmp->v==v) {
tmp->t++;
return 0;
}
tmp = tmp->next;
}
if (tmp->v==v) {
tmp->t++;
return 0;
} else {
tmp->next = newlink;
return 0;
}
}
static int prt2file(FILE *fout, int linenum)
{
LINK *tmp = head;
if (head==NULL) return 0;
while (tmp->next) {
fprintf(fout, "(%d,%d,%d),", tmp->v, tmp->t, linenum);
tmp = tmp->next;
}
fprintf(fout, "(%d,%d,%d)
", tmp->v, tmp->t, linenum);
return 0;
}
int main(int argc, char *argv[])
{
FILE *fin, *fout;
char line[MAXLINE+1], *str;
int linenum, v;
if (argc!=3) {
printf("USAGE: %s 输入文件 输出文件
", argv[0]);
}
fin = fopen(argv[1], "r");
if (fin==NULL) {
printf("input file[%s] can not open!
", argv[1]);
return -1;
}
fout = fopen(argv[2], "a+");
if (fout==NULL) {
printf("output file[%s] can not open!
", argv[2]);
fclose(fin);
return -1;
}
memset(line, 0, sizeof(line));
linenum = 0;
while (fgets(line, MAXLINE, fin)!=NULL) {
++linenum;
freelink();
str = strtok(line, DIM);
if (str==NULL) break;
v = atoi(str);
inode(v);
while((str=strtok(NULL, DIM))!=NULL)
{
v = atoi(str);
inode(v);
}
prt2file(fout, linenum);
}
fclose(fin);
fclose(fout);
return 0;
}
#include
#include
#include
#define MAXLINE 1024
#define DIM ","
typedef struct STRLINK
{
int v;
int t;
struct STRLINK *next;
} LINK;
static LINK *head = NULL;
static int freelink()
{
LINK *tmp = NULL;
if (head==NULL) return 0;
while (head->next) {
tmp = head->next;
free(head);
head = tmp;
}
free(head);
return 0;
}
static int inode(int v)
{
LINK *tmp, *newlink;
tmp = head;
newlink = (LINK*)malloc(sizeof(LINK));
if (newlink==NULL) return -1;
newlink->v = v;
newlink->t = 1;
newlink->next = NULL;
if (tmp==NULL) {
head = newlink;
return 0;
}
while (tmp->next!=NULL) {
if (tmp->v==v) {
tmp->t++;
return 0;
}
tmp = tmp->next;
}
if (tmp->v==v) {
tmp->t++;
return 0;
} else {
tmp->next = newlink;
return 0;
}
}
static int prt2file(FILE *fout, int linenum)
{
LINK *tmp = head;
if (head==NULL) return 0;
while (tmp->next) {
fprintf(fout, "(%d,%d,%d),", tmp->v, tmp->t, linenum);
tmp = tmp->next;
}
fprintf(fout, "(%d,%d,%d)
", tmp->v, tmp->t, linenum);
return 0;
}
int main(int argc, char *argv[])
{
FILE *fin, *fout;
char line[MAXLINE+1], *str;
int linenum, v;
if (argc!=3) {
printf("USAGE: %s 输入文件 输出文件
", argv[0]);
}
fin = fopen(argv[1], "r");
if (fin==NULL) {
printf("input file[%s] can not open!
", argv[1]);
return -1;
}
fout = fopen(argv[2], "a+");
if (fout==NULL) {
printf("output file[%s] can not open!
", argv[2]);
fclose(fin);
return -1;
}
memset(line, 0, sizeof(line));
linenum = 0;
while (fgets(line, MAXLINE, fin)!=NULL) {
++linenum;
freelink();
str = strtok(line, DIM);
if (str==NULL) break;
v = atoi(str);
inode(v);
while((str=strtok(NULL, DIM))!=NULL)
{
v = atoi(str);
inode(v);
}
prt2file(fout, linenum);
}
fclose(fin);
fclose(fout);
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯