主题:数据结构实验全集
orangelegend
[专家分:860] 发布于 2007-04-26 20:40:00
test
回复列表 (共31个回复)
沙发
orangelegend [专家分:860] 发布于 2007-04-26 20:45:00
一元稀疏多项式计算器
// Polyn.cpp : Defines the entry point for the console application.xmu csd 0412
//
#include<stdio.h>
#include<malloc.h>
typedef struct LNode{
int coef;/*系数*/
int expn;/*指数*/
struct LNode *next;
}LNode,*LinkList;
void CreateList(LinkList L,int m)
{
LinkList p,q;
int i=0;
p=q=L;
L->next=NULL;
printf("先输系数后输指数,中间空格\n");
for(i=0;i<m;i++){
p=(LNode *)malloc(sizeof(LNode));
scanf("%d %d",&(p->coef),&(p->expn));
L->next=p;
L=p;
}
L->next=NULL;
L=q;
}/*test success*/
void print(LinkList L)
{
LinkList p;
p=L->next;
while(p!=NULL){
printf("%d %d",p->coef,p->expn);
printf("\n");
p=p->next;
}
}
void DeleteNode(LNode *p)
{
LNode *q;
q=p->next;
p->next=q->next;
free(q);
}/*删除节点*/
int LocateElem(LinkList L,int expn)/*查找是否有与该多项式的系数相同的项*/
{
LNode *p;
p=L;
while(p->expn!=expn&&p!=NULL) p=p->next;
if (p->expn==expn) return p->coef;
else if(p==NULL) return 0;
}
int main()
{
LinkList L;
int m;
L=(LinkList)malloc(sizeof(LNode));
printf("多项式的项数\n");
scanf("%d",&m);
CreateList(L,m);
print(L);
return 0;
}
板凳
orangelegend [专家分:860] 发布于 2007-04-26 20:47:00
算术表达式求值
头文件stack.h
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct {
int *top;
int *base;
int stacksize;
}Sqstack;
void InitStack(Sqstack &s);
int StackEmpty(Sqstack s);
void Push(Sqstack &s,int x);
void Pop(Sqstack &s,int &x);
void PopChar(Sqstack &s,char &x);
void PushChar(Sqstack &s,char x);
int GetTop(Sqstack s);
主文件
// Operand.cpp : Defines the entry point for the console application.xmu csd
//
#include"stack.h"
#include<stdio.h>
char a[7]={'+','-','*','/','(',')','#'};
char result[7][7]={{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}};
int In(char x,char a[]){
int i;
for(i=0;i<7;i++)
if(a[i]==x) return i+1;
return 0;
}
char Precede(char letter1,char letter2){
return result[In(letter1,&a[0])-1][In(letter2,&a[0])-1];
}
int Operate(int a,char theta,int b){
int result=0;
switch(theta){
case '+':result=a+b;break;
case '-':result=a-b;break;
case '*':result=a*b;break;
case '/':result=a/b;break;
default:printf("error in operate\n");exit(1);break;
}
return result;
}
}
int Evaluate(Sqstack &OPTR,Sqstack &OPND){
char c,x,theta;
int Oper1,Oper2,flag;
flag=0;
InitStack(OPTR);
PushChar(OPTR,'#');
InitStack(OPND);
c=getchar();
while(c!='#'||GetTop(OPTR)!='#'){
if(!In(c,&a[0])) {
if (flag==0) {Push(OPND,c-'0');flag=1;}
else if (flag==1) *OPND.top=*OPND.top*10+c-'0';
c=getchar();
}
else{
flag=0;
switch(Precede(GetTop(OPTR),c)){
case '<':
PushChar(OPTR,c);
c=getchar();
break;
case '=':
PopChar(OPTR,x);
c=getchar();
break;
case '>':
PopChar(OPTR,theta);
Pop(OPND,Oper2);
Pop(OPND,Oper1);
Push(OPND,Operate(Oper1,theta,Oper2));
break;
}
}//switch
}//while
return GetTop(OPND);
}
int main(int argc, char* argv[])
{
Sqstack OPTR;
Sqstack OPND;
while(1){
printf("%d\n",Evaluate(OPTR,OPND));
}
return 0;
}
4 楼
orangelegend [专家分:860] 发布于 2007-04-26 20:50:00
#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct {
char *ch;
int length;
}HString;
typedef struct{
HString StrHead[100];
int count;
}StrHeadList;
void InitString(HString &s){
s.ch=NULL;
s.length=0;
}
int StringAssign(HString &s,char *str){
int i,j;
char *p;
for(i=0,p=str;*p;p++,i++);
if(!i) {s.ch=NULL;s.length=0;}
else {
s.ch=(char *)malloc(i*sizeof(char));
if(!s.ch) {
printf("strassign malloc error\n");
exit(1);
}
s.length=i;
for(j=0;j<i;j++)
s.ch[j]=str[j];
}
return 1;
}
int StrCopy(HString &s,HString t){
int i;
s.length=0;
s.ch=(char *)malloc(t.length*sizeof(char));
if(!s.ch){
printf("strcopy malloc error\n");
exit(1);
}
for(i=0;i<t.length;i++)
s.ch[i]=t.ch[i];
s.length=t.length;
return 1;
}
int Concat(HString &s,HString t){
int i;
HString temp;
if(t.length==0) return 0;
InitString(temp);
StrCopy(temp,s);
s.length=s.length+t.length;
//printf("%s\ntemp's len is\n",s.ch,s.length);
free(s.ch);
s.ch=(char *)malloc(s.length*sizeof(char));
if(!s.ch){
printf("s.ch malloc in concat error\n");
exit(1);
}
for(i=0;i<temp.length;i++)
s.ch[i]=temp.ch[i];
for(i=0;i<t.length;i++)
s.ch[i+temp.length]=t.ch[i];
return 1;
}
void StrOut(HString s){
int i;
for(i=0;i<s.length;i++)
putchar(s.ch[i]);
printf("\n");
}
int Equal(HString s,HString t){
int i;
if(s.length!=t.length) return s.length-t.length;
else{
for(i=0;i<s.length&&s.ch[i]==t.ch[i];i++);
if(i==s.length) return 0;
else return s.ch[i]-t.ch[i];
}
}
int GetSub(HString s,HString &t,int i,int j){//获取从i到J的子串于T
int k;
if(i<1||j>s.length) {
printf("子串首位置小于零或末位置超过子串长度\n");
return 1;
}
t.length=j-i+1;
if(t.length<=0) {InitString(t);return 0;}
t.ch=(char *)malloc(t.length*sizeof(char));
if(!t.ch){
printf("t.ch malloc error in GetSub\n");
exit(1);
}
else{
for(k=0;k<t.length;k++)
t.ch[k]=s.ch[i+k-1];
}
return 1;
}
int Length(HString s){
return s.length;
}
int Index(HString s,HString t,int pos){
int i,j;
i=pos;
j=0;
while(i<=s.length-t.length){
while(s.ch[i]==t.ch[j]&&j<t.length){
i++;j++;
}//while
if(j==t.length) return i-j;
else{
j=0;
i=i-j+1;
}//else
}//while
return -1;
}
int Replace(HString &L,HString &s,HString t,HString r){
int tlen,k;
HString temp;
InitString(L);
InitString(temp);
k=Index(s,t,0);
tlen=t.length;
while(k!=-1){
GetSub(s,temp,1,k);
Concat(L,temp);
Concat(L,r);
GetSub(s,temp,k+t.length+1,s.length);
StrCopy(s,temp);
k=Index(s,t,0);
}
Concat(L,s);
return 1;
}
int ViewStrAll(StrHeadList &StrList){
int i;
for(i=0;i<StrList.count;i++){
printf("Name:%d Value:",i);
StrOut(StrList.StrHead[i]);
printf("\n");
}
return 1;
}
5 楼
orangelegend [专家分:860] 发布于 2007-04-26 20:53:00
int DealArgv(StrHeadList &StrList){
char s[200],myargv[4][20],*p;
int pos,myargc;
HString temp,temp1;
StrList.count=0;
while(1){
gets(s);
myargc=0;pos=0;
p=s;
while(*p!='\0'){
if(myargc>4) {
printf("输入错误参数过多\n");
return 0;
}
if(*p!=' '){
if(*p==39||*p=='"'){//39为单引号的asc值因为无法在VC中用*p=''',现在已经明白用'\''但维持原错
p++;
continue;
}//if
myargv[myargc][pos]=*p;
p++;
pos++;
}//if 对第myargc个参数赋值
else {
myargv[myargc][pos]='\0';
pos=0;
myargc++;
p++;
}//else
}
myargv[myargc][pos]='\0';
//到此分割输入已经完成
switch(myargv[0][0]){
case 'A':
StringAssign(StrList.StrHead[StrList.count],myargv[1]);
StrList.count++;
//将第一个字符串装入
break;
case 'E':
if(myargc==1) {
printf("EQUAL\n");
break;
}//if
else {
StringAssign(StrList.StrHead[StrList.count],myargv[1]);
StrList.count++;
StringAssign(StrList.StrHead[StrList.count],myargv[2]);
StrList.count++;
if(Equal(StrList.StrHead[StrList.count-2],StrList.StrHead[StrList.count-1])==0)
printf("EQUAL\n");
else printf("UNEQUAL\n");
break;
}//else
6 楼
orangelegend [专家分:860] 发布于 2007-04-26 20:53:00
case 'C':
if(myargc==1) break;
else{
StringAssign(StrList.StrHead[StrList.count],myargv[1]);
StrList.count++;
StringAssign(StrList.StrHead[StrList.count],myargv[2]);
StrList.count++;
StringAssign(temp,myargv[1]);
StringAssign(temp1,myargv[2]);
Concat(temp,temp1);
StrOut(temp);
}
break;
case 'I':
if(myargc<=1)
printf("参数非法\n");
else {
StringAssign(StrList.StrHead[StrList.count],myargv[1]);
StrList.count++;
StringAssign(StrList.StrHead[StrList.count],myargv[2]);
StrList.count++;
printf("子串位置%d\n",Index(StrList.StrHead[StrList.count-2],StrList.StrHead[StrList.count-1],0));
}
break;
case 'R':
if(myargc<=2)
printf("参数非法\n");
else{
StringAssign(StrList.StrHead[StrList.count],myargv[1]);
StrList.count++;
StringAssign(StrList.StrHead[StrList.count],myargv[2]);
StrList.count++;
StringAssign(StrList.StrHead[StrList.count],myargv[3]);
StrList.count++;
StringAssign(temp,myargv[1]);
Replace(temp1,temp,StrList.StrHead[StrList.count-2],StrList.StrHead[StrList.count-1]);
StrOut(temp1);
}
break;
case 'P':
ViewStrAll(StrList);
break;
case 'Q':
exit(1);
break;
case 'D':int i,j;
i=myargv[1][0]-'0';
for(j=i;j<StrList.count;j++){
StrList.StrHead[j].ch=StrList.StrHead[j+1].ch;
StrList.StrHead[j].length=StrList.StrHead[j+1].length;
}
StrList.count--;
ViewStrAll(StrList);
break;
case 'S':int i1,j1;
StringAssign(StrList.StrHead[StrList.count],myargv[1]);
StrList.count++;
i1=myargv[2][0]-'0';
j1=myargv[3][0]-'0';
GetSub(StrList.StrHead[StrList.count-1],temp,i1,j1);
StrOut(temp);
break;
case 'L':StringAssign(StrList.StrHead[StrList.count],myargv[1]);
StrList.count++;
printf("%d",Length(StrList.StrHead[StrList.count-1]));
printf("\n");
break;
default:break;
}//switch
}//while
return 1;
}
int InitList(StrHeadList &StrList){
for(int i=0;i<100;i++)
StrList.StrHead[i].length=0;
return 1;
}//这个很有必要,不然会出现奇怪的错误
int main(int argc, char* argv[])
{
StrHeadList StrList;
InitList(StrList);
DealArgv(StrList);
return 0;
}
7 楼
orangelegend [专家分:860] 发布于 2007-04-26 20:58:00
稀疏矩阵运算器
自已下载
http://upload.programfan.com/upfile/200704262056510.rar
8 楼
orangelegend [专家分:860] 发布于 2007-04-26 20:59:00
重言式的判别
http://upload.programfan.com/upfile/200704262058538.rar
9 楼
orangelegend [专家分:860] 发布于 2007-04-26 21:03:00
内部排序算法比较
http://upload.programfan.com/upfile/200704262102760.rar
10 楼
moyuxiangtian [专家分:0] 发布于 2007-04-29 18:50:00
à,(R) 6.5 U–ú÷––ú
°? A,
ˉ(R)“n^μμ??£·μ±1£2£°n°“—÷ ?±μ
úO£±ì£…o“à(R)£ μ±+–£–±
° μ>>??
‘”/μ?μ,±μU–÷–ú££oà?êμ±+<<–ú£–
° ?ae>
”…?input.txtˉ^ ‰°μ“––1’?’ n£±ì(R)μ ? n^*
+1£2£°n°”?-|2–÷–£1––ê ?±μ–£±ì£2 – <<
ê ?±μ–£–±
°·?:
′oμ?μ,±μ<<–± ‰??output.txt°
‰>>I?? ‰^??
input.txt output.txt
1 4 3 2 5
5
3 4 5 2 1
3 4 1 5 2
我来回复