2014年7月14日 星期一

(綠色皮) 最新C程式語言, 施威銘研究室 著, 旗標 第十二章 '參考' 答案

==============================================
/* 程式檔名:test_12-49-1.c*/
/* 程式目的:利用指位器把變數i=5的值從螢幕輸出*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20140630*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    int i=5;
    int *ptr=NULL;
    ptr=&i;
    printf("%d \n",*ptr);
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}

void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}

==============================================
/* 程式檔名:test_12-49-2.c*/
/* 程式目的:從螢幕輸出變數a,b,c,d的位址*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20140630*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    int a,b,c,d;
    int *ptr1=NULL,*ptr2=NULL,*ptr3=NULL,*ptr4=NULL;
    ptr1=&a;
    ptr2=&b;
    ptr3=&c;
    ptr4=&d;
    printf("a 的位址 %p \n",ptr1);
    printf("b 的位址 %p \n",ptr2);
    printf("c 的位址 %p \n",ptr3);
    printf("d 的位址 %p \n",ptr4);
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}

void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 
}


==============================================
/* 程式檔名:test_12-49-3.c*/
/* 程式目的:將a,b,c,d的位址指定給指位器 *e,*f,*g,*h 並從螢幕輸出 *e,*f,*g,*h 的內容以其位址*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20140630*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    int a=1,b=2,c=3,d=4;
    int *e=NULL,*f=NULL,*g=NULL,*h=NULL;
    e=&a;
    f=&b;
    g=&c;
    h=&d;
    printf("a 的位址 %p \n",e);
    printf("b 的位址 %p \n",f);
    printf("c 的位址 %p \n",g);
    printf("d 的位址 %p \n",h);
    
    printf("a 的內容 %d \n",*e);
    printf("b 的內容 %d \n",*f);
    printf("c 的內容 %d \n",*g);
    printf("d 的內容 %d \n",*h);
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}

void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}

==============================================
/* 程式檔名:test_12-49-4.c*/
/* 程式目的:利用指位器從螢幕輸出陣列array[5]={2,3,4,5,6}*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20140630*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    int *ptr[5]={2,3,4,5,6};
    
    int i;
    
    for(i=0;i<5;i++)
    {
     printf("內容: %d \n",ptr[i]);                
    }
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}

void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 
}


==============================================
/* 程式檔名:test_12-49-5.c*/
/* 程式目的:利用指位器從螢幕輸出陣列array[3][5]={{2,3,4,5,6},{7,8,9,10,11},{12,13,14,15}}*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20140630*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    int array[3][5]={{2,3,4,5,6},{7,8,9,10,11},{12,13,14,15,16}};
    int i,j;
    
    for(i=0;i<3;i++)
    {
     for(j=0;j<5;j++)
     {
      printf("位址: %p  內容: %d \n",&array[i][j],array[i][j]);                    
     }
    }
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}

void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 
}


==============================================
/* 程式檔名:test_12-49-5.c*/
/* 程式目的:利用雙重指位器從螢幕輸出陣列array[3][5]={{2,3,4,5,6},{7,8,9,10,11},{12,13,14,15}}*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20140630*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    int array[3][5]={{2,3,4,5,6},{7,8,9,10,11},{12,13,14,15,16}};
    int i,j;
    
           
    for(i=0;i<3;i++)
    {                       
     for(j=0;j<5;j++)
     {               
       printf("位址: %p  內容: %d \n",array+j,*(*(array+i)+j));                    
     }
    }
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}

void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}

==============================================
/* 程式檔名:test_12-49-7.c*/
/* 程式目的:計算學生三科成績的總和與平均*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20140701*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int sumavg(int *);
void Pause(const char* message);
#define MAX 3

int main(int argc, char *argv[])
{
    int i,sum=0;
    float avg;
    int *array=(int *)malloc(sizeof(int) *MAX);
    
    printf("請輸入三科成績\n");
    for(i=0;i<MAX;i++)
    {
     printf("請輸入%d科成績:",i+1);
     scanf("%d",array+i);              
    }
    
    sum=sumavg(array);        
    avg=(float)sum/3;
    
    printf("三科總分:%d\n",sum); 
    printf("三科平均:%.2f\n",avg); 
    
    free(array);
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}

void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 
}

int sumavg(int *a)
{
    int sum=0,avg=0;
    int i;
    
    for(i=0;i<3;i++)
    {
     sum=sum+*(a+i);                                  
    }
    return sum;

}

==============================================
/* 程式檔名:test_12-49-7.c*/
/* 程式目的:計算學生三科成績的總和與平均*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20140701*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void swap(int *);
void sum(int *);
void Maxnum(int *);

void Pause(const char* message);

int main(int argc, char *argv[])
{
    int array[10]={45,65,24,49,68,78,45,12,32,40};
    int i;
    
    
    
    sum(array);     
    Maxnum(array);
    swap(array);   
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}

void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 
}

void swap(int *a)
{
    int temp;
    int i,j;
    
    for(i=0;i<10;i++)
    {
     for(j=0;j<10;j++)
     {    
       if(*(a+i)>*(a+j))
       {              
         temp=*(a+i);
         *(a+i)=*(a+j);
         *(a+j)=temp;
       }   
     }                  
    }
    
    printf("排序: "); 
    for(i=0;i<10;i++)
    {
     printf("%d ",*(a+i));                      
    }
    printf("\n"); 
}

void sum(int *a)
{
    int sum=0;
    int i;
    
    for(i=0;i<10;i++)
    {
     sum=sum+*(a+i);                                  
    }
    printf("總和:%d\n",sum); 
}

void Maxnum(int *a)
{
    int i,MAX;
    
    for(i=0;i<10;i++)
    {
     if( *a < *(a+i))
     {
      MAX=*(a+i);             
     }                                
    }
    printf("最大值:%d\n",MAX); 
}


==============================================
/* 程式檔名:test_12-49-9.c*/
/* 程式目的:利用雙重指位器從螢幕輸出*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20140702*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
char **ptr;
int n,m;
char *temp[100];
char *string1="下馬飲均酒,";
char *string2="問君何所之,";
char *string3="君言不得意,";
char *string4="歸臥南山陲,";
char *string5="但去莫復聞,";
char *string6="白雲無盡時。";

ptr=temp;

*(ptr+0) = string1;
*(ptr+1) = string2;
*(ptr+2) = string3;
*(ptr+3) = string4;
*(ptr+4) = string5;
*(ptr+5) = string6;

for(n=0;n<3;n++)
{
     printf("%s",*(ptr+n));              
    }
    printf("\n");  
    for(n=3;n<6;n++)
{
     printf("%s",*(ptr+n));              
    }
    printf("\n");  
Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}

void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 
}


==============================================
/* 程式檔名:test_12-49-10.c*/
/* 程式目的:利用指位器判斷書出自串是否回文*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20140702*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define SIZE 100

int main(void)
{
  

  char *str1="abcba"; 
  char *str2=(char *)malloc(sizeof(char) *strlen(str1)+1);
  
  unsigned int i=0,j=0,flag=0;
  int s=strlen(str1);
  
  
  for(i=0;i<s;i++)
  {
if(isalpha(*(str1+i))!=0)
{
if(*(str1+i)<'a')
          *(str1+i)=*(str1+i)+32;
   *(str2+j)=*(str1+i);
j++;
}
  }
  *(str2+j)='\0';
  j=j-1;
  i=0;
  while(i<=j)
  {
if(*(str2+i)!=*(str2+j-i))
{
flag=1;
break;
}
i++;
  }
  if(flag==1)
printf("此字串不是迴文\n");
  else
printf("此字串為迴文\n");

  free(str2);
  system("pause");
  return 0;

}
==============================================