0
点赞
收藏
分享

微信扫一扫

C语言填空

腊梅5朵 2022-03-18 阅读 33

code 1

填空题
反向输出一个链表(共5个整型元素)。
程序运行示例如下:
please input 5 data==>
1 2 3 4 5
The value is ==>5
The value is ==>4
The value is ==>3
The value is ==>2
The value is ==>1

#include <stdio.h>
#include <stdlib.h>

struct node{
    int data;
    struct node *next;
};

struct node* NewNode(void){
    struct node* p = (struct node*)malloc(sizeof(struct node));
    p->data = 0;
    p->next = NULL;
    ______1________;
}

void AddNode(struct node* head,int num){
    struct node *q,*p;
    q = NewNode();
    ______2________;
    
    p = _______3_______;
    head->next = ______4________;
    q->next = _______5_______;
}

void PrintNode(struct node* head){
    struct node* p = _______6_______;
    while(p != NULL){
        printf("The value is ==>%d\n",p->data);
        p = p->next;
    }
}


int main(){
    int n = 5,i;
    int num;
    printf("\nplease input 5 data==>\n");
    struct node* head = NewNode();
    for(i = 0 ;i < n;i++){
        scanf("%d",&num);
        _______7_______;
    }
    _______8_______;
    return 0;
}

完整答案

#include <stdio.h>
#include <stdlib.h>

struct node{
    int data;
    struct node *next;
};

struct node* NewNode(void){
    struct node* p = (struct node*)malloc(sizeof(struct node));
    p->data = 0;
    p->next = NULL;
    return p;
}

void AddNode(struct node* head,int num){
    struct node *q,*p;
    q = NewNode();
    q->data = num;
    
    p = head->next;
    head->next = q;
    q->next = p;
}

void PrintNode(struct node* head){
    struct node* p = head->next;
    while(p != NULL){
        printf("The value is ==>%d\n",p->data);
        p = p->next;
    }
}


int main(){
    int n = 5,i;
    int num;
    printf("\nplease input 5 data==>\n");
    struct node* head = NewNode();
    for(i = 0 ;i < n;i++){
        scanf("%d",&num);
        AddNode(head,num);
    }
    PrintNode(head);
    return 0;
}
  1. 【附加题】从键盘输入某单位职工的月收入(人数最多不超过40人),当输入负值时,表示输入结束,编程从键盘任意输入一个职工号,查找该职工的月收入。
    程序运行示例1:
    Input person’s ID and income:001 564↙
    Input person’s ID and income:002 365↙
    Input person’s ID and income:003 564↙
    Input person’s ID and income:004 654↙
    Input person’s ID and income:005 -9↙
    Total number is 4
    Input the searching ID:004↙
    income = 654
    程序运行示例2:
    Input person’s ID and income:001 234↙
    Input person’s ID and income:002 654↙
    Input person’s ID and income:003 897↙
    Input person’s ID and income:004 321↙
    Input person’s ID and income:005-7↙
    Total number is 4
    Input the searching ID:009↙
    Not found!
    下面给出的程序存在多处错误,请修正所有错误,使之能够得到正确的运行结果,并将正确程序填写在答题区。
#include <stdio.h> 
#define N 40 
int ReadScore(int income[], long num[]);           
________________1_____________________        
int main() 
{     
      int income[N], n, pos;     
      long num[N], x;     
      n = ReadScore(income, num);       
      printf("Total number is %d\n", n);     
      printf("Input the searching ID:");     
      ________________2____________                
      pos = BinSearch(num, x, n);      
      if (_____3______)              
      {     
           printf("income = %d\n", income[pos]);     
      }
      else
      {
           printf("Not found!\n"); 
      }     
      return 0; 
}   

int ReadScore(int income[], long num[]) 
{     
      ___________4___________;             
      do
      {         
           i++;
           printf("Input person's ID and income:");
           scanf("%d%d", &num[i], &income[i]);     
       }while(num[i] >0 && income[i] >= 0);      
       ___________5___________;                                
}   

int BinSearch(long num[], long x, int n) 
{     
       int  low, high, mid;     
        ___________6___________;             
        ___________7___________;                  
       while (low <= high)          
       {
              ___________8___________;
             if (x > num[mid])
             {             
                   low = mid + 1; 
             }         
             else  if ( ___________9_________)         
             {
                    high = mid - 1;
              }         
             else
             {             
                      ___________10___________;
             }     
        }     
        return -1;
}


