chinanoahli 2015-04-15 12:22 采纳率: 100%
浏览 4150
已采纳

C语言 如何将 两字符串的比较结果 作为 if 的判断条件

例如我有兩個字符串

 char i[9][13], j[13];

应该如何正确地將 i[2(或由前面的语句提供的一个数作)] ==(或!=) j 作为 if语句的判断条件?(或!=)
我试过

 if(strcmp(i[n], j) !=0)   //string.h

 if(i[n]!=j)

上面两个if代码片段都嵌套与一个for代码块,n由for提供
程序都没办法正确执行这个if判断,而是直接跳过了if代码块,执行下面的语句

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

FILE *input_file, *output_file;

int total_people, giving_to, accepting, having[9], money_counter;

char giver[9][13], receiver[9][13], the_giver[13], the_recever[13];

int people_scaner, people_judger;



int test_count;

int main () {
    input_file = fopen("gift1.in", "r");
    output_file = fopen("gift1.out", "w+");

    fscanf(input_file, "%d\n", &total_people);

    //read all name to a array called `giver[][]` and `receiver[][]`
    people_scaner = 0;

    while(people_scaner < total_people){
        fscanf(input_file, "%s", giver[people_scaner]);
        /*
         *&giver[people_scaner] = &receiver[people_judger];
         */
        people_scaner ++;
    }

    //read the first of who giving gift to others to `the_giver`
    fscanf(input_file, "%s", the_giver);

    //if the guy who is the first person on the list of `giver[]` eles increase the people_judger
    people_judger = 0;

    if(strcmp(the_giver, giver[people_judger]) != 0){
        people_judger ++;
    }


    //according to the number of last step, scan next two number to &having[people_judger] and giving_to, giving_to will be resuing 
    fscanf(input_file, "%d%d", &having[people_judger], &giving_to);
    accepting = having[people_judger] / giving_to;

    for(people_scaner = 0; people_scaner < giving_to; people_scaner++){
        //read a recever
        fscanf(input_file, "%s", the_recever);
        printf("%s\n", the_recever);
        //put the gift from the giver to the recever
        people_judger = 0;
        if(strcmp(the_giver, giver[people_judger]) != 0){       //这里
            people_judger++;
            printf("%d\n", people_judger);
            continue;
        }
        having[people_judger] = accepting + having[people_judger];

        people_scaner++;
    }

    fclose(input_file);
    fclose(output_file);
    return 0;
}

所读文件

5
dave
laura
owen
vick
arm
dave
200 3
laura
owen
vick
owen
500 1
dave
arm
150 2
vick
owen
laura
0 2
arm
vick
vick
0 0

题目来源
USACO Greedy Gift Givers

  • 写回答

7条回答 默认 最新

  • Y_F_Z_L 2015-04-15 12:29
    关注

    int strcmp(char *str1,char *str2);
    字符串比较函数
    str1 str1=str2 返回0
    str1>str2 返回正数

    这是C语言的字符串,不能直接比较,C++的string类型,可以直接比较。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题