⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lab3.c

📁 在C语言环境下编写的利用队列计算向量的+
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FALSE 0
#define NULL 0

struct listst {
    int     dataitem;
	int     index;
    struct listst *  next;
} ;

typedef struct listst listelement;

void Menu (int *choice);
listelement * readfile (listelement * list, char *filename, int (*array)[100], int *n);
listelement * compute (listelement * list, char *b, int (*array)[100], int *n, int * temp);
listelement * AddItem (listelement * list, int data);
listelement * RemoveItem (listelement * list);
void PrintQueue (listelement * list);
void ClearQueue (listelement * list);

int main () {
    listelement listmember, *list;
    int data, choice;
	int n[20],temp[100];
	int array[20][100];
	char b[100],filename[100];

    list = NULL;
    do {
	Menu (&choice);
	switch (choice) {
	    case 1: 
		printf ("Enter file name to read  ");
		gets(b);
		gets(b);
		strcpy(filename,"D:\\");
		strcat(filename,b);
		list = readfile (list, filename, array ,n);
		break;
	    case 2: 
		    printf ("Enter the equation\n");
			gets(b);
			gets(b);
		    list = compute (list, b, array, n,temp);
			break;
	    case 3: 
		PrintQueue (list);
		break;

	    case 4: 
			printf("Byebye");
		break;

	    default: 
		printf ("Invalid menu choice - try again\n");
		break;
	}
    } while (choice != 4);
    ClearQueue (list);
}

void Menu (int *choice) {

    char    local;

    printf ("\nEnter\t1 to read data file,\n\t2 to compute\n\
\t3 to print\n\t4 to quit\n");
    do {
   	local = getchar ();
	if ( !( '0'< local &&  local < '5') && (local != '\n')) {
	    printf ("\nyou must enter correct choise.\n");
	    printf ("Enter 1 to read file, 2 to compute, 3 to print, 4 to quit\n");
	}
    } while (!('0'< local &&  local < '5'));
 

   *choice = (int) local - '0';
}
listelement * AddItem (listelement * list, int data) {

    listelement * lp = list;

    if (list != NULL) {
	while (list -> next != NULL)
	    list = list -> next;
	list -> next = (listelement  *) malloc (sizeof (listelement));
	list = list -> next;
	list -> next = NULL;
	list -> dataitem = data;
	return lp;
    }
    else {
	list = (listelement  *) malloc (sizeof (listelement));
	list -> next = NULL;
	list -> dataitem = data;
	return list;
    }
}

listelement * readfile (listelement * list, char * filename, int (*array)[100], int * n) {
	int temp[100];
	int a=0;
	FILE * file;
	int i=0, j=0, p=0, q=0, m=0, sum=0, c=0;
	char mytemp;
	n[0]=0;

	file = fopen(filename, "r");
	while (!feof(file))
		{
		if ((mytemp= fgetc(file)) != '\n')
			{
			ungetc(mytemp,file);
			fscanf(file, "%d", &array[i][j]);
			j++;
			n[i]++;
			}
		else
		{
			i++;
			j=0;
			n[i]=0;
		}
		}
		a=i;
	for (j=0; j<=a; j++)
	{
		for (i=0;i<n[j] ;i++ )
		{
			printf("%d ", array[j][i]);
			list = AddItem (list, array[j][i]);
			printf(" ");
		}
		printf("\n");
		
	}
    fclose(file);
	return list;
	return array;
	return n;
}

