195080410李荪玮
_
上机运行如下程序,给出程序运行的结果(包括输入的数据)。
void exchange(int a, int b)
{
int temp;
temp=a; a=b; b=temp;
}
void main()
{
int n1,n2;
printf("\nPlease input 2 intgers: ");
scanf("%d %d",&n1,&n2);
exchange(n1,n2);
printf("\nn1=%d,n2=%d\n",n1,n2);
}
答:输入3 6,输出n1=3,n2=6
上机运行如下程序,给出程序运行的结果(包括输入的数据),并说明和上例程序的区别。
void exchange(int * a, int * b)
{
int temp;
temp=*a; *a=*b; *b=temp;
}
void main()
{
int n1,n2;
printf("\nPlease input 2 intgers: ");
scanf("%d %d",&n1,&n2);
exchange(&n1,&n2);
printf("\nn1=%d,n2=%d\n",n1,n2);
}
答:
输入3 6,输出n1=6,n2=3
区别:第一题调用函数的时候是直接传递了局部变量的值,函数运行结束销毁变量,没有影响main函数中的变量值。第二题传递了main函数中两个变量的地址,可以修改main函数中变量的值。编写函数len,其功能是求字符串的长度,主函数调用如下:
#define N 20
void main()
{
char str[N];
printf("\nPlease input a string: ");
scanf("%s",str);
printf("\nThe length of this string is : %d\n",len(str));
}
答:
int len(char* str){
int count=0;
while (*str!='\0'){
count++;
str++;
}
return count;
}
编写函数void inverse (char *s ),对字符串进行逆置。编写main函数,并在其中调用inverse函数。(比如输入串“abcdef”,输出“fedcba”。)
答:
#include <stdio.h>
#define N 20
void reverse(char *str) {
int count = 0, temp = 0;
char origin[N];
while (str[count] != '\0') {
origin[count]=str[count];
count++;
}
origin[count]='\0';
count--;
while (count >= 0) {
str[temp] = origin[count];
temp++;
count--;
}
str[temp]='\0';
}
void main() {
char str[N];
char rev[N];
printf("\nPlease input a string: ");
scanf("%s", str);
reverse(str);
printf("%s", str);
}
- 编写函数void sort ( int *a, int n ),对数组中的n个数利用选择法进行降序排序。编写main函数,并在其中调用sort函数。
答:
#include <stdio.h>
#define N 20
void sort(int *a, int n) {
int maxNum,temp;
for (int i = 0; i < n-1; ++i) {
maxNum=i;
for (int j = i+1; j < n; ++j) {
if (a[j]>a[maxNum]){
maxNum=j;
temp=a[i];
a[i]=a[maxNum];
a[maxNum]=temp;
}
}
}
}
int main() {
int a[N], n;
printf("\n你想输入几个数字? ");
scanf("%d", &n);
printf("\n请输入数字:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a,n);
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
}
- 编程判断所输入的字符串是否为“回文”。“回文”是指该串的逆序串与原串相同,如串“dad”就是回文串。要求回文串的判定由一个用户自定义函数来实现,并在主函数中调用该用户自定义函数。
答:
#include <stdio.h>
#define N 20
int ifHW(char *str) {
int count = 0, temp = 0;
while (str[count] != '\0') {
count++;
}
count--;
while (count > temp) {
if (str[count]!=str[temp])return 0;
temp++;
count--;
}
return 1;
}
void main() {
char str[N];
char rev[N];
printf("\nPlease input a string: ");
scanf("%s", str);
if (ifHW(str))printf("是回文序列");
else printf("不是回文");
}
- 编写函数void del_ch(char * s, char c),删除s串中的所有字符c。编写main函数,并在其中调用del_ch函数。(比如s串为“madam”, 删除字符‘m’后s串为“ada”。)
答:
#include <stdio.h>
#define N 20
void del_ch(char *s, char c) {
int i = 0;
while (s[i] != '\0') {
if (s[i] == c) {
int temp = i;
while (s[temp] != '\0') {
s[temp] = s[temp + 1];
temp++;
}
i--;
}
i++;
}
}
void main() {
char str[N];
char c;
printf("\nPlease input a string: ");
scanf("%s", str);
c = getc(stdin);
printf("\nPlease input a letter: ");
scanf("%c", &c);
del_ch(str, c);
printf("%s", str);
}
- 编写函数void sub_str (char s, char sub, int pos, int len ),从指针s所指向串的第pos个位置取出长度为len的子串存放到sub串中。编写main函数,并在其中调用sub_str函数。
答:
#include <stdio.h>
#define N 20
void sub_str(char *s, char *sub, int pos, int len) {
int i;
for (i = 0; i < len; ++i) {
sub[i] = s[i + pos-1];
}
sub[i]='\0';
}
void main() {
char s[N];
char sub[N];
int pos, len;
printf("\nPlease input a string: ");
scanf("%s", s);
printf("\nPlease input position and length: ");
scanf("%d%d", &pos, &len);
sub_str(s,sub,pos,len);
printf("%s", sub);
}
- 编写函数int index_str (char s, char t ),返回在s串中第一次出现的子串t的位置,如果s串中不存在子串t,则返回0。编写main函数,并在其中调用index_str函数。(比如s串为“abcdef”, t串为“de”,index_str函数返回值为4。)
答:
#include <stdio.h>
#define N 20
int index_str (char *s, char *t ){
int pos=0,temp=0;
while (*s!='\0' && *t!='\0'){
pos++;
while(*(s+temp)==*(t+temp)){
if(*(t+temp+1)=='\0')return pos;
temp++;
}
temp=0;
s++;
}
return 0;
}
void main() {
char s[N];
char t[N];
printf("\nPlease input a string: ");
scanf("%s", s);
printf("\nPlease input another string: ");
scanf("%s", t);
printf("%d",index_str(s,t));
}
void main() { int n; char* days[12]={“January”,”February”,”March”,”April”,”May”,”June”,”July”,”August”,”September”,”October”,”November”,”December”}; scanf(“%d”,&n); printf(“%s”,days[n-1]); } ```