一
//化简分式
#include <stdio.h>
void reduce(int numerator, int denominator, int* reduced_numerator, int* reduced_denominator);
int main()
{
int num = 0, den = 0;
int onum=0, oden=0;
int* rnum=&onum, * rden=&oden;
printf("Enter the number(n/d):");
scanf("%d/%d", &num, &den);
reduce(num, den, &onum, &oden);
printf("The reduced number is:%d/%d", onum, oden);
}
void reduce(int numerator, int denominator, int* reduced_numerator, int* reduced_denominator) {
int op = 0;
for (int i = 1; i <= ((numerator > denominator) ? denominator : numerator); i++) {
if (numerator % i == 0 && denominator % i == 0) {
op = i;
}
}
*reduced_numerator = numerator / op;
*reduced_denominator = denominator / op;
}
二
数组
//判断是否是回文
#include <stdio.h>
#include <ctype.h>
int main()
{
char in[100] = { 0 };
int i = 0;
while (1) {
in[i] = getchar();
if (in[i] == '\n')break;
if (isalpha(in[i]) == 0) {
continue;
}
else {
in[i] = tolower(in[i]);
i++;
}
}
char op = 1;
int b = i - 1;
while (i) {
if (in[i - 1] != in[b - i + 1]) {
op = 0;
break;
}
i--;
}
if (op == 1) {
printf("Palindrome\n");
}
else {
printf("Not a palindrome\n");
}
}
指针
#include <stdio.h>
#include <ctype.h>
int main()
{
char in[100] = { 0 };
char* p=in;
char* op = in;
while (1) {
*p = getchar();
if (*p == '\n')break;
if (isalpha(*p) == 0) {
continue;
}
else {
*p = tolower(*p);
p++;
}
}
int out = 1;
while (p != in) {
if (*(p - 1) != *op) {
out = 0;
break;
}
p--;
op++;
}
if (out == 1) {
printf("Palindrome\n");
}
else {
printf("Not a palindrome\n");
}
}
三
//复制指定字符串
#include <stdio.h>
int main()
{
char in[100] = { 0 };
char out[100] = { 0 };
char* p=in;
while ((*p = getchar()) != '\n')p++;
int m, n;
scanf("%d %d", &m, &n);
p = in + m - 1;
for (int i = 0; i < n; i++) {
out[i] = *p;
printf("%c", out[i]);
p++;
}
}
附加
//输入指定数量数据排序
#include <stdio.h>
void sele_sort(int in[], int i, int n);
int main()
{
int n = 0;
printf("Enter the number of number:");
scanf("%d", &n);
int* p;
p = (int*)malloc(sizeof(int) * n);
for (int i = 0; i < n;i++) {
scanf("%d", &p[i]);
}
sele_sort(p, 0, n);
for (int i = 0; i < n; i++) {
printf("%d\n", p[i]);
}
}
void sele_sort(int in[], int i, int n) { //i为排序初始位,n为排序个数
int j, min, t;
if (i < n - 1) {
min = i;
for (j = i + 1; j < n; j++) { //每次递归寻找从i到n个中的最小值并交换放在最前面
if (in[j] < in[min])
min = j; //记录最小值出现的位置
}
t = in[min]; //交换数值
in[min] = in[i];
in[i] = t;
sele_sort(in, i + 1, n); //进入下一次递归
}
}