Skip to content

Instantly share code, notes, and snippets.

View ruyut's full-sized avatar
🤪

Ruyut ruyut

🤪
View GitHub Profile
//904-2
#include<stdlib.h>
#include<stdio.h>
int main() {
FILE *fi;//宣告檔案指標
char name[20];//宣告字元陣列
int score;//宣告變數
fi = fopen("score.dat", "r"); //使用讀取的方式開啟檔名為score.dat的檔案
while (fscanf(fi, "%s %d", name, &score) != EOF)//如果讀不到資料時跳出
printf("%s的C語言分數是%d\n", name, score); //列印名字和分數
//904-1
#include<stdlib.h>
#include<stdio.h>
int main() {//r讀取 w寫入 a讀寫
FILE *fi;//宣告檔案指標
char name[20];//宣告字元陣列
int score;//宣告變數
fi = fopen("score.dat", "w");//使用寫入的方式開啟檔名為score.dat的檔案
do {
printf("請輸入學生的姓名(分數輸入負的分數時結束):");
//903
#include<stdlib.h>
#include<stdio.h>
int greater60(int*, int);//宣告整數greater60,要傳入整數記憶體位址和整數
int main() {
int data[6];//宣告整數陣列
int i;//宣告整數
for (i = 0; i<6; i++) {//i從0開始到5,共會出現6次
printf("請輸入第%d個分數:", i + 1);//顯示文字和i+1(1~6)
scanf("%d", &data[i]);//讓使用者輸入數字
//902
#include<stdlib.h>
#include<stdio.h>
int main() {
int i, c[6];//宣告整數,宣告整數陣列
c[0] = c[1] = c[2] = c[3] = c[4] = c[5] = 0;//整數陣列設為0
for (i = 0; i<6; i++) {//從0開始到5 會出現0 1 2 3 4 5 共6次
c[i] = rand() % 49 + 1;//整數陣列的第i個等於亂數產生0~48,+1之後變成1~49
while ((c[i] == c[0] && i != 0) || (c[i] == c[1] && i != 1) || (c[i] == c[2] && i != 2) || (c[i] == c[3] && i != 3) || (c[i] == c[4] && i != 4) || (c[i] == c[5] && i != 5)) {
//如果整數陣列中第i個等於第0~5個的話
//901-2
#include<stdlib.h>
#include<stdio.h>
void exchange(double*, double*);//宣告副程式,名字叫exchange
//呼叫時要傳入兩個雙精浮點數的記憶體位置
int main() {
double c, d;//宣告兩個雙精浮點數c和d
printf("請輸入兩個浮點數(數字跟數字中間請以空白間隔)");//列印文字
scanf("%lf %lf", &c, &d);//讓使用者輸入兩個數字
printf("c:%lf d:%lf\n", c, d);//列印變數
//901-1
#include<stdlib.h>
#include<stdio.h>
void exchange(int*, int*);//宣告副程式,名字叫exchange
//呼叫時要傳入兩個整數的記憶體位置
int main() {
int a, b;//宣告兩個整數
printf("請輸入兩個數字(數字跟數字中間請以空白間隔)");//列印文字
scanf("%d %d", &a, &b);//讓使用者輸入兩個數字
printf("a:%d b:%d\n", a, b);//列印出變數
//805-2
#include<stdlib.h>
#include<stdio.h>
int main() {
int arr[5][2], i, j, tot = 0;//宣告arr為整數二維陣列
for (i = 0; i<5; i++) {
printf("請輸入兩個數字(數字之間請以空白為間隔):");
for (j = 0; j<2; j++) {
scanf("%d", &arr[i][j]);//把整數存入arr陣列的記憶體位址中
tot += arr[i][j];//把每個輸入的數都加到tot中(計算總合)
//805-1
#include<stdlib.h>
#include<stdio.h>
int main() {
int arr[3][4], i, j, tot = 0;//宣告arr為整數二維陣列
for (i = 0; i<3; i++) {//0,1,2
printf("請輸入四個數字(數字之間請以空白為間隔):");
for (j = 0; j<4; j++) {//控制行
scanf("%d", &arr[i][j]);//把整數存入arr陣列的記憶體位址中
tot += arr[i][j];//把每個輸入的數都加到tot中(計算總合)
//804
#include<stdlib.h>
#include<stdio.h>
double average(double*, int);//宣告副程式average是一個雙精浮點數
//使用時要回傳 一個雙精浮點數的記憶體位址和一個整數
int main() {
int i;//宣告整數
double data[6];//宣告雙精浮點數陣列
for (i = 0; i<6; i++) {//迴圈0~5 共會執行6次
printf("請輸入第%d個浮點數:", i + 1);//顯示文字和整數i
//803
#include<stdlib.h>
#include<stdio.h>
int main() {
int i = 0, a = 0, b = 0, c = 0;//宣告整數變數
int tk;//宣告tk是一個整數
while (i<10) {//i從0開始到9共有10票
printf("(1)小蔡\n(2)小王\n(3)小史\n請你投票:");//列印
scanf("%d", &tk);//輸入整數 &為取址
if (tk == 1)a++;//如果投1號,a+1