#include <stdio.h> 
#define N 40 
int ReadScore(int income[], long num[]);           
int BinSearch(long num[], long x, int n);         
int main() 
{     
      int income[N], n, pos;     
      long num[N], x;     
      n = ReadScore(income, num);       
      printf("Total number is %d\n", n);     
      printf("Input the searching ID:");     
      scanf("%d", &x);                 
      pos = BinSearch(num, x, n);      
      if (pos != -1)              
      {     
           printf("income = %d\n", income[pos]);     
      }
      else
      {
           printf("Not found!\n"); 
      }     
      return 0; 
}   

int ReadScore(int income[], long num[]) 
{     
      int i = -1 ;              
      do
      {         
           i++;
           printf("Input person's ID and income:");
           scanf("%d%d", &num[i], &income[i]);     
       }while(num[i] >0 && income[i] >= 0);      
       return i;                               
}   

int BinSearch(long num[], long x, int n) 
{     
       int  low, high, mid;     
       low = 0;             
       high = n - 1 ;                   
       while (low <= high)          
       {
             mid = (high + low) / 2;
             if (x > num[mid])
             {             
                   low = mid + 1; 
             }         
             else  if (x < num[mid])         
             {
                    high = mid - 1;
              }         
             else
             {             
                     return mid;
             }     
        }     
        return -1;
}


______1_______

______2_______;
void Sorting(int a[], int n);
void Printout(int a[], int n);
int main()
{
    int a[10], b[10];
    ______3_______;
    printf("Input 10 numbers:");
    for (i = 0; i < 10; i++)
    {
        scanf("%d", &a[i]);
    }
    ______4_______;
    Printout(a, 10);
    return 0;
}

void Swap(int *a, int *b)
{
    ______5____;
    temp = *a;
    *a = *b;
    *b = temp;
}
//升序排序
void Sorting(int a[], int n)
{
    int i, j, minpos;
    for (i = 0 ;_____6______; i++)
    {
        ______7_____;
        for (j = i + 1; j < n; j++)
        {
            if (a[minpos] > a[j])
            {
                minpos = j;
            }
        }
        Swap(______8_____);
    }
}

void Printout(int a[], int n)
{
    int i;
    printf("Output: ");
    for (i = 0; i < n; i++)
    {
        if (a[i] % 2 != 0)
        {
            printf("%d,",a[i]);
        }
    }
    for (i = 0; i < n; i++)
    {
        if (a[i] % 2 == 0)
        {
            printf("%d,",a[i]);
        }
    }
}
#include <stdio.h>

void Swap(int *a, int *b);
void Sorting(int a[], int n);
void Printout(int a[], int n);
int main()
{
    int a[10];
    int i, j;
    printf("Input 10 numbers:");
    for (i = 0; i < 10; i++)
    {
        scanf("%d", &a[i]);
    }
    Sorting(a, 10);
    Printout(a, 10);
    return 0;
}

