例如我有兩個字符串
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
int strcmp(char *str1,char *str2);
字符串比較函數
str1
str1=str2 返回0
str1>str2 返回正數
這是C語言的字符串,不能直接比較,C++的string類型,可以直接比較。