8.17

来源:互联网 发布:法语自学软件app 编辑:程序博客网 时间:2024/05/01 01:20

21.
#include<stdio.h>
struct st{
      int a;
      char b;
};
struct st fun(struct st x){
       x.a=10;
       x.b='B';
       return x;
}
main(){
       struct st y;
       y.a=0;
       y.b='A';
       printf("返回值.a=%d 返回值.b=%c/n",fun(y).a,fun(y).b);
       printf("y.a=%d y.b=%c/n",y.a,y.b);
}


结构体的返回值,但其下层建筑还是值址传递

22。
一个单向链表,不知道头节点,一个指针指向其中的一个节点,问如何删除这个指针指向的节点?

设指针为p,x=p.next, p=x

23.
一个完整的打印输入输出程序
#include<stdio.h>
#include<malloc.h>
struct node
{
char data;
struct node *next;
};
void insert(struct node* *head,char item);
void instruction(void);
void printList(struct node *head);
//*********************************************************************
void main()
{
struct node  *startPtr=NULL;
char item;
int choice;
instruction();
printf("please input your choice!/n");
scanf("%d",&choice);/*choice为1的时候就生成一个节点,为2退出程序*/


while(choice!=2)
{
switch(choice)
{
case 1:
printf("please input char!");
                            scanf("/n%c",&item);
insert(&startPtr,item);
printList(startPtr);
break;
default:
printf("invalid choice!/n");
instruction();
//scanf("%c",&item);
break;
}
printf("?");
scanf("%d",&choice);

}
printf("End of run!/n");

}
//打印菜单命令
void instruction(void)
{
printf("Enter your choice:/n"
"1 to insert a node!/n"
"2 to end!/n");
}
//插入一个节点
void insert(struct node* *head,char item)
{
  struct node *newPtr,*currentPtr,*prePtr;
  newPtr=(struct node*)malloc(sizeof(struct node));
  if(newPtr==NULL)
  {
  printf("%c not inserted! No memory available!/n");
  return;
  }
  else
  {
  newPtr->next=NULL;
  (*newPtr).data=item;
 
  currentPtr=*head;
  prePtr=NULL;
  while((currentPtr!=NULL)&&(currentPtr->data<item))
  {
  prePtr=currentPtr;
  currentPtr=currentPtr->next;

  }
  if(prePtr==NULL)
  {
  //newPtr->next=head;
  *head=newPtr;
  //newPtr->next==NULL;
  }
  else
  {
  newPtr->next=currentPtr;
  prePtr->next=newPtr;
  }
  }
}

/*打印链表*/
void printList(struct node *head)
{
struct node *currentPtr=head;
if(currentPtr==NULL)
printf("List is empty!/n");
else
{
printf("List is:/n");
while(currentPtr!=NULL)
{
printf("%c->",currentPtr->data);
currentPtr=currentPtr->next;
}
printf("NULL/n");
}
}

24。
int main()
{
    int a = 1; 
    printf("%d", a+1,a++);
    cin.get();
}
输出什么?为什么?

int main()
{
    int a = 1;
    int b = (a+1, a++);
    printf("%d", b);
    cin.get();
}
输出什么?为什么?

第一个体应该输出a+1,不属于逗号运算符的问题。
第2个,同意ooy777(深白色):可简化为b=a++,所以结果是1。

int main()
{
    int a = 1; 
    printf("%d", a+1,a++);
    cin.get();
}

这个有可能是2,2,同时也有可能是3,2. 跟编译有关系
2,2的情况是你用VC,TC编译,他是从左到右编译的.
3,2的情况是你用Gcc便宜,他是从右到左便宜的.


25。
怎么输出下面的图形?
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *


#include <stdio.h>
void main()
{
    int i,m=1;
    for(i=0;i<19;i++)
    {
       
printf("%*s/n",m<=10?10+m:30-m,"*******************"+(m<=10?20-2*m:2*m-
20));
        m++;
       
    }
}


%号后面的星号是一个表示域宽的变量.就是m<=10?10+m:30-m

"*******************"+(m<=10?20-2*m:2*m-20));
指针 + int

 

原创粉丝点击