引言
C言語作為一種歷史長久且功能富強的編程言語,至今仍被廣泛利用於體系軟體、嵌入式體系、操縱體系等範疇。控制C言語不只有助於懂得打算機的任務道理,還能晉升編程技能。本文將介紹10個實用的C言語項目挑釁,幫助你輕鬆控制C言語編程。
項目一:打算器
項目描述
編寫一個簡單的打算器順序,支撐加、減、乘、除四種基本運算。
代碼示例
#include <stdio.h>
int main() {
char operator;
double firstNumber, secondNumber;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &firstNumber, &secondNumber);
switch (operator) {
case '+':
printf("%.1lf + %.1lf = %.1lf", firstNumber, secondNumber, firstNumber + secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", firstNumber, secondNumber, firstNumber - secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", firstNumber, secondNumber, firstNumber * secondNumber);
break;
case '/':
if (secondNumber != 0.0)
printf("%.1lf / %.1lf = %.1lf", firstNumber, secondNumber, firstNumber / secondNumber);
else
printf("Division by zero is not allowed.");
break;
default:
printf("Invalid operator!");
}
return 0;
}
項目二:冒泡排序
項目描述
實現一個冒泡排序演算法,對一組整數停止排序。
代碼示例
#include <stdio.h>
void bubbleSort(int array[], int size) {
int i, j, temp;
for (i = 0; i < size - 1; i++) {
for (j = 0; j < size - i - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
int main() {
int array[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(array) / sizeof(array[0]);
bubbleSort(array, size);
printf("Sorted array: \n");
for (int i = 0; i < size; i++)
printf("%d ", array[i]);
printf("\n");
return 0;
}
項目三:打算階乘
項目描述
編寫一個函數,打算一個給定整數的階乘。
代碼示例
#include <stdio.h>
long long factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int number;
printf("Enter a positive integer: ");
scanf("%d", &number);
printf("Factorial of %d = %lld", number, factorial(number));
return 0;
}
項目四:轉換單位
項目描述
編寫一個順序,將攝氏度轉換為華氏度,或將華氏度轉換為攝氏度。
代碼示例
#include <stdio.h>
double celsiusToFahrenheit(double celsius) {
return (celsius * 9 / 5) + 32;
}
double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
int main() {
double celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%lf", &celsius);
fahrenheit = celsiusToFahrenheit(celsius);
printf("%.2lf Celsius = %.2lf Fahrenheit\n", celsius, fahrenheit);
printf("Enter temperature in Fahrenheit: ");
scanf("%lf", &fahrenheit);
celsius = fahrenheitToCelsius(fahrenheit);
printf("%.2lf Fahrenheit = %.2lf Celsius\n", fahrenheit, celsius);
return 0;
}
項目五:文件複製
項目描述
編寫一個順序,將一個文件的內容複製到另一個文件中。
代碼示例
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
FILE *source, *destination;
if (argc != 3) {
printf("Usage: %s <source file> <destination file>\n", argv[0]);
exit(1);
}
source = fopen(argv[1], "r");
if (source == NULL) {
perror("Error opening source file");
exit(1);
}
destination = fopen(argv[2], "w");
if (destination == NULL) {
perror("Error opening destination file");
fclose(source);
exit(1);
}
char ch;
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination);
}
fclose(source);
fclose(destination);
printf("File copied successfully.\n");
return 0;
}
項目六:猜數字遊戲
項目描述
編寫一個猜數字遊戲,順序隨機生成一個1到100之間的整數,用戶實驗猜想這個數字,順序提示用戶猜想的數字是太高還是太低。
代碼示例
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number, guess, attempts = 0;
srand(time(NULL));
number = rand() % 100 + 1;
printf("Guess the number between 1 and 100: ");
while (1) {
scanf("%d", &guess);
attempts++;
if (guess < number)
printf("Too low, try again: ");
else if (guess > number)
printf("Too high, try again: ");
else {
printf("Congratulations! You guessed the number in %d attempts.\n", attempts);
break;
}
printf("Guess the number between 1 and 100: ");
}
return 0;
}
項目七:打算器高等版
項目描述
在項目一的基本上,增加以下功能:支撐三角函數(正弦、餘弦、正切)、指數運算、對數運算。
代碼示例
#include <stdio.h>
#include <math.h>
int main() {
char operator;
double firstNumber, secondNumber;
printf("Enter an operator (+, -, *, /, sin, cos, tan, exp, log): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &firstNumber, &secondNumber);
switch (operator) {
case '+':
printf("%.1lf + %.1lf = %.1lf", firstNumber, secondNumber, firstNumber + secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", firstNumber, secondNumber, firstNumber - secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", firstNumber, secondNumber, firstNumber * secondNumber);
break;
case '/':
if (secondNumber != 0.0)
printf("%.1lf / %.1lf = %.1lf", firstNumber, secondNumber, firstNumber / secondNumber);
else
printf("Division by zero is not allowed.");
break;
case 's':
printf("sin(%.2lf) = %.2lf", firstNumber, sin(firstNumber));
break;
case 'c':
printf("cos(%.2lf) = %.2lf", firstNumber, cos(firstNumber));
break;
case 't':
printf("tan(%.2lf) = %.2lf", firstNumber, tan(firstNumber));
break;
case 'e':
printf("exp(%.2lf) = %.2lf", firstNumber, exp(firstNumber));
break;
case 'l':
if (firstNumber > 0)
printf("log(%.2lf) = %.2lf", firstNumber, log(firstNumber));
else
printf("Logarithm of negative number is not allowed.");
break;
default:
printf("Invalid operator!");
}
return 0;
}
項目八:老師成績管理體系
項目描述
編寫一個簡單的老師成績管理體系,支撐增加老師信息、錄入成績、查詢成績、統計均勻分等功能。
代碼示例
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char name[50];
int score;
} Student;
Student students[100];
int studentCount = 0;
void addStudent() {
if (studentCount < 100) {
printf("Enter student's name: ");
scanf("%s", students[studentCount].name);
printf("Enter student's score: ");
scanf("%d", &students[studentCount].score);
studentCount++;
} else {
printf("Student list is full.\n");
}
}
void printStudents() {
printf("Name\tScore\n");
for (int i = 0; i < studentCount; i++) {
printf("%s\t%d\n", students[i].name, students[i].score);
}
}
void calculateAverage() {
int sum = 0;
for (int i = 0; i < studentCount; i++) {
sum += students[i].score;
}
printf("Average score: %.2lf\n", (double)sum / studentCount);
}
int main() {
int choice;
while (1) {
printf("\n1. Add student\n");
printf("2. Print students\n");
printf("3. Calculate average score\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
printStudents();
break;
case 3:
calculateAverage();
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
項目九:溫度轉換器
項目描述
編寫一個順序,容許用戶輸入一個溫度值,然後抉擇轉換到攝氏度或華氏度。
代碼示例
#include <stdio.h>
double celsiusToFahrenheit(double celsius) {
return (celsius * 9 / 5) + 32;
}
double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
int main() {
double temperature, convertedTemperature;
char choice;
printf("Enter temperature: ");
scanf("%lf", &temperature);
printf("Enter 'C' to convert to Celsius or 'F' to convert to Fahrenheit: ");
scanf(" %c", &choice);
if (choice == 'C' || choice == 'c') {
convertedTemperature = celsiusToFahrenheit(temperature);
printf("%.2lf Celsius = %.2lf Fahrenheit\n", temperature, convertedTemperature);
} else if (choice == 'F' || choice == 'f') {
convertedTemperature = fahrenheitToCelsius(temperature);
printf("%.2lf Fahrenheit = %.2lf Celsius\n", temperature, convertedTemperature);
} else {
printf("Invalid choice!\n");
}
return 0;
}
項目十:淺易文本編輯器
項目描述
編寫一個淺易的文本編輯器,支撐以下功能:創建文件、打開文件、保存文件、編輯文本內容。
代碼示例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 1024
char buffer[MAX_SIZE];
void createFile() {
char filename[50];
printf("Enter filename: ");
scanf("%s", filename);
FILE *file = fopen(filename, "w");
if (file == NULL) {
perror("Error creating file");
return;
}
printf("Enter text: ");
fgets(buffer, MAX_SIZE, stdin);
fputs(buffer, file);
fclose(file);
printf("File created successfully.\n");
}
void openFile() {
char filename[50];
printf("Enter filename: ");
scanf("%s", filename);
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
return;
}
while (fgets(buffer, MAX_SIZE, file)) {
printf("%s", buffer);
}
fclose(file);
}
void saveFile() {
char filename[50];
printf("Enter filename: ");
scanf("%s", filename);
FILE *file = fopen(filename, "w");
if (file == NULL) {
perror("Error opening file");
return;
}
printf("Enter text: ");
fgets(buffer, MAX_SIZE, stdin);
fputs(buffer, file);
fclose(file);
printf("File saved successfully.\n");
}
void editFile() {
char filename[50];
printf("Enter filename: ");
scanf("%s", filename);
FILE *file = fopen(filename, "r+");
if (file == NULL) {
perror("Error opening file");
return;
}
while (fgets(buffer, MAX_SIZE, file)) {
printf("%s", buffer);
}
printf("Enter new text: ");
fgets(buffer, MAX_SIZE, stdin);
fseek(file, 0, SEEK_SET);
fputs(buffer, file);
fclose(file);
printf("File edited successfully.\n");
}
int main() {
int choice;
while (1) {
printf("\n1. Create file\n");
printf("2. Open file\n");
printf("3. Save file\n");
printf("4. Edit file\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
createFile();
break;
case 2:
openFile();
break;
case 3:
saveFile();
break;
case 4:
editFile();
break;
case 5:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
經由過程以上10個C言語項目挑釁,你可能輕鬆控制C言語編程的基本技能。在現實編程過程中,壹直練習跟實驗新的項目,將有助於進步你的編程程度。