gpt4 book ai didi

c - c 中的多功能实用程序问题。

转载 作者:行者123 更新时间:2023-11-30 17:50:50 25 4
gpt4 key购买 nike

以下程序应该从命令行读取(使用argv[])并执行三个函数之一:

right(根据边长判断三角形是否为直角三角形:输入 mu -r [sidea] [sideb] [sidec]

findtext:(查找给定文件内的字符串序列:(即“hello”)并列出找到它的行号。

count:(计算制表符、单词和退格键的数量)。

Findtext.c 正在正常运行:

例如,如果我输入 ./mu -f [string] [file.txt]

它成功列出了文件以及找到该文件的文件中的行号。

但是当我使用 -r(right) 选项运行相同的代码时,它会出现以下段错误:

程序名称:./mu3段错误(核心转储)

这段代码哪里出错了?

#include <stdio.h>
#include <stdlib.h>
//#include "count.h"
//#include "right.h"
//#include "findtext.h"

#define STAND_ALONE 1
void right(int, char **);
void count(int, char **);
void findtext(int, char **);
#ifdef STAND_ALONE
int main(int argc, char *argv[])
{

printf("\n");
printf("Can only use one option(-f, -c, -r) at once. sorry!\n");
printf("\n");


printf("Program name: %s\n", argv[0]);

while ((argc > 1) && (argv[1][0] == '-'))
{
switch (argv[1][1])
{
case 'f': // findtext.c
printf("%s\n",&argv[1][2]);
findtext(argc, argv);
break;

case 'r': // right.c
printf("%s\n",&argv[1][2]);
right(argc, argv);
break;
case 'c': // count.c
printf("%s\n",&argv[1][2]);
count(argc,argv);
break;

default:
printf("Wrong Argument: %s\n", argv[1]);

}

++argv;
--argc;
}
return (0);
}

#endif

void right(int argc, char *argv[]){
int a;
int b;
int c;
int largest;
int a2;
int b2;
int c2;

/*if(argc != 4){
printf("please enter 3 sides, only \n");
} */

a = atoi(argv[2]);
b = atoi(argv[3]);
c = atoi(argv[4]);
//printf("argv2:%s ",argv[2]);
if((a <= 0 )|| (b <= 0) || (c <= 0))
printf("Only positive values allowed\n"); exit(0);

a2 = (a*a);
b2 = (b*b);
c2 = (c*c);


if((c > a) && (c > b))
largest = c;
if((b > a) && (b > c))
largest = b;
if((a > b) && (a > c))
largest = a;



if(largest == a){
printf("HEy hey hey!");
if((b2 + c2) == a2){ printf("%s %s %s is a right triangle\n",argv[2],argv[3],argv[4]); }
else{printf("%s %s %s is not a right triangle\n",argv[3],argv[2],argv[4]);}
}

if(largest == b){
printf("HEy");
if((a2 + c2) == b2){ printf("%s %s %s is a right triangle\n",argv[2],argv[3],argv[4]); }
else{printf("%s %s %s is not a right triangle\n",argv[2],argv[3],argv[4]);}

}

if(largest == c){
printf("yo");
if((a2 + b2) == c2){ printf("%s %s %s is a right triangle\n",argv[2],argv[3],argv[4]); }
else{printf("%s %s %s is not a right triangle\n",argv[2],argv[3],argv[4]);}

}




} /* end method right() */



void findtext(int argc, char *argv[]){
FILE *fin;
char buffer[100];
int counter;
char *ptr = buffer;
char *result;

//if(argc != 3) {printf("Usage: %s filename argc:%d\n", argv[0], argc); exit(1);}
fin = fopen(argv[3], "r");
if(!fin) {printf("Unable to open %s\n", argv[2]); exit(1); }

counter = 0;
while (fgets(buffer, 99, fin)){
counter = counter + 1;
if(strstr(ptr,argv[2])){
printf("%d. %s", counter, ptr);
printf("\n");

}

}
fclose (fin);


}


void count(int argc, char **argv){
FILE *fin;

int lcounter = 0;
int count = 0;
char name[100];
char ch;

int word = 0;
int nchar = 0;



fin = fopen(argv[1],"r"); // open file

if(fin==0){

printf("Could not find specified file.\n");
exit(0);
}

while((ch = getc(fin)) != EOF)
{

nchar++;
if(ch == '\n')
lcounter++;
if(isspace(ch) || ch == '\t' || ch == '\n')
word++;
}

printf("number of characters: %d\n",nchar);
printf("Lines: %d\n",lcounter);
printf("words: %d\n",word);
printf("\n");

fclose(fin);


}

最佳答案

if((a <= 0 )|| (b <= 0) || (c <= 0))
printf("Only positive values allowed\n"); exit(0);

注意 if block 。您没有使用大括号。无论如何,在 right 函数的这一点上,程序将结束。

当然,这不是您要寻找的错误,但它提供了一个起点:错误必须在此点之前。

您可能需要调试此处发生的情况:

a = atoi(argv[2]);
b = atoi(argv[3]);
c = atoi(argv[4]);

关于c - c 中的多功能实用程序问题。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17004977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com