北航c语言上机课作业答案,北航C语言上机8

400+第八波

//1

/*

#include

int main(){

int c,isSpace=0;

FILE *in,*out;

in=fopen("data.txt","r");

out=fopen("output.txt","w");

while((c=fgetc(in))!=EOF){

if(c==' '||c=='\t'){

if(isSpace==0){

isSpace=1;

fputc(' ',out);

}

}

else {

isSpace=0;

fputc(c,out);

}

}

fclose(in);

fclose(out);

return 0;

}

*/

//2

/*

#include

#include

#include

#include

using namespace std;

int main(){

stack st;

char s[1000];

int i,flag=1;

scanf("%s",s);

for(i=0;s[i]!='\0';i++){

if(s[i]=='{'||s[i]=='['||s[i]=='('){

st.push(s[i]);

}

else if(s[i]==')'||s[i]==']'||s[i]=='}'){

if(st.top()!=s[i]||st.empty()){

flag=0;

}

else st.pop();

}

}

if(!st.empty())flag=0;

if(flag)printf("YES\n");

else printf("NO\n");

return 0;

}

*/

//3

/*

#include

#include

#include

#include

struct node{

char *word;

int count;

node *left,*right;

}*root;

int getWord(char *word,FILE *in){

int c,i;

char *w=word;

while(isspace(c=fgetc(in)));

if(c!=EOF)*w++=c;

if(!isalpha(c)){*w='\0';return c;}

for(i=100;i>0;i--,w++){

if(!isalnum(*w=fgetc(in))){

ungetc(*w,in);

break;

}

}

*w='\0';

return word[0];

}

node *addTree(node *p,char *w){

int cond;

if(p==NULL){

p=(node *)malloc(sizeof(node));

p->word=strdup(w);

p->count=1;

p->left=p->right=NULL;

}

else if((cond=strcmp(p->word,w))==0){

p->count++;

}

else if(cond>0)p->left=addTree(p->left,w);

else p->right=addTree(p->right,w);

return p;

}

void treePrint(node *p,FILE *out){

if(p!=NULL){

treePrint(p->left,out);

fprintf(out,"%s:%d",p->word,p->count);

treePrint(p->right,out);

}

}

int main(){

FILE *in,*out;

in=fopen("data.txt","r");

out=fopen("output.txt","w");

char word[100];

root=NULL;

while(getWord(word,in)!=EOF){//while(~scanf("%s"))

if(isalpha(word[0]))

root=addTree(root,word);

}

treePrint(root,out);

return 0;

}

*/

//4

/*

#include

#include

#include

#include

int main(){

char c,key[1000];

FILE *in,*out;

in=fopen("data.txt","r");

out=fopen("output.txt","w");

scanf("%s",key);

int i=0;

while((c=fgetc(in))!=EOF){

fputc(c^key[i],out);

if(i

else i=0;

}

fclose(out);

fclose(in);

return 0;

}

*/

//5

/*

#include

#include

#include

#include

#define MAXLINE 512

void print(int n,FILE *in){

char *linePtr[MAXLINE];

char tmpLine[MAXLINE];

int nline,i=0;

while(fgets(tmpLine,MAXLINE,in)!=NULL){

linePtr[i]=(char *)malloc(strlen(tmpLine)+1);

strcpy(linePtr[i],tmpLine);

i++;

}

nline=i;

for(i=i-n;i

printf("%s",linePtr[i]);

}

}

int main(){

int n=10;

FILE *in;

char str1[10],str2[20],str3[20],tmp1[10],tmp2[20];

in=fopen("data.txt","r");

scanf("%s",str1);

//strcpy(str1,"tail");

//getchar();

int i=0,k=0,p=0;

scanf("%s",str2);

if(str2[0]=='-'){

scanf("%s",str3);

if(strcmp(str1,"tail")!=0){

printf("error\n");

}

else {

//printf("%c\n",str2[0]);

//printf("yes\n");

i=1;

while(str2[i]!='\0'){

tmp1[k++]=str2[i];

i++;

}

//while(str2[i]==' ')i++;

while(str3[i]!='\0')tmp2[p++]=str3[i++];

tmp1[k]='\0',tmp2[p]='\0';

n=atoi(tmp1);

//printf("%s\n",tmp1);

if(n>0)print(n,in);

}

}

else {

if(strcmp(str1,"tail")!=0){

printf("error\n");

}

else {

i=0;

//while(str2[i]==' ')i++;

while(str2[i]!='\0')tmp2[p++]=str2[i];

tmp2[p]='\0';

print(n,in);

}

}

return 0;

}

*/

//6

#include

#include

#include

#include

#define MAXLINE 512

int main(){

int n,i,j,k,pos;

char *manys[1001],s[1001],*temp;

FILE *in,*out;

in=fopen("data.txt","r");

out=fopen("output.txt","w");

fscanf(in,"%d\n%s",&n,s);

for(i=0;i

manys[i]=(char *)malloc(n+1);

for(j=i,k=0;j

manys[i][k]=s[j];

}

for(j=0;j

manys[i][k]=s[j];

}

manys[i][k]='\0';

}

if(i>1)strcpy(s,manys[1]);

for(i=0;i

for(j=n-1;j>i;j--){

if(*manys[j]

temp=manys[j];

manys[j]=manys[j-1];

manys[j-1]=temp;

}

}

}

for(i=0;i

if(strcmp(manys[i],s)==0){

pos=i+1;break;

}

}

for(i=0;i

fprintf(out,"%c",manys[i][n-1]);

}

fprintf(out,"\n%d\n",pos);

fclose(in);

fclose(out);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值