Trung tâm đào tạo thiết kế vi mạch Semicon


  • ĐĂNG KÝ TÀI KHOẢN ĐỂ TRUY CẬP NHIỀU TÀI LIỆU HƠN!
  • Create an account
    *
    *
    *
    *
    *
    Fields marked with an asterisk (*) are required.
semicon_lab.jpg

Tổng hợp bài tập và bài giải C/C++ (P1)

E-mail Print PDF

1:MÃ HÓA THÔNG ĐIỆP

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <alloc.h>
  4.  
  5. char *crypt(char *tdiep, int column)
  6. {
  7. char tam[255], *result;
  8. int i = 0, k = 0, n, j=0;
  9.  
  10. while(tdiep[i] != 0)
  11. {
  12. if (isalnum(tdiep[i]))
  13. tam[k++] = tdiep[i];
  14. i++;
  15. }
  16. tam[k] = 0;
  17. result = (char *)malloc(k+1);
  18. for (i=0; i<column; i++)
  19. {
  20. n = 0;
  21. while(n+i < k)
  22. {
  23. result[j++] = tolower(tam[n+i]);
  24. n += column;
  25. }
  26. }
  27. result[k] = 0;
  28. return result;
  29. }
  30.  
  31. void main()
  32. {
  33. char thongdiep[255], *mahoa;
  34. int col;
  35.  
  36. printf("\nNhap thong diep can ma hoa : ");
  37. gets(thongdiep);
  38. printf("\nCho biet so cot : ");
  39. scanf("%d", &col);
  40. mahoa = crypt(thongdiep, col);
  41. printf("\nThong diep da duoc ma hoa thanh : %s", mahoa);
  42. getch();
  43. }

 2:GIẢI PHƯƠNG TRÌNH BẬC NHẤT

  1. #include <stdio.h>
  2.  
  3. void main()
  4. {
  5. float a, b;
  6.  
  7. printf("\nGiai phuong trinh bac nhat AX + B = 0");
  8. printf("\nCho biet ba he so A B : ");
  9. scanf("%f%f", &a, &b);
  10.  
  11. if (a==0)
  12. if (b!=0)
  13. printf("Phuong trinh vo nghiem");
  14. else
  15. printf("Phuong trinh co nghiem khong xac dinh");
  16. else
  17. printf("Dap so cua phuong trinh tren = %f", -b/a);
  18. getch();
  19. }

 3:TÍNH CĂN BẬC HAI THEO PHƯƠNG PHÁP LẶP NEWTON

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. void main()
  5. {
  6. double a, xn, ketqua;
  7.  
  8. printf("\nNhap vao so muon tinh can bac hai : ");
  9. scanf("%lf", &a);
  10. xn = (a+1)/2;
  11. do {
  12. ketqua = xn;
  13. xn = 0.5 * (xn + a/xn);
  14. } while (fabs(xn-ketqua) > 0.0001);
  15. printf("\nKet qua = %lf", xn);
  16. getch();
  17. }

 4:CẤU TRÚC VÀ CÁC HÀM THAO TÁC TRÊN SỐ PHỨC

  1. #include <math.h>
  2.  
  3. typedef struct tagcomplex {
  4. float thuc, ao;
  5. } complex;
  6.  
  7. complex tong(complex a, complex
  8. {
  9. complex c;
  10. c.thuc = a.thuc + b.thuc;
  11. c.ao = a.ao + b.ao;
  12. return c;
  13. }
  14.  
  15. complex hieu(complex a, complex
  16. {
  17. complex c;
  18. c.thuc = a.thuc - b.thuc;
  19. c.ao = a.ao - b.ao;
  20. return c;
  21. }
  22.  
  23. complex tich(complex a, complex
  24. {
  25. complex c;
  26. c.thuc = a.thuc*b.thuc - a.ao*b.ao;
  27. c.ao = a.thuc*b.ao + a.ao*b.thuc;
  28. return c;
  29. }
  30.  
  31. complex thuong(complex a, complex
  32. {
  33. complex c;
  34. float tongbp;
  35. tongbp = b.thuc*b.thuc + b.ao*b.ao;
  36. c.thuc = (a.thuc*a.ao + b.thuc*b.ao)/tongbp;
  37. c.ao = (a.ao*b.thuc - a.thuc*b.ao)/tongbp;
  38. return c;
  39. }
  40.  
  41. float argument(complex a)
  42. {
  43. return acos(a.thuc/sqrt(a.thuc*a.thuc + a.ao*a.ao));
  44. }
  45.  
  46. float modul(complex a)
  47. {
  48. return sqrt(a.thuc*a.thuc + a.ao*a.ao);
  49. }
  50.  
  51. void print_complex(complex a)
  52. {
  53. printf("%.2f + %.2fi", a.thuc, a.ao);
  54. }
  55.  
  56. void main()
  57. {
  58. complex a, b, c;
  59. printf("\nNhap he so thuc va phuc cua A : ");
  60. scanf("%f%f", &a.thuc, &a.ao);
  61. printf("\nNhap he so thuc va phuc cua B : ");
  62. scanf("%f%f", &b.thuc, &b.ao);
  63. printf("\nSo phuc A = ");
  64. print_complex(a);
  65. printf("\nSo phuc B = ");
  66. print_complex( ;
  67. printf("\nTong cua chung = ");
  68. c = tong(a, ;
  69. print_complex©;
  70. printf("\nHieu cua chung = ");
  71. c = hieu(a, ;
  72. print_complex©;
  73. printf("\nTich cua chung = ");
  74. c = tich(a, ;
  75. print_complex©;
  76. printf("\nThuong cua chung = ");
  77. c = thuong(a, ;
  78. print_complex©;
  79. printf("\nArgument cua a = %f", argument(a));
  80. printf("\nModul cua a = %f", modul(a));
  81. getch();
  82. }

 5:DÃY TĂNG DẦN

  1. #include <stdio.h>
  2.  
  3. void main()
  4. {
  5. int a[10], i, maxstart, maxend, maxlen, tmpstart, tmpend, tmplen;
  6.  
  7. printf("\nNhap vao 10 phan tu nguyen cua day :");
  8. for (i=0; i<10; i++)
  9. scanf("%d", &a[i]);
  10. printf("Day da cho :\n");
  11. for (i=0; i<10; i++)
  12. printf("%6d", a[i]);
  13.  
  14. maxstart = maxend = tmpstart = tmpend = 0;
  15. maxlen = tmplen = 1;
  16. for (i=1; i< 10; i++)
  17. {
  18. if (a[i] < a[tmpend])
  19. {
  20. if (maxlen < tmplen)
  21. {
  22. maxstart = tmpstart;
  23. maxend = tmpend;
  24. maxlen = tmplen;
  25. }
  26. tmpstart = tmpend = i;
  27. tmplen = 1;
  28. }
  29. else
  30. {
  31. tmplen++;
  32. tmpend++;
  33. }
  34. }
  35. if (maxlen < tmplen)
  36. {
  37. maxstart = tmpstart;
  38. maxend = tmpend;
  39. }
  40. printf("\nDay tang co so phan tu nhieu nhat la : \n");
  41. for (i=maxstart; i<=maxend; i++)
  42. printf("%6d", a[i]);
  43. getch();
  44. }

 6:DÃY TĂNG CÓ TỔNG DÀI NHẤT

  1. #include <stdio.h>
  2.  
  3. void main()
  4. {
  5. int a[10], i, maxstart, maxend, maxtotal, tmpstart, tmpend, tmptotal;
  6.  
  7. printf("\nNhap vao 10 phan tu nguyen cua day :");
  8. for (i=0; i<10; i++)
  9. scanf("%d", &a[i]);
  10. printf("Day da cho :\n");
  11. for (i=0; i<10; i++)
  12. printf("%6d", a[i]);
  13.  
  14. maxstart = maxend = tmpstart = tmpend = 0;
  15. maxtotal = tmptotal = a[0];
  16. for (i=1; i< 10; i++)
  17. {
  18. if (a[i] < a[tmpend])
  19. {
  20. if (maxtotal < tmptotal)
  21. {
  22. maxstart = tmpstart;
  23. maxend = tmpend;
  24. maxtotal = tmptotal;
  25. }
  26. tmpstart = tmpend = i;
  27. tmptotal = a[i];
  28. }
  29. else
  30. {
  31. tmptotal += a[i];
  32. tmpend++;
  33. }
  34. }
  35. if (maxtotal < tmptotal)
  36. {
  37. maxstart = tmpstart;
  38. maxend = tmpend;
  39. }
  40. printf("\nDay tang co tong nhieu nhat la : \n");
  41. for (i=maxstart; i<=maxend; i++)
  42. printf("%6d", a[i]);
  43. getch();
  44. }

 7:QUẢN LÝ SINH VIÊN

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <mem.h>
  4. #include <string.h>
  5.  
  6. #define MAX 100
  7. #define TOAN 0
  8. #define LY   1
  9. #define HOA  2
  10.  
  11. struct sinhvien {
  12.   char mslop[5];
  13.   char hoten[35];
  14.   float diem[3];
  15. } danhsach[MAX];
  16. int n = 0;
  17.  
  18. void nhapmoi()
  19. {
  20.   char mslop[5], tmp[3];
  21.   int i;
  22.   float diem[3];
  23.   do {
  24.     printf("\nCho biet ma so lop : ");
  25.     gets(mslop);
  26.     if (strlen(mslop))
  27.     {
  28.       strcpy(danhsach[n].mslop, mslop);
  29.       printf("\nCho biet ho ten : ");
  30.       gets(danhsach[n].hoten);
  31.       printf("\nCho biet diem so : ");
  32.       for (i=0; i<3; i++)
  33.       {
  34.         scanf("%f", &diem[i]);
  35.         danhsach[n].diem[i] = diem[i];
  36.       }
  37.       gets(tmp);
  38.       n++;
  39.     }
  40.   } while (strlen(mslop));
  41. }
  42.  
  43. void timkiem()
  44. {
  45.   char mslop[5];
  46.   int i = 0, found = 0;
  47.   printf("\nCho biet ma so lop : ");
  48.   gets(mslop);
  49.   if (strlen(mslop))
  50.     while (i<n)
  51.       if (stricmp(danhsach[i].mslop, mslop) == 0)
  52.       {
  53.          printf("\nMa so lop : %s", danhsach[i].mslop);
  54.          printf("\nHo va ten : %s", danhsach[i].hoten);
  55.          printf("\nDiem Toan : %f", danhsach[i].diem[TOAN]);
  56.          printf("\nDiem Ly   : %f", danhsach[i].diem[LY]);
  57.          printf("\nDiem Hoa  : %f", danhsach[i].diem[HOA]);
  58.          found = 1;
  59.          break;
  60.       }
  61.       else
  62.         i++;
  63.   if (!found)
  64.     printf("\nKhong tim thay!!!");
  65. }
  66.  
  67. void xoa()
  68. {
  69.   char mslop[5], traloi;
  70.   int i = 0, j;
  71.   printf("\nCho biet ma so lop : ");
  72.   gets(mslop);
  73.   if (strlen(mslop))
  74.     while (i<n)
  75.       if (stricmp(danhsach[i].mslop, mslop) == 0)
  76.       {
  77.          printf("\nMa so lop : %s", danhsach[i].mslop);
  78.          printf("\nHo va ten : %s", danhsach[i].hoten);
  79.          printf("\nDiem Toan : %f", danhsach[i].diem[TOAN]);
  80.          printf("\nDiem Ly   : %f", danhsach[i].diem[LY]);
  81.          printf("\nDiem Hoa  : %f", danhsach[i].diem[HOA]);
  82.          printf("\nCo muon xoa khong (C/K)? ");
  83.          do {
  84.            traloi = toupper(getch());
  85.          } while (traloi != 'C' && traloi != 'K');
  86.          putc(traloi, stdout);
  87.          if (traloi == 'C')
  88.          {
  89.            n--;
  90.            memcpy(&danhsach[i], &danhsach[i+1], sizeof(struct sinhvien) * (n-i));
  91.            break;
  92.          }
  93.       }
  94.       else
  95.         i++;
  96. }
  97.  
  98. void menu()
  99. {
  100.   printf("\n***************");
  101.   printf("\n* 1. Them     *");
  102.   printf("\n* 2. Xoa      *");
  103.   printf("\n* 3. Tim kiem *");
  104.   printf("\n* 0. Thoat    *");
  105.   printf("\n***************");
  106.   printf("\nChon lua ? ");
  107. }
  108.  
  109. void main()
  110. {
  111.   char traloi;
  112.   do {
  113.     menu();
  114.     do {
  115.       traloi = getch();
  116.     } while (traloi < '0' || traloi > '3');
  117.     putc(traloi, stdout);
  118.     switch (traloi)
  119.     {
  120.        case '1' : nhapmoi();
  121.                   break;
  122.        case '2' : xoa();
  123.                   break;
  124.        case '3' : timkiem();
  125.                   break;
  126.     }
  127.   } while (traloi != '0');
  128. }

 8:GIẢI PHƯƠNG TRÌNH BẬC HAI

  1. #include <stdio.h>
  2. #include <math.h>
  3. void main()
  4. {
  5. float a, b, c, delta;
  6.  
  7. printf("\nGiai phuong trinh bac hai AXý + BX + C = 0");
  8. printf("\nCho biet ba he so A B C : ");
  9. scanf("%f%f%f", &a, &b, &c);
  10.  
  11. delta = b * b - 4 * a * c;
  12. if (delta<0)
  13. printf("Phuong trinh vo nghiem");
  14. else if (delta == 0)
  15. printf("Phuong trinh co nghiem kep x1 = x2 = %f", -b/(2*a));
  16. else
  17. {
  18. printf("Phuong trinh co hai nghiem phan biet\nx1 = %f", (-b + sqrt(delta))/(2*a));
  19. printf("\nx2 = %f", (-b - sqrt(delta))/(2*a));
  20. }
  21. getch();
  22. }

 9:MA PHƯƠNG

  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. // func declaration
  5. void matrix( int n );
  6.  
  7. // main()
  8. int main(void)
  9. {
  10.     int n;
  11.    
  12.     // input until it's valid.
  13.     do
  14.     {
  15.     printf("\n Plz input size of matrix [ odd size & n < 20 ]: n = ");
  16.     scanf("%d",&n);
  17.     if ( n % 2 == 0 ) printf("\n Invalid input value .. Plz re-input ... \n");
  18.     }
  19.     while ( n % 2 == 0 );
  20.    
  21.     if ( n > 20 ) { n = 19 ; // in case of n is greater than 20
  22.     printf("\n %d is greater than 20 & set to be default as 19 .",n ); } // end if
  23.    
  24.     // call matrix()
  25.     matrix(n);
  26.     // stop to watch
  27.     getch();
  28.     return 0;
  29. }
  30.  
  31. // function matrix(int n)
  32. void matrix( int n )
  33. {
  34.      int a[20][20];
  35.      int i, j, row, col, count = 1;
  36.      int old_row, old_col, sum = 0;
  37.      
  38.      // set starting value of array
  39.      for ( i = 0 ; i < n ; i++ )
  40.      for ( j = 0 ; j < n ; j++ )
  41.          a[i][j] = 0;
  42.      
  43.      // set the 1st value to start
  44.      row = 0; col = (n-1) / 2;
  45.      
  46.      while ( count < n*n + 1 )
  47.      {
  48.            a[row][col] = count++ ; // set value for elements
  49.            old_row = row ; old_col = col; // save the last addresses
  50.            // define whether going out of array
  51.            row -= 1; if ( row == -1 ) row = n - 1;
  52.            col += 1; if ( col == n ) col = 0;
  53.            // in case of already having number
  54.            if ( a[row][col] != 0 )
  55.            {
  56.                 row = old_row + 1;
  57.                 col = old_col;
  58.            } // end if
  59.      } // end while
  60.      // print result
  61.      printf("\n");
  62.      for ( i = 0 ; i < n ; i++ )
  63.      {
  64.      for ( j = 0 ; j < n ; j++ )
  65.          printf("%4d",a[i][j]);
  66.      printf("\n");
  67.      } // end for
  68.      
  69.      // calculate sum
  70.      for ( j = 0 ; j < n ; j++ )
  71.          sum += a[0][j];
  72.      printf("\n Sum of each row - column - diagonal line is : %d " , sum);
  73.      
  74.      return;
  75. }

 10:FILE VÀ HỆ THỐNG

1. Xóa 1 file dùng Remove

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. remove("d:/urls1.dat");
  6.  
  7. return 0;
  8. }

2. Xóa 1 File dùng Unlink
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. remove("C:/pete.txt");
  6.  
  7. return 0;
  8. }
3. Cho biết thông tin FAT

  1. #include <stdio.h>
  2. #include <dos.h>
  3.  
  4. void main(void)
  5. {
  6. struct fatinfo fat;
  7.  
  8. getfatd(&fat);
  9.  
  10. printf("Sectors per cluster %d\n", fat.fi_sclus);
  11. printf("Clusters per disk %u\n", fat.fi_nclus);
  12. printf("Bytes per cluster %d\n", fat.fi_bysec);
  13. printf("Disk type %x\n", fat.fi_fatid & 0xFF);
  14. }
4. Đếm tần suất 1 kí tự trong 1 file
  1. # include <stdio.h>
  2. # include <string.h>
  3. main()
  4. {
  5.  FILE *fp;
  6.  char in[100];
  7.  long int freq[257];
  8.  int i;
  9.  
  10.  printf("\nFile frequency table generator\n\n");
  11.  
  12.  printf("\nInput file:");
  13.  scanf("%s",in);
  14.  fp=fopen(in,"rb");
  15.  if(fp==NULL)
  16.  {
  17.   printf("\nCould not open input file.Aborting\n");
  18.   return 1;
  19.  }
  20.  for(i=0;i<257;i++)
  21.   freq[i]=0;
  22.  while(i=fgetc(fp),i!=EOF)
  23.  {
  24.   freq[i]++;
  25.  }
  26.  fcloseall();
  27.  fp=fopen("count.txt","w");
  28.  fprintf(fp,"\nCharacter frequency table of %s\n",in);
  29.  fprintf(fp,"\nCharacter ASCII frequency\n\n");
  30.  for(i=0;i<256;i++)
  31.  {
  32.   if(i==26)
  33.   {
  34.    fprintf(fp,"\t    26\t  %ld\n",freq[26]);
  35.   }
  36.   else if(i==9)
  37.   {
  38.    fprintf(fp,"\t    9\t  %ld",freq[9]);
  39.   }
  40.   else if(i<10)
  41.   {
  42.    fprintf(fp,"%c\t    %d\t  %ld\n",i,i,freq[i]);
  43.   }
  44.   else if(i<100)
  45.   {
  46.    fprintf(fp,"%c\t    %d\t  %ld\n",i,i,freq[i]);
  47.   }
  48.   else
  49.   {
  50.    fprintf(fp,"%c\t    %d\t  %ld\n",i,i,freq[i]);
  51.   }
  52.  }
  53.  
  54.  fcloseall();
  55.  printf("\nFrequency table copied to count.txt\n");
  56. }
5. Đọc nội dung 1 file
  1. #include <stdio.h>
  2.  
  3. void main(void)
  4. {
  5. FILE *fp;
  6. char ch;
  7.  
  8. fp = fopen("websites.txt","r");
  9. ch = getc(fp);
  10. while(ch!=EOF)
  11. {
  12. putchar(ch);
  13. ch = getc(fp);
  14. }
  15. printf("\n\n");
  16. }

6. Chọn ổ đĩa trong DOS
  1. #include <stdio.h>
  2. #include <dir.h>
  3.  
  4. void main(void)
  5. {
  6. int drives;
  7.  
  8. drives = setdisk(3);
  9. printf("The number of available drives is %d\n", drives);
  10. }
7.Chọn ổ đĩa trong WINS
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. void main(void)
  6.  {
  7.     char szBuffer[MAX_PATH+100];
  8.     UINT nDrive, AvailDrive = 0;
  9.     int dwLogicalDrives = GetLogicalDrives();
  10.     DWORD Success;
  11.  
  12.     printf("Number of logical drives: %d\n", dwLogicalDrives);
  13.         for (nDrive = 0; nDrive < 32; nDrive++)
  14.        {
  15.         if (dwLogicalDrives & (1 << nDrive))
  16.           { // Is drive available?
  17.             AvailDrive++;
  18.             // Get disk information.
  19.             wsprintf(szBuffer, "%c:\\", nDrive+'A', '\0');
  20.             // Print out information.
  21.              if(SetCurrentDirectory(szBuffer))
  22.               printf("%s Is Now Current\n", szBuffer);
  23.              else
  24.               printf("Could not set %s as the current drive\n", szBuffer);
  25.           }
  26.        }
  27.       printf("Number of drives available: %d\n", AvailDrive);
  28.  
  29.  }
8. Cho biết kích thước 1 file
  1. #include <stdio.h>
  2. #include <io.h>
  3. #include <fcntl.h>
  4. #include <sys\stat.h>
  5.  
  6. int main()
  7. {
  8. int fp;
  9.  
  10. long file_size;
  11.  
  12. if ((fp = open("f:/cprojects/urls.txt", O_RDONLY)) == -1)
  13. printf("Error opening the file \n");
  14. else
  15. {
  16. file_size = filelength(file_handle);
  17. printf("The file size in bytes is %ld\n", file_size);
  18. close(fp);
  19. }
  20. return 0;
  21. }

Bạn có đam mê ngành thiết kế vi mạch và bạn muốn có mức lương 1000 usd cùng lúc bạn

đang muốn tìm một Trung tâm để học vậy hãy đến với ngành vi mạch tại SEMICON

  HotLine: 0972 800 931 Ms Duyên

 

Last Updated ( Sunday, 04 October 2015 20:30 )  

Related Articles

Chat Zalo