c语言如何实现多行文本输入,这个程序如何实现多行输入?求高手帮忙!

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include "stdafx.h"

#include 

#include 

#include 

#include 

#include 

#define STACK_INIT_SIZE 100

#define STACKINCREMENT 10

struct SqStack

{

char *base;

char *top;

int stacksize;

};

void InitStack(SqStack &S)

{

S.base=(char*)malloc(STACK_INIT_SIZE *sizeof(char));

if (!S.base)

exit(1);

S.top=S.base;

S.stacksize=STACK_INIT_SIZE;

}

void push(SqStack &S,char e)

{

if(S.top-S.base>=S.stacksize)

{

S.base=(char*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(char));

if (!S.base)

exit(1);

S.top=S.base+S.stacksize;

S.stacksize+=STACKINCREMENT;

}

*S.top++=e;

}

char pop(SqStack &S,char &e)

{

if (S.top==S.base)

return false;

e=*--S.top;

return e;

}

void ClearStack(SqStack &S)

{

S.top=S.base;

}

void DestroyStack(SqStack &S)

{

free(S.base);

S.top=S.base;

}

bool StackEmpty(SqStack &S)

{

if (S.top==S.base)

return true;

return false;

}

/*void PrintStack(SqStack &S)

{

char e;

while (!StackEmpty(S))

{

pop(S,e);

printf("%d",e);

}

}*/

void main()

{

char ch,e;

SqStack S,D;

InitStack(S);

InitStack(D);

ch=getchar();

while (ch!=EOF)

{

while(ch!=EOF&&ch!='\n')

{

switch(ch)

{

case'#':pop(S,e);break;

case'@':ClearStack(S);break;

default:push(S,ch);break;

}

ch=getchar();

}

while (!StackEmpty(S))

{

e=pop(S,e);

push (D,e);

}

while (!StackEmpty(D))

{

e=pop(D,e);

printf("%c",e);

}

ClearStack(S);

if(ch!=EOF)

ch=getchar();

}

DestroyStack(S);

}

这个程序在运行时遇到回车就运行,如何改造成遇到*运行,而碰到回车可以继续输入�

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。以下是一个基于链表的C语言KWIC索引程序的示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE_LEN 1000 #define MAX_WORD_LEN 100 typedef struct Node { char *line; struct Node *next; } Node; void insert(Node **head, char *line) { Node *new_node = (Node *) malloc(sizeof(Node)); new_node->line = line; new_node->next = NULL; if (*head == NULL) { *head = new_node; } else { Node *curr = *head; while (curr->next != NULL) { curr = curr->next; } curr->next = new_node; } } void print_kwic(Node *head) { Node *curr = head; while (curr != NULL) { char *line = curr->line; int len = strlen(line); char *words[MAX_WORD_LEN]; int word_count = 0; // Split line into words char *word = strtok(line, " "); while (word != NULL) { words[word_count++] = word; word = strtok(NULL, " "); } // Print all rotations of the line for (int i = 0; i < word_count; i++) { for (int j = 0; j < word_count; j++) { printf("%s ", words[(i + j) % word_count]); } printf("\n"); } curr = curr->next; } } int main() { Node *head = NULL; char line[MAX_LINE_LEN]; // Read input lines while (fgets(line, MAX_LINE_LEN, stdin) != NULL) { int len = strlen(line); if (line[len - 1] == '\n') { line[len - 1] = '\0'; } insert(&head, strdup(line)); } // Print KWIC index print_kwic(head); // Free memory Node *curr = head; while (curr != NULL) { Node *next = curr->next; free(curr->line); free(curr); curr = next; } return 0; } ``` 这个程序可以从标准输入中读取多行输入,并输出它们的KWIC索引。KWIC索引是一种文本索引方法,它将每行文本旋转成所有可能的位置,并按字母顺序排序,以便更容易地查找特定单词。 请注意,这个程序并不完美,可能存在一些边界情况和错误处理问题,但它可以作为一个基本的参考实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值