ungetc
ungetch
~
ungetch
~
[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<assert.h> #define MAX_BUFF 100 typedef struct Word_Stock //字库 { char num[5]; //字库编号 char content[100]; //字库内容 }Word_Stock,*P_Word_Stock; Word_Stock word_stock[]= { "0","", "0001","请输入第一个数的实部X1:", "0002","请输入第一个数的虚部Y1:", "0003","请输入第二个数的实部X2:", "0004","请输入第二个数的虚部Y2:", "1001","第一个数据的实部是:%-8.2f\n", "1002","第一个数据的虚部是:%-8.2f\n", "1003","第二个数据的实部是:%-8.2f\n", "1004","第二个数据的虚部是:%-8.2f\n", }; typedef struct Complex_Number { float Real_Component; //复数实部 float Imaginary_Part; //复数虚部 }Complex_Number,*P_Complex_Number; int Comp_Bserach(const void* p1,const void* p2); void Creat_Node(void** p,const int k); //创造一个节点 void Free_Node(void** p); //删除一个节点 void Input(const void* express[],P_Complex_Number comp); //输入一个节点 void Input_Data(const char* format,void* data); //输入处理函数 char* Prompt_Message(const void* str); //提示信息函数 void Complex_Add(P_Complex_Number comp1,P_Complex_Number comp2); //复数加法 void Print(const void* express[],P_Complex_Number p); //输出函数 int main() { P_Complex_Number t1=NULL; P_Complex_Number t2=NULL; const void* express1[]={"0001","0002"}; const void* express2[]={"0003","0004"}; const void* express3[]={"1001","1002"}; const void* express4[]={"1003","1004"}; Creat_Node((void**)&t1,sizeof(Complex_Number)); Creat_Node((void**)&t2,sizeof(Complex_Number)); Input(express1,t1); puts(""); Input(express2,t2); puts(""); Print(express3,t1); Print(express4,t2); Complex_Add(t1,t2); Free_Node((void** )&t1); Free_Node((void** )&t2); return 0; } int Comp_Bsearch(const void* p1,const void* p2) //二分法比较函数 { return strcmp(((Word_Stock* )p1)->num,((Word_Stock* )p2)->num); } char* Prompt_Message(const void* str) //提示信息函数 { P_Word_Stock p=NULL; int len=sizeof(word_stock)/sizeof(*word_stock); if ((p=(P_Word_Stock)(P_Complex_Number)bsearch(str,word_stock,len,sizeof(*word_stock),Comp_Bsearch))!=NULL) return p->content; return ""; } void Creat_Node(void** p,const int k) //创造并初始化一个节点 { *p=malloc(k); assert(*p); memset(*p,0,k); } void Free_Node(void** p) { if (*p==NULL) return ; free(*p); *p=NULL; } void Input(const void* express[],P_Complex_Number comp) //输入一个节点 { printf(Prompt_Message(express[0])); Input_Data("%f",(void* )&comp->Real_Component); printf(Prompt_Message(express[1])); Input_Data("%f",(void* )&comp->Imaginary_Part); } void Input_Data(const char* format,void* data) //输入处理函数 { char str[MAX_BUFF]={0}; fscanf(stdin,"%s",str); while (sscanf(str,format,data)!=1) { while (getchar()!='\n'); puts("输入数据不合法,请重新输入!"); fscanf(stdin,"%s",str); } while (getchar()!='\n'); } void Print(const void* express[],P_Complex_Number p) { printf(Prompt_Message(express[0]),p->Real_Component); printf(Prompt_Message(express[1]),p->Imaginary_Part); puts(""); } void Complex_Add(P_Complex_Number comp1,P_Complex_Number comp2) //复数加法 { float Real_Component=comp1->Real_Component+comp2->Real_Component; float Imaginary_Part=comp1->Imaginary_Part+comp2->Imaginary_Part; printf("两个复数相加结果:\n%.2f",Real_Component); if (Imaginary_Part>=0) printf("+"); printf("%.2fi\n",Imaginary_Part); }
[此贴子已经被作者于2017-9-13 22:10编辑过]
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<assert.h> #include<math.h> #define MAX_BUFF 100 typedef struct Word_Stock //字库 { char num[5]; //字库编号 char content[100]; //字库内容 }Word_Stock,*P_Word_Stock; Word_Stock word_stock[]= { "0","", "0001","请输入第一个数的实部X1:", "0002","请输入第一个数的虚部Y1:", "0003","请输入第二个数的实部X2:", "0004","请输入第二个数的虚部Y2:", "1001","第一个数据的实部是:%-8.2f\n", "1002","第一个数据的虚部是:%-8.2f\n", "1003","第二个数据的实部是:%-8.2f\n", "1004","第二个数据的虚部是:%-8.2f\n", "2001","两个复数相加结果:\n", "2002","两个复数相减结果:\n", }; typedef struct Complex_Number { float Real_Component; //复数实部 float Imaginary_Part; //复数虚部 }Complex_Number,*P_Complex_Number; int Comp_Bserach(const void* p1,const void* p2); void Creat_Node(void** p,const int k); //创造一个节点 void Free_Node(void** p); //删除一个节点 void Input(const void* express[],P_Complex_Number comp); //输入一个节点 void Input_Data(const char* format,void* data); //输入处理函数 char* Prompt_Message(const void* str); //提示信息函数 void Complex_Add(P_Complex_Number comp1,P_Complex_Number comp2); //复数加法 void Complex_Sub(P_Complex_Number comp1,P_Complex_Number comp2); //复数减法 void Complex_Add_or_Sub(P_Complex_Number comp1,P_Complex_Number comp2, const int k,const void* express); //复数加减法运算 void Print(const void* express[],P_Complex_Number p); //输出函数 void Memu(); //菜单函数 int main() { P_Complex_Number t1=NULL; P_Complex_Number t2=NULL; const void* express1[]={"0001","0002"}; const void* express2[]={"0003","0004"}; const void* express3[]={"1001","1002"}; const void* express4[]={"1003","1004"}; Creat_Node((void**)&t1,sizeof(Complex_Number)); Creat_Node((void**)&t2,sizeof(Complex_Number)); Input(express1,t1); puts(""); Input(express2,t2); puts(""); Print(express3,t1); Print(express4,t2); Complex_Add_or_Sub(t1,t2,1,"2001"); //复数加减法运算 Complex_Add_or_Sub(t1,t2,-1,"2002"); //复数加减法运算 Free_Node((void** )&t1); Free_Node((void** )&t2); return 0; } int Comp_Bsearch(const void* p1,const void* p2) //二分法比较函数 { return strcmp(((Word_Stock* )p1)->num,((Word_Stock* )p2)->num); } char* Prompt_Message(const void* str) //提示信息函数 { P_Word_Stock p=NULL; int len=sizeof(word_stock)/sizeof(*word_stock); if ((p=(P_Word_Stock)(P_Complex_Number)bsearch(str,word_stock,len,sizeof(*word_stock),Comp_Bsearch))!=NULL) return p->content; return NULL; } void Creat_Node(void** p,const int k) //创造并初始化一个节点 { *p=malloc(k); assert(*p); memset(*p,0,k); } void Free_Node(void** p) { if (*p==NULL) return ; free(*p); *p=NULL; } void Input(const void* express[],P_Complex_Number comp) //输入一个节点 { printf(Prompt_Message(express[0])); Input_Data("%f",(void* )&comp->Real_Component); printf(Prompt_Message(express[1])); Input_Data("%f",(void* )&comp->Imaginary_Part); } void Input_Data(const char* format,void* data) //输入处理函数 { char str[MAX_BUFF]={0}; fscanf(stdin,"%s",str); while (sscanf(str,format,data)!=1) { while (getchar()!='\n'); puts("输入数据不合法,请重新输入!"); fscanf(stdin,"%s",str); } while (getchar()!='\n'); } void Print(const void* express[],P_Complex_Number p) { printf(Prompt_Message(express[0]),p->Real_Component); printf(Prompt_Message(express[1]),p->Imaginary_Part); puts(""); } void Complex_Add_or_Sub(P_Complex_Number comp1,P_Complex_Number comp2, const int k,const void* express) //复数加减法运算 { float Real_Component=comp1->Real_Component+k*comp2->Real_Component; float Imaginary_Part=comp1->Imaginary_Part+k*comp2->Imaginary_Part; float fabs_Real_Component=(float)fabs(Real_Component); float fabs_Imaginary_Part=(float)fabs(Imaginary_Part); printf(Prompt_Message(express)); if (fabs_Real_Component>=1e-6) printf("%.2f",Real_Component); else if (fabs_Imaginary_Part<1e-6) { printf("%.2f\n",Real_Component); return ; } if (fabs_Imaginary_Part>=1e-6&&fabs_Real_Component>=1e-6) printf("+"); if (fabs(Imaginary_Part)<1e-6) { puts(""); return ; } printf("%.2fi\n",Imaginary_Part); }
[此贴子已经被作者于2017-9-19 18:54编辑过]
#include<stdio.h> #include<conio.h> #include<string.h> # define LEN sizeof(struct Lnode) typedef struct list { char data[80];//80字符为1行,1行一个节点 int line[80];//列 int row;//行 struct Lnode *next; } list,*linklist; void search(char *s,char *t); void BF(char *s,char *t); void delete(linklist L,char* ch); void insert1(linklist L,int row,int line,char *ch); void insert(int x,char *s,char *t); void print(linklist L)//80字符为1行,1行一个节点 { int len,i=0; while(L) { printf("第%d行:",L->row); while(i<strlen(L->data)) { printf("%c",L->data[i]); printf("列:%d",L->line[i]); i++; } printf("\n"); L=L->next; i=0; } } linklist create() { int i=0,j,line; char express[80]="\0"; linklist head,r,p; head=(linklist)malloc(sizeof(list)); head->next=NULL; printf("输入quit退出\n"); memset(head->data,'\0',80); fflush(stdin); head->row=i; printf("第%d行:",head->row); gets(express); strcpy(head->data,express); for(line=0; line<strlen(express); line++) { head->line[line]=line; } r=head; while(1) { i++; p=(linklist)malloc(sizeof(list));//为新节点开辟空间 memset(p->data,'\0',80); printf("第%d行:",i); gets(express); for(line=0; line<strlen(express); line++) { p->line[line]=line; } strcpy(p->data,express); p->row=i; if(strcmp(express,"quit")==0) break; p->next=NULL; r->next=p; r=p; } //r->next=NULL; return head; } void count(linklist L) { int Chinese=0; int lowcase=0; int capital=0; int punction=0; int space=0; int i=0,num=0; while(L) { while(i<strlen(L->data)) { if((L->data[i])>=48&&(L->data[i])<=57) { num++; } else if((L->data[i])>=65&&(L->data[i])<=90) { capital++; } else if((L->data[i])>=97&&(L->data[i])<=122) { lowcase++; } else if((L->data[i])>=65&&(L->data[i])<=90) { capital++; } else if((L->data[i])>=33&&(L->data[i])<=47||(L->data[i])>=58&&(L->data[i])<=63||(L->data[i])>=123&&(L->data[i])<=126) { punction++; } else if(L->data[i]==32) { space++; } //printf("%c",L->data[i]); i++; } L=L->next; i=0; } printf("数字个数为: %d\n",num); printf("空格个数为: %d\n",space); printf("标点符号个数为: %d\n",punction); printf("小写字母个数为: %d\n",lowcase); printf("大写字母个数为: %d\n",capital); } int main() { int c,c1,end,line1,row1,m; char ch[80],ch1[80],x[80]; linklist L,L1,position; L1=NULL; printf(" \t\t ----------->\n"); printf("\t\t \t |文本编辑器| \t\n"); printf(" \t\t <-----------\n"); printf(" \t \t \t 1.开始 \t\n"); printf(" \t \t \t 2.退出 \t\n"); printf("请选择:\n"); while( scanf("%d",&c)==1) { printf(" 0.退出 "); printf(" 1.写文本 "); printf(" 2.文本内容统计 "); printf(" 3.文本内容查找 "); printf(" 4.文本内容删除"); printf(" 5.文本内容插入 "); printf("请选择:\n"); while(scanf("%d",&c1)!=0) { switch(c1) { case 0: return 0; break; case 1: printf("请输入文档\n") ; L1= create(); print(L1); //count(L1); break; case 2: count(L1); ; break; case 3: printf("请输入想查找的字符或者字符串:"); scanf("%s",ch); while(L1) { search(L1->data,ch); L1=L1->next; } print(L1); break; case 4: printf("请输入想删除的字符或者字符串:"); scanf("%s",ch1); delete(L1,ch1); break; case 5: printf("请输入想插入的字符或者字符串:"); scanf("%s",x); printf("请输入想插入的行数和列数: "); scanf("%d %d",&row1,&line1); m=0; while(L1) { while(m<=strlen(L1->data)) { printf("m0=%d\n",m); if((L1->line[m]==line1)&&(L1->row==row1)) { printf("m1=%d\n",m); printf("%d %d",L1->line[m],L1->row); insert(L1->line[m],L1->data,x);break; } m++; } printf("m2=%d\n",m); L1=L1->next; m=0; } break; } } } } void BF(char *s,char *t) { int i=0,j=0,t1,t2; int slen=strlen(s); int tlen=strlen(t); while(i<slen) { if(s[i]==t[j]) { i++; j++; if(j==tlen) { //printf("%d ",i-tlen); for(t1=0; t1<tlen; t1++) { for(t2=(i-tlen)+1; t2<=slen-1; t2++) { s[t2-1]=s[t2]; } } s[slen-tlen]='\0'; slen=strlen(s); i=0; j=0; } } else { i=i-j+1; j=0; } } } void insert(int x,char *s,char *t)//在字符串s的x位置插入t { int i,j,m; i=strlen(s); printf("插入前:%s\n",s); for(m=0; m<strlen(t); m++) { for(j=i-1; j>=x-1; j--) s[j+1]=s[j]; s[x++]=t[m]; ++i; } printf("插入后:%s",s); } void delete(linklist L,char* ch) { while(L) { BF(L->data,ch); printf("第%d行:%s\n",L->row,L->data); L=L->next; } //return L; //print(L); } void search(char *s,char *t) { int i=0,j=0,t1,t2; int slen=strlen(s); int tlen=strlen(t); while(i<slen) { if(s[i]==t[j]) { i++; j++; if(j==tlen) { printf("%s在%s 的%d位置 ",s,t,i-tlen); } } else { i=i-j+1; j=0; } } }