c语言程序综合设计cpp文件.docx
《c语言程序综合设计cpp文件.docx》由会员分享,可在线阅读,更多相关《c语言程序综合设计cpp文件.docx(16页珍藏版)》请在冰豆网上搜索。
c语言程序综合设计cpp文件
1
#include
#include
#defineLENsizeof(structnode)
structnode
{
doublenum;
structnode*next;
};
intn1=0;
intn2=0;
intlen=0;
structnode*create_list(structnode*head,intn,intflag)
{
structnode*p1,*p2;
printf("请输入数列,以回车键结束一个数字的输入,以ctrl+z结束一数列的输入.\n");
p1=p2=(structnode*)malloc(LEN);
while(scanf("%lf",&p1->num)!
=EOF)
{
n++;
if(n==1)head=p1;
elsep2->next=p1;
p2=p1;
p1=(structnode*)malloc(LEN);
}
p2->next=NULL;
free(p1);
if(flag==1)n1=n;
if(flag==2)n2=n;
return(head);
}
structnode*convert_list(structnode*head)
{
structnode*p1,*p2;
doublet;
p1=p2=(structnode*)malloc(LEN);
p1=p2=head;
while(p1!
=NULL)
{
p2=p1->next;
while(p2!
=NULL){
if(p1->numnum){
t=p1->num;
p1->num=p2->num;
p2->num=t;
}
p2=p2->next;
}
p1=p1->next;
}
free(p1);free(p2);
return(head);
}
intmain(){
structnode*head,*head1,*head2,*temp,*p,*p1,*p2;
head=head1=head2=temp=p=p1=p2=(structnode*)malloc(LEN);
head1=create_list(head1,n1,1);
head2=create_list(head2,n2,2);
head1=convert_list(head1);
head2=convert_list(head2);
temp=head1;
while(temp!
=NULL){
printf("%5.2f",temp->num);
temp=temp->next;
}
printf("\n");
temp=head2;
while(temp!
=NULL){
printf("%5.2f",temp->num);
temp=temp->next;
}
printf("\n");
p1=head1;p2=head2;
while((n1!
=0)&&(n2!
=0)){
len++;
if(p1->num>=p2->num){
p->num=p1->num;
p1=p1->next;
n1--;
}else{
p->num=p2->num;
p2=p2->next;
n2--;
}
if(len==1)head=p;
elsetemp->next=p;
temp=p;
p=(structnode*)malloc(LEN);
}
if(n1==0)temp->next=p2;
if(n2==0)temp->next=p1;
temp=head;
while(temp!
=NULL){
printf("%5.2f",temp->num);
temp=temp->next;
}
printf("\n");
free(head);free(head1);free(head2);free(temp);free(p);free(p1);free(p2);
return0;
}
2
#include
#defineN100000
voidmain()
{
inti,j,n,sum=1,top=0;
inta[N]={0},b[N]={0};
scanf("%d",&n);
if(n<10)
{
for(i=1;i<=n;i++)
sum*=i;
printf("%d\n",sum);
}
else
{
for(i=1;i<=9;i++)
sum*=i;
i=0;
while(sum)
{
a[i]=sum%10;
sum/=10;
i++;
}
top=i-1;
for(j=10;j<=n;j++)
{
a[0]*=j;
b[0]=a[0]/10;
a[0]%=10;
for(i=1;i{
a[i]=a[i]*j+b[i-1];
b[i]=a[i]/10;
a[i]%=10;
}
a[top]=a[top]*j+b[i-1];
b[top]=a[top]/10;
a[top]%=10;
while(b[top])
{
top++;
a[top]=b[top-1]%10;
b[top]=b[top-1]/10;
}
}
for(j=0;j<=top;j++)
{
if(j%50==0)printf("\n");
printf("%d",a[top-j]);
}
printf("\n");
}
}
3
#include
intresult=1;
intchess[8];
intcheck(intn)
{
inti;
for(i=1;i<=n-1;i++)
{
if((chess[i]==chess[n])||((chess[n]-chess[i])==(n-i))||((chess[n]-chess[i])==(i-n)))
return0;
}
return1;
}
voidshow_chess(void)
{
inti;
printf("第%2d个解决方案:
",result++);
for(i=1;i<=8;i++)
{
printf("%d",chess[i]);
}
printf("\n");
}
voidputchess(intn)
{
inti;
if(n<=8){
for(i=1;i<=8;i++)
{
chess[n]=i;
if(check(n)==1)
{
if(n==8)
show_chess();
else
putchess(n+1);
}
}
}
}
intmain()
{
intb;
key:
printf("这里是8皇后问题的递归解决方法:
\n");
printf("请输入数字8\n");
scanf("%d",&b);
if(b==8)
{
putchess
(1);
}else
{
gotokey;
}
return0;
}
4
/*
**************************
高斯消元解N元一次方程
**************************
*/
#include
#include
#include
#defineEPS1e-6
#defineN200
intn;
doublegauss[N][N];//存序数矩阵
doubleanswer[N];//用来存有唯一解是的答案
intinput()
{
inti,j;
if(scanf("%d",&n)==EOF)return0;
for(i=0;ifor(j=0;j<=n;j++)
scanf("%lf",&gauss[i][j]);
return1;
}
voidswap(double&a,double&b)
{
doublec;
c=a;
a=b;
b=c;
}
/*
*********************************
判断方程是否有解,返回值是0表示无解
返回值是1表示有唯一解
返回值是2表示有无穷多解
*********************************
*/
intslove_gaussa()
{
inti,j,k,p;
for(j=0;j{
p=j;
for(i=j+1;iif(fabs(gauss[i][j])>fabs(gauss[p][j]))p=i;
if(fabs(gauss[p][j])if(p!
=j)
{
for(k=j;k<=n;k++)
swap(gauss[p][k],gauss[j][k]);
}
for(i=j+1;ifor(k=n;k>=j;k--)
gauss[i][k]-=gauss[j][k]*gauss[i][j]/gauss[j][j];
}
boolr=false;
for(i=0;i{
intnum1=0,num2=0;
for(j=0;j{
if(fabs(gauss[i][j])if(fabs(gauss[j][i])}
if(num1==n)return0;
if(num2==n)r=true;
}
if(r)return2;
for(j=n-1;j>=0;--j){
answer[j]=gauss[j][n]/gauss[j][j];
for(i=j-1;i>=0;--i)
gauss[i][n]-=gauss[i][j]*answer[j];
}
return1;
}
intmain()
{
freopen("in4.txt","r",stdin);
freopen("out4.txt","w",stdout);
while(input()){
inti;
intyes=slove_gaussa();
/*for(inti=0;ifor(intj=0;j<=n;j++)
printf("%lf",gauss[i][j]);
putchar(10);
}*/
if(yes==0)
{
printf("此N元一次方程无解\n");
}
elseif(yes==2)
{
printf("此N元一次方程有无穷多解\n");
}
else
{
printf("此N元一次方程有唯一解:
\n");
for(i=0;iprintf("%.2lf",answer[i]);
putchar(10);
}
}
return0;
}
5
#include
#include
charcmp[1001][51],stack1[51],stack2[51],num[1001][51];
boolcheck(chark)
{
if(k>='A'&&k<='Z'||k>='a'&&k<='z'||k>='0'&&k<='9'||k=='_')
returntrue;
returnfalse;
}
boolcheck2(chark)
{
if(k>='A'&&k<='Z'||k>='a'&&k<='z'||k=='_')
returntrue;
returnfalse;
}
intmain()
{
intn,i,j;
chara[1001],x[1001];
scanf("%d%*c",&n);
inticase=0;
while(n--)
{
printf("Case%d:
\n",++icase);
gets(a);
intlena=strlen(a);
intt=0,s=-1;
for(i=0;iif(a[i]!
=''){
cmp[t][++s]=a[i];
if(i==lena-1){
cmp[t][++s]='\0';
t++;
}
}
else{
if(s!
=-1){
cmp[t][++s]='\0';
s=-1;
t++;
}
}
gets(x);
intlenx=strlen(x);
intk=0,ok=1;
s=-1;
for(i=0;i<=lenx;i++){
if(check(x[i])&&inum[k][++s]=x[i];
else
if(s!
=-1){
num[k][++s]='\0';
for(j=0;jif(strcmp(cmp[j],num[k])==0){
break;
}
if(j==t){
for(j=0;j
if(check2(num[k][j]))
break;
if(j
printf("未定义变量%s\n\n",num[k]);
ok=0;
break;
}
}
s=-1;
k++;
}
}//判断变量是否定义
inttop1=-1,top2=-1;
if(ok){
for(i=0;iif(x[i]=='(')
stack1[++top1]='(';
else
if(x[i]==')'){
if(top1>=0)
top1--;
else
stack1[++top1]=')';
}
else
if(x[i]=='[')
stack2[++top2]='[';
else{
if(top2>=0)
top2--;
else
stack2[++top2]=']';
}
if(top1>=0){
if(top1==top2)
printf("%c与%c不匹配\n\n",stack1[0],stack2[0]);
else{
if(stack1[0]=='(')
printf("左括号多余\n\n");
else
printf("右括号多余\n\n");
}
ok=0;
}
}//堆栈
if(ok){
for(i=0;iif(x[i]=='+'&&x[i+1]=='+'||x[i]=='-'&&x[i+1]=='-'){
if(i+2printf("%c%c后面不能再跟变量\n\n",x[i],x[i]);
ok=0;
break;
}
}
else
if(check(x[i])&&i+1printf("变量后面不能直接跟左括号\n\n");
ok=0;
break;
}
else
if(i+1&&(x[i+1]=='*'||x[i+1]=='['||x[i+1]==']'||x[i+1]==')'||x[i+1]=='/'||x[i+1]=='+'||x[i+1]=='-'||x[i+1]=='%')){
if((x[i]=='+'||x[i]=='-')&&(x[i]==x[i+1])||(i+3i+2ok=1;
else{
printf("运算符%c的右边不能接%c\n\n",x[i],x[i+1]);
ok=0;
break;
}
}
else
if(x[i]=='%'&&i+1printf("0不能做%运算的除数\n\n");
ok=0;
break;
}
}
if(ok)
printf("OK\n\n");
}//n
return0;
}