void Swap(int *a, int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

void Sorting(int a[], int n)
{
    int i, j, minpos;
    for (i = 0 ; i < n - 1; i++)
    {
        minpos = i;
        for (j = i + 1; j < n; j++)
        {
            if (a[minpos] > a[j])
            {
                minpos = j;
            }
        }
        Swap(&a[minpos], &a[i]);
    }
}

void Printout(int a[], int n)
{
    int i;
    printf("Output: ");
    for (i = 0; i < n; i++)
    {
        if (a[i] % 2 != 0)
        {
            printf("%d,",a[i]);
        }
    }
    for (i = 0; i < n; i++)
    {
        if (a[i] % 2 == 0)
        {
            printf("%d,",a[i]);
        }
    }
}

code 4

#include <stdio.h>
 ______1____;
 ______2____;
#define MAX 100

char* FindMaxSingle(char *pstr, char *pin)
{
    char *p1, *p2;
    char *resu = (char*)malloc(sizeof(char)*MAX);
    int  ______3____,templen;
    p1 = pstr;
    p2 = pin;
    while(1)
    {
        if ( ______4____ ){
            *p2 = *p1;
            p2++;
        }
        else{
            *p2 = '\0';
             ______5____;
            if(len < templen){
                len = templen;
                strcpy(resu,pin);
                
            }
            if( *p1 == '\0' ){
                 ______6____;
            }
            p2 = pin;
        }
        p1++;
    }
     ______7____;
}

int main()
{
    char str[MAX];
    char single[MAX];
    char *resu;
    int len;
    printf("输入一行文本:\n");
    gets(str);
    printf("\n");
     ______8____;
    printf("最长的单词是:%s",resu);
}
//输入 I am a student.
//输出 student
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100

char* FindMaxSingle(char *pstr, char *pin)
{
    char *p1, *p2;
    char *resu = (char*)malloc(sizeof(char)*MAX);
    int len = 0,templen;
    p1 = pstr;
    p2 = pin;
    while(1)
    {
        if ( ((*p1) >= 'a' && (*p1) <= 'z')  ||  ((*p1) >= 'A' && (*p1) <= 'Z') ){
            *p2 = *p1;
            p2++;
        }
        else{
            *p2 = '\0';
            templen = strlen(pin);
            if(len < templen){
                len = templen;
                strcpy(resu,pin);
                
            }
            if( *p1 == '\0' ){
                break;
            }
            p2 = pin;
        }
        p1++;
    }
    return resu;
}

int main()
{
    char str[MAX];
    char single[MAX];
    char *resu;
    int len;
    printf("输入一行文本:\n");
    gets(str);
    printf("\n");
    resu = FindMaxSingle(str, single);
    printf("最长的单词是:%s",resu);
}
//输入 I am a student.
//输出 student

code 5

将一个字符串插入至另一个源字符串的某个位置:
将一个字符串2插入到源字符串1中 第一次出现某字符的位置,并打印出形成的新串。
如果 字符串1中找不到输入的字符, 则显示“Not found!”并结束程序。
注:源字符串长度及待插入字符串长度不超过50

提示信息:
printf(“Input source string 1:\n”)
printf(“Input inserted string 2:\n”)
printf(“Input a letter to locate the index:\n”)

输出信息格式:
printf(“The new string is:%s”)
printf(“Not found!”)

测试样例1:
输入信息:
Input source string 1:
abcdecfg
Input inserted string 2:
---
Input the a letter to locate the index:
c
输出结果:
The new string is:ab*---*cdecfg

测试样例2:
输入信息:
Input source string 1:
abcdecfg
Input inserted string 2:


Input the a letter to locate the index:
h
输出结果:
Not found!

#include <stdio.h>
#include <string.h>
#define MAX 100

int InsertSrcToDst(char *src, char *dst, char ch){
    char *p,*p2;
    p = dst;
    while(*p != '\0'){
        if(*p == ch){
            break;
        }
        p++;
    }
    if(*p == '\0'){
        return -1;
    }
    p2 = src;
    while(*p2 != '\0'){
        p2++;
    }
    strcpy(p2,p);
    strcpy(p,src);
    
    return 0;
}

int main(){
    char src[MAX],dst[MAX];
    char ch;
    int num;
    int flag = 0;
    printf("Input source string 1:\n");
    gets(dst);
    printf("Input inserted string 2:\n");
    gets(src);
    printf("Input a letter to locate the index:\n");
    do{
        scanf("%c",&ch);
        if(ch != ' '){
            flag = 1;
        }
    }while(flag == 0);
    
    num = InsertSrcToDst(src,dst,ch);
    if(num == -1){
        printf("Not found!");
    }
    else{
        printf("The new string is:%s",dst);
    }

}
#include <stdio.h>
#include <string.h>
______1____

int InsertSrcToDst(char *src, char *dst, char ch){
    char *p,*p2;
    ______2____;
    while(*p != '\0'){
        if(*p == ch){
            break;
        }
        ______3____;
    }
    if(*p == '\0'){
        return -1;
    }
    ______4____;
    while(*p2 != '\0'){
        p2++;
    }
    strcpy(______5____);
    strcpy(p,src);
    
    return 0;
}

int main(){
    char src[MAX],dst[MAX];
    char ch;
    int num;
    ______6____;
    printf("Input source string 1:\n");
    gets(dst);
    printf("Input inserted string 2:\n");
    gets(src);
    printf("Input a letter to locate the index:\n");
    do{
        ______7____;
        if(ch != ' '){
            flag = 1;
        }
    }while(flag == 0);
    
    num = InsertSrcToDst(src,dst,ch);
    if(______8____){
        printf("Not found!");
    }
    else{
        printf("The new string is:%s",dst);
    }

}

code 6

超长正整数的加法
请设计一个算法完成两个超长正整数的加法。
**输出格式要求:" s1=" " s2=" “s1+s2=”
程序运行示例如下:
3488934387589
374849389
s1=3488934387589
s2=374849389
s1+s2=3489309236978

#include <stdio.h>
#include <string.h>
#define MAX 50

void ReArray(char *str){
    ______1_____;
    int i;
    char temp;
    for(i = 0; i < len/2 ;i++){
        temp = str[i];
        str[i] = ______2_____;
        ______3_____ = temp;
    }
}

void LongSum(char *str1,char *str2,char *str3){
    int carry,i,sum;
    int len1 = strlen(str1);
    int len2 = strlen(str2);
    int minlen,maxlen;
    ______4_____;
    if(len1 > len2){
        minlen = len2;
        maxlen = len1;
        maxstr = str1;
    }
    else{
        minlen = len1;
        maxlen = len2;
        maxstr = str2;
    }
    ______5_____;
    for(i = 0; i < minlen;i++){
        sum = (str1[i]-'0') + (str2[i]-'0') + carry;
        carry = sum / 10;
        str3[i] = (sum % 10 +'0');
    }
    for(; i < maxlen ;i++){
        sum = ______6_____;
        carry = sum / 10;
        str3[i] = (sum % 10 + '0');
    }
    while(carry != 0){
        str3[i] = (carry % 10 - '0');
        carry = carry / 10;
        ______7_____;
    }
    ______8_____;
}

int main(){
    char str1[MAX],str2[MAX],str3[MAX];
    gets(str1);
    gets(str2);
    ReArray(str1);
    ReArray(str2);
    ______9_____;
    ReArray(str1);
    ReArray(str2);
    ______10_____;
    printf("   s1=%s\n",str1);
    printf("   s2=%s\n",str2);
    printf("s1+s2=%s\n",str3);
}
#include <stdio.h>
#include <string.h>
#define MAX 50

void ReArray(char *str){
    int len = strlen(str);
    int i;
    char temp;
    for(i = 0; i < len/2 ;i++){
        temp = str[i];
        str[i] = str[len-1-i];
        str[len-1-i] = temp;
    }
}

void LongSum(char *str1,char *str2,char *str3){
    int carry,i,sum;
    int len1 = strlen(str1);
    int len2 = strlen(str2);
    int minlen,maxlen;
    char *maxstr;
    if(len1 > len2){
        minlen = len2;
        maxlen = len1;
        maxstr = str1;
    }
    else{
        minlen = len1;
        maxlen = len2;
        maxstr = str2;
    }
    carry = 0;
    for(i = 0; i < minlen;i++){
        sum = (str1[i]-'0') + (str2[i]-'0') + carry;
        carry = sum / 10;
        str3[i] = (sum % 10 +'0');
    }
    for(; i < maxlen ;i++){
        sum = (maxstr[i] - '0') + carry;
        carry = sum / 10;
        str3[i] = (sum % 10 + '0');
    }
    while(carry != 0){
        str3[i] = (carry % 10 - '0');
        carry = carry / 10;
        i++;
    }
    str3[i] = '\0';
}

int main(){
    char str1[MAX],str2[MAX],str3[MAX];
    gets(str1);
    gets(str2);
    ReArray(str1);
    ReArray(str2);
    LongSum(str1,str2,str3);
    ReArray(str1);
    ReArray(str2);
    ReArray(str3);
    printf("   s1=%s\n",str1);
    printf("   s2=%s\n",str2);
    printf("s1+s2=%s\n",str3);
}

code 7

输入一个英文句子(输入句子以回车作为结束,句子长度包括标点符号并且不超过20),翻转句子中单词的顺序,但单词内字符的顺序不变。标点符号和普通字母一样处理。例如输入字符串“I am a student.”,则输出“student.a am I”
输入提示信息:Input Sentence:
输出提示信息:Output Reverse Sentence:

#include <stdio.h>
#include <string.h>
______1_____;

void ReverseSingle(char* str,int head ,int last){
    int i;
    char temp;
    for(i = 0;______2_____; i++){
        temp = str[______3_____];
        str[______4_____] = str[______5_____];
        str[______6_____] = temp;
    }
}

void ReverseSentence(char* str){
    ______7_____;
    int i;
    char ch;
    int head,last;
    for(i = 0;i < len; i++){
        head = i;
        last = i;
        ______8_____;
        while(ch != ' ' && ch != '\0'){
            last++;
            ch = str[last];
        }
        if(head != last-1){
            ReverseSingle(______9_____);
        }
        i = ______10_____;
    }
    ______11_____;
}

int main(){
    char str[LEN];
    printf("Input Sentence:");
    ______12_____;
    ReverseSentence(str);
    printf("Output Reverse Sentence:%s",str);
}
#include <stdio.h>
#include <string.h>
#define LEN 20

void ReverseSingle(char* str,int head ,int last){
    int i;
    char temp;
    for(i = 0; i <= (last-head)/2; i++){
        temp = str[head + i];
        str[head + i] = str[last - i];
        str[last - i] = temp;
    }
}

void ReverseSentence(char* str){
    int len = strlen(str);
    int i,flag = 1;
    char ch;
    int head,last;
    for(i = 0;i < len; i++){
        head = i;
        last = i;
        ch = str[last];
        while(ch != ' ' && ch != '\0'){
            last++;
            ch = str[last];
        }
        if(head != last-1){
            ReverseSingle(str,head,last-1);
        }
        flag = 1;
        i = last;
    }
    ReverseSingle(str,0,len-1);
}

int main(){
    char str[LEN];
    printf("Input Sentence:");
    gets(str);
    ReverseSentence(str);
    printf("Output Reverse Sentence:%s",str);
}

code 8

给定一个[10,100]闭区间范围内的正整数N,编写函数void func(int N, int prime[], int *prime_count, int composite[], int *composite_count);,实现将大于等于2小于等于N的整数按是否为素数分成两类。
要求:
(1)输入:N为整数,若输入数据不在题目要求范围内,输出错误提示”Input error! Please input an integer N, 10<=N<=100.\n”,并重新输入。
(2)素数存放在指针prime指向的数组中,指针prime_count指向的位置存放素数个数,合数存放在指针composit指向的数组中,指针composite_count指向的位置存放合数的个数。
(3)输出:共输出四行。第一行输出素数的个数;第二行输出所有的素数,两个数字之间用空格隔开;第三行输出合数的个数;第四行输出所有的合数,两个数字之间用空格隔开。
输入样例:
10
输出样例:
4
2 3 5 7
5
4 6 8 9 10

#include <stdio.h>
#define MAX 100

void func(int N, int prime[], int *prime_count, int composite[], int *composite_count);
______1_____;

int main(){
    int prime_count = 0,composite_count = 0;
    int prime[MAX],composite[MAX];
    int input,flag,i;
    do{
        ______2_____;
        scanf("%d",&input);
        if( ______3_____ ){
            printf("Input error! Please input an integer N, 10<=N<=100.\n");
            flag = 0;
        }
    }while(flag == 0);
    ______4_____;
    printf("%d\n",prime_count);
    for(i = 0; i < prime_count ;i++){
        printf("%d",prime[i]);
        if(i != prime_count-1){
            printf(" ");
        }
    }
    printf("\n");
    printf("%d\n",composite_count);
    for(i = 0; i < composite_count ;i++){
        printf("%d",composite[i]);
        if(i != composite_count-1){
            printf(" ");
        }
    }
    printf("\n");
}

void func(int N, int prime[], int *prime_count, int composite[], int *composite_count){
    int flag;
    int i,count1,count2;
    ______6_____;
    ______7_____;
    for(i = 2; i <= N ;i++){
        ______8_____;
        if(______9_____;){
            prime[count1] = i;
            count1++;
        }else{
            composite[count2] = i;
            count2++;
        }
    }
    ______10_____;    
    ______11_____;
}

int Prime(int num){
    int i;
    for(______12_____; i < num;i++){
        if( num % i == 0){
            return 0;
        }
    }
    return 1;
    
}
#include <stdio.h>
#define MAX 100

void func(int N, int prime[], int *prime_count, int composite[], int *composite_count);
int isPrime(int num);

int main(){
    int prime_count = 0,composite_count = 0;
    int prime[MAX],composite[MAX];
    int input,flag,i;
    do{
        flag = 1;
        scanf("%d",&input);
        if( !(input >= 10 && input <= 100) ){
            printf("Input error! Please input an integer N, 10<=N<=100.\n");
            flag = 0;
        }
    }while(flag == 0);
    func(input,prime,&prime_count,composite,&composite_count);
    printf("%d\n",prime_count);
    for(i = 0; i < prime_count ;i++){
        printf("%d",prime[i]);
        if(i != prime_count-1){
            printf(" ");
        }
    }
    printf("\n");
    printf("%d\n",composite_count);
    for(i = 0; i < composite_count ;i++){
        printf("%d",composite[i]);
        if(i != composite_count-1){
            printf(" ");
        }
    }
    printf("\n");
}

void func(int N, int prime[], int *prime_count, int composite[], int *composite_count){
    int flag;
    int i,count1,count2;
    count1 = 0;
    count2 =0;
    for(i = 2; i <= N ;i++){
        flag = Prime(i);
        if(flag == 1){
            prime[count1] = i;
            count1++;
        }else{
            composite[count2] = i;
            count2++;
        }
    }
    *prime_count = count1;
    *composite_count = count2;
}

int Prime(int num){
    int i;
    for(i = 2; i < num;i++){
        if( num % i == 0){
            return 0;
        }
    }
    return 1;
    
}
举报

相关推荐

0 条评论