实验5
include<stdio.h>
define N 5
void input(int x[],int n);
void output(int x[],int n);
void find_min_max(int x[],int n,int *pmin,int *pmax);
int main(){
int a[N];
int min,max;
printf("录入%d个数据:\n",N);
input(a,N);
printf("数据是:\n");
output(a,N);
printf("数据处理......\n");
find_min_max(a,N,&min,&max);
printf("输出结果:");
printf("min=%d,max=%d\n",min,max);
return 0;
}
void input(int x[],int n){
int i;
for(i=0;i<n;++i)
scanf("%d",&x[i]);
}
void output(int x[],int n){
int i;
for(i=0;i<n;++i)
printf("%d",&x[i]);
printf("\n");
}
void find_min_max(int x[],int n,int *pmin,int *pmax){
int i;
*pmin=*pmax=0;
for(i=0;i<n;++i)
if(x[i]<*pmin)
*pmin=x[i];
if(x[i]>*pmax)
*pmax=x[i];
}
1.找到输入的数组中的最大值和最小值
2.pmax指向x[0],pmin指向pmax指向的地址
include<stdio.h>
define N 80
include<string.h>
int main(){
char s1[N]="learning makes me happy";
char s2[N]="learning makes me sleepy";
char tmp[N];
printf("sizeof(s1)vs.strlen(s1):\n");
printf("sizeof(s1)=%d\n",sizeof(s1));
printf("strlen(s1) = %d\n", strlen(s1));
printf("\nbefore swap: \n");
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);
printf("\nswapping...\n");
strcpy(tmp, s1);
strcpy(s1, s2);
strcpy(s2, tmp);
printf("\nafter swap: \n");
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);
return 0;
}
1.find_max的返回值是输入的数组中的最大项
2.可以
include <stdio.h>
include <string.h>
define N 80
int main() {
char s1[N] = "Learning makes me happy";
char s2[N] = "Learning makes me sleepy";
char tmp[N];
printf("sizeof(s1) vs. strlen(s1): \n");
printf("sizeof(s1) = %d\n", sizeof(s1));
printf("strlen(s1) = %d\n", strlen(s1));
printf("\nbefore swap: \n");
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);
printf("\nswapping...\n");
strcpy(tmp, s1);
strcpy(s1, s2);
strcpy(s2, tmp);
printf("\nafter swap: \n");
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);
return 0;
}
1.s1的大小为80字节,sizeof(s1)计算的是s1[N]的字节数,strlen(s1)统计的是实际s1使用的字节数(即除去y后面空格之后的大小)
2.不可以,如果这样命名的话会将所有字符串赋值给s1[0]
3.是
include <stdio.h>
include <string.h>
define N 80
int main() {
char *s1 = "Learning makes me happy";
char *s2 = "Learning makes me sleepy";
char *tmp;
printf("sizeof(s1) vs. strlen(s1): \n");
printf("sizeof(s1) = %d\n", sizeof(s1));
printf("strlen(s1) = %d\n", strlen(s1));
printf("\nbefore swap: \n");
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);
printf("\nswapping...\n");
tmp = s1;
s1 = s2;
s2 = tmp;
printf("\nafter swap: \n");
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);
return 0;
}
1.s1中存储的是char 类型的数组变量,以每一个单词存储为一个返回值,sizeof(s1)计算的是指针*s1的单词和空格的数量和,strlen(s1)统计的是字母和空格的总和。
2.可以。一个是建立数组型变量存储字符串,每一个字母单独存放一个返回值,而task2_2.c则是建立指针,把每一个单词存放为一个返回值。
3.交换的是s1和s2对应的返回值,两个字符串常量在内存中没有交换
include <stdio.h>
int main() {
int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
int i, j;
int ptr1;
int(ptr2)[4];
printf("输出1: 使用数组名、下标直接访问二维数组元素\n");
for (i = 0; i < 2; ++i) {
for (j = 0; j < 4; ++j)
printf("%d ", x[i][j]);
printf("\n");
}
printf("\n输出2: 使用指针变量ptr1(指向元素)间接访问\n");
for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) {
printf("%d ", *ptr1);
if ((i + 1) % 4 == 0)
printf("\n");
}
printf("\n输出3: 使用指针变量ptr2(指向一维数组)间接访问\n");
for (ptr2 = x; ptr2 < x + 2; ++ptr2) {
for (j = 0; j < 4; ++j)
printf("%d ", *(*ptr2 + j));
printf("\n");
}
return 0;
}
int (ptr)[4]中ptr表示建立的指向数组的指针
intptr[4]中ptr表示建立的指针数组。
include <stdio.h>
define N 80
void replace(char *str, char old_char, char new_char);
int main() {
char text[N] = "Programming is difficult or not, it is a question.";
printf("原始文本: \n");
printf("%s\n", text);
replace(text, 'i', '*');
printf("处理后文本: \n");
printf("%s\n", text);
return 0;
}
void replace(char *str, char old_char, char new_char) {
int i;
while(*str) {
if(*str == old_char)
*str = new_char;
str++;
}
}
1.replace 的功能是把所有原始文本的i替换为*。
2.可以
include <stdio.h>
define N 80
char *str_trunc(char *str, char x);
int main() {
char str[N];
char ch;
while(printf("输入字符串: "), gets(str) != NULL) {
printf("输入一个字符: ");
ch = getchar();
printf("截断处理...\n");
str_trunc(str, ch);
printf("截断处理后的字符串: %s\n\n", str);
getchar();
}
return 0;
}
char *str_trunc(char str, char x){
int i;
for(i=0;i<N;++i,str++){
if(str == x){
*str = '\0';
}
}
}
18行用于接收回车符.
include <stdio.h>
include <string.h>
define N 5
int check_id(char *str);
int main()
{
char *pid[N] = {"31010120000721656X",
"3301061996X0203301",
"53010220051126571",
"510104199211197977",
"53010220051126133Y"};
int i;
for (i = 0; i < N; ++i)
if (check_id(pid[i]))
printf("%s\tTrue\n", pid[i]);
else
printf("%s\tFalse\n", pid[i]);
return 0;
}
int check_id(char *str) {
int i;
if(strlen(str)!=18)
return 0;
for(i=0;i<17;i++){
if(str[i] < '0'||str[i] > '9')
return 0;
}
if((str[i] < '0'||str[i] > '9')&& str[i] != 'X')
return 0;
return 1;
}
include <stdio.h>
define N 80
void encoder(char *str, int n);
void decoder(char *str, int n);
int main() {
char words[N];
int n;
printf("输入英文文本: ");
gets(words);
printf("输入n: ");
scanf("%d", &n);
printf("编码后的英文文本: ");
encoder(words, n);
printf("%s\n", words);
printf("对编码后的英文文本解码: ");
decoder(words, n);
printf("%s\n", words);
return 0;
}
void encoder(char *str, int n) {
int i;
for (i = 0; str[i]!= '\0'; i++) {
if (isalpha(str[i])) {
if (islower(str[i])) {
str[i] = ((str[i] - 'a' + n) % 26) + 'a';
} else {
str[i] = ((str[i] - 'A' + n) % 26) + 'A';
}
}
}
}
void decoder(char *str, int n) {
int i;
for (i = 0; str[i]!= '\0'; i++) {
if (isalpha(str[i])) {
if (islower(str[i])) {
str[i] = ((str[i] - 'a' - n + 26) % 26) + 'a';
} else {
str[i] = ((str[i] - 'A' - n + 26) % 26) + 'A';
}
}
}
}
include <stdio.h>
include <string.h>
int main(int argc, char argv[]) {
int i,j;
char temp;
for(i = 1; i < argc; i++){
for(j=1;j<argc-i;j++){
if(strcmp(argv[j],argv[j+1])>0){
temp = argv[j];
argv[j] = argv[j+1];
argv[j+1] = temp;
}
}
}
for(i = 1; i < argc; ++i)
printf("hello, %s\n", argv[i]);
return 0;
}