listelement * compute (listelement * list, char *b, int (*array)[100], int * n, int * temp) {
	int a=0;
	FILE * file;
	int i=0, j=0, p=0, q=0, m=0, sum=0, c=0;
	char mytemp;
		m=strlen(b);
		for (i=0;i<1 ;i++ )
		{
			if (b[0]=='t'&&b[1]=='e'&&b[2]=='m'&&b[3]=='p'&&b[4]!='=')
			{
				switch (b[4])
				{
				case '+':
					{
					if (b[7]!='\0')
					{
						p=(b[6]-48)*10+(b[7]-48)-1;
					}
					else
					{
						p=b[6]-48-1;
					}
					if (1)
					{
						for (j=0;j<n[p] ;j++ )
						{
							temp[j]=temp[j]+array[p][j];
						}

					}
					else
					{
						printf("The dimension of matrix are not the same");
						printf("\n");
					}
					for (j=0;j<n[p] ;j++ )
					{
						printf("%d",temp[j]);
						printf(" ");
					}
					printf("\n");
					}
					break;
				case '-':
					{
					if (b[7]!='\0')
					{
						p=(b[6]-48)*10+b[7]-48-1;
					}
					else
					{
						p=b[6]-48-1;
					}
					if (1)
					{
						for (j=0;j<n[p] ;j++ )
						{
							temp[j]=temp[j]-array[p][j];
						}

					}
					else
					{
						printf("The dimension of matrix are not the same");
						printf("\n");
					}
					for (j=0;j<n[p] ;j++ )
					{
						printf("%d",temp[j]);
						printf(" ");
					}
					printf("\n");
					}
					break;
				case '.':
					{
					if (b[7]!='\0')
					{
						p=(b[6]-48)*10+b[7]-48-1;
					}
					else
					{
						p=b[6]-48-1;
					}
					if (1)
					{
						for (j=0;j<n[p] ;j++ )
						{
							sum+=temp[j]*array[p][j];
						}
						printf("sum=%d",sum);
					}
					else
					{
						printf("The dimension of matrix are not the same");
						printf("\n");
					}
					printf("\n");
					}
					break;
				}
			}
			else if (b[0]=='t'&&b[1]=='e'&&b[2]=='m'&&b[3]=='p'&&b[4]=='=')
			{
				if (b[6]=='.')
				{
					q=b[5]-48;
						if (b[9]!='\0')
						{
							p=(b[8]-48)*10+b[9]-48-1;
						}
						else
						{
							p=b[8]-48-1;
						}
						
						for (j=0;j<n[p] ;j++ )
						{
							temp[j]=q*array[p][j];
						}
						for (j=0;j<n[p] ;j++ )
						{
							printf("%d",temp[j]);
							printf(" ");
						}
						printf("\n");
				}
				else
				{
				if (b[7]=='+'||'-'||'.')
				{
					q=b[6]-48-1;
					switch (b[7])
					{
					case '+':
						{
						if (b[10]!='\0')
						{
							p=(b[9]-48)*10+b[10]-48-1;
						}
						else
						{
							p=b[9]-48-1;
						}
						if (n[q]==n[p])
						{
							c=n[q];
							for (j=0;j<n[q] ;j++ )
							{
								temp[j]=array[q][j]+array[p][j];
							}

						}
						else
						{
							printf("The dimension of matrix are not the same");
							printf("\n");
						}
						for (j=0;j<n[q] ;j++ )
						{
							printf("%d",temp[j]);
							printf(" ");
						}						
						printf("\n");
						}
						break;
					case '-':
						{
						if (b[10]!='\0')
						{
							p=(b[9]-48)*10+b[10]-48-1;
						}
						else
						{
							p=b[9]-48-1;
						}
						if (n[q]==n[p])
						{
							c=n[q];
							for (j=0;j<n[q] ;j++ )
							{
								temp[j]=array[q][j]-array[p][j];
							}

						}
						else
						{
							printf("The dimension of matrix are not the same");
							printf("\n");
						}
						for (j=0;j<n[q] ;j++ )
						{
							printf("%d",temp[j]);
							printf(" ");
						}
						printf("\n");
						}
						break;
					case '.':
					{
						if (b[10]!='\0')
						{
							p=(b[9]-48)*10+b[10]-48-1;
						}
						else
						{
							p=b[9]-48-1;
						}
						q=(b[5]-48)*10+(b[6]-48);
						for (j=0;j<n[p] ;j++ )
						{
							temp[j]=q*array[p][j];
						}
						for (j=0;j<n[p] ;j++ )
						{
							printf("%d",temp[j]);
							printf(" ");
						}
						printf("\n");
						break;
					}
					}

				}
				else if (b[7]=='0'||'1'||'2'||'3'||'4'||'5'||'6'||'7'||'8'||'9')
				{
					q=(b[6]-48)*10+b[7]-48-1;
					switch (b[8])
					{
					case '+':
						{
						if (b[11]!='\0')
						{
							p=(b[10]-48)*10+b[11]-48-1;
						}
						else
						{
							p=b[10]-48-1;
						}
						if (n[q]==n[p])
						{
							c=n[q];
							for (j=0;j<n[q] ;j++ )
							{
								temp[j]=array[q][j]+array[p][j];
							}

						}
						else
						{
							printf("The dimension of matrix are not the same");
							printf("\n");
						}
						for (j=0;j<n[q] ;j++ )
						{
							printf("%d",temp[j]);
							printf(" ");
						}
						printf("\n");
						}
						break;
					case '-':
						{
						if (b[11]!='\0')
						{
							p=(b[10]-48)*10+b[11]-48-1;
						}
						else
						{
							p=b[10]-48-1;
						}
						if (n[q]==n[p])
						{
							c=n[q];
							for (j=0;j<n[q] ;j++ )
							{
								temp[j]=array[q][j]-array[p][j];
							}

						}
						else
						{
							printf("The dimension of matrix are not the same");
							printf("\n");
						}
						for (j=0;j<n[q] ;j++ )
						{
							printf("%d",temp[j]);
							printf(" ");
						}
						printf("\n");
						}
						break;
					default:
						{
						printf("Error");
						printf("\n");
						break;
						}
					}
				}
				else
				{
					printf("Error");
					printf("\n");
				}
				}
			}
			else if (b[0]=='q')
			{
				printf("Byebye!");
				printf("\n");
			}
			else
			{
				printf("Error");
				printf("\n");
			}
		}
return temp;
}

void PrintQueue (listelement * list) {

    if (list == NULL)
	printf ("queue is empty!\n");
    else
	while (list != NULL) {
	    printf ("%d\t", list -> dataitem);
	    list = list -> next;
	}
    printf ("\n");
}

void ClearQueue (listelement * list) {
    while (list != NULL) {
	list = RemoveItem (list);
    }

}



listelement * RemoveItem (listelement * list) {

    listelement * tempp;
    printf ("Element removed is %d\n", list -> dataitem);
    tempp = list -> next;
    free (list);
    return tempp;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -