第十一章--指针的孪生兄弟
2019-12-17 本文已影响0人
小小Henry
1找出按字典顺序排在最前面的国名
#include <stdio.h>
#include <string.h>
int main()
{
char* country[5][10];
char* pmin;
pmin = *country;
printf("Input five countries' names:\n");
for (int i = 0; i < 5; i++)
{
gets(country[i]);
if (strcmp(country[i], pmin) < 0)
pmin = *(country + i);
}
printf("The minimum is:%s\n", pmin);
return 0;
}
2学生成绩管理系统V2.0
还是这段代码,依然没通过,希望哪位大神看见我的错误了告诉我声。感激不尽
#include <stdio.h>
#include <string.h>
void Display(long int number[], int scores[], int students);
void Input(long int number[], int scores[], int students);
void GetSumAver(const int scores[], int students);
void SortByScore(int long number[], int scores[], int students, int flag);
void SortByNumber(int long number[], int scores[], int students);
void Search(const long int number[], const int scores[], int students);
void Rate(int scores[], int students);
int main()
{
int students;
long int number[30];
int scores[30];
printf("Input student number(n<30):\n");
scanf("%d", &students);
Display(number, scores, students);
return 0;
}
void Display(long int number[], int scores[], int students)
{
int choice;
do
{
printf("Management for Students' scores\
\n1.Input record\
\n2.Caculate total and average score of course\
\n3.Sort in descending order by score\
\n4.Sort in ascending order by score\
\n5.Sort in ascending order by number\
\n6.Search by number\
\n7.Statistic analysis\
\n8.List record\
\n0.Exit\
\nPlease Input your choice:\n");
scanf("%d", &choice);
switch (choice)
{
case 1: Input(number, scores, students); break;
case 2: GetSumAver(scores, students); break;
case 3: SortByScore(number, scores, students, 1); break;
case 4: SortByScore(number, scores, students, 0); break;
case 5: SortByNumber(number, scores, students); break;
case 6: Search(number, scores, students); break;
case 7: Rate(scores, students); break;
case 0: printf("End of program!\n"); break;
default:printf("Input error!\n");
}
} while (choice);
}
void Input(long int number[], int scores[], int students)
{
printf("Input student's ID and score:\n");
for (int i = 0; i < students; i++)
{
scanf("%ld", number + i);
scanf("%d", scores + i);
}
}
void GetSumAver(const int scores[], int students)
{
float aver;
int sum=0;
for (int i = 0; i < students; i++)
sum += *(scores + i);
aver = (float)sum / students;
printf("sum=%d,aver=%.2f\n", sum, aver);
}
void SortByScore(long int number[], int scores[], int students, int flag)
{
int tempn = *number, temps = *scores;
for (int i=0; i<students-1; i++)
for (int j = 0; j < students -i-1; j++)
{
if (*(scores + j) < *(scores + j+1))
{
tempn = *(number + j); temps = *(scores + j);
*(number + j) = *(number + j + 1); *(scores + j) = *(scores + j + 1);
*(number + j + 1) = tempn; *(scores + j + 1) = temps;
}
}
if (flag)
{
printf("Sort in descending order by score:\n");
for (int i = 0; i < students; i++)
printf("%ld\t%d\n", *(number + i), *(scores + i));
}
else
{
printf("Sort in ascending order by score:\n");
for (int i = students - 1; i >= 0; i--)
printf("%ld\t%d\n", *(number + i), *(scores + i));
}
}
void SortByNumber(long int number[],int scores[],int students)
{
long int tempn = *number, temps = *scores;
for (int i = 0; i < students - 2; i++)
for (int j = 0; j < students -i - 1; j++)
{
if (*(number + j) > *(number + j + 1))
{
tempn = *(number + j); temps = *(scores + j);
*(number + j) = *(number + j + 1); *(scores + j) = *(scores + j + 1);
*(number + j + 1) = tempn; *(scores + j + 1) = temps;
}
}
printf("Sort in ascending order by number:\n");
for (int i = 0; i < students; i++)
{
printf("%ld\t%d\n", *(number + i), *(scores + i));
}
}
void Search(const long int number[],const int scores[], int students)
{
long int a;
printf("Input the number you want to search:\n");
scanf("%ld", &a);
for (int i = 0; i < students; i++)
{
if (*(number + i) == a)
{
printf("%ld\t%d\n", a, *(scores + i));
return;
}
}
printf("Not found!\n");
}
void Rate(int scores[], int students)
{
int r[6] = { 0 };
int a;
for (int i = 0; i < students; i++)
{
a = (*(scores + i));
if (a < 60)
(*r)++;
else if (a < 70)
(*(r + 1))++;
else if (a < 80)
(*(r + 2))++;
else if (a < 90)
(*(r + 3))++;
else if (a < 100)
(*(r + 4))++;
else if (a == 100)
(*(r + 5))++;
}
printf("<60\t%d\t%.2f%%\
\n60-69\t%d\t%.2f%%\
\n70-79\t%d\t%.2f%%\
\n80-89\t%d\t%.2f%%\
\n90-99\t%d\t%.2f%%\
\n100\t%d\t%.2f%%\n", r[0], 100.0 * r[0] / students, r[1], 100.0 * r[1] / students, r[2], 100.0 * r[2] / students, r[3], 100.0 * r[3] / students, r[4], 100.0 * r[4] / students, r[5], 100.0 * r[5] / students);
}
3月份表示
# include <stdio.h>
int main()
{
int m;
char *month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
printf("Input month number:\n");
scanf("%d", &m);
if (m < 1 || m >12)
printf("Illegal month\n");
else
printf("month %d is %s\n", m, month[m - 1]);
return 0;
}
4程序改错——1
#include <stdio.h>
#define STUD 30 //最多可能的学生人数
#define COURSE 5 //最多可能的考试科目数
void Total(int* score, int sum[], float aver[], int m, int n);
void Print(int* score, int sum[], float aver[], int m, int n);
int main(void)
{
int i, j, m, n, score[STUD][COURSE], sum[STUD];
float aver[STUD];
printf("Enter the total number of students and courses:\n");
scanf("%d%d", &m, &n);
printf("Enter score:\n");
getchar();
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &score[i][j]);
}
}
Total(score, sum, aver, m, n);
Print(score, sum, aver, m, n);
return 0;
}
void Total(int* score, int sum[], float aver[], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
sum[i] = 0;
for (j = 0; j < n; j++)
{
sum[i] = sum[i] + *(score + i * COURSE + j);
}
aver[i] = (float)sum[i] / n;
}
}
void Print(int* score, int sum[], float aver[], int m, int n)
{
int i, j;
printf("Result:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%4d", *(score + i * COURSE + j));
}
printf("%5d%6.1f\n", sum[i], aver[i]);
}
}
5程序改错——2
#include <stdio.h>
#include <string.h>
#define M 150 /* 最多的字符串个数 */
#define N 10 /* 字符串最大长度 */
void SortString(char* ptr[], int n);
int main()
{
int i, n;
char* pStr[M];
char a[M][N];
printf("How many countries?\n");
scanf("%d", &n);
getchar(); /* 读走输入缓冲区中的回车符 */
printf("Input their names:\n");
for (i = 0; i < n; i++)
{
pStr[i] = (a + i);
gets(pStr[i]); /* 输入n个字符串 */
}
SortString(pStr, n); /* 字符串按字典顺序排序 */
printf("Sorted results:\n");
for (i = 0; i < n; i++)
{
puts(pStr[i]); /* 输出排序后的n个字符串 */
}
return 0;
}
void SortString(char* ptr[], int n)
{
int i, j;
char* temp = NULL;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(ptr[j], ptr[i]) < 0)
{
temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;
}
}
}
}
6找数组最值
#include <stdio.h>
#include <string.h>
#define M 10
void InputArray(int* p, int m, int n);
int FindMax(int* p, int m, int n, int* pRow, int* pCol);
int main()
{
int m, n;
int row, col;
int max;
int p[M][M];
printf("Input m,n:\n");
scanf("%d,%d", &m, &n);
printf("Input %d*%d array:\n", m, n);
InputArray(*p, m, n);
max = FindMax(*p, m, n, &row, &col);
printf("max=%d,row=%d,col=%d\n", max, row, col);
return 0;
}
void InputArray(int* p, int m, int n)
{
for (int i = 0; i < m ; i++)
for (int j = 0; j < n; j++)
{
scanf("%d", (p+i*M + j));
}
}
int FindMax(int* p, int m, int n, int* pRow, int* pCol)
{
int max = *p;
*pRow = *pCol = 0;
for (int i = 0; i < m ; i++)
for (int j =0; j<n; j++)
{
if (*(p+i*M + j) > max)
{
max = *(p + i*M+j);
*pRow = i ;
*pCol = j;
}
}
return max;
}
7冒泡排序
#include <stdio.h>
#include <string.h>
#define N 10
void Input(int *a, int n);
void Sort(int* p, int n);
int main()
{
int n;
int number[N];
printf("Input n:");
scanf("%d", &n);
printf("Input %d numbers:", n);
Input(number, n);
printf("Sorting results:");
Sort(number, n);
return 0;
}
void Input(int* a, int n)
{
for (int i = 0; i < n; i++)
scanf("%d", (a + i));
}
void Sort(int* p, int n)
{
for (int i=0; i<n-1; i++)
for (int j = 0; j < n - i - 1; j++)
{
int temp = 0;
if (*(p + j) > * (p + j + 1))
{
temp = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = temp;
}
}
for (int i = 0; i < n; i++)
printf("%4d", *(p + i));
}
8删除字符串中与某字符相同的字符
#include <stdio.h>
#include <string.h>
#define N 100
void Input(int *a, int n);
void Sort(int* p, int n);
int main()
{
char string[N];
char character;
printf("Input a string:\n");
gets(string);
printf("Input a character:\n");
character = getchar();
for (int i = 0; *(string + i); i++)
{
if (strncmp(&character, (string + i), 1));
else
{
for (int j = i; *(string + j); j++)
*(string + j) = *(string + j + 1);
}
}
printf("Results:%s", string);
return 0;
}
9求最大数和最小数的最大公约数
#include <stdio.h>
#include <string.h>
#define N 10
int handle(int *a, int *max, int *min);
int GCD(int max, int min);
int main()
{
int number[N];
int max, min;
int gcd;
printf("Input 10 numbers:\n");
for (int i = 0; i < 10; i++)
{
scanf("%d", (number + i));
}
gcd = handle(number, &max, &min);
printf("maxNum=%d\nminNum=%d\n", max, min);
if (gcd)
printf("%d\n", gcd);
return 0;
}
int handle(int* a, int *max, int *min)
{
*max = *min = *a;
for (int i = 0; i < N; i++)
{
if (*(a + i) > *max)
*max = *(a + i);
if (*(a + i) < *min)
*min = *(a + i);
}
if (*min)
return GCD(*max, *min);
else
return 0;
}
int GCD(int max, int min)
{
if (max % min)
GCD(min, max % min);
else
return min;
}
10数列合并
#include <stdio.h>
#include <string.h>
#define N 5
void Merge(int a[], int b[], int c[], int m, int n);
int main()
{
int a[N];
int b[N];
int c[N + N];
int m, n;
printf("Input m,n:");
scanf("%d,%d", &m, &n);
printf("Input array a:");
for (int i = 0; i < m; i++)
scanf("%d", a + i);
printf("Input array b:");
getchar();
for (int i = 0; i < n; i++)
scanf("%d", b + i);
Merge(a, b, c, m, n);
return 0;
}
void Merge(int a[], int b[], int c[], int m, int n)
{
int i = 0; int j = 0;
int counter=0;
while (i < m && j < n)
{
if (*(a + i) > * (b + j))
{
*(c + counter) = *(a + i);
i++;
}
else
{
*(c + counter) = *(b + j);
j++;
}
counter++;
}
for (; i < m; i++)
{
*(c + counter) = *(a + i);
counter++;
}
for (; j < n; j++)
{
*(c + counter) = *(b + j);
counter++;
}
for (int i = 0; i < counter; i++)
printf("%4d", *(c + i));
}