2013年10月19日 星期六

TQC+ C 結構 501 ~ 510

TQC+ C 試題總覽:LINK
Github 備份:LINK

#include <stdio.h>
#include <stdlib.h>
int main ()
{
struct student {
char name[10];
int score;
}; /* 宣告結構後要分號 */
struct student st1;
printf("請輸入姓名:");
scanf("%s", &st1.name);
/* name是宣告在student的結構內,所以必須呼叫 */
printf("請輸入分數:");
scanf("%d", &st1.score);
printf("%s的成績是%d\n", st1.name, st1.score);
if (st1.score > 60)
printf("恭喜您通過了");
else
printf("抱歉您被當了");
system("PAUSE");
return 0;
}
view raw TQCplus_C_501 hosted with ❤ by GitHub
#include <stdio.h>
#include <stdlib.h>
int main ()
{
struct circle {
int x, y;
double radius;
}; /* 結構的宣告後面要有分號 */
double area;
struct circle c1={10, 10, 2.5};
area = c1.radius * c1.radius * 3.14159;
/* 補上結構的宣告c1.??? */
printf("此圓的圓心為(%d, %d), 面積為%.2f\n", c1.x, c1.y, area);
system("PAUSE");
return 0;
}
view raw TQCplus_C_502 hosted with ❤ by GitHub
#include <stdio.h>
#include <stdlib.h>
int main ()
{
struct circle {
int x, y;
double radius;
};
double area;
struct circle c1={10, 10, 2.5};
struct circle *pc=&c1; /* 這邊已經透過指標將pc指向c1了 */
area = (*pc).radius * (*pc).radius * 3.14159;
/* 此時,我們必須透過括號,先用(*pc)將的結構取出,再呼叫裡面的項目
* 如果使用*pc.radius有點意味*(pc.radius),但這樣取不出來
*/
printf("此圓的圓心為(%d, %d), 面積為%.2f\n", (*pc).x, (*pc).y, area);
system("PAUSE");
return 0;
}
view raw TQCplus_C_503 hosted with ❤ by GitHub
/* TQC+ C - 504 */
#include <stdio.h>
#include <stdlib.h>
int callarea(struct rect *pr);
struct rect {
int length, width;
};
int main ()
{
int area;
struct rect r1={20, 18};
area = callarea(&r1);
printf("此矩形的長為%d, 寬為%d, 面積為%d\n", r1.length, r1.width, area);
system("PAUSE");
return 0;
}
int callarea(struct rect *pr)
{
int area;
area = pr->length * pr->width;
/* 上面這行的意思有如area = (*pr).length * (*pr).width;
* 先取出結構後,呼叫內部的項目
*/
return area;
}
view raw TQCplus_C_504 hosted with ❤ by GitHub
/* TQC+ C - 505 */
#include <stdio.h>
#include <stdlib.h>
int callarea(struct rect *pr);
struct rect {
int length, width;
};
int main ()
{
int area;
struct rect r1={20, 18};
area = callarea(&r1); /* 補上&的參照 */
printf("此矩形的長為%d, 寬為%d, 面積為%d\n", r1.length, r1.width, area);
system("PAUSE");
return 0;
}
int callarea(struct rect *pr)
{
int area;
area = pr->length * pr->width;//把原本的.改成->,因為這邊是指標指向的
return area;
}
view raw TQCplus_C_505 hosted with ❤ by GitHub
/* TQC+ C - 506 */
#include <stdio.h>
#include <stdlib.h>
struct circle {
int x, y;
double radius;
};
double callarea(struct circle *pr);
int main ()
{
double area;
struct circle c1={10, 10, 8.5};
area = callarea(&c1); /* 補上&參照 */
printf("此圓的圓心為(%d, %d), 半徑為%.2f, 面積為%.2f\n",
c1.x, c1.y, c1.radius, area);
/* radius是宣告在callarea裡面的所以必須從裡面呼叫 */
system("PAUSE");
return 0;
}
double callarea(struct circle *pr) /* 宣告的形態要一致,改成double */
{
double area;//一併改成double
area = pr->radius * pr->radius * 3.14159;
return area;
}
view raw TQCplus_C_506 hosted with ❤ by GitHub
/* TQC+ C - 507 */
#include <stdio.h>
#include <stdlib.h>
struct company {
char name[10];
int hour;
double pay;
};
int main ()
{
double salary[5];
int i;
/* 透過結構在宣告時,不須加上struct字樣 */
company employee[5]={
/* 在這邊,因為有五個元素的陣列,所以括號要分配好 */
{"John", 20, 100.34},
{"Mary", 30, 99.78},
{"Peter", 25, 89.45},
{"Nancy", 33, 87.42},
{"Tom", 54, 77.89}
};
for(i = 0; i < 5; i++){
/* 改變邏輯,判斷式僅需要小於5 */
salary[i] = employee[i].hour*employee[i].pay;
/* 陣列的中的index改成動態的i */
printf("%-10s 的薪水為 %.2f\n", employee[i].name,salary[i]);
/* salary[i]不屬於company結構的項目,所以不用透過結構方式來呼叫 */
}
system("PAUSE");
return 0;
}
view raw TQCplus_C_507 hosted with ❤ by GitHub
/* TQC+ C - 508 */
#include <stdio.h>
#include <stdlib.h>
struct company {
char name[10];
int hour;
double pay;
};
int main ()
{
company employee[5];
double salary[5];
int i;
for (i = 0 ; i < 5 ; i++) {
printf("請輸入第%d位員工的姓名: ", i+1);
scanf("%s", &employee[i].name); /* 補上&參照 */
printf("請輸入第%d位員工的工作時數: ", i+1);
scanf("%d", &employee[i].hour); /* 補上&參照 */
printf("請輸入第%d位員工一小時的工資: ", i+1);
scanf("%lf", &employee[i].pay);
/* 補上&參照,並且改成%lf因為要輸入的是double */
}
for(i = 0 ; i < 5 ; i++) {
salary[i]=employee[i].hour*employee[i].pay;
printf("%-10s 的薪水為 %.2f\n", employee[i].name, salary[i]);
/* salary[i]不屬於結構company的項目 */
}
system("PAUSE");
return 0;
}
view raw TQCplus_C_508 hosted with ❤ by GitHub
/* TQC+ C - 509 */
#include <stdio.h>
#include <stdlib.h>
struct triangle {
int width;
int height;
};
int main ()
{
triangle *ptri;
/* 宣告一個triangle的指標 */
double area;
ptri=(triangle*) malloc(sizeof(triangle));
/* 跟系統要記憶體空間
* malloc()用來跟系統要記憶體空間的函數
* 但是因為預設回傳型態是void*
* 所以我們必須自行設定其回傳的型態(triangle*)
* 別看到(triangle*)覺得不習慣,它也是一種型態
* 例如:平常是這樣宣告的int *p,其實這跟int* p意思是一樣的
*/
printf("請輸入三角形的底為多少: ");
scanf("%d", &ptri->width); /* ptri是指標,要呼叫裡面的項目必須用-> */
printf("請輸入三角形的高為多少: ");
scanf("%d", &ptri->height);
area = (ptri->width * ptri->height)/2.;
printf("三角形的面積為%.2f\n", area);
system("PAUSE");
return 0;
}
view raw TQCplus_C_509 hosted with ❤ by GitHub
/* TQC+ C - 510 */
#include <stdio.h>
#include <stdlib.h>
struct circle {
int x, y;
int radius;
struct circle *next;
};
int main ()
{
circle *a, *b, *c, *current;
a = (circle *)malloc(sizeof(circle));
printf("請輸入第一個圓的圓心(x, y): ");
scanf("%d %d", &a->x, &a->y);
printf("請輸入第一個圓的半徑: ");
scanf("%d", &a->radius);
a->next = NULL;
/* 第一個寫入後,因不確定有沒有下一個,先將next值寫成NULL */
b = (circle *)malloc(sizeof(circle));
printf("請輸入第二個圓的圓心(x, y): ");
scanf("%d %d", &b->x, &b->y);
printf("請輸入第二個圓的半徑: ");
scanf("%d", &b->radius);
b->next = NULL; /* 因不確定有沒有下一個,先將next值寫成NULL */
a->next = b; /* 確定有上一個後,在將上一個的next寫成自己位置 */
c = (circle *)malloc(sizeof(circle));
printf("請輸入第三個圓的圓心(x, y): ");
scanf("%d %d", &c->x, &c->y);
printf("請輸入第三個圓的半徑: ");
scanf("%d", &c->radius);
c->next=NULL; /* 因不確定有沒有下一個,先將next值寫成NULL */
b->next=c; /* 確定有上一個後,在將上一個的next寫成自己位置 */
/* 配置方向寫顛倒了,請修正 */
current = a ;
int i=1;
while (current != NULL) {
printf("第%d個圓的圓心為(%d, %d), 半徑為%d\n",i, current->x, current->y, current->radius);
i++;
current = current->next;//做完一輪後,要把記憶體位置導向下一個
}
free(a); /* 釋放記憶體 */
free(b); /* 釋放記憶體 */
free(c); /* 釋放記憶體 */
system("PAUSE");
return 0;
}
view raw TQCplus_C_510 hosted with ❤ by GitHub


TQC+ C 試題總覽:LINK
Github 備份:LINK


本篇教學的程式碼皆由筆者編輯,歡迎轉貼本教學,但請全文轉貼,謝啦~

沒有留言:

張貼留言