求助//读取的第一个数据输出有问题,不知道怎么改那里
查看原帖
求助//读取的第一个数据输出有问题,不知道怎么改那里
1392779
luluhexiaolan楼主2024/12/8 00:02
#include <stdio.h>

int calculate(int type, int num1, int num2) 
{
    switch (type) 
	{
        case 'a':
            return num1+num2;
        case 'b':
            return num1-num2;
        case 'c':
            return num1*num2;
        default:
            return 0;
    }
}

int main() {
    int n;
    scanf("%d", &n);
    char types[n];
    int nums1[n], nums2[n];
    int results[n];
    for (int i = 0; i < n; i++) 
	{
        scanf(" %c",&types[i]);
        if (types[i] == 'a' || types[i] == 'b' || types[i] == 'c') 
		{
            scanf("%d %d", &nums1[i], &nums2[i]);
            results[i] = calculate(types[i], nums1[i], nums2[i]);
        } 
		else 
		{
            scanf("%d", &nums1[i]);
            scanf("%d", &nums2[i]);
            results[i] = calculate(types[i-1], nums1[i], nums2[i]);//读取的第一个数据输出有问题 
        }
        
    }
	char type1;
    for (int i = 0; i < n; i++) 
	{
        if (types[i] == 'a' || types[i] == 'b' || types[i] == 'c') 
		{
			switch(types[i])
			{
				case 'a':
						type1='+';
						break;
				case 'b':
						type1='-';
						break;
				case 'c':
						type1='*';
						break;
				default:break;	
			}
				
            printf("%d %c %d=%d\n", nums1[i], type1, nums2[i], results[i]);
            
        } 
		else 
		{
            printf("%d %c %d=%d\n", nums1[i], type1, nums2[i], results[i]);
        }
    }

    return 0;
}

一个数据输出有问题 }

}
char type1;
for (int i = 0; i < n; i++) 
{
    if (types[i] == 'a' || types[i] == 'b' || types[i] == 'c') 
	{
		switch(types[i])
		{
			case 'a':
					type1='+';
					break;
			case 'b':
					type1='-';
					break;
			case 'c':
					type1='*';
					break;
			default:break;	
		}
			
        printf("%d %c %d=%d\n", nums1[i], type1, nums2[i], results[i]);
        
    } 
	else 
	{
        printf("%d %c %d=%d\n", nums1[i], type1, nums2[i], results[i]);
    }
}

return 0;

}

2024/12/8 00:02
加载中...