gpt4 book ai didi

add/sub/div/mul 的 C 解析器程序。读取用户的输入想要在分号处终止我的程序而不是按回车

转载 作者:太空宇宙 更新时间:2023-11-04 04:11:24 26 4
gpt4 key购买 nike

我已经编写了 C 程序,它将使用 read() 用户输入进行读取,例如:

添加 1 2 3
结果:6

现在的问题是,一旦按下 enter,read 就会停止读取。如果我想使用除按 Enter 以外的任何其他字符终止怎么办?例如:

添加 1 2 3;
结果:6

注意--> 当我按下 ;阅读应该停止阅读并显示结果。

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

int main(void) {
while(1) {
char buff[50];
int rcount = read(STDIN_FILENO, buff, 50); //Reading user input
//write(1, buff, rcount);
buff[rcount - 1] = '\0'; //Converting user input as a string(char array)

/*If nothing is given and user press enter*/
if(strcasecmp(buff, "") == 0) {
write(1, "Invalid argument\n", sizeof("Invalid argument\n"));
}

/*Getting the first argument*/
char *arr = strtok(buff, " ");

if(strcasecmp(arr, "exit") == 0) {
break; //Break out of loop when exit is typed in
}
if(strcasecmp(arr, "add") == 0) {
add(arr); //When first arg is add, call to add func
}
else if(strcasecmp(arr, "sub") == 0) {
subtract(arr); //When first arg is sub, call to sub func
}
else if(strcasecmp(arr, "mul") == 0) {
multiply(arr); //When first arg is mul, call to mul func
}
else if(strcasecmp(arr, "div") == 0) {
divide(arr); //When first arg is div, call to div func
}
/*When first argument is anything else*/
else {
write(1, "Invalid argument\n", sizeof("Invalid argument\n"));
}

printf("\n");


}
}

void add(char *arr) { //To add numbers --> args > 1

int sum = 0;
while(arr != NULL) { //Loop to get all the numbers and using them to perform task
//printf("%s\n", arr);
int a = atoi(arr); //Converting arg/number to int
sum += a;
arr = strtok(NULL, " \n;");
}
char w[50];
int count = sprintf(w, "Result: %d\n", sum); //to use 'write', need to store sum as result so using sprintf instead of printf. Guess we've got no other option?
write(1, w, count);
return;
}

void subtract(char *arr) { //To subtract numbers --> args > 1

arr = strtok(NULL, " \n"); //To move to second arg
int subt = (int) atoi(arr); //to get the first number/2nd arg, as we will subtract remaining nums from this

arr = strtok(NULL, " \n"); //To move to remaining numbers, args > 2
while (arr != NULL) {
//printf("%s\n", arr);
int a = atoi(arr);
subt = subt - a;
arr = strtok(NULL, " \n");
}

char w[50];
int count=sprintf(w, "Result: %d\n", subt);
write(1, w, count);
return;
}

void multiply(char *arr) { //To multiply numbers --> args > 1

int mult = 1;
arr = strtok(NULL, " \n");
while (arr != NULL) {
//printf("%s\n", arr);
int a = atoi(arr);
mult = mult * a;
arr = strtok(NULL, " \n");
}
char w[50];
int count=sprintf(w, "Result: %d\n", mult);
write(1, w, count);
return;
}

void divide(char *arr) { //To divide numbers --> args > 1

arr = strtok(NULL, " \n");
float div = (float) atoi(arr);
//printf("%f", div);
//printf("\n");
arr = strtok(NULL, " \n");
while (arr != NULL) {
//printf("%s\n", arr);
float a = (float) atoi(arr);
div = div / a;
arr = strtok(NULL, " \n");
}

char w[50];
int count=sprintf(w, "Result: %f\n", div);
write(1, w, count);
return;
}

最佳答案

这是一个可能有帮助的示例,您可以根据需要将任何终止字符传递给它:

int add(char *str, char term)
{
int res = 0;
while (*str)
{
while (*str && !isdigit(*str))
str++;
if (*str && isdigit(*str))
res += atoi(*str);
if (*str == term)
break ;
while (*str && isdigit(*str))
str++;
}
return res;
}

关于add/sub/div/mul 的 C 解析器程序。读取用户的输入想要在分号处终止我的程序而不是按回车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56978186/

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